10. Quiz Who here is a sysadmin? Who here is a developer? Who here is both?
11. WARNING! Performance is a HIGHLY context sensitive topic There are no answers here; only possible directions Do not micro-optimize 20ms cf. 100ms is not a 5 fold improvement in performance, it is a 5 fold waste of your time Unless you’re Yahoo, Google, Facebook, etc. The perception of performance is as important as actual performance Use the scientific method to find performance problems Observe Hypothesize Experiment Solve one problem at a time and MEASURE RESULTS!
13. Performance – Database Symptoms Pages take a long time to load with Apache/IIS/PHP using proportionally less of the CPU time compared to DB Causes The list could go on for 4.23x105slides There are so many ways you can screw up your queries Tools Intra-application query monitoring, usually via a DB adapter abstraction layer Zend Server – A distinct group of monitoring options for DB calls in PHP
14. Performance - Database Possible remedies Simplify your queries Make your queries more complex Tune your database Don’t just automatically allocate more memory Don’t just create indexes Keeping this light because there are other, better, MySQL performance experts (many of them here)
15. Performance – Network Latency 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” TCP Backlog … and much more Tools Wireshark Tcpdump Zend Server – monitor slow socket function execution
16. The TCP handshake T/TCP SYN + Response Data + FIN T/TCP SYN + Response Data + ACK +FIN ACK
17. Performance – IO Contention Symptoms Unpredictable and difficult to reproduce page load times, but often corresponds to request frequency Causes Too few spindles serving too much data High write concurrency Insufficient free system RAM Insufficient depth of data directory structure Tools Zend Server (in extreme cases, long fopen() calls) vmstat & top (Linux) System Internals Process Explorer (Windows)
18. Performance – IO Contention Remedies Increase the number of disks Favor directory depth over width for storing raw data files Increase RAM so read operations can be pulled from memory
19. Performance – CPU Utilization 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 Tools top & vmstat (Linux) System Internals Process Explorer (Windows)
20. Performance – CPU Utilization There are often 2 different paths this will usually 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.
21. Performance – Network Connectivity Symptoms Database or web service calls take long to execute Causes If read functions die unexpectedly, bad network hardware might be a cause Slow DNS queries Full TCP backlog Tools Zend Server – monitor failed calls to socket_connect(), fsockopen() or fread() Wireshark Tcpdump
22. 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
23. !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(‘/^*$/’); Casting “if ($test == (int)$test)” is well over 5 times faster than preg_match(‘/^*$/) Preg will probably not bite you unless you have a lot of recursive logic that depends on it
25. OS tools to use Devs are really bad about thinking about the OS Use OS tooling!!! vmstat top tcpdump & wireshark strace Try stracehttpd –X at least once lsof You can map file descriptors found in strace to descriptors in lsof
26. Zend Server Monitors for several events like slow requests, slow DB calls, high memory usage, etc. Provides the context for those requests Can be configured to provide profile-like data from production installations Has a built in queuing mechanism that allows you to offload non-immediate processing (which can actually be quite a lot)
27. 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 Server 5 (http://www.eschrade.com/ tag: job-queue) Gearman Beanstalk Message Queues ActiveMQ RabbitMQ
29. What not to do This is not complete!!! “Oh we’ll just cache it” “Our NFS server is just fine for serving PHP files” Use a frigging deployment mechanism “That LIKE query is necessary” You may need to structure your data a little different Don’t make your DB gues 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”
30. List of things to do This is not complete!!! THINK before you code!! 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 There is often a correlation between long stack traces and poor performance Use Lazy Loading/Load on Demand SPL arrays and such are great for this
31. List of things to do Use diagnostic tools! Zend Server Monitoring and Code Tracing Some kind of profiler Utilize compiled PHP functionality for iterative complex functionality. Use Ajax Allows you to make a more responsive page by having fast content available immediately and slow content available as it’s ready
32. List of things to do With multiple service calls, consider batching multiple NB-IO requests Facebook does this, so it must be cool Synchronous requests – 4.2 seconds Asynchronous requests using curl – 1.5 seconds
35. To contact me after my presentation, text XIK to INTRO (46876) Follow us! Zend Technologies http://twitter.com/zend http://twitter.com/kpschrade (me!)
42. List of things to do Similarly, be careful when dealing with lots of Ajax data Developers running test data often under-estimate how much data their code will send back 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
43. List of things to do Be aware of your page size Oracle – 104kb – 861ms Zend – 183kb – 2.4 seconds Microsoft – 238kb – 4.4 seconds IBM – 640 kb – 4.6 seconds There is a relatively direct relationship between the amount of data on your web page and the render time