Assure Ecommerce and Retail Operations Uptime with ThousandEyes
Northeast PHP - High Performance PHP
1. August 11th, 2012
High Performance PHP
Northeast PHP 2012
Jonathan Klein
jonathan.n.klein@gmail.com
@jonathanklein
2. Agenda
Who am I?
Why Performance Matters
Profiling PHP Applications
Code Level Optimizations
Big Wins
Load Testing
Takeaways
3. Introduction
• Web Performance Platform Lead at Wayfair
• Boston Web Performance Meetup Group Organizer
• Led a team that converted most of Wayfair’s storefront
codebase from Classic ASP to PHP.
1. Rewrote ~100,000 lines of code
• Wayfair Stats:
1. 450 reqs/sec for dynamic content (PHP pages)
2. 1600 reqs/sec for static content (including CDN)
3. Codebase has > 400,000 lines of PHP (some is 3rd party)
3
16. Graphite
• Written by Orbitz
• Real-time graphing engine for StatsD data (among other things)
• http://graphite.wikidot.com/
• Architecture: http://www.aosabook.org/en/graphite.html
16
17.
18.
19.
20. Xdebug
• PHP extension (need to install) - http://xdebug.org/
• Code level tracing
1. Exposes hotspots
• Significant overhead, use in DEV only!
• Add ?XDEBUG_PROFILE=true to a URL
• View results:
1. kcachegrind on linux
2. wincachegrind on Windows
20
27. Writing Efficient PHP
• Set max value before loop:
$max = count($rows);
for ($i = 0; $i < $max; $i++) {
echo $i;
}
• require_once() is slow
• Minimize use of define()
• Yes, single quotes are slightly faster than double
quotes
27
32. Writing Efficient PHP
• Set the max loop value before the loop:
$max = count($rows);
for ($i = 0; $i < $max; $i++) {
echo $i;
}
Okay, this one is pretty good
32
43. APC User Cache Optimizations
• Avoid fragmentation - keep utilization under 10%
1. Assign 1GB, only fill 100MB
• Compress objects that are > 10KB before storing
• Reduce garbage collection in the source of
apc_store()
• Consider CDB
1. http://engineering.wayfair.com/moving-constants-out-of-apc-and-into-cdb/
43
44. Caching
• Avoid hitting the database
• Cache in APC/Memcached
• 1:00 tomorrow: “Caching With Memcached”
44
45. Fix All Errors
• PHP 5.3: E_ALL | E_STRICT
• PHP 5.4: E_ALL
• Can also do error_reporting(-1);
45
46. Child Processes
• 4-6 processes per CPU core.
• Beyond that just add servers.
(Test your app)
46
47. HipHop for PHP
• Developed/Open Sourced by Facebook
• Transforms PHP source code into highly optimized C++
• Reduces CPU for Facebook’s servers by 50%
http://developers.facebook.com/blog/post/2010/02/02/hiphop-for-php--move-fast/
https://github.com/facebook/hiphop-php
47
49. JMeter
• Open Source
• Generate load via a GUI or command line
• Can watch req/s peak out
• Easy to use (just make sure you set the
correct path to Java on your computer).
49
50.
51.
52.
53.
54.
55. Be Careful
• JMeter looks a lot like a DOS attack
• Make sure you know what is failing
• Look at monitoring while test is running
• Run in Production
• Run a test, make a change, run it again
55
59. Why is it terrible?
• Lack of context
• Are we talking about average or a percentile?
• Server side time or client?
• Who is measuring it?
• When is it being measured?
• Real users or synthetic?
59
61. Pick Tight SLAs
“The homepage of our site will load in
<300ms at the 80th percentile, measured
by sampling 10% of our real users over a
24 hour period every day at 8AM.”
61
62. Pick Tight SLAs
“The homepage of our site will load in
<300ms at the 80th percentile, measured
by sampling 10% of our real users over a
24 hour period every day at 8AM.”
62
63. Things to Remember
1. Measure and monitor your application
2. Focus on big wins
3. Run the latest (stable) version of PHP
4. Make sure you are using APC correctly
5. It’s always the database: go to “The Proper Care
and Feeding of a MySQL Database” talk
6. Caching is your friend: go to the “Caching With
Memcached” talk
7. Know what system resources you depend on
63