+ All Categories
Home > Technology > Northeast PHP - High Performance PHP

Northeast PHP - High Performance PHP

Date post: 02-Nov-2014
Category:
Upload: jonathan-klein
View: 9,479 times
Download: 2 times
Share this document with a friend
Description:
This is a talk that I gave at the Northeast PHP conference in Boston, MA on 8/11/12.
Popular Tags:
65
High Performance PHP Northeast PHP 2012 Jonathan Klein [email protected] @jonathanklein August 11 th , 2012
Transcript
Page 1: Northeast PHP - High Performance PHP

High Performance PHPNortheast PHP 2012

Jonathan Klein

[email protected]

@jonathanklein

August 11th, 2012

Page 2: Northeast PHP - High Performance PHP

Agenda

Who am I?

Why Performance Matters

Profiling PHP Applications

Code Level Optimizations

Big Wins

Load Testing

Takeaways

Page 3: Northeast PHP - High Performance PHP

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)

Page 4: Northeast PHP - High Performance PHP

The Value of Performance

Page 5: Northeast PHP - High Performance PHP
Page 6: Northeast PHP - High Performance PHP

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/

Page 7: Northeast PHP - High Performance PHP

7

80/20 Rule

80% of page load time takes place on the client

Page 8: Northeast PHP - High Performance PHP

We’re going to talk about the other 20%

Page 9: Northeast PHP - High Performance PHP

A fast page load is 2 seconds

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

Page 10: Northeast PHP - High Performance PHP

But network time could be ~100ms

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

Page 11: Northeast PHP - High Performance PHP

The average length of an eye blink is 100-400 milliseconds

Full Page Load – 2 Seconds

Base HTML – 400ms

Server Generation Time – 300ms

Page 12: Northeast PHP - High Performance PHP

Profiling PHP ApplicationsMonitoring and Tracing

Page 13: Northeast PHP - High Performance PHP

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

Page 14: Northeast PHP - High Performance PHP

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

Page 15: Northeast PHP - High Performance PHP

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/

Page 16: Northeast PHP - High Performance PHP

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

Page 17: Northeast PHP - High Performance PHP
Page 18: Northeast PHP - High Performance PHP
Page 19: Northeast PHP - High Performance PHP
Page 20: Northeast PHP - High Performance PHP

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

Page 21: Northeast PHP - High Performance PHP
Page 22: Northeast PHP - High Performance PHP
Page 23: Northeast PHP - High Performance PHP
Page 24: Northeast PHP - High Performance PHP
Page 25: Northeast PHP - High Performance PHP

Lesson: Profile Your Code

Page 26: Northeast PHP - High Performance PHP

Code Level Optimizations

Page 27: Northeast PHP - High Performance PHP

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

Page 28: Northeast PHP - High Performance PHP

Almost Every Micro-Optimization is

Worthless

Page 29: Northeast PHP - High Performance PHP

http://phpbench.com/

Page 30: Northeast PHP - High Performance PHP

http://phpbench.com/

Page 31: Northeast PHP - High Performance PHP

http://phpbench.com/

Page 32: Northeast PHP - High Performance PHP

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

Page 33: Northeast PHP - High Performance PHP
Page 34: Northeast PHP - High Performance PHP

So Why Even Mention Micro-Optimizations?

Page 35: Northeast PHP - High Performance PHP
Page 36: Northeast PHP - High Performance PHP

Lesson: Focus on the Big Wins

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

Page 37: Northeast PHP - High Performance PHP

Big Wins

Page 38: Northeast PHP - High Performance PHP

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

Page 39: Northeast PHP - High Performance PHP

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

Page 40: Northeast PHP - High Performance PHP

40

Use APC (Correctly)

APC – Alternative PHP Cache

Page 41: Northeast PHP - High Performance PHP

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

Page 42: Northeast PHP - High Performance PHP

42

APC User Cache

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

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

Page 43: Northeast PHP - High Performance PHP

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/  

Page 44: Northeast PHP - High Performance PHP

44

Caching

• Avoid hitting the database

• Cache in APC/Memcached

• 1:00 tomorrow: “Caching With Memcached”

Page 45: Northeast PHP - High Performance PHP

45

Fix All Errors

• PHP 5.3: E_ALL | E_STRICT

• PHP 5.4: E_ALL

• Can also do error_reporting(-1);

Page 46: Northeast PHP - High Performance PHP

46

Child Processes

• 4-6 processes per CPU core.

• Beyond that just add servers.

(Test your app)

Page 47: Northeast PHP - High Performance PHP

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

Page 48: Northeast PHP - High Performance PHP

Load Testing

Page 49: Northeast PHP - High Performance PHP

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

Page 50: Northeast PHP - High Performance PHP
Page 51: Northeast PHP - High Performance PHP
Page 52: Northeast PHP - High Performance PHP
Page 53: Northeast PHP - High Performance PHP
Page 54: Northeast PHP - High Performance PHP
Page 55: Northeast PHP - High Performance PHP

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

Page 56: Northeast PHP - High Performance PHP

Takeaways

Page 57: Northeast PHP - High Performance PHP

“How Fast Is Your Site?”

Page 58: Northeast PHP - High Performance PHP

This is a terrible question

Page 59: Northeast PHP - High Performance PHP

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?

Page 60: Northeast PHP - High Performance PHP

We still have to answer it

Page 61: Northeast PHP - High Performance PHP

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

Page 62: Northeast PHP - High Performance PHP

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

Page 63: Northeast PHP - High Performance PHP

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

Page 64: Northeast PHP - High Performance PHP

There is a lot more


Recommended