profiling php - phpbenelux unconference track - 2015-01-24
TRANSCRIPT
![Page 1: Profiling PHP - PHPBenelux Unconference track - 2015-01-24](https://reader030.vdocument.in/reader030/viewer/2022032616/55a9321b1a28ab35368b466d/html5/thumbnails/1.jpg)
PROFILING PHPA DIVE INTO YOUR APPLICATION
/ Dennis de Greef @dennisdegreef
PHPBenelux Conference 2015
![Page 2: Profiling PHP - PHPBenelux Unconference track - 2015-01-24](https://reader030.vdocument.in/reader030/viewer/2022032616/55a9321b1a28ab35368b466d/html5/thumbnails/2.jpg)
WHAT IS PROFILING?
![Page 3: Profiling PHP - PHPBenelux Unconference track - 2015-01-24](https://reader030.vdocument.in/reader030/viewer/2022032616/55a9321b1a28ab35368b466d/html5/thumbnails/3.jpg)
WIKIPEDIAprofiling is a form of dynamic program analysis that measures,
for example, the space (memory) or time complexity of aprogram, the usage of particular instructions, or the frequency
and duration of function calls. Most commonly, profilinginformation serves to aid program optimization.
![Page 4: Profiling PHP - PHPBenelux Unconference track - 2015-01-24](https://reader030.vdocument.in/reader030/viewer/2022032616/55a9321b1a28ab35368b466d/html5/thumbnails/4.jpg)
SO... DYNAMIC PROGRAM ANALYSIS?
![Page 5: Profiling PHP - PHPBenelux Unconference track - 2015-01-24](https://reader030.vdocument.in/reader030/viewer/2022032616/55a9321b1a28ab35368b466d/html5/thumbnails/5.jpg)
YEAH...LETS FIRST LOOK AT IT'S COUNTERPART...
![Page 6: Profiling PHP - PHPBenelux Unconference track - 2015-01-24](https://reader030.vdocument.in/reader030/viewer/2022032616/55a9321b1a28ab35368b466d/html5/thumbnails/6.jpg)
STATIC ANALYSIS
![Page 7: Profiling PHP - PHPBenelux Unconference track - 2015-01-24](https://reader030.vdocument.in/reader030/viewer/2022032616/55a9321b1a28ab35368b466d/html5/thumbnails/7.jpg)
WIKIPEDIAStatic program analysis is the analysis of computer software that
is performed without actually executing programs
The term is usually applied to the analysis performed by anautomated tool, with human analysis being called programunderstanding, program comprehension or code review.
![Page 8: Profiling PHP - PHPBenelux Unconference track - 2015-01-24](https://reader030.vdocument.in/reader030/viewer/2022032616/55a9321b1a28ab35368b466d/html5/thumbnails/8.jpg)
STATIC ANALYSIS TOOLSThere are a set of tools which perform static code analysis.
These tools can be integrated within an automated build.
PHP Mess DetectorPHP Copy/Paste DetectorPHP CodeSnifferPHP Dead Code Detector
There is a nice page containing a predefined set of tools for abuild to be found at Jenkins PHP
![Page 9: Profiling PHP - PHPBenelux Unconference track - 2015-01-24](https://reader030.vdocument.in/reader030/viewer/2022032616/55a9321b1a28ab35368b466d/html5/thumbnails/9.jpg)
BUT...
![Page 10: Profiling PHP - PHPBenelux Unconference track - 2015-01-24](https://reader030.vdocument.in/reader030/viewer/2022032616/55a9321b1a28ab35368b466d/html5/thumbnails/10.jpg)
THESE TOOLS ONLY ANALYSE HOW YOURCODE IS STRUCTURED, NOT HOW IT BEHAVES.
![Page 11: Profiling PHP - PHPBenelux Unconference track - 2015-01-24](https://reader030.vdocument.in/reader030/viewer/2022032616/55a9321b1a28ab35368b466d/html5/thumbnails/11.jpg)
DYNAMIC ANALYSIS
![Page 12: Profiling PHP - PHPBenelux Unconference track - 2015-01-24](https://reader030.vdocument.in/reader030/viewer/2022032616/55a9321b1a28ab35368b466d/html5/thumbnails/12.jpg)
WIKIPEDIAThe analysis of computer software that is performed by
executing programs on a real or virtual processor.
For dynamic program analysis to be effective, the target program must be executed with sufficient test inputs
to produce interesting behavior.
Use of software testing measures such as code coverage helpsensure that an adequate slice of the program's set of possible
behaviors has been observed.
![Page 13: Profiling PHP - PHPBenelux Unconference track - 2015-01-24](https://reader030.vdocument.in/reader030/viewer/2022032616/55a9321b1a28ab35368b466d/html5/thumbnails/13.jpg)
CALLSTACKA callstack is the order in which statements are exectuted.
An example commonly known, is an exception trace. This traceshows all the statements executed before an exception is
thrown.
![Page 14: Profiling PHP - PHPBenelux Unconference track - 2015-01-24](https://reader030.vdocument.in/reader030/viewer/2022032616/55a9321b1a28ab35368b466d/html5/thumbnails/14.jpg)
CALLGRAPHA callgraph is a visual representation of a callstack.
In large applications this graph can give you better insight on howan application is wired.
![Page 15: Profiling PHP - PHPBenelux Unconference track - 2015-01-24](https://reader030.vdocument.in/reader030/viewer/2022032616/55a9321b1a28ab35368b466d/html5/thumbnails/15.jpg)
PROFILING DATAUsually, the data gathered with a profiler can be represented in
stacks or graphs.They can include information regarding memory- and cpu-usage.
![Page 16: Profiling PHP - PHPBenelux Unconference track - 2015-01-24](https://reader030.vdocument.in/reader030/viewer/2022032616/55a9321b1a28ab35368b466d/html5/thumbnails/16.jpg)
WHY?
![Page 17: Profiling PHP - PHPBenelux Unconference track - 2015-01-24](https://reader030.vdocument.in/reader030/viewer/2022032616/55a9321b1a28ab35368b466d/html5/thumbnails/17.jpg)
REASONSDebugging CPU performanceDebugging memory performanceDebugging IO performanceSee which function is called how many timesGain insight of the black box that is the application
![Page 18: Profiling PHP - PHPBenelux Unconference track - 2015-01-24](https://reader030.vdocument.in/reader030/viewer/2022032616/55a9321b1a28ab35368b466d/html5/thumbnails/18.jpg)
REASONSWe live in a digital age where we want everything instantly.According to a , 51 percent of onlineshoppers in the U.S claimed if a site is too slow they will notcomplete a purchase.Nowadays, search engine indexing also accounts for page load.
case study from Radware
The psychology of web performanceSEO 101: How important is site speed in 2014?
Case study from Radware
![Page 19: Profiling PHP - PHPBenelux Unconference track - 2015-01-24](https://reader030.vdocument.in/reader030/viewer/2022032616/55a9321b1a28ab35368b466d/html5/thumbnails/19.jpg)
WARNING!
Premature optimization is the root of all evil-- Donald Knuth
Only perform optimization when there is a need to.
![Page 20: Profiling PHP - PHPBenelux Unconference track - 2015-01-24](https://reader030.vdocument.in/reader030/viewer/2022032616/55a9321b1a28ab35368b466d/html5/thumbnails/20.jpg)
CAUSE OF ISSUES
![Page 21: Profiling PHP - PHPBenelux Unconference track - 2015-01-24](https://reader030.vdocument.in/reader030/viewer/2022032616/55a9321b1a28ab35368b466d/html5/thumbnails/21.jpg)
COMMON ISSUESNetwork slowdownDatastore slowdownExternal resources (API, Filesystems, Network sockets, etc)Bad code(tm)
![Page 22: Profiling PHP - PHPBenelux Unconference track - 2015-01-24](https://reader030.vdocument.in/reader030/viewer/2022032616/55a9321b1a28ab35368b466d/html5/thumbnails/22.jpg)
ACTIVE VS PASSIVE
Profiling PHP Part 1 (Davey Shafik)
![Page 23: Profiling PHP - PHPBenelux Unconference track - 2015-01-24](https://reader030.vdocument.in/reader030/viewer/2022032616/55a9321b1a28ab35368b466d/html5/thumbnails/23.jpg)
ACTIVE PROFILERUsed during developmentGather more information than passive profilers (like variables/values)Performance impact is biggerShould _NOT_ be used in productionExample: Xdebug
![Page 24: Profiling PHP - PHPBenelux Unconference track - 2015-01-24](https://reader030.vdocument.in/reader030/viewer/2022032616/55a9321b1a28ab35368b466d/html5/thumbnails/24.jpg)
PASSIVE PROFILERMinimal impact on performanceGathers less but sufficient information to diagnose issues(Only records function calls and cpu + mem)Examples: , , XHProf New Relic Blackfire.io
![Page 25: Profiling PHP - PHPBenelux Unconference track - 2015-01-24](https://reader030.vdocument.in/reader030/viewer/2022032616/55a9321b1a28ab35368b466d/html5/thumbnails/25.jpg)
XDEBUG
![Page 26: Profiling PHP - PHPBenelux Unconference track - 2015-01-24](https://reader030.vdocument.in/reader030/viewer/2022032616/55a9321b1a28ab35368b466d/html5/thumbnails/26.jpg)
XDEBUGGenerates files (like Valgrind for C)Can be analysed by KCacheGrind among othersCachegrind files are relatively big in sizeAlso a developer tool for breakpoints and remote debuggingActive profiler
cachegrind
![Page 27: Profiling PHP - PHPBenelux Unconference track - 2015-01-24](https://reader030.vdocument.in/reader030/viewer/2022032616/55a9321b1a28ab35368b466d/html5/thumbnails/27.jpg)
ENABLE XDEBUG PROFILING# php.ini settingsxdebug.profiler_enable=1xdebug.profiler_output_dir=/path/to/store/snapshotsxdebug.profiler_enable_trigger=1
![Page 28: Profiling PHP - PHPBenelux Unconference track - 2015-01-24](https://reader030.vdocument.in/reader030/viewer/2022032616/55a9321b1a28ab35368b466d/html5/thumbnails/28.jpg)
XDEBUG WITH KCACHEGRIND
![Page 29: Profiling PHP - PHPBenelux Unconference track - 2015-01-24](https://reader030.vdocument.in/reader030/viewer/2022032616/55a9321b1a28ab35368b466d/html5/thumbnails/29.jpg)
XDEBUG IN PHPSTORM
![Page 30: Profiling PHP - PHPBenelux Unconference track - 2015-01-24](https://reader030.vdocument.in/reader030/viewer/2022032616/55a9321b1a28ab35368b466d/html5/thumbnails/30.jpg)
XDEBUG IN PHPSTORM
![Page 31: Profiling PHP - PHPBenelux Unconference track - 2015-01-24](https://reader030.vdocument.in/reader030/viewer/2022032616/55a9321b1a28ab35368b466d/html5/thumbnails/31.jpg)
XDEBUG IN PHPSTORM
![Page 32: Profiling PHP - PHPBenelux Unconference track - 2015-01-24](https://reader030.vdocument.in/reader030/viewer/2022032616/55a9321b1a28ab35368b466d/html5/thumbnails/32.jpg)
XDEBUG IN PHPSTORM
![Page 33: Profiling PHP - PHPBenelux Unconference track - 2015-01-24](https://reader030.vdocument.in/reader030/viewer/2022032616/55a9321b1a28ab35368b466d/html5/thumbnails/33.jpg)
XHPROF
![Page 34: Profiling PHP - PHPBenelux Unconference track - 2015-01-24](https://reader030.vdocument.in/reader030/viewer/2022032616/55a9321b1a28ab35368b466d/html5/thumbnails/34.jpg)
XHPROFDeveloped by Facebook and released as open-source in 2009PECL extensionLightweight for being a passive profilerIncludes webgui for reviewing and comparing profiling data
![Page 35: Profiling PHP - PHPBenelux Unconference track - 2015-01-24](https://reader030.vdocument.in/reader030/viewer/2022032616/55a9321b1a28ab35368b466d/html5/thumbnails/35.jpg)
# Linux (using apt or yum)apt-get install -y php5-xhprof
# OSX (using homebrew)brew install php56-xhprof
# For Windows, use PECL or download a .dll somewhere, or compile for your own
INSTALLATION
![Page 36: Profiling PHP - PHPBenelux Unconference track - 2015-01-24](https://reader030.vdocument.in/reader030/viewer/2022032616/55a9321b1a28ab35368b466d/html5/thumbnails/36.jpg)
WORDPRESS EXAMPLE// index.php
xhprof_enable(XHPROF_FLAGS_CPU + XHPROF_FLAGS_MEMORY);
/** Loads the WordPress Environment and Template */require( dirname( __FILE__ ) . '/wp-blog-header.php' );
$xhprof_data = xhprof_disable();
include_once 'xhprof_lib/utils/xhprof_lib.php';include_once 'xhprof_lib/utils/xhprof_runs.php';
$xhprof_runs = new XHProfRuns_Default();
$run_id = $xhprof_runs->save_run($xhprof_data, "xhprof_foo");
![Page 37: Profiling PHP - PHPBenelux Unconference track - 2015-01-24](https://reader030.vdocument.in/reader030/viewer/2022032616/55a9321b1a28ab35368b466d/html5/thumbnails/37.jpg)
CALLSTACK
![Page 38: Profiling PHP - PHPBenelux Unconference track - 2015-01-24](https://reader030.vdocument.in/reader030/viewer/2022032616/55a9321b1a28ab35368b466d/html5/thumbnails/38.jpg)
CALLGRAPH
![Page 39: Profiling PHP - PHPBenelux Unconference track - 2015-01-24](https://reader030.vdocument.in/reader030/viewer/2022032616/55a9321b1a28ab35368b466d/html5/thumbnails/39.jpg)
CALLGRAPH
![Page 40: Profiling PHP - PHPBenelux Unconference track - 2015-01-24](https://reader030.vdocument.in/reader030/viewer/2022032616/55a9321b1a28ab35368b466d/html5/thumbnails/40.jpg)
CALLGRAPH
![Page 41: Profiling PHP - PHPBenelux Unconference track - 2015-01-24](https://reader030.vdocument.in/reader030/viewer/2022032616/55a9321b1a28ab35368b466d/html5/thumbnails/41.jpg)
USEFUL TOOLS
Sets $_COOKIE['_profile'] to 1
XHProf Helper for ChromeXHProf Helper for Firefox
![Page 42: Profiling PHP - PHPBenelux Unconference track - 2015-01-24](https://reader030.vdocument.in/reader030/viewer/2022032616/55a9321b1a28ab35368b466d/html5/thumbnails/42.jpg)
XHGUIWeb frontend for profile dataRequires MongoDBShows callstacksShows callgraphsCan compare different runs
![Page 43: Profiling PHP - PHPBenelux Unconference track - 2015-01-24](https://reader030.vdocument.in/reader030/viewer/2022032616/55a9321b1a28ab35368b466d/html5/thumbnails/43.jpg)
XHGUI
![Page 44: Profiling PHP - PHPBenelux Unconference track - 2015-01-24](https://reader030.vdocument.in/reader030/viewer/2022032616/55a9321b1a28ab35368b466d/html5/thumbnails/44.jpg)
XHGUI
![Page 45: Profiling PHP - PHPBenelux Unconference track - 2015-01-24](https://reader030.vdocument.in/reader030/viewer/2022032616/55a9321b1a28ab35368b466d/html5/thumbnails/45.jpg)
XHGUI COMPARE
![Page 46: Profiling PHP - PHPBenelux Unconference track - 2015-01-24](https://reader030.vdocument.in/reader030/viewer/2022032616/55a9321b1a28ab35368b466d/html5/thumbnails/46.jpg)
XHGUI COMPARE
![Page 47: Profiling PHP - PHPBenelux Unconference track - 2015-01-24](https://reader030.vdocument.in/reader030/viewer/2022032616/55a9321b1a28ab35368b466d/html5/thumbnails/47.jpg)
XHGUI COMPARE DIFFERENCE
![Page 48: Profiling PHP - PHPBenelux Unconference track - 2015-01-24](https://reader030.vdocument.in/reader030/viewer/2022032616/55a9321b1a28ab35368b466d/html5/thumbnails/48.jpg)
XHGUI CALLSTACK
![Page 49: Profiling PHP - PHPBenelux Unconference track - 2015-01-24](https://reader030.vdocument.in/reader030/viewer/2022032616/55a9321b1a28ab35368b466d/html5/thumbnails/49.jpg)
LINK0
![Page 50: Profiling PHP - PHPBenelux Unconference track - 2015-01-24](https://reader030.vdocument.in/reader030/viewer/2022032616/55a9321b1a28ab35368b466d/html5/thumbnails/50.jpg)
LINK0/PROFILERFocused on XHProfHas multiple persistence layers for storing profiles
MemoryFlysystemZend\Db\AdapterMongoDB (work in progress)
Available on composer/packagistFully Object-orientated100% code coveragehttp://github.com/link0/profiler
![Page 51: Profiling PHP - PHPBenelux Unconference track - 2015-01-24](https://reader030.vdocument.in/reader030/viewer/2022032616/55a9321b1a28ab35368b466d/html5/thumbnails/51.jpg)
GETTING STARTEDBootstrapping the profiler
$profiler = new \Link0\Profiler\Profiler();$profiler->start();print_r($profiler->stop());
Adding a PersistenceHandler
profiler = new \Link0\Profiler\Profiler($persistenceHandler = new \Link0\Profiler\PersistenceHandler\MemoryHandler();$ $persistenceHandler);
Flysystem example
filesystem = new \Link0\Profiler\Filesystem(persistenceHandler = new \Link0\Profiler\PersistenceHandler\FilesystemHandler(profiler = new \Link0\Profiler\Profiler(
$filesystemAdapter = new \League\Flysystem\Adapter\Local('/tmp/profiler');$ $filesystemAdapter);$$ $persistenceHandler);
![Page 52: Profiling PHP - PHPBenelux Unconference track - 2015-01-24](https://reader030.vdocument.in/reader030/viewer/2022032616/55a9321b1a28ab35368b466d/html5/thumbnails/52.jpg)
FUTURE?*EXCITING SOUNDS*
![Page 53: Profiling PHP - PHPBenelux Unconference track - 2015-01-24](https://reader030.vdocument.in/reader030/viewer/2022032616/55a9321b1a28ab35368b466d/html5/thumbnails/53.jpg)
SOME IDEASEnable on production with samplingAggregate all profiles to centralized machine/clusterIntegrate into continuous deployment
Run profiling on acceptance environmentAlert when compared differences surpass threshold
Codeception integrationFind business use-cases that are slowMake a case for refactoring to the businessFocus on the customers emulated experience
![Page 54: Profiling PHP - PHPBenelux Unconference track - 2015-01-24](https://reader030.vdocument.in/reader030/viewer/2022032616/55a9321b1a28ab35368b466d/html5/thumbnails/54.jpg)
ANY QUESTIONS?
![Page 55: Profiling PHP - PHPBenelux Unconference track - 2015-01-24](https://reader030.vdocument.in/reader030/viewer/2022032616/55a9321b1a28ab35368b466d/html5/thumbnails/55.jpg)
QUESTIONS? I <3 FEEDBACKJoind.in: GitHub: Twitter: IRC: link0 on Freenode
https://joind.in/talk/view/13420http://github.com/dennisdegreef@dennisdegreef
SLIDES ARE ALSO ON JOIND.IN
![Page 56: Profiling PHP - PHPBenelux Unconference track - 2015-01-24](https://reader030.vdocument.in/reader030/viewer/2022032616/55a9321b1a28ab35368b466d/html5/thumbnails/56.jpg)
USEFUL LINKSProfiling PHP with PhpStorm and XdebugProfiling PHP with PhpStorm and Zend DebuggerXDebug Profiler documentationXHProf PHP documentationProfiling with XHProf and XHGuihttp://github.com/link0/profiler