The need for speed Marcus Brger international PHP

  • Slides: 59
Download presentation
The need for speed Marcus Börger international PHP 2004 conference Marcus Börger The need

The need for speed Marcus Börger international PHP 2004 conference Marcus Börger The need for speed

The need for speed þ General aspects Ø Communication Ø Hardware Ø Operating system

The need for speed þ General aspects Ø Communication Ø Hardware Ø Operating system þ How to use PHP Ø Ø þ As a web scripting language As a template system As a RAD tool The Rasmus way What to do and what not to do with PHP Marcus Börger The need for speed 2

Optimization? Ef|fekt[lat. ] der; -[e]s, -e: a) Wirkung, Erfolg; b) (meist Plural) auf Wirkung

Optimization? Ef|fekt[lat. ] der; -[e]s, -e: a) Wirkung, Erfolg; b) (meist Plural) auf Wirkung abzielendes Ausdrucks- u. Gestaltungsmittel; c) Ergebnis, sich aus etwas ergebender nutzen. […] ef|fek|tiv[lat. ]: a) tatsächlich, wriklich; b) wirkungsvoll (im Verhältnis zu den aufgewendeten Mitteln); c) (ugs. ) überhaupt, ganz u. gar, z. B. – nichts leisten; d) lohnend. […] Ef|fek|ti|vitätdie; Wirksamkeit, Durchschlagskraft, Leistungsfähigkeit, Wirkungskraft Marcus Börger The need for speed 3

Optimization? ef|fi|zi|ent ; -este [lat. ] wirksam; wirtschaftlich; Ef|fi|zi|enz , die; -, -en Wirksamkeit

Optimization? ef|fi|zi|ent ; -este [lat. ] wirksam; wirtschaftlich; Ef|fi|zi|enz , die; -, -en Wirksamkeit Effizienz(engl. efficiency): Ein Algorithmus heißt effizient, wenn er ein vorgegebenes Problem in möglichst kurzer Zeit und/oder mit möglichst geringem Aufwand an Betriebsmitteln löst. In der Praxis interessiert man sich meist für die benötigte Laufzeit (bzw. Für die Anzahl der auszuführenden Operationen), für die Größe des Speichers oder für die Zahl der Zugriffe auf Hintergrundspeicher. Die Komplexitätstheorie untersucht die Ordnung dieser Funktionen in Abhängigkeit von der Länge der Eingabe. Marcus Börger The need for speed 4

General aspects þ Do not loose your focus Ø Ø Think before you do

General aspects þ Do not loose your focus Ø Ø Think before you do anything Always check you are still on track Estimate the time and money you (still) have Estimate the time and money you (still) need Ø Are you using the right tools? Ø Is PHP the correct choice? Ø After all is a web application the right thing? Ø Are you using the right algorithms? Ø Is there a better way? Ø Know your environment Ø Know your team Marcus Börger The need for speed 5

Communication internet your server Marcus Börger or serverfarm The need for speed 6

Communication internet your server Marcus Börger or serverfarm The need for speed 6

Communication þ The sum is smaller than the whole Ø No need to apply

Communication þ The sum is smaller than the whole Ø No need to apply more servers if no more bandwidth is available Marcus Börger The need for speed 7

Communication þ The sum is smaller than the whole Ø No need to apply

Communication þ The sum is smaller than the whole Ø No need to apply more servers if no more bandwith is available þ þ A prepared DDo. S can put down anything Applying more servers means they communicate Marcus Börger The need for speed 8

Communication þ The sum is smaller than the whole Ø No need to apply

Communication þ The sum is smaller than the whole Ø No need to apply more servers if no more bandwith is available þ þ A prepared DDo. S can put down anything Applying more servers might help Ø They will communicate Ø You need more software Ø You have more points of failure Marcus Börger The need for speed 9

Communication þ The sum is smaller than the whole Ø No need to apply

Communication þ The sum is smaller than the whole Ø No need to apply more servers if no more bandwith is available þ þ A prepared DDo. S can put down anything Applying more servers might help Ø They will communicate Ø You need more software Ø You have more points of failure þ New ideas can help Marcus Börger The need for speed 10

Hardware þ Every single hardware piece is a point of failure þ þ Avoid

Hardware þ Every single hardware piece is a point of failure þ þ Avoid single point of failures Use the hardware as specified (speed, temperature) Don’t use it to emulate other hardware Don't use it to imitate other hardware þ If you don't have enough knowledge give it away Marcus Börger The need for speed 11

Operating system þ Choose the OS based on þ your hardware þ your software

Operating system þ Choose the OS based on þ your hardware þ your software þ what you are going to do Marcus Börger The need for speed 12

Architecture þ Apply specialization DB-Server App. Server Web. Server Firewall Proxy Internet Data Templates

Architecture þ Apply specialization DB-Server App. Server Web. Server Firewall Proxy Internet Data Templates d. HTML VPN ISDN Marcus Börger The need for speed 13

Database Server þ þ þ What kind of data What size does your data

Database Server þ þ þ What kind of data What size does your data have Who is responsible for data integrity Who is responsible for security Does the database need its own logic Marcus Börger The need for speed 14

Application Server You want dependency injection? You need inversion of control? PHP would need

Application Server You want dependency injection? You need inversion of control? PHP would need state first DB-Server Data Marcus Börger App. Server Templates The need for speed Web. Server d. HTML 15

Web server þ Apache þ Suitable for nearly all needs þ Microsoft IIS þ

Web server þ Apache þ Suitable for nearly all needs þ Microsoft IIS þ Perfect when the rest is also Microsoft þ Threadsafty issues þ Not the major/focused development platform þ Zeus þ Very fast Marcus Börger The need for speed 16

Web server þ TUX - kernel-based web server þ Virtual Host support. þ thttpd

Web server þ TUX - kernel-based web server þ Virtual Host support. þ thttpd - tiny/turbo/throttling HTTP server þ Non-blocking I/O is good. þ Throttling capabilities. þ lighttpd þ On the fly compression. þ Excellent virtual host support. Marcus Börger The need for speed 17

Web server Plenty of CPU power but limited bandwidth Turn on output compression Much

Web server Plenty of CPU power but limited bandwidth Turn on output compression Much bandwidth but limited CPU power Do not use output compression Marcus Börger The need for speed 18

Web Server þ Use different web servers for different things internet static contents Marcus

Web Server þ Use different web servers for different things internet static contents Marcus Börger scripts multimedia content The need for speed 19

Reverse Proxy þ Cache static portions of your output Marcus Börger The need for

Reverse Proxy þ Cache static portions of your output Marcus Börger The need for speed 20

Other tricks þ Use a RAM disk where appropriate þ Use short paths and

Other tricks þ Use a RAM disk where appropriate þ Use short paths and a flat layout Marcus Börger The need for speed 21

After all, Apache is slow? þ Compile your own apache þ þ þ Build

After all, Apache is slow? þ Compile your own apache þ þ þ Build with static modules Use –disable-all Enable all compiler optimizations with -O 3 Tell the compiler what CPU you use via -march -mcpu Use CPU specific features -msse -mmmx -mfpmath=sse Marcus Börger The need for speed 22

After all, CGI is slow? þ Compile your own CGI þ þ þ Build

After all, CGI is slow? þ Compile your own CGI þ þ þ Build with static modules Use –disable-all Enable all compiler optimizations with -O 3 Tell the compiler what CPU you use via -march -mcpu Use CPU specific features -msse -mmmx -mfpmath=sse þ Use strip to clean up your binaries þ Saves loading time þ Saves memory usage Marcus Börger The need for speed 23

Security þ Today security is the most important thing þ þ þ Many script

Security þ Today security is the most important thing þ þ þ Many script kiddies will penetrate your application Without deep knowledge you cannot detect attacks Detecting attacks leads to protection Protection prevents misuse of your hard- and software Protection keeps your data safe N Unsafe data or open systems lead directly to court Marcus Börger The need for speed 24

What is PHP is a scripting language specifically designed to help developers solve web

What is PHP is a scripting language specifically designed to help developers solve web problems, it works by embedding sections of code within HTML blocks. PHP Advantages C C C C Easy to learn Targeted, built-in functions for web developers Good introduction to programming Configurable Simple extension API PEAR Runs britneyspears. com PHP Disadvantages D D D Focused on the Web environment Poor OO support until PHP 5 Configurability Hurts Portability Easy for beginning users, Easy for beginning users to make mistakes Marcus Börger The need for speed 25

PHP - As web scripting language þ Every page is its own PHP script

PHP - As web scripting language þ Every page is its own PHP script C Flexible and easy C Independent scripts by independent programmers D Hard to apply general tasks to all pages Includes can help CSS can help Marcus Börger The need for speed 26

PHP - As a template system þ PHP was developed as a template system

PHP - As a template system þ PHP was developed as a template system þ PHP can be used as template system þ PHP can be the language to develop a template system Marcus Börger The need for speed 27

PHP - As a RAD tool þ No PHP in your real applications þ

PHP - As a RAD tool þ No PHP in your real applications þ Test with PHP þ Implement in another language Marcus Börger The need for speed 28

PHP - The Rasmus way þ þ Small basic PHP scripts Small include files

PHP - The Rasmus way þ þ Small basic PHP scripts Small include files to solve general aspects Include files for the business logic Specialized extensions for the actual work Marcus Börger The need for speed 29

Optimize þ þ þ Everything has a cost Limit the number of includes per

Optimize þ þ þ Everything has a cost Limit the number of includes per request Use the right tool for the right problem Use an opcode cache Use short and easy regular expressions Cache whatever you can þ Optimization is the root of all evil þ It steals all your time þ It makes everything complicated þ In rare cases it leads to less and easier code Marcus Börger The need for speed 30

Everything has a cost þ Do not use features you do not need þ

Everything has a cost þ Do not use features you do not need þ þ þ þ CGI means module startup/shutdown for every page include_path means every possibility has to be tested open_basedir means every entry has to be checked variables_order lets you decide what you need magic_quotes_* means parsing/changing overhead register_argc_argv is only for CLI always_populate_raw_post_data only if necessary Marcus Börger The need for speed 31

Everything has a cost þ ext/tidy can beautify your output you could do it

Everything has a cost þ ext/tidy can beautify your output you could do it before you send the data þ ext/tidy can strip out whitespace reduces the bandwidth needed takes CPU time Marcus Börger The need for speed 32

Everything has a cost þ PHP can dynamically resize images you could supply the

Everything has a cost þ PHP can dynamically resize images you could supply the resized images you could cache the resized images Marcus Börger The need for speed 33

Everything has a cost þ þ þ Function calls are expensive User Functions are

Everything has a cost þ þ þ Function calls are expensive User Functions are more expensive Passing parameters takes time Learn about the PHP API Always have the manual at hand Do not write PHP functions when available by PHP Do not have long optional parameter lists Do not use functions for multiple purposes Also: Do not write spaghetti code Document your code Marcus Börger The need for speed 34

Everything has a cost þ Copying a variable takes time Learn when PHP needs

Everything has a cost þ Copying a variable takes time Learn when PHP needs to copy Learn about references Marcus Börger The need for speed 35

Everything has a cost þ Close your sessions early þ Use session_write_close() þ An

Everything has a cost þ Close your sessions early þ Use session_write_close() þ An open session prevents others accessing the session Marcus Börger The need for speed 36

References A famous PHP 4 rule: If your code doesn't work spread some '&'s

References A famous PHP 4 rule: If your code doesn't work spread some '&'s into it If it still doesn't work use more '&' Understand references Marcus Börger The need for speed 37

References þ References are aliases þ If you change one you change all others

References þ References are aliases þ If you change one you change all others <? php // empty global table $a = 25; // creates a zval $b = $a; // creates a pointer to $a $b = 42; // makes $b a copy of $a and changes it $c = $a; // create another pointer to $a $d = &$a; // split/copy $a, creates $d as a reference to $a $c = 43; // change $c only $d = 0; // changes $d and hence $a ? > Marcus Börger The need for speed 38

References þ Variables are normally copied on function calls <? php function test($a) {

References þ Variables are normally copied on function calls <? php function test($a) { } $a = array(25); test($a); // creates a global zval // creates a new symbol table, copies $a ? > Marcus Börger The need for speed 39

References þ Variables can be passed as references <? php function test(&$b) { $b[]

References þ Variables can be passed as references <? php function test(&$b) { $b[] = 42; // adds a new value to local $b = global $a } $a = array(25); test($a); // creates a global zval // creates a new symbol table ? > Marcus Börger The need for speed 40

References þ Variables are normally copied on return <? php function test(&$b) { return

References þ Variables are normally copied on return <? php function test(&$b) { return $b; } $a = array(25); $b = test($a); // $b is a new value, copied on return ? > Marcus Börger The need for speed 41

References þ Functions can return aliases <? php function &test(&$b) { return $b; }

References þ Functions can return aliases <? php function &test(&$b) { return $b; } $a = array(25); $b = test($a); // $b is a new value, copied after return ? > Marcus Börger The need for speed 42

References þ þ Functions can return aliases Explicit use of the returned reference is

References þ þ Functions can return aliases Explicit use of the returned reference is needed <? php function &test(&$b) { return $b; } $a = array(25); $b = &test($a); // $b is a reference to $a ? > Marcus Börger The need for speed 43

References þ Objects should always be references þ In PHP 5 they are object-references

References þ Objects should always be references þ In PHP 5 they are object-references <? php class test { function factory() { return new test(); } } $obj = test: : factory(); ? > Marcus Börger The need for speed 44

References þ Objects should always be references þ In PHP 5 they are object-references

References þ Objects should always be references þ In PHP 5 they are object-references þ In PHP 3 and 4 you have to take care yourself <? php class test { function &factory() { $a = &new test(); return $a; } } $obj = &test: : factory(); ? > Marcus Börger The need for speed 45

References þ Most internal functions don't use references þ This is to allows you

References þ Most internal functions don't use references þ This is to allows you to pass arrays and strings without copying them into a variable first <? php $a = array_fill(0, $cnt, 'foo'); array_key_exists($i, $a); // is_ref == 0, refcount == 1 $b = $a; array_key_exists($i, $a); // is_ref == 0, refcount == 2 array_key_exists($i, &$a); // is_ref == 0, refcount == 2 unset($b); $b =& $a; // making a reference, but not using it array_key_exists($i, $b); // is_ref == 1, refcount > 1 (pass as var) array_key_exists($i, &$a); // is_ref == 1, refcount > 1 (pass as ref) unset($b); array_key_exists($i, $a); // is_ref == ? , refcount == 1 ? > Marcus Börger The need for speed 46

Use the right tool For the right problem þ Use OOP where appropriate not

Use the right tool For the right problem þ Use OOP where appropriate not where nice þ Use layers not because it is easy or looks nice þ Use abstraction if derived or used often þ Use indirection if it is of any advantage Marcus Börger The need for speed 47

Profile your code þ Do not use microtime() for performance measurements þ Use a

Profile your code þ Do not use microtime() for performance measurements þ Use a profiler for your PHP script þ APD þ XDebug þ. . . þ Use a profiler for 'grown up' problems þ Valgrind/calltree Marcus Börger The need for speed 48

The 80 / 20 rule þ 80% of your code takes less than 20%

The 80 / 20 rule þ 80% of your code takes less than 20% runtime þ You don't need to optimize anything in the 80% þ Find out which are the 20% to optimize Marcus Börger The need for speed 49

XDebug þ A tool to debug PHP þ Tracing function calls þ A profiler

XDebug þ A tool to debug PHP þ Tracing function calls þ A profiler Marcus Börger The need for speed 50

Cache whatever you can þ Most dynamic data does not change þ At least

Cache whatever you can þ Most dynamic data does not change þ At least not every time it is requested Marcus Börger Use cache control header The need for speed 51

Cache whatever you can þ Pre generation þ Generate your data once þ Server

Cache whatever you can þ Pre generation þ Generate your data once þ Server the generated data statically þ On demand þ Generate when requested for the first time þ Dynamic caching þ Generate when necessary þ Serve generated data statically otherwise Marcus Börger The need for speed 52

Use an opcode cache Marcus Börger The need for speed 53

Use an opcode cache Marcus Börger The need for speed 53

Use an opcode cache Marcus Börger The need for speed 54

Use an opcode cache Marcus Börger The need for speed 54

Use an opcode cache þ Turck MMCache (GPL) þ Implements many features þ Development

Use an opcode cache þ Turck MMCache (GPL) þ Implements many features þ Development halted þ APC (PHP) þ Slow but development continues þ Weak optimizer þ ion. Cube PHP Accelerator þ It works þ Development halted? þ Free, but closed source þ Zend Cache (Proprietary) þ Implements many features þ Expensive Marcus Börger The need for speed 55

Use an opcode cache Marcus Börger The need for speed 56

Use an opcode cache Marcus Börger The need for speed 56

Use an opcode cache Marcus Börger The need for speed 57

Use an opcode cache Marcus Börger The need for speed 57

Stop þ Don’t get overexcited about optimization þ Sometimes it is cheaper and more

Stop þ Don’t get overexcited about optimization þ Sometimes it is cheaper and more efficient þ to buy another server þ to increase bandwidth þ To buy faster software Marcus Börger The need for speed 58

THANK YOU http: //somabo. de/talks/ http: //talks. php. net Marcus Börger The need for

THANK YOU http: //somabo. de/talks/ http: //talks. php. net Marcus Börger The need for speed 59