SlideShare a Scribd company logo
August 11th, 2012

High Performance PHP
               Northeast PHP 2012
                      Jonathan Klein
        jonathan.n.klein@gmail.com
                     @jonathanklein
Agenda
  Who am I?
  Why Performance Matters
  Profiling PHP Applications
  Code Level Optimizations
  Big Wins
  Load Testing
  Takeaways
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
The Value of Performance
Northeast PHP - High Performance PHP
Real World Examples

• Firefox: -2.2 seconds = 15.4% more downloads

• Shopzilla: -5 seconds = 7-12% increase in revenue

• Google: +400ms = 0.76% fewer searches

• Amazon: +100ms = -1% revenue



http://www.phpied.com/the-performance-business-pitch/



                                                        6
80/20 Rule




  80% of page load time takes place on the
                  client




                                             7
We’re going to talk about the other 20%
A fast page load is 2 seconds



This means you have 400ms to get that HTML off
                 your server
But network time could be ~100ms



This means you have 400ms 300ms to build the
                   page
The average length of an eye blink
     is 100-400 milliseconds




         Full Page Load – 2 Seconds




      Base HTML – 400ms

   Server Generation Time – 300ms
Profiling PHP Applications
        Monitoring and Tracing
Monitoring/Tracing Tools

• Paid:
   1. Tracelytics (bought by AppNeta)
   2. AppDynamics (building a PHP solution)
   3. dynaTrace (building a PHP solution
   4. New Relic

• Free:
   1. StatsD/Graphite
   2. Xdebug
   3. Nagios
   4. Ganglia

                                              13
Monitoring/Tracing Tools

• Paid:
   1. Tracelytics (bought by AppNeta)
   2. AppDynamics (building a PHP solution)
   3. dynaTrace (building a PHP solution
   4. New Relic

• Free:
   1. StatsD/Graphite
   2. Xdebug
   3. Nagios
   4. Ganglia

                                              14
StatsD

UDP Packets – extremely low overhead

<?php
$start = microtime(true);

…script content…

$end      = microtime(true);

MyStats::timing('foo.bar', $end - $start);
?>


http://codeascraft.etsy.com/2011/02/15/measure-anything-measure-everything/



                                                                              15
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
Northeast PHP - High Performance PHP
Northeast PHP - High Performance PHP
Northeast PHP - High Performance PHP
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
Northeast PHP - High Performance PHP
Northeast PHP - High Performance PHP
Northeast PHP - High Performance PHP
Northeast PHP - High Performance PHP
Lesson: Profile Your Code
Code Level Optimizations
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
Almost Every Micro-Optimization is
           Worthless
http://phpbench.com/
http://phpbench.com/
http://phpbench.com/
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
Northeast PHP - High Performance PHP
So Why Even Mention
Micro-Optimizations?
Northeast PHP - High Performance PHP
Lesson: Focus on the Big Wins


“Premature optimization is the root of all evil”
              - Donald Knuth
Big Wins
Upgrade PHP

       5.3 is ~20% faster than 5.2
          http://news.php.net/php.internals/36484


      5.4 is ~20-40% faster than 5.3
          http://news.php.net/php.internals/57760




   Upgrading 5.2  5.4 gives a 45-70%
             improvement!

              http://php.net/migration53
              http://php.net/migration54


                                                    38
PHP 5.4 is 5 Times Faster than PHP4
      http://static.zend.com/topics/White-paper-PHP4-PHP5.pdf
Use APC (Correctly)

           APC – Alternative PHP Cache




                                         40
Opcode Cache Performance

• Vanilla settings  30-40% improvement

• Turn off APC Stat  additional ~2x
  improvement!
      -- Understand what is happening here




http://www.slideshare.net/vortexau/improving-php-application-performance-with-apc-
presentation



                                                                                     41
APC User Cache

<?php
$bar = 'Hello World!';
apc_store('foo', $bar);
?>

<?php
var_dump(apc_fetch('foo'));
?>
-------- Output --------
string(12) "Hello World!"


                              42
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
Caching

• Avoid hitting the database

• Cache in APC/Memcached

• 1:00 tomorrow: “Caching With Memcached”




                                            44
Fix All Errors

• PHP 5.3: E_ALL | E_STRICT

• PHP 5.4: E_ALL

• Can also do error_reporting(-1);




                                     45
Child Processes

• 4-6 processes per CPU core.

• Beyond that just add servers.

                 (Test your app)




                                   46
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
Load Testing
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
Northeast PHP - High Performance PHP
Northeast PHP - High Performance PHP
Northeast PHP - High Performance PHP
Northeast PHP - High Performance PHP
Northeast PHP - High Performance PHP
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
Takeaways
“How Fast Is Your Site?”
This is a terrible question
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
We still have to answer it
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
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
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
There is a lot more
Get In Touch

http://www.meetup.com/Web-Performance-Boston/


               wayfair.com/careers

            jonathan.n.klein@gmail.com

                 @jonathanklein




                                                65

More Related Content

What's hot (20)

PDF
How to scale PHP applications
Enrico Zimuel
 
ODP
Caching and tuning fun for high scalability @ FOSDEM 2012
Wim Godden
 
ODP
Caching and tuning fun for high scalability @ phpBenelux 2011
Wim Godden
 
PDF
Developing cacheable PHP applications - Confoo 2018
Thijs Feryn
 
ODP
Caching and tuning fun for high scalability @ PHPTour
Wim Godden
 
PDF
Single page apps with drupal 7
Chris Tankersley
 
PDF
Going crazy with Varnish and Symfony
David de Boer
 
PDF
Running php on nginx
Harald Zeitlhofer
 
PDF
Developing cacheable PHP applications - PHPLimburgBE 2018
Thijs Feryn
 
PDF
eZ Publish Caching Mechanisms
Kaliop-slide
 
PDF
A reviravolta do desenvolvimento web
Wallace Reis
 
PDF
Leverage HTTP to deliver cacheable websites - Codemotion Rome 2018
Thijs Feryn
 
PDF
Leverage HTTP to deliver cacheable websites - Thijs Feryn - Codemotion Rome 2018
Codemotion
 
PPTX
Php internal architecture
Elizabeth Smith
 
PDF
PHP7 - The New Engine for old good train
Xinchen Hui
 
PDF
Nginx pres
James Fuller
 
PDF
How to build a High Performance PSGI/Plack Server
Masahiro Nagano
 
PPTX
Making Symofny shine with Varnish - SymfonyCon Madrid 2014
Barel Barelon
 
KEY
SydPHP March 2012 Meetup
Graham Weldon
 
PDF
Challenges when building high profile editorial sites
Yann Malet
 
How to scale PHP applications
Enrico Zimuel
 
Caching and tuning fun for high scalability @ FOSDEM 2012
Wim Godden
 
Caching and tuning fun for high scalability @ phpBenelux 2011
Wim Godden
 
Developing cacheable PHP applications - Confoo 2018
Thijs Feryn
 
Caching and tuning fun for high scalability @ PHPTour
Wim Godden
 
Single page apps with drupal 7
Chris Tankersley
 
Going crazy with Varnish and Symfony
David de Boer
 
Running php on nginx
Harald Zeitlhofer
 
Developing cacheable PHP applications - PHPLimburgBE 2018
Thijs Feryn
 
eZ Publish Caching Mechanisms
Kaliop-slide
 
A reviravolta do desenvolvimento web
Wallace Reis
 
Leverage HTTP to deliver cacheable websites - Codemotion Rome 2018
Thijs Feryn
 
Leverage HTTP to deliver cacheable websites - Thijs Feryn - Codemotion Rome 2018
Codemotion
 
Php internal architecture
Elizabeth Smith
 
PHP7 - The New Engine for old good train
Xinchen Hui
 
Nginx pres
James Fuller
 
How to build a High Performance PSGI/Plack Server
Masahiro Nagano
 
Making Symofny shine with Varnish - SymfonyCon Madrid 2014
Barel Barelon
 
SydPHP March 2012 Meetup
Graham Weldon
 
Challenges when building high profile editorial sites
Yann Malet
 

Viewers also liked (17)

PDF
PHP High Availability High Performance
Amazee Labs
 
PPTX
PHP Optimization
djesch
 
PPTX
Scaling a High Traffic Web Application: Our Journey from Java to PHP
120bi
 
PPT
Architechture of a social network for 30M users
Fotostrana
 
PPTX
EscConf - Deep Dive Frontend Optimization
Jonathan Klein
 
PDF
UXFest - RUM Distillation 101
Jonathan Klein
 
PDF
Riding rails for 10 years
jduff
 
PDF
Edge Conf Rendering Performance Panel
Jonathan Klein
 
PDF
JSDay 2013 - Practical Responsive Web Design
Jonathan Klein
 
PDF
DIY Synthetic: Private WebPagetest Magic
Jonathan Klein
 
PDF
PHP On Steroids
Jonathan Oxer
 
PDF
High Performance Php My Sql Scaling Techniques
ZendCon
 
PPTX
BTV PHP - Building Fast Websites
Jonathan Klein
 
PDF
Integrating React.js with PHP projects
Ignacio Martín
 
PDF
PHP UK 2017 - Don't Lose Sleep - Secure Your REST
Adam Englander
 
PPTX
How Slow Load Times Hurt Your Bottom Line (And 17 Things You Can Do to Fix It)
Tammy Everts
 
PDF
How Shopify Scales Rails
jduff
 
PHP High Availability High Performance
Amazee Labs
 
PHP Optimization
djesch
 
Scaling a High Traffic Web Application: Our Journey from Java to PHP
120bi
 
Architechture of a social network for 30M users
Fotostrana
 
EscConf - Deep Dive Frontend Optimization
Jonathan Klein
 
UXFest - RUM Distillation 101
Jonathan Klein
 
Riding rails for 10 years
jduff
 
Edge Conf Rendering Performance Panel
Jonathan Klein
 
JSDay 2013 - Practical Responsive Web Design
Jonathan Klein
 
DIY Synthetic: Private WebPagetest Magic
Jonathan Klein
 
PHP On Steroids
Jonathan Oxer
 
High Performance Php My Sql Scaling Techniques
ZendCon
 
BTV PHP - Building Fast Websites
Jonathan Klein
 
Integrating React.js with PHP projects
Ignacio Martín
 
PHP UK 2017 - Don't Lose Sleep - Secure Your REST
Adam Englander
 
How Slow Load Times Hurt Your Bottom Line (And 17 Things You Can Do to Fix It)
Tammy Everts
 
How Shopify Scales Rails
jduff
 
Ad

Similar to Northeast PHP - High Performance PHP (20)

PDF
2013 - Dustin whittle - Escalando PHP en la vida real
PHP Conference Argentina
 
PPTX
ExpressionEngine - Simple Steps to Performance and Security (EECI 2014)
Nexcess.net LLC
 
PDF
Top ten-list
Brian DeShong
 
KEY
Php through the eyes of a hoster: PHPNW10
Combell NV
 
PDF
Dutch php conference_2010_opm
isnull
 
PPTX
Optimizing performance
Zend by Rogue Wave Software
 
PDF
Php through the eyes of a hoster phpbnl11
Combell NV
 
PDF
Ipc mysql php
Anis Berejeb
 
PDF
Improve WordPress performance with caching and deferred execution of code
Danilo Ercoli
 
PDF
Php through the eyes of a hoster confoo
Combell NV
 
PDF
php & performance
simon8410
 
PDF
Intro to CakePHP
Walther Lalk
 
PDF
Scaling with Symfony - PHP UK
Ricard Clau
 
PDF
Алексей Петров "PHP at Scale: Knowing enough to be dangerous!"
Fwdays
 
PDF
Php through the eyes of a hoster
Combell NV
 
PDF
The 5 most common reasons for a slow WordPress site and how to fix them – ext...
Otto Kekäläinen
 
PDF
What is Nginx and Why You Should to Use it with Wordpress Hosting
WPSFO Meetup Group
 
PDF
PHP & Performance
毅 吕
 
PPTX
Joomla! Performance on Steroids
SiteGround.com
 
KEY
Performance and scalability with drupal
Ronan Berder
 
2013 - Dustin whittle - Escalando PHP en la vida real
PHP Conference Argentina
 
ExpressionEngine - Simple Steps to Performance and Security (EECI 2014)
Nexcess.net LLC
 
Top ten-list
Brian DeShong
 
Php through the eyes of a hoster: PHPNW10
Combell NV
 
Dutch php conference_2010_opm
isnull
 
Optimizing performance
Zend by Rogue Wave Software
 
Php through the eyes of a hoster phpbnl11
Combell NV
 
Ipc mysql php
Anis Berejeb
 
Improve WordPress performance with caching and deferred execution of code
Danilo Ercoli
 
Php through the eyes of a hoster confoo
Combell NV
 
php & performance
simon8410
 
Intro to CakePHP
Walther Lalk
 
Scaling with Symfony - PHP UK
Ricard Clau
 
Алексей Петров "PHP at Scale: Knowing enough to be dangerous!"
Fwdays
 
Php through the eyes of a hoster
Combell NV
 
The 5 most common reasons for a slow WordPress site and how to fix them – ext...
Otto Kekäläinen
 
What is Nginx and Why You Should to Use it with Wordpress Hosting
WPSFO Meetup Group
 
PHP & Performance
毅 吕
 
Joomla! Performance on Steroids
SiteGround.com
 
Performance and scalability with drupal
Ronan Berder
 
Ad

Recently uploaded (20)

PDF
Windsurf Meetup Ottawa 2025-07-12 - Planning Mode at Reliza.pdf
Pavel Shukhman
 
PPTX
WooCommerce Workshop: Bring Your Laptop
Laura Hartwig
 
PDF
Presentation - Vibe Coding The Future of Tech
yanuarsinggih1
 
PDF
Empower Inclusion Through Accessible Java Applications
Ana-Maria Mihalceanu
 
PDF
July Patch Tuesday
Ivanti
 
PDF
Exolore The Essential AI Tools in 2025.pdf
Srinivasan M
 
PDF
Complete Network Protection with Real-Time Security
L4RGINDIA
 
PDF
Chris Elwell Woburn, MA - Passionate About IT Innovation
Chris Elwell Woburn, MA
 
PPTX
MSP360 Backup Scheduling and Retention Best Practices.pptx
MSP360
 
PDF
LLMs.txt: Easily Control How AI Crawls Your Site
Keploy
 
PDF
The Builder’s Playbook - 2025 State of AI Report.pdf
jeroen339954
 
PDF
HubSpot Main Hub: A Unified Growth Platform
Jaswinder Singh
 
PDF
Complete JavaScript Notes: From Basics to Advanced Concepts.pdf
haydendavispro
 
PDF
SFWelly Summer 25 Release Highlights July 2025
Anna Loughnan Colquhoun
 
PDF
NewMind AI Journal - Weekly Chronicles - July'25 Week II
NewMind AI
 
PPTX
Top iOS App Development Company in the USA for Innovative Apps
SynapseIndia
 
PDF
CIFDAQ Weekly Market Wrap for 11th July 2025
CIFDAQ
 
PDF
Smart Trailers 2025 Update with History and Overview
Paul Menig
 
PPTX
AUTOMATION AND ROBOTICS IN PHARMA INDUSTRY.pptx
sameeraaabegumm
 
PPTX
UiPath Academic Alliance Educator Panels: Session 2 - Business Analyst Content
DianaGray10
 
Windsurf Meetup Ottawa 2025-07-12 - Planning Mode at Reliza.pdf
Pavel Shukhman
 
WooCommerce Workshop: Bring Your Laptop
Laura Hartwig
 
Presentation - Vibe Coding The Future of Tech
yanuarsinggih1
 
Empower Inclusion Through Accessible Java Applications
Ana-Maria Mihalceanu
 
July Patch Tuesday
Ivanti
 
Exolore The Essential AI Tools in 2025.pdf
Srinivasan M
 
Complete Network Protection with Real-Time Security
L4RGINDIA
 
Chris Elwell Woburn, MA - Passionate About IT Innovation
Chris Elwell Woburn, MA
 
MSP360 Backup Scheduling and Retention Best Practices.pptx
MSP360
 
LLMs.txt: Easily Control How AI Crawls Your Site
Keploy
 
The Builder’s Playbook - 2025 State of AI Report.pdf
jeroen339954
 
HubSpot Main Hub: A Unified Growth Platform
Jaswinder Singh
 
Complete JavaScript Notes: From Basics to Advanced Concepts.pdf
haydendavispro
 
SFWelly Summer 25 Release Highlights July 2025
Anna Loughnan Colquhoun
 
NewMind AI Journal - Weekly Chronicles - July'25 Week II
NewMind AI
 
Top iOS App Development Company in the USA for Innovative Apps
SynapseIndia
 
CIFDAQ Weekly Market Wrap for 11th July 2025
CIFDAQ
 
Smart Trailers 2025 Update with History and Overview
Paul Menig
 
AUTOMATION AND ROBOTICS IN PHARMA INDUSTRY.pptx
sameeraaabegumm
 
UiPath Academic Alliance Educator Panels: Session 2 - Business Analyst Content
DianaGray10
 

Northeast PHP - High Performance PHP

  • 1. August 11th, 2012 High Performance PHP Northeast PHP 2012 Jonathan Klein [email protected] @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
  • 4. The Value of Performance
  • 6. Real World Examples • Firefox: -2.2 seconds = 15.4% more downloads • Shopzilla: -5 seconds = 7-12% increase in revenue • Google: +400ms = 0.76% fewer searches • Amazon: +100ms = -1% revenue http://www.phpied.com/the-performance-business-pitch/ 6
  • 7. 80/20 Rule 80% of page load time takes place on the client 7
  • 8. We’re going to talk about the other 20%
  • 9. A fast page load is 2 seconds This means you have 400ms to get that HTML off your server
  • 10. But network time could be ~100ms This means you have 400ms 300ms to build the page
  • 11. The average length of an eye blink is 100-400 milliseconds Full Page Load – 2 Seconds Base HTML – 400ms Server Generation Time – 300ms
  • 12. Profiling PHP Applications Monitoring and Tracing
  • 13. Monitoring/Tracing Tools • Paid: 1. Tracelytics (bought by AppNeta) 2. AppDynamics (building a PHP solution) 3. dynaTrace (building a PHP solution 4. New Relic • Free: 1. StatsD/Graphite 2. Xdebug 3. Nagios 4. Ganglia 13
  • 14. Monitoring/Tracing Tools • Paid: 1. Tracelytics (bought by AppNeta) 2. AppDynamics (building a PHP solution) 3. dynaTrace (building a PHP solution 4. New Relic • Free: 1. StatsD/Graphite 2. Xdebug 3. Nagios 4. Ganglia 14
  • 15. StatsD UDP Packets – extremely low overhead <?php $start = microtime(true); …script content… $end = microtime(true); MyStats::timing('foo.bar', $end - $start); ?> http://codeascraft.etsy.com/2011/02/15/measure-anything-measure-everything/ 15
  • 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
  • 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
  • 34. So Why Even Mention Micro-Optimizations?
  • 36. Lesson: Focus on the Big Wins “Premature optimization is the root of all evil” - Donald Knuth
  • 38. Upgrade PHP 5.3 is ~20% faster than 5.2 http://news.php.net/php.internals/36484 5.4 is ~20-40% faster than 5.3 http://news.php.net/php.internals/57760 Upgrading 5.2  5.4 gives a 45-70% improvement! http://php.net/migration53 http://php.net/migration54 38
  • 39. PHP 5.4 is 5 Times Faster than PHP4 http://static.zend.com/topics/White-paper-PHP4-PHP5.pdf
  • 40. Use APC (Correctly) APC – Alternative PHP Cache 40
  • 41. Opcode Cache Performance • Vanilla settings  30-40% improvement • Turn off APC Stat  additional ~2x improvement! -- Understand what is happening here http://www.slideshare.net/vortexau/improving-php-application-performance-with-apc- presentation 41
  • 42. APC User Cache <?php $bar = 'Hello World!'; apc_store('foo', $bar); ?> <?php var_dump(apc_fetch('foo')); ?> -------- Output -------- string(12) "Hello World!" 42
  • 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
  • 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
  • 57. “How Fast Is Your Site?”
  • 58. This is a terrible question
  • 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
  • 60. We still have to answer it
  • 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
  • 64. There is a lot more

Editor's Notes

  • #18: StatsD Aggregates things by 5 second intervals by default