SlideShare a Scribd company logo
PROFILING PHP 
A DIVE INTO YOUR APPLICATION 
Dennis de Greef / @dennisdegreef 
AmsterdamPHP
WHAT IS PROFILING?
DEFINITION 
profiling is a form of dynamic program analysis that measures, for 
example, the space (memory) or time complexity of a program, 
the usage of particular instructions, or the frequency and 
duration of function calls. Most commonly, profiling information 
serves to aid program optimization.
DYNAMIC PROGRAM ANALYSIS?
YEAH... 
LETS FIRST LOOK AT IT'S COUNTERPART...
STATIC ANALYSIS
DEFINITION 
Static 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 an 
automated tool, with human analysis being called program 
understanding, program comprehension or code review.
STATIC ANALYSIS TOOLS 
There are a set of tools which perform static code analysis. 
These tools can be integrated within an automated build. 
PHP Mess Detector 
PHP Copy/Paste Detector 
PHP CodeSniffer 
PHP Dead Code Detector 
There is a nice page containing a predefined set of tools for a 
build to be found at Jenkins PHP
BUT...
THESE TOOLS ONLY ANALYSE HOW YOUR 
CODE IS STRUCTURED, NOT HOW IT BEHAVES.
DYNAMIC ANALYSIS
DEFINITION 
The 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 helps 
ensure that an adequate slice of the program's set of possible 
behaviors has been observed.
CALLSTACK 
A callstack is the order in which statements are exectuted. 
A common callstack, is an Exception trace. This trace shows all 
the statements executed before an exception is thrown.
CALLGRAPH 
A callgraph is a visual representation of a callstack. 
In large applications this graph can give you better insight on how 
an application is wired.
PROFILING DATA 
Usually, the data gathered with a profiler can be represented in 
stacks or graphs. 
They can include information regarding memory- and cpu-usage.
WHY?
REASONS 
Debugging CPU performance 
Debugging memory performance 
Debugging IO performance 
See what function is called how much 
Gain insight of the black box that is the application
REASONS 
We live in a digital age where we want everything instantly. 
According to a case study from Radware 
, 51 percent of online 
shoppers in the U.S claimed if a site is too slow they will not 
complete a purchase. 
Nowadays, search engine indexing also accounts for page load. 
The psychology of web performance 
SEO 101: How important is site speed in 2014? 
Case study from Radware
WARNING! 
Premature optimization is the root of all evil 
-- Donald Knuth 
Only perform optimization when there is a need to.
CAUSE OF ISSUES
COMMON ISSUES 
Network slowdown 
Datastore slowdown 
External resources (API, Filesystems, Network sockets, etc) 
Bad code(tm)
ACTIVE VS PASSIVE 
Profiling PHP Part 1 (Davey Shafik)
ACTIVE PROFILER 
Used during development 
Gather more information than passive profilers 
Performance impact is bigger 
Should _NOT_ be used in production 
Example: Xdebug
PASSIVE PROFILER 
Minimal impact on performance 
Gathers less but sufficient information to diagnose issue 
Examples: XHProf, New Relic, Blackfire.io
XDEBUG
XDEBUG 
Generates cachegrind 
files (like Valgrind for C) 
Can be analysed by KCacheGrind among others 
Cachegrind files are relatively big in size 
Also a developer tool for breakpoints and remote debugging 
Active profiler
ENABLE XDEBUG PROFILING 
# php.ini settings 
xdebug.profiler_enable=1 
xdebug.profiler_output_dir=/path/to/store/snapshots 
xdebug.profiler_enable_trigger=1
XDEBUG WITH KCACHEGRIND
XDEBUG IN PHPSTORM
XDEBUG IN PHPSTORM
XDEBUG IN PHPSTORM
XDEBUG IN PHPSTORM
XHPROF
XHPROF 
Developed by Facebook and released as open-source in 2009 
PECL extension 
Lightweight on profiling data 
Lightweight on profiling performance hit 
Passive profiler 
Includes webgui for reviewing and comparing profiling data
INSTALLATION 
# 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 
/! NOTE: Don't forget to restart if running through a webserver /!
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");
CALLSTACK
CALLGRAPH
CALLGRAPH
CALLGRAPH
USEFUL TOOLS 
XHProf Helper for Chrome 
XHProf Helper for Firefox 
Sets $_COOKIE['_profile'] to 1
XHGUI 
Web frontend for profile data 
Requires MongoDB 
Shows callstacks 
Shows callgraphs 
Can compare different runs
XHGUI
XHGUI
XHGUI COMPARE
XHGUI COMPARE
XHGUI COMPARE DIFFERENCE
XHGUI CALLSTACK
LINK0
LINK0/PROFILER 
Focused on XHProf 
Has multiple persistence layers for storing profiles 
Memory 
Flysystem 
ZendDbAdapter 
MongoDB (work in progress) 
Available on composer/packagist 
Fully Object-orientated 
100% code coverage 
http://github.com/link0/profiler
GETTING STARTED 
Bootstrapping the profiler 
$profiler = new Link0ProfilerProfiler(); 
$profiler->start(); 
print_r($profiler->stop()); 
Adding a PersistenceHandler 
$persistenceHandler = new Link0ProfilerPersistenceHandlerMemoryHandler(); 
$ profiler = new Link0ProfilerProfiler( 
$persistenceHandler); 
Flysystem example 
$filesystemAdapter = new LeagueFlysystemAdapterLocal('/tmp/profiler'); 
$ $filesystemAdapter); 
filesystem = new Link0ProfilerFilesystem( 
persistenceHandler = new Link0ProfilerPersistenceHandlerFilesystemHandler( 
profiler = new Link0ProfilerProfiler( 
$$ 
$persistenceHandler);
FUTURE? 
*EXCITING SOUNDS*
SOME IDEAS 
Enable on production with sampling 
Aggregate all profiles to centralized machine/cluster 
Integrate into continuous deployment 
Run profiling on acceptance environment 
Alert when compared differences surpass threshold 
Codeception integration 
Find business use-cases that are slow 
Make a case for refactoring to the business 
Focus on the customers emulated experience
ANY QUESTIONS?
USEFUL LINKS 
Profiling PHP with PhpStorm and Xdebug 
Profiling PHP with PhpStorm and Zend Debugger 
XDebug Profiler documentation 
XHProf PHP documentation 
Profiling with XHProf and XHGui 
http://github.com/link0/profiler
I <3 FEEDBACK 
Joind.in: 
GitHub: 
Twitter: 
IRC: link0 on Freenode 
https://joind.in/12802 
http://github.com/dennisdegreef 
@dennisdegreef 
#amsterdamphp 
SLIDES ARE ALSO ON JOIND.IN

More Related Content

Similar to Profiling PHP - AmsterdamPHP Meetup - 2014-11-20 (20)

ODP
Profiling PHP & Javascript
Dave Ross
 
PPTX
PHP Profiling/performance
Nicolas Degardin
 
PPTX
Profiling and Tuning a Web Application - The Dirty Details
Achievers Tech
 
PDF
Performance Profiling Tools and Tricks
Phase2
 
PDF
Performance Profiling Tools & Tricks
All Things Open
 
PPTX
Performance Tuning with XHProf
Salesforce Engineering
 
PDF
Performance profiling and testing of symfony application 2
Andrew Yatsenko
 
PDF
Python performance profiling
Jon Haddad
 
PDF
Deep into your applications, performance & profiling
Fabien Arcellier
 
PDF
Profiling and inspection with Blackfire.io
Emir Beganović
 
PDF
Performance and optimization
markstory
 
PDF
Profiling PHP with Xdebug / Webgrind
Sam Keen
 
PDF
[ CNCF Q1 2024 ] Intro to Continuous Profiling and Grafana Pyroscope.pdf
Steve Caron
 
PPT
Php Code Profiling Using X Debug
SergeyChernyshev
 
PDF
Php code profiling_using_x_debug
Gennady Feldman
 
PPTX
Northeast PHP - High Performance PHP
Jonathan Klein
 
PDF
stackconf 2022: Optimize Performance with Continuous Production Profiling
NETWAYS
 
PPT
PHP: Debugger, Profiler and more
Võ Duy Tuấn
 
PDF
Performance and optimization CakeFest 2014
markstory
 
PPTX
Resolving problems & high availability
Zend by Rogue Wave Software
 
Profiling PHP & Javascript
Dave Ross
 
PHP Profiling/performance
Nicolas Degardin
 
Profiling and Tuning a Web Application - The Dirty Details
Achievers Tech
 
Performance Profiling Tools and Tricks
Phase2
 
Performance Profiling Tools & Tricks
All Things Open
 
Performance Tuning with XHProf
Salesforce Engineering
 
Performance profiling and testing of symfony application 2
Andrew Yatsenko
 
Python performance profiling
Jon Haddad
 
Deep into your applications, performance & profiling
Fabien Arcellier
 
Profiling and inspection with Blackfire.io
Emir Beganović
 
Performance and optimization
markstory
 
Profiling PHP with Xdebug / Webgrind
Sam Keen
 
[ CNCF Q1 2024 ] Intro to Continuous Profiling and Grafana Pyroscope.pdf
Steve Caron
 
Php Code Profiling Using X Debug
SergeyChernyshev
 
Php code profiling_using_x_debug
Gennady Feldman
 
Northeast PHP - High Performance PHP
Jonathan Klein
 
stackconf 2022: Optimize Performance with Continuous Production Profiling
NETWAYS
 
PHP: Debugger, Profiler and more
Võ Duy Tuấn
 
Performance and optimization CakeFest 2014
markstory
 
Resolving problems & high availability
Zend by Rogue Wave Software
 

Recently uploaded (20)

PDF
Driver Easy Pro 6.1.1 Crack Licensce key 2025 FREE
utfefguu
 
PDF
4K Video Downloader Plus Pro Crack for MacOS New Download 2025
bashirkhan333g
 
PDF
SAP Firmaya İade ABAB Kodları - ABAB ile yazılmıl hazır kod örneği
Salih Küçük
 
PPTX
OpenChain @ OSS NA - In From the Cold: Open Source as Part of Mainstream Soft...
Shane Coughlan
 
PDF
The 5 Reasons for IT Maintenance - Arna Softech
Arna Softech
 
PDF
Download Canva Pro 2025 PC Crack Full Latest Version
bashirkhan333g
 
PDF
NEW-Viral>Wondershare Filmora 14.5.18.12900 Crack Free
sherryg1122g
 
PPTX
Empowering Asian Contributions: The Rise of Regional User Groups in Open Sour...
Shane Coughlan
 
PDF
Technical-Careers-Roadmap-in-Software-Market.pdf
Hussein Ali
 
PDF
Digger Solo: Semantic search and maps for your local files
seanpedersen96
 
PDF
유니티에서 Burst Compiler+ThreadedJobs+SIMD 적용사례
Seongdae Kim
 
PPTX
In From the Cold: Open Source as Part of Mainstream Software Asset Management
Shane Coughlan
 
PDF
Open Chain Q2 Steering Committee Meeting - 2025-06-25
Shane Coughlan
 
PDF
Wondershare PDFelement Pro Crack for MacOS New Version Latest 2025
bashirkhan333g
 
PDF
Build It, Buy It, or Already Got It? Make Smarter Martech Decisions
bbedford2
 
PPTX
Foundations of Marketo Engage - Powering Campaigns with Marketo Personalization
bbedford2
 
PPTX
Agentic Automation: Build & Deploy Your First UiPath Agent
klpathrudu
 
PPTX
AEM User Group: India Chapter Kickoff Meeting
jennaf3
 
PPTX
ChiSquare Procedure in IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
PPTX
Milwaukee Marketo User Group - Summer Road Trip: Mapping and Personalizing Yo...
bbedford2
 
Driver Easy Pro 6.1.1 Crack Licensce key 2025 FREE
utfefguu
 
4K Video Downloader Plus Pro Crack for MacOS New Download 2025
bashirkhan333g
 
SAP Firmaya İade ABAB Kodları - ABAB ile yazılmıl hazır kod örneği
Salih Küçük
 
OpenChain @ OSS NA - In From the Cold: Open Source as Part of Mainstream Soft...
Shane Coughlan
 
The 5 Reasons for IT Maintenance - Arna Softech
Arna Softech
 
Download Canva Pro 2025 PC Crack Full Latest Version
bashirkhan333g
 
NEW-Viral>Wondershare Filmora 14.5.18.12900 Crack Free
sherryg1122g
 
Empowering Asian Contributions: The Rise of Regional User Groups in Open Sour...
Shane Coughlan
 
Technical-Careers-Roadmap-in-Software-Market.pdf
Hussein Ali
 
Digger Solo: Semantic search and maps for your local files
seanpedersen96
 
유니티에서 Burst Compiler+ThreadedJobs+SIMD 적용사례
Seongdae Kim
 
In From the Cold: Open Source as Part of Mainstream Software Asset Management
Shane Coughlan
 
Open Chain Q2 Steering Committee Meeting - 2025-06-25
Shane Coughlan
 
Wondershare PDFelement Pro Crack for MacOS New Version Latest 2025
bashirkhan333g
 
Build It, Buy It, or Already Got It? Make Smarter Martech Decisions
bbedford2
 
Foundations of Marketo Engage - Powering Campaigns with Marketo Personalization
bbedford2
 
Agentic Automation: Build & Deploy Your First UiPath Agent
klpathrudu
 
AEM User Group: India Chapter Kickoff Meeting
jennaf3
 
ChiSquare Procedure in IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
Milwaukee Marketo User Group - Summer Road Trip: Mapping and Personalizing Yo...
bbedford2
 
Ad

Profiling PHP - AmsterdamPHP Meetup - 2014-11-20

  • 1. PROFILING PHP A DIVE INTO YOUR APPLICATION Dennis de Greef / @dennisdegreef AmsterdamPHP
  • 3. DEFINITION profiling is a form of dynamic program analysis that measures, for example, the space (memory) or time complexity of a program, the usage of particular instructions, or the frequency and duration of function calls. Most commonly, profiling information serves to aid program optimization.
  • 5. YEAH... LETS FIRST LOOK AT IT'S COUNTERPART...
  • 7. DEFINITION Static 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 an automated tool, with human analysis being called program understanding, program comprehension or code review.
  • 8. STATIC ANALYSIS TOOLS There are a set of tools which perform static code analysis. These tools can be integrated within an automated build. PHP Mess Detector PHP Copy/Paste Detector PHP CodeSniffer PHP Dead Code Detector There is a nice page containing a predefined set of tools for a build to be found at Jenkins PHP
  • 10. THESE TOOLS ONLY ANALYSE HOW YOUR CODE IS STRUCTURED, NOT HOW IT BEHAVES.
  • 12. DEFINITION The 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 helps ensure that an adequate slice of the program's set of possible behaviors has been observed.
  • 13. CALLSTACK A callstack is the order in which statements are exectuted. A common callstack, is an Exception trace. This trace shows all the statements executed before an exception is thrown.
  • 14. CALLGRAPH A callgraph is a visual representation of a callstack. In large applications this graph can give you better insight on how an application is wired.
  • 15. PROFILING DATA Usually, the data gathered with a profiler can be represented in stacks or graphs. They can include information regarding memory- and cpu-usage.
  • 16. WHY?
  • 17. REASONS Debugging CPU performance Debugging memory performance Debugging IO performance See what function is called how much Gain insight of the black box that is the application
  • 18. REASONS We live in a digital age where we want everything instantly. According to a case study from Radware , 51 percent of online shoppers in the U.S claimed if a site is too slow they will not complete a purchase. Nowadays, search engine indexing also accounts for page load. The psychology of web performance SEO 101: How important is site speed in 2014? Case study from Radware
  • 19. WARNING! Premature optimization is the root of all evil -- Donald Knuth Only perform optimization when there is a need to.
  • 21. COMMON ISSUES Network slowdown Datastore slowdown External resources (API, Filesystems, Network sockets, etc) Bad code(tm)
  • 22. ACTIVE VS PASSIVE Profiling PHP Part 1 (Davey Shafik)
  • 23. ACTIVE PROFILER Used during development Gather more information than passive profilers Performance impact is bigger Should _NOT_ be used in production Example: Xdebug
  • 24. PASSIVE PROFILER Minimal impact on performance Gathers less but sufficient information to diagnose issue Examples: XHProf, New Relic, Blackfire.io
  • 26. XDEBUG Generates cachegrind files (like Valgrind for C) Can be analysed by KCacheGrind among others Cachegrind files are relatively big in size Also a developer tool for breakpoints and remote debugging Active profiler
  • 27. ENABLE XDEBUG PROFILING # php.ini settings xdebug.profiler_enable=1 xdebug.profiler_output_dir=/path/to/store/snapshots xdebug.profiler_enable_trigger=1
  • 34. XHPROF Developed by Facebook and released as open-source in 2009 PECL extension Lightweight on profiling data Lightweight on profiling performance hit Passive profiler Includes webgui for reviewing and comparing profiling data
  • 35. INSTALLATION # 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 /! NOTE: Don't forget to restart if running through a webserver /!
  • 36. 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");
  • 41. USEFUL TOOLS XHProf Helper for Chrome XHProf Helper for Firefox Sets $_COOKIE['_profile'] to 1
  • 42. XHGUI Web frontend for profile data Requires MongoDB Shows callstacks Shows callgraphs Can compare different runs
  • 43. XHGUI
  • 44. XHGUI
  • 49. LINK0
  • 50. LINK0/PROFILER Focused on XHProf Has multiple persistence layers for storing profiles Memory Flysystem ZendDbAdapter MongoDB (work in progress) Available on composer/packagist Fully Object-orientated 100% code coverage http://github.com/link0/profiler
  • 51. GETTING STARTED Bootstrapping the profiler $profiler = new Link0ProfilerProfiler(); $profiler->start(); print_r($profiler->stop()); Adding a PersistenceHandler $persistenceHandler = new Link0ProfilerPersistenceHandlerMemoryHandler(); $ profiler = new Link0ProfilerProfiler( $persistenceHandler); Flysystem example $filesystemAdapter = new LeagueFlysystemAdapterLocal('/tmp/profiler'); $ $filesystemAdapter); filesystem = new Link0ProfilerFilesystem( persistenceHandler = new Link0ProfilerPersistenceHandlerFilesystemHandler( profiler = new Link0ProfilerProfiler( $$ $persistenceHandler);
  • 53. SOME IDEAS Enable on production with sampling Aggregate all profiles to centralized machine/cluster Integrate into continuous deployment Run profiling on acceptance environment Alert when compared differences surpass threshold Codeception integration Find business use-cases that are slow Make a case for refactoring to the business Focus on the customers emulated experience
  • 55. USEFUL LINKS Profiling PHP with PhpStorm and Xdebug Profiling PHP with PhpStorm and Zend Debugger XDebug Profiler documentation XHProf PHP documentation Profiling with XHProf and XHGui http://github.com/link0/profiler
  • 56. I <3 FEEDBACK Joind.in: GitHub: Twitter: IRC: link0 on Freenode https://joind.in/12802 http://github.com/dennisdegreef @dennisdegreef #amsterdamphp SLIDES ARE ALSO ON JOIND.IN