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

Introduction to Google App Engine with Python
Introduction to Google App Engine with PythonIntroduction to Google App Engine with Python
Introduction to Google App Engine with Python
Brian Lyttle
 
WoMakersCode 2016 - Shit Happens
WoMakersCode 2016 -  Shit HappensWoMakersCode 2016 -  Shit Happens
WoMakersCode 2016 - Shit Happens
Jackson F. de A. Mafra
 
Product! - The road to production deployment
Product! - The road to production deploymentProduct! - The road to production deployment
Product! - The road to production deployment
Filippo Zanella
 
cakephp UDUYKTHA (1)
cakephp UDUYKTHA (1)cakephp UDUYKTHA (1)
cakephp UDUYKTHA (1)
Varsha Krishna
 
Throwing Laravel into your Legacy App™
Throwing Laravel into your Legacy App™Throwing Laravel into your Legacy App™
Throwing Laravel into your Legacy App™
Joe Ferguson
 
So You Just Inherited a $Legacy Application...
So You Just Inherited a $Legacy Application...So You Just Inherited a $Legacy Application...
So You Just Inherited a $Legacy Application...
Joe Ferguson
 
Building Large Scale PHP Web Applications with Laravel 4
Building Large Scale PHP Web Applications with Laravel 4Building Large Scale PHP Web Applications with Laravel 4
Building Large Scale PHP Web Applications with Laravel 4
Darwin Biler
 
Lamp Zend Security
Lamp Zend SecurityLamp Zend Security
Lamp Zend Security
Ram Srivastava
 
So You Just Inherited a $Legacy Application… NomadPHP July 2016
So You Just Inherited a $Legacy Application… NomadPHP July 2016So You Just Inherited a $Legacy Application… NomadPHP July 2016
So You Just Inherited a $Legacy Application… NomadPHP July 2016
Joe Ferguson
 
Php security
Php securityPhp security
Php security
Karthik Vikarm
 
MobSecCon 2015 - Dynamic Analysis of Android Apps
MobSecCon 2015 - Dynamic Analysis of Android AppsMobSecCon 2015 - Dynamic Analysis of Android Apps
MobSecCon 2015 - Dynamic Analysis of Android Apps
Ron Munitz
 
8_reasons_php_developers_love_using_laravel.pptx
8_reasons_php_developers_love_using_laravel.pptx8_reasons_php_developers_love_using_laravel.pptx
8_reasons_php_developers_love_using_laravel.pptx
sarah david
 
GNUCITIZEN Dwk Owasp Day September 2007
GNUCITIZEN Dwk Owasp Day   September 2007GNUCITIZEN Dwk Owasp Day   September 2007
GNUCITIZEN Dwk Owasp Day September 2007
guest20ab09
 
10 interesting things about java
10 interesting things about java10 interesting things about java
10 interesting things about java
kanchanmahajan23
 
Building and deploying LLM applications with Apache Airflow
Building and deploying LLM applications with Apache AirflowBuilding and deploying LLM applications with Apache Airflow
Building and deploying LLM applications with Apache Airflow
Kaxil Naik
 
Aspect Oriented Software Development
Aspect Oriented Software DevelopmentAspect Oriented Software Development
Aspect Oriented Software Development
Jignesh Patel
 
Software Reverse Engineering in a Security Context
Software Reverse Engineering in a Security ContextSoftware Reverse Engineering in a Security Context
Software Reverse Engineering in a Security Context
Lokendra Rawat
 
Shift Left Security
Shift Left SecurityShift Left Security
Shift Left Security
gjdevos
 
Automated User Tests with Apache Flex
Automated User Tests with Apache FlexAutomated User Tests with Apache Flex
Automated User Tests with Apache Flex
Gert Poppe
 
My Saminar On Php
My Saminar On PhpMy Saminar On Php
My Saminar On Php
Arjun Kumawat
 

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

Introduction to Google App Engine with Python
Introduction to Google App Engine with PythonIntroduction to Google App Engine with Python
Introduction to Google App Engine with Python
 
WoMakersCode 2016 - Shit Happens
WoMakersCode 2016 -  Shit HappensWoMakersCode 2016 -  Shit Happens
WoMakersCode 2016 - Shit Happens
 
Product! - The road to production deployment
Product! - The road to production deploymentProduct! - The road to production deployment
Product! - The road to production deployment
 
cakephp UDUYKTHA (1)
cakephp UDUYKTHA (1)cakephp UDUYKTHA (1)
cakephp UDUYKTHA (1)
 
Throwing Laravel into your Legacy App™
Throwing Laravel into your Legacy App™Throwing Laravel into your Legacy App™
Throwing Laravel into your Legacy App™
 
So You Just Inherited a $Legacy Application...
So You Just Inherited a $Legacy Application...So You Just Inherited a $Legacy Application...
So You Just Inherited a $Legacy Application...
 
Building Large Scale PHP Web Applications with Laravel 4
Building Large Scale PHP Web Applications with Laravel 4Building Large Scale PHP Web Applications with Laravel 4
Building Large Scale PHP Web Applications with Laravel 4
 
Lamp Zend Security
Lamp Zend SecurityLamp Zend Security
Lamp Zend Security
 
So You Just Inherited a $Legacy Application… NomadPHP July 2016
So You Just Inherited a $Legacy Application… NomadPHP July 2016So You Just Inherited a $Legacy Application… NomadPHP July 2016
So You Just Inherited a $Legacy Application… NomadPHP July 2016
 
Php security
Php securityPhp security
Php security
 
MobSecCon 2015 - Dynamic Analysis of Android Apps
MobSecCon 2015 - Dynamic Analysis of Android AppsMobSecCon 2015 - Dynamic Analysis of Android Apps
MobSecCon 2015 - Dynamic Analysis of Android Apps
 
8_reasons_php_developers_love_using_laravel.pptx
8_reasons_php_developers_love_using_laravel.pptx8_reasons_php_developers_love_using_laravel.pptx
8_reasons_php_developers_love_using_laravel.pptx
 
GNUCITIZEN Dwk Owasp Day September 2007
GNUCITIZEN Dwk Owasp Day   September 2007GNUCITIZEN Dwk Owasp Day   September 2007
GNUCITIZEN Dwk Owasp Day September 2007
 
10 interesting things about java
10 interesting things about java10 interesting things about java
10 interesting things about java
 
Building and deploying LLM applications with Apache Airflow
Building and deploying LLM applications with Apache AirflowBuilding and deploying LLM applications with Apache Airflow
Building and deploying LLM applications with Apache Airflow
 
Aspect Oriented Software Development
Aspect Oriented Software DevelopmentAspect Oriented Software Development
Aspect Oriented Software Development
 
Software Reverse Engineering in a Security Context
Software Reverse Engineering in a Security ContextSoftware Reverse Engineering in a Security Context
Software Reverse Engineering in a Security Context
 
Shift Left Security
Shift Left SecurityShift Left Security
Shift Left Security
 
Automated User Tests with Apache Flex
Automated User Tests with Apache FlexAutomated User Tests with Apache Flex
Automated User Tests with Apache Flex
 
My Saminar On Php
My Saminar On PhpMy Saminar On Php
My Saminar On Php
 

Recently uploaded

Top Features to Include in Your Winzo Clone App for Business Growth (4).pptx
Top Features to Include in Your Winzo Clone App for Business Growth (4).pptxTop Features to Include in Your Winzo Clone App for Business Growth (4).pptx
Top Features to Include in Your Winzo Clone App for Business Growth (4).pptx
rickgrimesss22
 
socradar-q1-2024-aviation-industry-report.pdf
socradar-q1-2024-aviation-industry-report.pdfsocradar-q1-2024-aviation-industry-report.pdf
socradar-q1-2024-aviation-industry-report.pdf
SOCRadar
 
OpenMetadata Community Meeting - 5th June 2024
OpenMetadata Community Meeting - 5th June 2024OpenMetadata Community Meeting - 5th June 2024
OpenMetadata Community Meeting - 5th June 2024
OpenMetadata
 
GOING AOT WITH GRAALVM FOR SPRING BOOT (SPRING IO)
GOING AOT WITH GRAALVM FOR  SPRING BOOT (SPRING IO)GOING AOT WITH GRAALVM FOR  SPRING BOOT (SPRING IO)
GOING AOT WITH GRAALVM FOR SPRING BOOT (SPRING IO)
Alina Yurenko
 
Energy consumption of Database Management - Florina Jonuzi
Energy consumption of Database Management - Florina JonuziEnergy consumption of Database Management - Florina Jonuzi
Energy consumption of Database Management - Florina Jonuzi
Green Software Development
 
SWEBOK and Education at FUSE Okinawa 2024
SWEBOK and Education at FUSE Okinawa 2024SWEBOK and Education at FUSE Okinawa 2024
SWEBOK and Education at FUSE Okinawa 2024
Hironori Washizaki
 
openEuler Case Study - The Journey to Supply Chain Security
openEuler Case Study - The Journey to Supply Chain SecurityopenEuler Case Study - The Journey to Supply Chain Security
openEuler Case Study - The Journey to Supply Chain Security
Shane Coughlan
 
Neo4j - Product Vision and Knowledge Graphs - GraphSummit Paris
Neo4j - Product Vision and Knowledge Graphs - GraphSummit ParisNeo4j - Product Vision and Knowledge Graphs - GraphSummit Paris
Neo4j - Product Vision and Knowledge Graphs - GraphSummit Paris
Neo4j
 
Microservice Teams - How the cloud changes the way we work
Microservice Teams - How the cloud changes the way we workMicroservice Teams - How the cloud changes the way we work
Microservice Teams - How the cloud changes the way we work
Sven Peters
 
Essentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FMEEssentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FME
Safe Software
 
Introducing Crescat - Event Management Software for Venues, Festivals and Eve...
Introducing Crescat - Event Management Software for Venues, Festivals and Eve...Introducing Crescat - Event Management Software for Venues, Festivals and Eve...
Introducing Crescat - Event Management Software for Venues, Festivals and Eve...
Crescat
 
GreenCode-A-VSCode-Plugin--Dario-Jurisic
GreenCode-A-VSCode-Plugin--Dario-JurisicGreenCode-A-VSCode-Plugin--Dario-Jurisic
GreenCode-A-VSCode-Plugin--Dario-Jurisic
Green Software Development
 
A Study of Variable-Role-based Feature Enrichment in Neural Models of Code
A Study of Variable-Role-based Feature Enrichment in Neural Models of CodeA Study of Variable-Role-based Feature Enrichment in Neural Models of Code
A Study of Variable-Role-based Feature Enrichment in Neural Models of Code
Aftab Hussain
 
May Marketo Masterclass, London MUG May 22 2024.pdf
May Marketo Masterclass, London MUG May 22 2024.pdfMay Marketo Masterclass, London MUG May 22 2024.pdf
May Marketo Masterclass, London MUG May 22 2024.pdf
Adele Miller
 
E-commerce Development Services- Hornet Dynamics
E-commerce Development Services- Hornet DynamicsE-commerce Development Services- Hornet Dynamics
E-commerce Development Services- Hornet Dynamics
Hornet Dynamics
 
How to write a program in any programming language
How to write a program in any programming languageHow to write a program in any programming language
How to write a program in any programming language
Rakesh Kumar R
 
Utilocate provides Smarter, Better, Faster, Safer Locate Ticket Management
Utilocate provides Smarter, Better, Faster, Safer Locate Ticket ManagementUtilocate provides Smarter, Better, Faster, Safer Locate Ticket Management
Utilocate provides Smarter, Better, Faster, Safer Locate Ticket Management
Utilocate
 
UI5con 2024 - Boost Your Development Experience with UI5 Tooling Extensions
UI5con 2024 - Boost Your Development Experience with UI5 Tooling ExtensionsUI5con 2024 - Boost Your Development Experience with UI5 Tooling Extensions
UI5con 2024 - Boost Your Development Experience with UI5 Tooling Extensions
Peter Muessig
 
Graspan: A Big Data System for Big Code Analysis
Graspan: A Big Data System for Big Code AnalysisGraspan: A Big Data System for Big Code Analysis
Graspan: A Big Data System for Big Code Analysis
Aftab Hussain
 
Oracle Database 19c New Features for DBAs and Developers.pptx
Oracle Database 19c New Features for DBAs and Developers.pptxOracle Database 19c New Features for DBAs and Developers.pptx
Oracle Database 19c New Features for DBAs and Developers.pptx
Remote DBA Services
 

Recently uploaded (20)

Top Features to Include in Your Winzo Clone App for Business Growth (4).pptx
Top Features to Include in Your Winzo Clone App for Business Growth (4).pptxTop Features to Include in Your Winzo Clone App for Business Growth (4).pptx
Top Features to Include in Your Winzo Clone App for Business Growth (4).pptx
 
socradar-q1-2024-aviation-industry-report.pdf
socradar-q1-2024-aviation-industry-report.pdfsocradar-q1-2024-aviation-industry-report.pdf
socradar-q1-2024-aviation-industry-report.pdf
 
OpenMetadata Community Meeting - 5th June 2024
OpenMetadata Community Meeting - 5th June 2024OpenMetadata Community Meeting - 5th June 2024
OpenMetadata Community Meeting - 5th June 2024
 
GOING AOT WITH GRAALVM FOR SPRING BOOT (SPRING IO)
GOING AOT WITH GRAALVM FOR  SPRING BOOT (SPRING IO)GOING AOT WITH GRAALVM FOR  SPRING BOOT (SPRING IO)
GOING AOT WITH GRAALVM FOR SPRING BOOT (SPRING IO)
 
Energy consumption of Database Management - Florina Jonuzi
Energy consumption of Database Management - Florina JonuziEnergy consumption of Database Management - Florina Jonuzi
Energy consumption of Database Management - Florina Jonuzi
 
SWEBOK and Education at FUSE Okinawa 2024
SWEBOK and Education at FUSE Okinawa 2024SWEBOK and Education at FUSE Okinawa 2024
SWEBOK and Education at FUSE Okinawa 2024
 
openEuler Case Study - The Journey to Supply Chain Security
openEuler Case Study - The Journey to Supply Chain SecurityopenEuler Case Study - The Journey to Supply Chain Security
openEuler Case Study - The Journey to Supply Chain Security
 
Neo4j - Product Vision and Knowledge Graphs - GraphSummit Paris
Neo4j - Product Vision and Knowledge Graphs - GraphSummit ParisNeo4j - Product Vision and Knowledge Graphs - GraphSummit Paris
Neo4j - Product Vision and Knowledge Graphs - GraphSummit Paris
 
Microservice Teams - How the cloud changes the way we work
Microservice Teams - How the cloud changes the way we workMicroservice Teams - How the cloud changes the way we work
Microservice Teams - How the cloud changes the way we work
 
Essentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FMEEssentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FME
 
Introducing Crescat - Event Management Software for Venues, Festivals and Eve...
Introducing Crescat - Event Management Software for Venues, Festivals and Eve...Introducing Crescat - Event Management Software for Venues, Festivals and Eve...
Introducing Crescat - Event Management Software for Venues, Festivals and Eve...
 
GreenCode-A-VSCode-Plugin--Dario-Jurisic
GreenCode-A-VSCode-Plugin--Dario-JurisicGreenCode-A-VSCode-Plugin--Dario-Jurisic
GreenCode-A-VSCode-Plugin--Dario-Jurisic
 
A Study of Variable-Role-based Feature Enrichment in Neural Models of Code
A Study of Variable-Role-based Feature Enrichment in Neural Models of CodeA Study of Variable-Role-based Feature Enrichment in Neural Models of Code
A Study of Variable-Role-based Feature Enrichment in Neural Models of Code
 
May Marketo Masterclass, London MUG May 22 2024.pdf
May Marketo Masterclass, London MUG May 22 2024.pdfMay Marketo Masterclass, London MUG May 22 2024.pdf
May Marketo Masterclass, London MUG May 22 2024.pdf
 
E-commerce Development Services- Hornet Dynamics
E-commerce Development Services- Hornet DynamicsE-commerce Development Services- Hornet Dynamics
E-commerce Development Services- Hornet Dynamics
 
How to write a program in any programming language
How to write a program in any programming languageHow to write a program in any programming language
How to write a program in any programming language
 
Utilocate provides Smarter, Better, Faster, Safer Locate Ticket Management
Utilocate provides Smarter, Better, Faster, Safer Locate Ticket ManagementUtilocate provides Smarter, Better, Faster, Safer Locate Ticket Management
Utilocate provides Smarter, Better, Faster, Safer Locate Ticket Management
 
UI5con 2024 - Boost Your Development Experience with UI5 Tooling Extensions
UI5con 2024 - Boost Your Development Experience with UI5 Tooling ExtensionsUI5con 2024 - Boost Your Development Experience with UI5 Tooling Extensions
UI5con 2024 - Boost Your Development Experience with UI5 Tooling Extensions
 
Graspan: A Big Data System for Big Code Analysis
Graspan: A Big Data System for Big Code AnalysisGraspan: A Big Data System for Big Code Analysis
Graspan: A Big Data System for Big Code Analysis
 
Oracle Database 19c New Features for DBAs and Developers.pptx
Oracle Database 19c New Features for DBAs and Developers.pptxOracle Database 19c New Features for DBAs and Developers.pptx
Oracle Database 19c New Features for DBAs and Developers.pptx
 

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