1 / 25

PHP Performance

PHP Performance. Principles and Tools By Kevin Schroeder Technology Evangelist Zend Technologies. About me. Kevin Schroeder Technology Evangelist for Zend Programmer Sys Admin Author IBM i Programmer’s Guide to PHP You want to do WHAT with PHP? Coming up

ananda
Download Presentation

PHP Performance

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. PHP Performance Principles and Tools By Kevin Schroeder Technology Evangelist Zend Technologies

  2. About me • Kevin Schroeder • Technology Evangelist for Zend • Programmer • Sys Admin • Author • IBM i Programmer’s Guide to PHP • You want to do WHAT with PHP? • Coming up • Race Ferraris on the weekend • My Honda has a dismal win record

  3. Follow us! • http://bit.ly/cjueZg (or search for Zend) • http://twitter.com/zend • http://twitter.com/kpschrade (me!)

  4. Things we’ll look at • Database • Network Latency • IO Contention • CPU utilization • Network Connectivity • Best Practices/Pit Falls/Things to consider

  5. Performance – Database • Tools • Zend Server – A distinct group of monitoring options for DB’s • Inter-application query monitoring, usually via a DB adapter abstraction layer • Symptoms • Pages take a long time to load with Apache/IIS/PHP using proportionally less of the CPU time • Causes • The list could go on for 1x105 slides

  6. Performance - Database • Possible remedies • Simplify your queries • Make your queries more complex • Tune your database

  7. Performance – Network Latency • Tools • Zend Server – monitor slow socket function execution • Wireshark • Tcpdump • Symptoms • Depends on where the latency is occurring • Often slow page responses with low CPU usage • Causes • Network Saturation (probably not too likely) • DNS Reverse Lookups • TCP Handshakes • High number of “hops” • … and much more

  8. The TCP handshake T/TCP SYN + Response Data + FIN T/TCP SYN + Response Data + ACK +FIN ACK

  9. Hops (not the good kind)

  10. Performance – IO Contention • Tools • Zend Server (in extreme cases, long fopen() calls) • vmstat & top (Linux) • System Internals Process Explorer (Windows) • Symptoms • Unpredictable and difficult to reproduce page load times • Causes • Too few spindles serving too much data • High write concurrency • Insufficient free system RAM

  11. Performance – CPU Utilization • Tools • top (Linux) • System Internals Process Explorer (Windows) • Symptoms • Page load times either correspond directly to CPU utilization or are consistently slow • Causes • Badcode or over-architected solutions • Excessive recursion • Excessive IO • High server utilization • too few servers serving too much traffic

  12. Performance – CPU Utilization • There are often 2 different paths this can follow • User time • Time spent executing the code that you wrote • System time • Time spent running functionality that your code calls, such as networking, file system, etc.

  13. Performance – Network Connectivity • Tools • Zend Server – monitor failed calls to socket_connect(), fsockopen() or fread() • Wireshark • Tcpdump • Symptoms • Database or web service calls take long to execute • If read functions die unexpectedly, bad network hardware might be a cause

  14. Choosing the right architecture • If you deal with large collections of data, be careful of various ORM techniques or data access methods. This can pertain to the cloud too

  15. OO • Beware of magic! • __get, __set, __call • Situations where they are often used • When representing another object that it has no knowledge of - I.e. Soap requests, COM objects, Java objects, database tables • When there are property-level permissions on an object’s properties. Keeping the properties private and using __get/set to determine access • If used sparingly they won’t affect you, but many modern applications have lots of recursion or large underlying architectures that can have unpredicted consequences

  16. !Preg • Use preg_match for data checking only when you have to • stripos(‘http://’, $website) is over twice as fast as preg_match(‘/http:\/\//i’, $website) • ctype_alnum() is almost 5 times as fast as preg_match(‘/^\s*$/’); • Casting “if ($test == (int)$test)” is well over 5 times faster than preg_match(‘/^\d*$/)

  17. Code Acceleration • From a test on Wordpress • Without acceleration • Requests per second: 2.28 • Time per request: 437.766 ms • With acceleration • Requests per second: 8.11 • Time per request: 123.377 ms • 4-fold increase in performance by flipping a (free) switch • Zend Server CE is free and has this. You have no excuse!!

  18. Do you queue? • Offloading long running processes to a queuing system can help make your front end servers scale more vertically • Examples include • Job Queues • Zend Platform/Zend Server 5 • Gearman • Message Queues • ActiveMQ • RabbitMQ

  19. What not to do • “Oh we’ll just cache it” • “Our NFS server is just fine for serving PHP files” • “That LIKE query is necessary” • You may need to structure your data a little different • You might need to use a search engine instead • Zend_Search_Lucene • SOLR • “Using arrays is faster than objects, we’ll use them instead” • “Monitoring/diagnostic tools are too expensive”

  20. What TO do • Minimize require_once calls/use an autoloader • Try to make your architecture more horizontal rather than vertical • People from Java backgrounds tend to go more vertical • Use Lazy Loading/Load on Demand • THINK before you code!! • Don’t just do something because someone else is • Use diagnostic tools! • Zend Server Monitoring and Code Tracing • Zend Studio Profiler (pays for itself if you are 0.5% more productive in a year (according to my calculation)

  21. Non-PHP PHP performance rules • PHP performance is not the only factor in determining the overall performance of a website. Here are some suggestions • Make fewer HTTP requests • Use an Expires header • Gzip cached content • Put stylesheets at the top • Put Javascript at the bottom • Reduce DNS lookups • Avoid redirects

  22. Non-PHP PHP performance rules • Be aware of your page size • Microsoft – 238kb – 4.4 seconds • Oracle – 104kb – 861ms • IBM – 640 kb – 4.6 seconds • Zend – 183kb – 2.4 seconds • There is a fairly direct relationship between the amount of data on your web page and the render time

  23. Non-PHP PHP performance rules • Similarly, be careful when dealing with lots of Ajax data • The browser needs to process that data • With large data sets the browser may take a while to update the DOM • 2 options • Use staggered loading • Pass rendered content to the browser and attach with innerHTML

  24. Questions?

  25. Fin Kevin Schroeder kevin@zend.com

More Related