Northeast PHP - High Performance PHP

Post on 02-Nov-2014

9,479 views 2 download

Tags:

description

This is a talk that I gave at the Northeast PHP conference in Boston, MA on 8/11/12.

transcript

High Performance PHPNortheast PHP 2012

Jonathan Klein

jonathan.n.klein@gmail.com

@jonathanklein

August 11th, 2012

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)

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/

7

80/20 Rule

80% of page load time takes place on the client

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 ApplicationsMonitoring 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

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

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/

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

20

Xdebug

• PHP extension (need to install) - http://xdebug.org/

• Code level tracing1. 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

Lesson: Profile Your Code

Code Level Optimizations

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

Almost Every Micro-Optimization is

Worthless

http://phpbench.com/

http://phpbench.com/

http://phpbench.com/

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

So Why Even Mention Micro-Optimizations?

Lesson: Focus on the Big Wins

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

Big Wins

38

Upgrade PHP

5.3 is ~20% faster than 5.2http://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

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

40

Use APC (Correctly)

APC – Alternative PHP Cache

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

42

APC User Cache

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

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

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 CDB1. http://engineering.wayfair.com/moving-constants-out-of-apc-and-into-cdb/  

44

Caching

• Avoid hitting the database

• Cache in APC/Memcached

• 1:00 tomorrow: “Caching With Memcached”

45

Fix All Errors

• PHP 5.3: E_ALL | E_STRICT

• PHP 5.4: E_ALL

• Can also do error_reporting(-1);

46

Child Processes

• 4-6 processes per CPU core.

• Beyond that just add servers.

(Test your app)

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

Load Testing

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

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

Takeaways

“How Fast Is Your Site?”

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?

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

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

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

There is a lot more