Slide 1

Slide 1 text

Profiling in PHP Artūras Šmorgun, VilniusPHP 0x1C

Slide 2

Slide 2 text

Profiling: a way to measure where a program spends time.

Slide 3

Slide 3 text

Because performance matters • +1 second = -16% satisfaction rate • 50% visitors will share bad experience • 64% mobile users expect <4s load time • +100ms = -1% of sales (for Amazon) • +500ms = -25% of searches (Google)


Slide 4

Slide 4 text

Latency • Network • Web server • Filesystem • Application • Database • Rendering

Slide 5

Slide 5 text

Latency • Network • Web server • Filesystem • Application • Database • Rendering PHP

Slide 6

Slide 6 text

Slide 7

Slide 7 text

Slide 8

Slide 8 text

Slide 9

Slide 9 text

Framework Specific

Slide 10

Slide 10 text

New Relic (web monitoring with advanced features)

Slide 11

Slide 11 text

Features • Application monitoring • Integration with languages - PHP, Python, etc. • Integration with frameworks - Symfony, etc. • Plugins - varnish, etc. • Fast • Has free tear (24h retention)

Slide 12

Slide 12 text

Install 1. Create a free account at http://newrelic.com 2. Add new relic repository
 
 wget -O - https://download.newrelic.com/548C16BF.gpg | sudo apt-key add -
 echo "deb http://apt.newrelic.com/debian/ newrelic non- free” | sudo tee /etc/apt/sources.list.d/newrelic.list
 sudo apt-get update 3. Install package:
 sudo apt-get install newrelic-php5


Slide 13

Slide 13 text

Configure In newrelic.ini file: newrelic.enabled = true
 newrelic.licence = “secret-licence”
 newrelic.appname = “The next big thing”

Slide 14

Slide 14 text

Enable • Cannot be enabled/disabled per request • Is fast - designed for production monitoring
 
 
 
 


Slide 15

Slide 15 text

Dashboard

Slide 16

Slide 16 text

Transactions

Slide 17

Slide 17 text

Trace

Slide 18

Slide 18 text

XHProf

Slide 19

Slide 19 text

Features • Free! • Runs on your machine • Fast enough (claimed to be OK for production) • Detailed execution info • Historical graphs • Comparison of the runs

Slide 20

Slide 20 text

Install 1. Install PEAR
 wget http://pear.php.net/go-pear.phar
 php go-pear.phar 2. Install Xhprof package
 sudo pecl install -f xhprof

Slide 21

Slide 21 text

Configure 1/4 • Edit /etc/php5/conf.d/xhprof.ini
 [xhprof]
 extension=xhprof.so
 xhprof.output_dir=“/var/tmp/xhprof”
 
 
 
 
 
 


Slide 22

Slide 22 text

Configure 2/4 • Auto prepend/append xhprof files on host in php
 # /etc/apache2/sites-enabled/php-profiling.dev.conf
 
 # … … …
 php_value auto_prepend_file
 “/usr/share/xhprof_external/header.php”
 php_value auto_append_file 
 “/usr/share/xhprof_external/footer.php”
 


Slide 23

Slide 23 text

Configure 3/4 • /usr/share/php/xhprof_external/header.php
 
 xhprof_enable(); • /usr/share/php/xhprof_external/footer.php
 
 $xhprof_data = xhprof_disable();
 $XHPROF_LIB = realpath(dirname(__FILE__) .’/../xhprof_lib’);
 include_once $XHPROF_ROOT . "/xhprof_lib/utils/ xhprof_lib.php";
 include_once $XHPROF_ROOT . "/xhprof_lib/utils/ xhprof_runs.php";$xhprof_runs = new XHProfRuns_Default();
 $run_id = $xhprof_runs->save_run($xhprof_data, "xhprof_foo");

Slide 24

Slide 24 text

Configure 4/4 • Create a host for viewer on your server
 OR
 php -S 0:8080 -t xhprof_html/


Slide 25

Slide 25 text

Enable • Always enabled (with this example) • Configurable in header.php (when used):
 
 if (rand(1, 100) <> 42) {
 return;
 }
 
 


Slide 26

Slide 26 text

Dashboard

Slide 27

Slide 27 text

Trace

Slide 28

Slide 28 text

Function View

Slide 29

Slide 29 text

Call Graph

Slide 30

Slide 30 text

XHGui more sophisticated alternative web gui for XHProf

Slide 31

Slide 31 text

Xdebug (not only a debugger)

Slide 32

Slide 32 text

Features • Well known PHP Debugger • Free! • Generates widely used cachegrind files • Broad choice of viewers • On demand • Only for dev (+15~30% execution time)

Slide 33

Slide 33 text

Install 1. Install PEAR
 wget http://pear.php.net/go-pear.phar
 php go-pear.phar • Install extension
 sudo pecl install xdebug
 
 


Slide 34

Slide 34 text

Configure • Edit /etc/php5/conf.d/xdebug.ini
 
 [xdebug]
 zend_extension=xdebug.so
 
 [xdebug-profiling]
 xdebug.profiler_enable_trigger = 1
 xdebug.profiler_enable = 0
 xdebug.profiler_output_dir = /tmp/cachegrind


Slide 35

Slide 35 text

Enable • Install“Xdebug helper”browser extension • Enable Profiling in browser
 
 
 
 
 
 
 


Slide 36

Slide 36 text

QCacheGrind also KCacheGrind, WinCacheGrind, WebCacheGrind, etc

Slide 37

Slide 37 text

Install • QCacheGrind via homebrew on a Mac:
 
 brew install qcachegrind • WinCacheGrind via sourceforge on Windows:
 
 http://sourceforge.net/projects/wincachegrind/ • Compile it from sources of KCacheGrind:
 
 http://kcachegrind.sourceforge.net/
 


Slide 38

Slide 38 text

Dashboard

Slide 39

Slide 39 text

Function View

Slide 40

Slide 40 text

Call Graph

Slide 41

Slide 41 text

blackfire.io low overhead profiler

Slide 42

Slide 42 text

Features • “Profiling in the cloud” • Free if you are using 20 slots • Very quick to setup • Can handle cachegrind and xhprof output files • On demand • fast

Slide 43

Slide 43 text

Install 1. Create a free account at http://blackfire.io 2. Add blackfire repository
 
 curl -s https://packagecloud.io/gpg.key | sudo apt-key add -
 echo "deb http://packages.blackfire.io/debian any main” | sudo tee /etc/apt/sources.list.d/blackfire.list
 sudo apt-get update 3. Install package:
 sudo apt-get install blackfire-agent


Slide 44

Slide 44 text

Configure • Configure credentials:
 sudo blackfire-agent -register
 
 
 
 
 
 
 


Slide 45

Slide 45 text

Enable • Install “Blackfire”browser extension • Enable profiling in browser
 
 
 
 
 
 
 
 


Slide 46

Slide 46 text

Slot View

Slide 47

Slide 47 text

Function View

Slide 48

Slide 48 text

Metrics

Slide 49

Slide 49 text

Comparison

Slide 50

Slide 50 text

Final thoughts

Slide 51

Slide 51 text

Focusing on the
 right problem.

Slide 52

Slide 52 text

No micro optimisations • echo “something” vs echo ‘something’ • file_exists($file) vs is_file($file) • isset($array[$key]) vs 
 array_key_exists($key, $array)
 
 
 


Slide 53

Slide 53 text

–Someone at BuildStuff 2014 (approximate) “Find the biggest bottleneck. Everything else is not your biggest bottleneck.”

Slide 54

Slide 54 text

Monitoring first, profiling later.

Slide 55

Slide 55 text

–Donald Knuth “Premature optimisation is the root of all evil.”

Slide 56

Slide 56 text

Monitoring is not absolutely right. Sometimes not accurate. Sometimes reports false positives.

Slide 57

Slide 57 text

Exploit fast wins • Caching • CDN (akamai, cloudinary, etc) • Opcode • Latest software (PHP 5.6 anyone?) • Reverse proxies (cloudflare, varnish) • Moooooore server power! • Do less?

Slide 58

Slide 58 text

Thank you! Follow me:
 @asarturas
 Contact me:
 [email protected]
 


Slide 59

Slide 59 text

Questions? blackfire.io XHProf New Relic Xdebug Free ✓/✗ ✓ ✗/✓ ✓ Quick ✓ ✓ ✓ ✗ Online ✓ ✓ ✓ ✗/✓ On demand ✓ ✓ ✗ ✓ With Viewer ✓ ✓/✗ ✓ ✗/✓ Fast setup ✓ ✗ ✓ ✗ Comparison ✓ ✓ ✓ ✗ History graph ✗ ✓ ✓/✗ ✗ Call stack ✓ ✓ ✓ ✓ Call graph ✓ ✓ ✗ ✓ 8.6 8.6 7.0 4.6