123
High Performance PHP PHPDay 2013 Jonathan Klein @jonathanklein Saturday, May 18, 13

PHPDay 2013 - High Performance PHP

Embed Size (px)

DESCRIPTION

This is a talk that I gave in Verona, Italy at PHPDay 2013 on May 18th.

Citation preview

Page 1: PHPDay 2013 - High Performance PHP

High Performance PHPPHPDay 2013Jonathan Klein@jonathanklein

Saturday, May 18, 13

Page 2: PHPDay 2013 - High Performance PHP

Slides, Links:jkle.in/phpday

Saturday, May 18, 13

Page 3: PHPDay 2013 - High Performance PHP

Some Etsy Stats• 1.4 billion page views/month• Almost $1B in sales last year• Over 1M lines of PHP

Saturday, May 18, 13

Page 4: PHPDay 2013 - High Performance PHP

Agenda• Why Performance Matters

• Profiling PHP Applications

• Code Level Optimizations

• Big Wins

• Load Testing

• Takeaways

Saturday, May 18, 13

Page 5: PHPDay 2013 - High Performance PHP

The Value of Performance

Saturday, May 18, 13

Page 6: PHPDay 2013 - High Performance PHP

Saturday, May 18, 13

Page 7: PHPDay 2013 - High Performance PHP

Real World Examples

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

Saturday, May 18, 13

Page 8: PHPDay 2013 - High Performance PHP

Real World Examples• Firefox: -2.2 seconds = 15.4% more downloads

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

Saturday, May 18, 13

Page 9: PHPDay 2013 - High Performance PHP

Real World Examples• Firefox: -2.2 seconds = 15.4% more downloads

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

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

Saturday, May 18, 13

Page 10: PHPDay 2013 - 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

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

Saturday, May 18, 13

Page 11: PHPDay 2013 - 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/

Saturday, May 18, 13

Page 12: PHPDay 2013 - High Performance PHP

~80% of page load time takes

place on the client

Saturday, May 18, 13

Page 13: PHPDay 2013 - High Performance PHP

...if your backend is fast

Saturday, May 18, 13

Page 14: PHPDay 2013 - High Performance PHP

A fast page load is 2 seconds

This means you have 400ms to get that HTML off your server

Saturday, May 18, 13

Page 15: PHPDay 2013 - High Performance PHP

But network time could be ~100ms

This means you have 400ms 300ms to build the page

Saturday, May 18, 13

Page 16: PHPDay 2013 - High Performance PHP

< 100ms feels instant< 1 sec feels like flow

< 10 sec to keep user’s attention

http://www.nngroup.com/articles/response-times-3-important-limits/

Saturday, May 18, 13

Page 17: PHPDay 2013 - High Performance PHP

< 100ms feels instant< 1 sec feels like flow

< 10 sec to keep user’s attention

Full Page Load – 2 Seconds

http://www.nngroup.com/articles/response-times-3-important-limits/

Saturday, May 18, 13

Page 18: PHPDay 2013 - High Performance PHP

< 100ms feels instant< 1 sec feels like flow

< 10 sec to keep user’s attention

Full Page Load – 2 Seconds

Base HTML – 400ms

http://www.nngroup.com/articles/response-times-3-important-limits/

Saturday, May 18, 13

Page 19: PHPDay 2013 - High Performance PHP

< 100ms feels instant< 1 sec feels like flow

< 10 sec to keep user’s attention

Full Page Load – 2 Seconds

Base HTML – 400ms

Server Generation Time – 300ms

http://www.nngroup.com/articles/response-times-3-important-limits/

Saturday, May 18, 13

Page 20: PHPDay 2013 - High Performance PHP

Profiling PHP Applications

Saturday, May 18, 13

Page 21: PHPDay 2013 - High Performance PHP

Monitoring/Tracing • Paid:

• Tracelytics (bought by AppNeta)

• AppDynamics (building a PHP solution)

• dynaTrace (building a PHP solution)

• New Relic (has a free option)

• Free:

• StatsD/Graphite

• xhprof

Saturday, May 18, 13

Page 22: PHPDay 2013 - High Performance PHP

Monitoring/Tracing • Paid:

• Tracelytics (bought by AppNeta)

• AppDynamics (building a PHP solution)

• dynaTrace (building a PHP solution)

• New Relic

• Free:

• StatsD/Graphite

• xhprof

Saturday, May 18, 13

Page 23: PHPDay 2013 - High Performance PHP

StatsD (UDP packets)$start = microtime(true);/* script content */$end = microtime(true);

StatsD::timing('foo.bar', $end - $start);

More Info: http://goo.gl/LbDPE

Saturday, May 18, 13

Page 24: PHPDay 2013 - High Performance PHP

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

Saturday, May 18, 13

Page 25: PHPDay 2013 - High Performance PHP

Etsy Conversations PHP Time

Execution Count

Saturday, May 18, 13

Page 26: PHPDay 2013 - High Performance PHP

Search Page PHP Time (95th Percentile)

Saturday, May 18, 13

Page 27: PHPDay 2013 - High Performance PHP

Search Page Execution Count

Saturday, May 18, 13

Page 28: PHPDay 2013 - High Performance PHP

Stacked Search Timers

Saturday, May 18, 13

Page 29: PHPDay 2013 - High Performance PHP

xhprof• PHP Extension (need to install)

• http://pecl.php.net/package/xhprof

• Code level tracing

• Significant overhead, use in DEV only!

• Add ?xhprof=1 to URL

• Results in browser

Saturday, May 18, 13

Page 30: PHPDay 2013 - High Performance PHP

Saturday, May 18, 13

Page 31: PHPDay 2013 - High Performance PHP

Saturday, May 18, 13

Page 32: PHPDay 2013 - High Performance PHP

Saturday, May 18, 13

Page 33: PHPDay 2013 - High Performance PHP

Saturday, May 18, 13

Page 34: PHPDay 2013 - High Performance PHP

Saturday, May 18, 13

Page 35: PHPDay 2013 - High Performance PHP

Lesson: Profile Your Code

Saturday, May 18, 13

Page 36: PHPDay 2013 - High Performance PHP

Code Level Optimizations

Saturday, May 18, 13

Page 37: PHPDay 2013 - High Performance PHP

Writing Efficient PHPSet 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, but...

Saturday, May 18, 13

Page 38: PHPDay 2013 - High Performance PHP

Almost Every Micro-Optimization is Worthless

Saturday, May 18, 13

Page 39: PHPDay 2013 - High Performance PHP

http://phpbench.com/

Saturday, May 18, 13

Page 40: PHPDay 2013 - High Performance PHP

http://phpbench.com/

Saturday, May 18, 13

Page 41: PHPDay 2013 - High Performance PHP

http://phpbench.com/

Saturday, May 18, 13

Page 42: PHPDay 2013 - High Performance PHP

Writing Efficient PHPSet max value before loop:

$max = count($rows); for ($i = 0; $i < $max; $i++) { echo $i; }

Okay, this one is pretty good

Saturday, May 18, 13

Page 43: PHPDay 2013 - High Performance PHP

Saturday, May 18, 13

Page 44: PHPDay 2013 - High Performance PHP

Saturday, May 18, 13

Page 45: PHPDay 2013 - High Performance PHP

So Why Even Mention Micro-Optimizations?

Saturday, May 18, 13

Page 46: PHPDay 2013 - High Performance PHP

http://code.flickr.net/2009/12/02/flipping-out/

Saturday, May 18, 13

Page 47: PHPDay 2013 - High Performance PHP

strtok() to implode()

Saturday, May 18, 13

Page 48: PHPDay 2013 - High Performance PHP

Lesson:Focus on the Big

Wins

Saturday, May 18, 13

Page 49: PHPDay 2013 - High Performance PHP

“Premature optimization is the root of all evil”- Donald Knuth

Saturday, May 18, 13

Page 50: PHPDay 2013 - High Performance PHP

Big Wins

Saturday, May 18, 13

Page 51: PHPDay 2013 - High Performance PHP

Upgrade PHP

Saturday, May 18, 13

Page 52: PHPDay 2013 - High Performance PHP

Upgrade PHP5.3 is ~20% faster than 5.2

Saturday, May 18, 13

Page 53: PHPDay 2013 - High Performance PHP

Upgrade PHP5.3 is ~20% faster than 5.2

http://news.php.net/php.internals/36484

Saturday, May 18, 13

Page 54: PHPDay 2013 - High Performance PHP

Upgrade PHP5.3 is ~20% faster than 5.2

http://news.php.net/php.internals/36484

Saturday, May 18, 13

Page 55: PHPDay 2013 - High Performance PHP

Upgrade PHP5.3 is ~20% faster than 5.2

http://news.php.net/php.internals/36484

5.4 is ~20-40% faster than 5.3

Saturday, May 18, 13

Page 56: PHPDay 2013 - High Performance PHP

Upgrade PHP5.3 is ~20% faster than 5.2

http://news.php.net/php.internals/36484

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

Saturday, May 18, 13

Page 57: PHPDay 2013 - High Performance PHP

Upgrade PHP5.3 is ~20% faster than 5.2

http://news.php.net/php.internals/36484

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

Saturday, May 18, 13

Page 58: PHPDay 2013 - High Performance PHP

Upgrade PHP5.3 is ~20% faster than 5.2

http://news.php.net/php.internals/36484

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

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

Saturday, May 18, 13

Page 59: PHPDay 2013 - High Performance PHP

Upgrade PHP5.3 is ~20% faster than 5.2

http://news.php.net/php.internals/36484

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

Upgrading 5.2 --> 5.4 gives a 45-70% improvement!http://php.net/migration53

Saturday, May 18, 13

Page 60: PHPDay 2013 - High Performance PHP

Upgrade PHP5.3 is ~20% faster than 5.2

http://news.php.net/php.internals/36484

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

Upgrading 5.2 --> 5.4 gives a 45-70% improvement!http://php.net/migration53

http://php.net/migration54

Saturday, May 18, 13

Page 61: PHPDay 2013 - High Performance PHP

Upgrade PHP5.3 is ~20% faster than 5.2

http://news.php.net/php.internals/36484

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

Upgrading 5.2 --> 5.4 gives a 45-70% improvement!http://php.net/migration53

http://php.net/migration54

Saturday, May 18, 13

Page 62: PHPDay 2013 - High Performance PHP

PHP 5.4 is 5 Times Faster than PHP 4http://static.zend.com/topics/White-paper-PHP4-PHP5.pdf

Saturday, May 18, 13

Page 63: PHPDay 2013 - High Performance PHP

Etsy’s Upgrade to PHP 5.4

Saturday, May 18, 13

Page 64: PHPDay 2013 - High Performance PHP

Use an Opcode Cache (APC)

Saturday, May 18, 13

Page 65: PHPDay 2013 - High Performance PHP

Standard Page Execution

Saturday, May 18, 13

Page 66: PHPDay 2013 - High Performance PHP

With An Opcode Cache

Saturday, May 18, 13

Page 67: PHPDay 2013 - High Performance PHP

Opcode Cache Vanilla settings: 30-40% improvement

Turn off APC Stat: additional ~2x improvement -- Understand what is happening herehttp://www.slideshare.net/vortexau/improving-php-application-performance-with-apc-presentation

Saturday, May 18, 13

Page 68: PHPDay 2013 - High Performance PHP

Cache Data in a Key-Value Store

Saturday, May 18, 13

Page 69: PHPDay 2013 - High Performance PHP

APC User Cache<?php$foo = "Hello, World!";apc_store('some_key', $foo);?>

<?phpvar_dump(apc_fetch('some_key'));?>

-------- Output --------

string(12) "Hello World!"

Saturday, May 18, 13

Page 70: PHPDay 2013 - High Performance PHP

APC User Cache

Saturday, May 18, 13

Page 71: PHPDay 2013 - High Performance PHP

APC User Cache • Avoid fragmentation - keep utilization under 10%

Saturday, May 18, 13

Page 72: PHPDay 2013 - High Performance PHP

APC User Cache • Avoid fragmentation - keep utilization under 10%

• Assign 1GB, only fill 100MB

Saturday, May 18, 13

Page 73: PHPDay 2013 - High Performance PHP

APC User Cache • Avoid fragmentation - keep utilization under 10%

• Assign 1GB, only fill 100MB

• Compress objects that are > 10KB before storing

Saturday, May 18, 13

Page 74: PHPDay 2013 - High Performance PHP

APC User Cache • Avoid fragmentation - keep utilization under 10%

• Assign 1GB, only fill 100MB

• Compress objects that are > 10KB before storing

• Reduce garbage collection in the source of apc_store()

Saturday, May 18, 13

Page 75: PHPDay 2013 - High Performance PHP

APC User Cache • Avoid fragmentation - keep utilization under 10%

• Assign 1GB, only fill 100MB

• Compress objects that are > 10KB before storing

• Reduce garbage collection in the source of apc_store()

• Consider CDB

Saturday, May 18, 13

Page 76: PHPDay 2013 - High Performance PHP

APC User Cache • Avoid fragmentation - keep utilization under 10%

• Assign 1GB, only fill 100MB

• Compress objects that are > 10KB before storing

• Reduce garbage collection in the source of apc_store()

• Consider CDB• http://engineering.wayfair.com/moving-constants-out-of-apc-

and-into-cdb/

Saturday, May 18, 13

Page 77: PHPDay 2013 - High Performance PHP

Memcached• Usually a separate server• In-memory key-value store• Extremely simple and fast• http://memcached.org/

Saturday, May 18, 13

Page 78: PHPDay 2013 - High Performance PHP

APC vs. MemcachedAPC User Cache Memcached

Local to the Server Shared Network Resource

Good for small objects Large or small objects

Good for mostly read workloads

Can read and write quickly

Only one instance Can be clustered

Saturday, May 18, 13

Page 79: PHPDay 2013 - High Performance PHP

Fix All Errors• PHP 5.3: E_ALL | E_STRICT

• PHP 5.4: E_ALL

• Can also do error_reporting(-1);

Saturday, May 18, 13

Page 80: PHPDay 2013 - High Performance PHP

Child Processes4-6 processes per CPU core.

Beyond that just add servers.

(Test your app)

Saturday, May 18, 13

Page 81: PHPDay 2013 - High Performance PHP

HipHop for PHP• Developed/Open Sourced by Facebook

• Now a VM + JIT compilation

• 5x improvement in throughput over PHP 5.2• http://developers.facebook.com/blog/post/2010/02/02/

hiphop-for-php--move-fast/

• https://www.facebook.com/notes/facebook-engineering/speeding-up-php-based-development-with-hiphop-vm/10151170460698920

• https://github.com/facebook/hiphop-php

Saturday, May 18, 13

Page 82: PHPDay 2013 - High Performance PHP

Understand Framework

Overhead

Saturday, May 18, 13

Page 83: PHPDay 2013 - High Performance PHP

http://systemsarchitect.net/performance-benchmark-of-popular-php-frameworks/

Saturday, May 18, 13

Page 84: PHPDay 2013 - High Performance PHP

Saturday, May 18, 13

Page 85: PHPDay 2013 - High Performance PHP

Saturday, May 18, 13

Page 86: PHPDay 2013 - High Performance PHP

Saturday, May 18, 13

Page 87: PHPDay 2013 - High Performance PHP

Symfony is 20x slower than raw

PHP

Saturday, May 18, 13

Page 88: PHPDay 2013 - High Performance PHP

Load Testing

Saturday, May 18, 13

Page 89: PHPDay 2013 - High Performance PHP

JMeter• Open Source• Generate load via a GUI or command

line• Can watch req/s degrade with more

users• Easy to use

Saturday, May 18, 13

Page 90: PHPDay 2013 - High Performance PHP

Saturday, May 18, 13

Page 91: PHPDay 2013 - High Performance PHP

Saturday, May 18, 13

Page 92: PHPDay 2013 - High Performance PHP

Saturday, May 18, 13

Page 93: PHPDay 2013 - High Performance PHP

Saturday, May 18, 13

Page 94: PHPDay 2013 - High Performance PHP

Saturday, May 18, 13

Page 95: PHPDay 2013 - High Performance PHP

Be Careful

Saturday, May 18, 13

Page 96: PHPDay 2013 - High Performance PHP

Be Careful• JMeter looks a lot like a DOS attack

Saturday, May 18, 13

Page 97: PHPDay 2013 - High Performance PHP

Be Careful• JMeter looks a lot like a DOS attack• Make sure you know what is failing

Saturday, May 18, 13

Page 98: PHPDay 2013 - 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

Saturday, May 18, 13

Page 99: PHPDay 2013 - 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

Saturday, May 18, 13

Page 100: PHPDay 2013 - 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

Saturday, May 18, 13

Page 101: PHPDay 2013 - High Performance PHP

Takeaways

Saturday, May 18, 13

Page 102: PHPDay 2013 - High Performance PHP

“How Fast Is Your Site?”

Saturday, May 18, 13

Page 103: PHPDay 2013 - High Performance PHP

This is a terrible quesiton

Saturday, May 18, 13

Page 104: PHPDay 2013 - High Performance PHP

Why is it terrible?

Saturday, May 18, 13

Page 105: PHPDay 2013 - High Performance PHP

Why is it terrible?• Lack of context

Saturday, May 18, 13

Page 106: PHPDay 2013 - High Performance PHP

Why is it terrible?• Lack of context

• Are we talking about average or a percentile?

Saturday, May 18, 13

Page 107: PHPDay 2013 - High Performance PHP

Why is it terrible?• Lack of context

• Are we talking about average or a percentile?

• Server side time or client?

Saturday, May 18, 13

Page 108: PHPDay 2013 - High Performance PHP

Why is it terrible?• Lack of context

• Are we talking about average or a percentile?

• Server side time or client?

• Who is measuring it?

Saturday, May 18, 13

Page 109: PHPDay 2013 - High Performance PHP

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?

Saturday, May 18, 13

Page 110: PHPDay 2013 - High Performance PHP

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?

Saturday, May 18, 13

Page 111: PHPDay 2013 - High Performance PHP

We still have to answer it

Saturday, May 18, 13

Page 112: PHPDay 2013 - High Performance PHP

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.”

Saturday, May 18, 13

Page 113: PHPDay 2013 - High Performance PHP

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.”

Saturday, May 18, 13

Page 114: PHPDay 2013 - High Performance PHP

Things to Remember

Saturday, May 18, 13

Page 115: PHPDay 2013 - High Performance PHP

Things to Remember• Measure and monitor your application

Saturday, May 18, 13

Page 116: PHPDay 2013 - High Performance PHP

Things to Remember• Measure and monitor your application

• Focus on big wins

Saturday, May 18, 13

Page 117: PHPDay 2013 - High Performance PHP

Things to Remember• Measure and monitor your application

• Focus on big wins

• Run the latest (stable) version of PHP

Saturday, May 18, 13

Page 118: PHPDay 2013 - High Performance PHP

Things to Remember• Measure and monitor your application

• Focus on big wins

• Run the latest (stable) version of PHP

• Make sure you are using APC correctly

Saturday, May 18, 13

Page 119: PHPDay 2013 - High Performance PHP

Things to Remember• Measure and monitor your application

• Focus on big wins

• Run the latest (stable) version of PHP

• Make sure you are using APC correctly

• It’s always the database (stay in this room)

Saturday, May 18, 13

Page 120: PHPDay 2013 - High Performance PHP

Things to Remember• Measure and monitor your application

• Focus on big wins

• Run the latest (stable) version of PHP

• Make sure you are using APC correctly

• It’s always the database (stay in this room)

• Caching is your friend

Saturday, May 18, 13

Page 121: PHPDay 2013 - High Performance PHP

Things to Remember• Measure and monitor your application

• Focus on big wins

• Run the latest (stable) version of PHP

• Make sure you are using APC correctly

• It’s always the database (stay in this room)

• Caching is your friend

• Know what system resources you depend on

Saturday, May 18, 13

Page 122: PHPDay 2013 - High Performance PHP

There is a lot more to talk about

Saturday, May 18, 13