SlideShare a Scribd company logo
PHP Profiling/performance
PHP profiling/perfomance
Processing time,
Transmission time
CPU Bandwith
Memory
I/O
PHP profiling/perfomance
• Expected traffic
• Traffic growth
• Scalability means
• Evolutions to come…
Set the context
• Contextually : search engine, file generation,
task…
Establish the objectives
PHP profiling/perfomance
•Language
•Framework
•Libraries
•…
Adapt technologies
PHP profiling/perfomance
• Smart, structured code
• Lazy loading
• Static variables (singletons/registries)
• Cache
Code
• Autoloader
• Reduce and order include_path
• Client cache (HTTP headers Cache-Control, Expire)
• Avoid regexps
• …
Good practices
• Php.ini : realpath_cache_size
• Nginx : worker_processes, open_file_cache, multi_accept, keepalive, gzip
• Cache op-code
• Disable logs
Server configuration
PHP profiling/perfomance
• Ponctual inefficiencies/bottlenecks
detection
• Monitoring (analytics, alert tools)
• Take comparison points (before/after)
• Know when to stop
• Never rely on intuition!
Measure
PHP profiling/perfomance
Load tests
• ApacheBenchmark
• Siege
• Httperf
• …
Profiling
• Xdebug
• XHProf
• …
Measuring
tools
PHP profiling/perfomance
ApacheBenchmark
ab -n 50 -c 2 -v 1 -p 'post.txt' -T 'application/json'
-H 'Authorization: Bearer eyJ0eXAi.eyJpc3nYXJ.KznbYbc'
'http://api.gma.my/account/authenticate?alt=json&metadata
[foo]=Hello&metadata[bar]=World&metadata[random Val]=Ted'
> bench.txt
{
"application_key":"afaf770",
"email":"ndegardin@tribalnova.com",
"key":"ndegardin«
}
post.txt
PHP profiling/perfomance
…
Concurrency Level: 2
Time taken for tests: 25.704 seconds
Complete requests: 50
Failed requests: 44
(Connect: 0, Receive: 0, Length: 44, Exceptions: 0)
Write errors: 0
Non-2xx responses: 50
Total transferred: 2231974 bytes
Total POSTed: 28100
HTML transferred: 2220474 bytes
Requests per second: 1.95 [#/sec] (mean)
Time per request: 1028.176 [ms] (mean)
Time per request: 514.088 [ms] (mean, across all concurrent requests)
Transfer rate: 84.80 [Kbytes/sec] received
1.07 kb/s sent
85.86 kb/s total
Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 0 0.1 0 0
Processing: 704 1014 154.5 990 1693
Waiting: 704 1014 154.5 990 1693
Total: 705 1015 154.5 990 1693
…
bench.txt
PHP profiling/perfomance
• PHP profiling extension
• Developed by Facebook
• The basic version stores traces
• I diddn’t succeed in using the bundled
PHP/HTML app…
• With XHPROF, the profiling is triggered by
calling xhprof_enable(), xhprof_disable()
• Cool solution : auto_append_file,
auto_prepend_file in php.ini
XHProf
PHP profiling/perfomance
• Website http://xhprof.io
• Sources https://github.com/gajus/xhprof.io
• Profiling stored in database
• php.ini
• auto_append_file=/var/www/xhprofio/inc/append.php
• auto_prepend_file=/var/www/xhprofio/inc/prepend.php
• Not my recommandation
XHProf.io
PHP profiling/perfomance
• Sources https://github.com/sepehr/xhprof
• Profiling stored in database (one table « details »)
• Very good profiler
• php.ini
• auto_prepend_file=/var/www/xhprof/external/header.php
• auto_append_file=/var/www/xhprof/external/footer.php
• In CGI :
• activation by cookie _profile=1
• or by adding the parameter _profile=1 to the URL (adds the cookie,
then redirects)
• In CLI :
• auto_prepend/auto_append in/etc/php5/cli/php.ini
• activation by « export XHPROF_PROFILE=1 »
XHGUI
PHP profiling/perfomance
• Activate profiling by vhost in Apache
• Activate profiling by using a nginx/PHP-FPM pool :
/etc/php5/fpm/pool.d/www.conf :
• Activate profiling by vhost in nginx/PHP-FPM, in a
location section
XHProf notes
fastcgi_param PHP_ADMIN_VALUE
"auto_prepend_file=/var/www/xhprof/external/header.phpnauto_append_file=/var/
www/xhprof/external/footer.php";
php_admin_value auto_prepend_file "/var/www/xhprof/external/header.php"
php_admin_value auto_append_file "/var/www/xhprof/external/footer.php"
php_admin_value[auto_prepend_file] = /var/www/xhprofio/inc/prepend.php"
php_admin_value[auto_appendfile] = /var/www/xhprofio/inc/append.php"
PHP profiling/perfomance
• Web site http://xdebug.org/
• Developed by Derik Rethans
• PHP extension, now generally bundled with PHP
• Features :
• Debugging functions (time, calls, scream)
• Maximum nesting protection
• Enhanced stacktraces
• Remote debugging
• Profiling
• Code coverage
Xdebug
PHP profiling/perfomance
Xdebug notes about debugging
zend_extension=/usr/lib/php5/20090626/xdebug.so
xdebug.remote_enable = 1
xdebug.remote_handler = "dbgp"
xdebug.remote_port = 9000
xdebug.remote_mode = req
xdebug.remote_host = 192.168.56.1
xdebug.profiler_enable = 1
xdebug.profiler_output_dir = /var/log/xdebug
xdebug.profiler_output_name =
cachegrind.out.%H_%s_%R.xt
php.ini
PHP profiling/perfomance
• Prevent nginx from sending a gateway timeout (504) :
in /etc/nginx/nginx.conf, add
• Remote debug events triggered externally by setting a
XDEBUG_SESSION cookie and, if needed, mapping
paths (client/server) in your IDE
• Remote debug CLI scripts by running export
XDEBUG_CONFIG="idekey=netbeans-xdebug"and
setting php.ini xdebug.remote_host
Xdebug notes about debugging
fastcgi_read_timeout 3000;
keepalive_timeout 3000;
keepalive_requests 3000;
client_header_timeout 3000;
client_body_timeout 3000;
nginx.conf
PHP profiling/perfomance
• The traces are stored as files
• It often weights about 2Mo (be careful when forgetting it)
• Using a fixed name (without timestamp) in php.ini
xdebug.profiler.output_name allows a run script to erase the
previous one
• No official Xdebug trace extension, I suggest
cachegrind.out.[…].xt
• Profilers :
• WinCacheGrind (Windows) : somewhat simple
• WebGrind (Web) : « For quick’n’dirty optimizations it does the
job »
• KCacheGrind (KDE) : the best one
• PHPStorm : not really useful
Xdebug notes about profiling
PHP profiling/perfomance
• To run it under Windows, use KDE4Win
(package system similar to Cygwin’s one),
it should be in the « kdesdk » package.
Not available on all mirrors.
• Install GraphViz to enable drawing some
charts (and add its /bin directory the the
PATH)
• Check the Call Graph view!
kCacheGrind
PHP profiling/perfomance
• About different times :
• The wall time includes I/O (network latency, database
accesses, waiting for an user input…)
• The including time includes the children call
• The CPU exclusion time (self time in KCacheGrind) is the
more relevant value
• Switch between display formats :
• Percentage is better for comparing values
• Time is better for assessing values
• Tweaking script parameters (input configuration, number
of entries processed in database, number of iterations…)
may allow to observe different phenomena
Notes about profiling
Nicolas Degardin
ndegardin@tribalnova.com
Merci !

More Related Content

PPTX
Php internal architecture
PDF
Unleashing Creative Freedom with MODX (2015-07-21 @ PHP FRL)
PDF
Go for SysAdmins - LISA 2015
PPTX
Taming the resource tiger
PDF
On the Edge Systems Administration with Golang
PPTX
PyGrunn 2017 - Django Performance Unchained - slides
PDF
Shiny, Let’s Be Bad Guys: Exploiting and Mitigating the Top 10 Web App Vulner...
PDF
Gajendra sharma Drupal Module development
Php internal architecture
Unleashing Creative Freedom with MODX (2015-07-21 @ PHP FRL)
Go for SysAdmins - LISA 2015
Taming the resource tiger
On the Edge Systems Administration with Golang
PyGrunn 2017 - Django Performance Unchained - slides
Shiny, Let’s Be Bad Guys: Exploiting and Mitigating the Top 10 Web App Vulner...
Gajendra sharma Drupal Module development

What's hot (20)

PPTX
Php extensions
PDF
Django Documentation
PDF
OSCON2014 : Quick Introduction to System Tools Programming with Go
PDF
Code quality tools for dev
PDF
Web Development with Python and Django
PDF
Anatomy of PHP Shells
PPT
PHP - Introduction to PHP Date and Time Functions
PPTX
Pragmatic plone projects
PDF
eZ Find workshop: advanced insights & recipes
PPTX
10 Most Important Features of New PHP 5.6
ODP
PHP BASIC PRESENTATION
PDF
A content repository for your PHP application or CMS?
PPTX
Northeast PHP - High Performance PHP
PPT
Build Automation of PHP Applications
PDF
PHP, Under The Hood - DPC
PDF
Preparing code for Php 7 workshop
PDF
Unleashing Creative Freedom with MODX - 2015-08-26 at PHP Zwolle
PPT
Introduction To PHP
PPTX
TakeDownCon Rocket City: WebShells by Adrian Crenshaw
Php extensions
Django Documentation
OSCON2014 : Quick Introduction to System Tools Programming with Go
Code quality tools for dev
Web Development with Python and Django
Anatomy of PHP Shells
PHP - Introduction to PHP Date and Time Functions
Pragmatic plone projects
eZ Find workshop: advanced insights & recipes
10 Most Important Features of New PHP 5.6
PHP BASIC PRESENTATION
A content repository for your PHP application or CMS?
Northeast PHP - High Performance PHP
Build Automation of PHP Applications
PHP, Under The Hood - DPC
Preparing code for Php 7 workshop
Unleashing Creative Freedom with MODX - 2015-08-26 at PHP Zwolle
Introduction To PHP
TakeDownCon Rocket City: WebShells by Adrian Crenshaw
Ad

Similar to PHP Profiling/performance (20)

PDF
Profiling PHP - AmsterdamPHP Meetup - 2014-11-20
PDF
Profiling PHP - PHPAmersfoort Meetup 2015-03-10
PDF
Profiling PHP - WordPress Meetup Nijmegen 2015-03-11
PDF
php & performance
PPTX
Profiling and Tuning a Web Application - The Dirty Details
PDF
PHP & Performance
PPTX
Performance Tuning with XHProf
PDF
Profiling PHP - PHPBenelux Unconference track - 2015-01-24
PPTX
Performance tips for Symfony2 & PHP
ODP
Profiling PHP & Javascript
PDF
Profiling PHP with Xdebug / Webgrind
PPTX
PHP Aberdeen Quick optimisation of PHP with Webgrind
PDF
Performance profiling and testing of symfony application 2
PDF
Php go vrooom!
PDF
PHPDay 2013 - High Performance PHP
PPTX
Xdebug
PPT
PHP: Debugger, Profiler and more
PPT
Php Code Profiling Using X Debug
PDF
Profiling with Xhprof
PDF
Profiling and inspection with Blackfire.io
Profiling PHP - AmsterdamPHP Meetup - 2014-11-20
Profiling PHP - PHPAmersfoort Meetup 2015-03-10
Profiling PHP - WordPress Meetup Nijmegen 2015-03-11
php & performance
Profiling and Tuning a Web Application - The Dirty Details
PHP & Performance
Performance Tuning with XHProf
Profiling PHP - PHPBenelux Unconference track - 2015-01-24
Performance tips for Symfony2 & PHP
Profiling PHP & Javascript
Profiling PHP with Xdebug / Webgrind
PHP Aberdeen Quick optimisation of PHP with Webgrind
Performance profiling and testing of symfony application 2
Php go vrooom!
PHPDay 2013 - High Performance PHP
Xdebug
PHP: Debugger, Profiler and more
Php Code Profiling Using X Debug
Profiling with Xhprof
Profiling and inspection with Blackfire.io
Ad

Recently uploaded (20)

DOCX
Unit-3 cyber security network security of internet system
PDF
The New Creative Director: How AI Tools for Social Media Content Creation Are...
PPTX
Introuction about WHO-FIC in ICD-10.pptx
PDF
Sims 4 Historia para lo sims 4 para jugar
PDF
Cloud-Scale Log Monitoring _ Datadog.pdf
PDF
An introduction to the IFRS (ISSB) Stndards.pdf
PDF
SASE Traffic Flow - ZTNA Connector-1.pdf
PPTX
SAP Ariba Sourcing PPT for learning material
PDF
💰 𝐔𝐊𝐓𝐈 𝐊𝐄𝐌𝐄𝐍𝐀𝐍𝐆𝐀𝐍 𝐊𝐈𝐏𝐄𝐑𝟒𝐃 𝐇𝐀𝐑𝐈 𝐈𝐍𝐈 𝟐𝟎𝟐𝟓 💰
PPTX
Introuction about ICD -10 and ICD-11 PPT.pptx
PPTX
international classification of diseases ICD-10 review PPT.pptx
PPT
Design_with_Watersergyerge45hrbgre4top (1).ppt
PDF
Testing WebRTC applications at scale.pdf
PPTX
Digital Literacy And Online Safety on internet
PDF
Vigrab.top – Online Tool for Downloading and Converting Social Media Videos a...
PDF
The Internet -By the Numbers, Sri Lanka Edition
PPTX
INTERNET------BASICS-------UPDATED PPT PRESENTATION
PDF
How to Ensure Data Integrity During Shopify Migration_ Best Practices for Sec...
PPTX
Funds Management Learning Material for Beg
PPTX
introduction about ICD -10 & ICD-11 ppt.pptx
Unit-3 cyber security network security of internet system
The New Creative Director: How AI Tools for Social Media Content Creation Are...
Introuction about WHO-FIC in ICD-10.pptx
Sims 4 Historia para lo sims 4 para jugar
Cloud-Scale Log Monitoring _ Datadog.pdf
An introduction to the IFRS (ISSB) Stndards.pdf
SASE Traffic Flow - ZTNA Connector-1.pdf
SAP Ariba Sourcing PPT for learning material
💰 𝐔𝐊𝐓𝐈 𝐊𝐄𝐌𝐄𝐍𝐀𝐍𝐆𝐀𝐍 𝐊𝐈𝐏𝐄𝐑𝟒𝐃 𝐇𝐀𝐑𝐈 𝐈𝐍𝐈 𝟐𝟎𝟐𝟓 💰
Introuction about ICD -10 and ICD-11 PPT.pptx
international classification of diseases ICD-10 review PPT.pptx
Design_with_Watersergyerge45hrbgre4top (1).ppt
Testing WebRTC applications at scale.pdf
Digital Literacy And Online Safety on internet
Vigrab.top – Online Tool for Downloading and Converting Social Media Videos a...
The Internet -By the Numbers, Sri Lanka Edition
INTERNET------BASICS-------UPDATED PPT PRESENTATION
How to Ensure Data Integrity During Shopify Migration_ Best Practices for Sec...
Funds Management Learning Material for Beg
introduction about ICD -10 & ICD-11 ppt.pptx

PHP Profiling/performance

  • 3. PHP profiling/perfomance • Expected traffic • Traffic growth • Scalability means • Evolutions to come… Set the context • Contextually : search engine, file generation, task… Establish the objectives
  • 5. PHP profiling/perfomance • Smart, structured code • Lazy loading • Static variables (singletons/registries) • Cache Code • Autoloader • Reduce and order include_path • Client cache (HTTP headers Cache-Control, Expire) • Avoid regexps • … Good practices • Php.ini : realpath_cache_size • Nginx : worker_processes, open_file_cache, multi_accept, keepalive, gzip • Cache op-code • Disable logs Server configuration
  • 6. PHP profiling/perfomance • Ponctual inefficiencies/bottlenecks detection • Monitoring (analytics, alert tools) • Take comparison points (before/after) • Know when to stop • Never rely on intuition! Measure
  • 7. PHP profiling/perfomance Load tests • ApacheBenchmark • Siege • Httperf • … Profiling • Xdebug • XHProf • … Measuring tools
  • 8. PHP profiling/perfomance ApacheBenchmark ab -n 50 -c 2 -v 1 -p 'post.txt' -T 'application/json' -H 'Authorization: Bearer eyJ0eXAi.eyJpc3nYXJ.KznbYbc' 'http://api.gma.my/account/authenticate?alt=json&metadata [foo]=Hello&metadata[bar]=World&metadata[random Val]=Ted' > bench.txt { "application_key":"afaf770", "email":"ndegardin@tribalnova.com", "key":"ndegardin« } post.txt
  • 9. PHP profiling/perfomance … Concurrency Level: 2 Time taken for tests: 25.704 seconds Complete requests: 50 Failed requests: 44 (Connect: 0, Receive: 0, Length: 44, Exceptions: 0) Write errors: 0 Non-2xx responses: 50 Total transferred: 2231974 bytes Total POSTed: 28100 HTML transferred: 2220474 bytes Requests per second: 1.95 [#/sec] (mean) Time per request: 1028.176 [ms] (mean) Time per request: 514.088 [ms] (mean, across all concurrent requests) Transfer rate: 84.80 [Kbytes/sec] received 1.07 kb/s sent 85.86 kb/s total Connection Times (ms) min mean[+/-sd] median max Connect: 0 0 0.1 0 0 Processing: 704 1014 154.5 990 1693 Waiting: 704 1014 154.5 990 1693 Total: 705 1015 154.5 990 1693 … bench.txt
  • 10. PHP profiling/perfomance • PHP profiling extension • Developed by Facebook • The basic version stores traces • I diddn’t succeed in using the bundled PHP/HTML app… • With XHPROF, the profiling is triggered by calling xhprof_enable(), xhprof_disable() • Cool solution : auto_append_file, auto_prepend_file in php.ini XHProf
  • 11. PHP profiling/perfomance • Website http://xhprof.io • Sources https://github.com/gajus/xhprof.io • Profiling stored in database • php.ini • auto_append_file=/var/www/xhprofio/inc/append.php • auto_prepend_file=/var/www/xhprofio/inc/prepend.php • Not my recommandation XHProf.io
  • 12. PHP profiling/perfomance • Sources https://github.com/sepehr/xhprof • Profiling stored in database (one table « details ») • Very good profiler • php.ini • auto_prepend_file=/var/www/xhprof/external/header.php • auto_append_file=/var/www/xhprof/external/footer.php • In CGI : • activation by cookie _profile=1 • or by adding the parameter _profile=1 to the URL (adds the cookie, then redirects) • In CLI : • auto_prepend/auto_append in/etc/php5/cli/php.ini • activation by « export XHPROF_PROFILE=1 » XHGUI
  • 13. PHP profiling/perfomance • Activate profiling by vhost in Apache • Activate profiling by using a nginx/PHP-FPM pool : /etc/php5/fpm/pool.d/www.conf : • Activate profiling by vhost in nginx/PHP-FPM, in a location section XHProf notes fastcgi_param PHP_ADMIN_VALUE "auto_prepend_file=/var/www/xhprof/external/header.phpnauto_append_file=/var/ www/xhprof/external/footer.php"; php_admin_value auto_prepend_file "/var/www/xhprof/external/header.php" php_admin_value auto_append_file "/var/www/xhprof/external/footer.php" php_admin_value[auto_prepend_file] = /var/www/xhprofio/inc/prepend.php" php_admin_value[auto_appendfile] = /var/www/xhprofio/inc/append.php"
  • 14. PHP profiling/perfomance • Web site http://xdebug.org/ • Developed by Derik Rethans • PHP extension, now generally bundled with PHP • Features : • Debugging functions (time, calls, scream) • Maximum nesting protection • Enhanced stacktraces • Remote debugging • Profiling • Code coverage Xdebug
  • 15. PHP profiling/perfomance Xdebug notes about debugging zend_extension=/usr/lib/php5/20090626/xdebug.so xdebug.remote_enable = 1 xdebug.remote_handler = "dbgp" xdebug.remote_port = 9000 xdebug.remote_mode = req xdebug.remote_host = 192.168.56.1 xdebug.profiler_enable = 1 xdebug.profiler_output_dir = /var/log/xdebug xdebug.profiler_output_name = cachegrind.out.%H_%s_%R.xt php.ini
  • 16. PHP profiling/perfomance • Prevent nginx from sending a gateway timeout (504) : in /etc/nginx/nginx.conf, add • Remote debug events triggered externally by setting a XDEBUG_SESSION cookie and, if needed, mapping paths (client/server) in your IDE • Remote debug CLI scripts by running export XDEBUG_CONFIG="idekey=netbeans-xdebug"and setting php.ini xdebug.remote_host Xdebug notes about debugging fastcgi_read_timeout 3000; keepalive_timeout 3000; keepalive_requests 3000; client_header_timeout 3000; client_body_timeout 3000; nginx.conf
  • 17. PHP profiling/perfomance • The traces are stored as files • It often weights about 2Mo (be careful when forgetting it) • Using a fixed name (without timestamp) in php.ini xdebug.profiler.output_name allows a run script to erase the previous one • No official Xdebug trace extension, I suggest cachegrind.out.[…].xt • Profilers : • WinCacheGrind (Windows) : somewhat simple • WebGrind (Web) : « For quick’n’dirty optimizations it does the job » • KCacheGrind (KDE) : the best one • PHPStorm : not really useful Xdebug notes about profiling
  • 18. PHP profiling/perfomance • To run it under Windows, use KDE4Win (package system similar to Cygwin’s one), it should be in the « kdesdk » package. Not available on all mirrors. • Install GraphViz to enable drawing some charts (and add its /bin directory the the PATH) • Check the Call Graph view! kCacheGrind
  • 19. PHP profiling/perfomance • About different times : • The wall time includes I/O (network latency, database accesses, waiting for an user input…) • The including time includes the children call • The CPU exclusion time (self time in KCacheGrind) is the more relevant value • Switch between display formats : • Percentage is better for comparing values • Time is better for assessing values • Tweaking script parameters (input configuration, number of entries processed in database, number of iterations…) may allow to observe different phenomena Notes about profiling

Editor's Notes