SlideShare a Scribd company logo
1 of 56
Download to read offline
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 PythonBrian Lyttle
 
Product! - The road to production deployment
Product! - The road to production deploymentProduct! - The road to production deployment
Product! - The road to production deploymentFilippo Zanella
 
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 4Darwin Biler
 
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 2016Joe Ferguson
 
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 AppsRon 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.pptxsarah david
 
GNUCITIZEN Dwk Owasp Day September 2007
GNUCITIZEN Dwk Owasp Day   September 2007GNUCITIZEN Dwk Owasp Day   September 2007
GNUCITIZEN Dwk Owasp Day September 2007guest20ab09
 
10 interesting things about java
10 interesting things about java10 interesting things about java
10 interesting things about javakanchanmahajan23
 
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 AirflowKaxil Naik
 
Aspect Oriented Software Development
Aspect Oriented Software DevelopmentAspect Oriented Software Development
Aspect Oriented Software DevelopmentJignesh 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 ContextLokendra Rawat
 
Shift Left Security
Shift Left SecurityShift Left Security
Shift Left Securitygjdevos
 
Automated User Tests with Apache Flex
Automated User Tests with Apache FlexAutomated User Tests with Apache Flex
Automated User Tests with Apache FlexGert Poppe
 

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

A Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxA Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxComplianceQuest1
 
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...kellynguyen01
 
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️anilsa9823
 
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...ICS
 
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...harshavardhanraghave
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...Health
 
HR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comHR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comFatema Valibhai
 
Unlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language ModelsUnlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language Modelsaagamshah0812
 
Software Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsSoftware Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsArshad QA
 
Diamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with PrecisionDiamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with PrecisionSolGuruz
 
How To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.jsHow To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.jsAndolasoft Inc
 
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerHow To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerThousandEyes
 
Hand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxHand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxbodapatigopi8531
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providermohitmore19
 
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Steffen Staab
 
5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdfWave PLM
 
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...OnePlan Solutions
 

Recently uploaded (20)

A Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxA Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docx
 
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
 
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️
 
Microsoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdfMicrosoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdf
 
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
 
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
 
HR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comHR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.com
 
Unlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language ModelsUnlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language Models
 
Software Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsSoftware Quality Assurance Interview Questions
Software Quality Assurance Interview Questions
 
Diamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with PrecisionDiamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with Precision
 
How To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.jsHow To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.js
 
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerHow To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
 
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICECHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
 
Hand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxHand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptx
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service provider
 
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS LiveVip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
 
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
 
5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf
 
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
 

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