Upgrade to Pro — share decks privately, control downloads, hide ads and more …

[PHPBenelux 2015] PHP Performance Tuning: Prepa...

[PHPBenelux 2015] PHP Performance Tuning: Prepare for Ludicrous Speed!

There's a saying "hardware is cheaper than developers", but this only holds true if you treat your hardware with the respect it deserves.

Learn how to diagnose, confirm and fix bottlenecks in your application using XHProf and Xhgui.

After an in-depth look at how to use these tools, we'll take a practical look at actually using them to achieve measurable (~20%) performance improvements in a popular open source project.

Davey Shafik

January 23, 2015
Tweet

More Decks by Davey Shafik

Other Decks in Programming

Transcript

  1. Proprietary and Confidential •Community Engineer at Engine Yard •Author of

    Zend PHP 5 Certification Study Guide, Sitepoints PHP Anthology: 101 Essential Tips, Tricks & Hacks & PHP Master: Write Cutting Edge Code •A contributor to Zend Framework 1 & 2, phpdoc, & PHP internals • Original creator of PHAR/ PHP_Archive • Lead for PHPWomen US •@dshafik Davey Shafik
  2. Proprietary and Confidential • Datastore • Doesn’t matter if it’s

    PostgreSQL, MySQL, Oracle, MongoDB, CouchDB, or MSSQL • External Resources • APIs, Filesystems, Network Sockets, External Processes • Bad Code • The only great code, is code that never has to run. Everything else, is just good code. Common Causes of Slowdowns
  3. Proprietary and Confidential • Profiling Records Relative Speed + Memory

    Usage + # of calls per function + Call graph • The act of profiling affects the speed (the outcome) of the code (just like quantum physics!) • Benchmarking Tests Actual Speed • What your users actually see Benchmarking Vs Profiling
  4. Proprietary and Confidential • The #1 issue with performance tuning,

    is assuming you even have a problem. • Premature Optimization is a waste of time. • Determine desired performance (e.g. 100 concurrent users with less than 1s response times). Benchmark on production hardware. Do you even have a problem? How big? Do I Have a Problem?
  5. Proprietary and Confidential • Development Only • Enabled/Activated by the

    Developer • Gather more information than passive profilers • Negatively impacts performance Active Profilers
  6. Proprietary and Confidential • Minimal impact on performance • Gathers

    enough information to diagnose performance issues • Can be used in production Passive Profilers http://adw0rd.com/media/ 2010/04/xhprof.jpg
  7. Proprietary and Confidential $ pecl install xhprof-beta // php.ini (or

    similar) [xhprof]
 extension=xhprof.so Installing Xhprof
  8. Proprietary and Confidential $ cd /var/www $ git clone https://github.com/perftools/xhgui.git

    $ cd xhgui $ php install.php # runs composer install & checks permissions Installing Xhgui
  9. Proprietary and Confidential • Default config found in: 
 /path/to/xhgui/config/config.default.php

    • Defaults to using mongodb on localhost without authentication Xhgui Configuration
  10. Proprietary and Confidential 'db.host' => ‘mongodb://mongo.host:27017', 'db.db' => 'xhprof',
 'db.options'

    => array(
 'username' => DB_USERNAME,
 'password' => DB_PASSWORD,
 ), Mongodb Configuration
  11. Proprietary and Confidential Configuration: 'save.handler' = 'file', 'save.handler.filename' => 


    '/path/to/xhgui/xhprof-' .uniqid("", true). '.dat' Load it into Xhgui: $ php 
 /path/to/xhgui/external/import.php 
 /path/to/file.dat Saving to File
  12. Proprietary and Confidential Nginx/Apache • Setup a virtual host as

    you would for any other PHP app • Document Root: /path/to/xhgui/webroot CLI Server $ cd /path/to/xhgui $ php -S 0:8080 -t webroot/ Web Server
  13. Proprietary and Confidential • Use auto_prepend_file to include xhgui/external/header.php CLI

    Server $ php -dauto_prepend_file=/path/to/xhgui/external/ header.php -S 0.0.0.0:8080
 Nginx fastcgi_param PHP_VALUE 
 "auto_prepend_file=/path/to/xhgui/external/header.php"; Apache php_admin_value auto_prepend_file
 "/path/to/xhgui/external/header.php Using Xhgui
  14. Proprietary and Confidential • In xhgui/config/config.default.php
 'profiler.enable' => function() {

    return rand(0, 100) === 42; // ~1:100 Requests }, 'profiler.enable' => function() { return rand(0, 10) === 4; // ~1:10 Requests }, 'profiler.enable' => function() { return true; // Always }, Enabling the Profiler
  15. Proprietary and Confidential // Replace all numbers, and query params

    with XXX 'profiler.simple_url' => function($url) { $url = preg_replace("/[0-9]+/", "XXX", $url);
 $qs = parse_url($url, PHP_URL_QUERY); if ($qs) { $parts = array(); parse_str($qs, $parts); $v = array_pad(array(), sizeof($parts), "XXX"); $normalized = array_combine(array_keys($parts), $v); $replace = http_build_query($normalized); $url = str_replace($qs, $replace, $url); } return $url; }, URL Simplification (Cont.)
  16. Proprietary and Confidential • Exclusive is just the current scope

    (e.g. a function), excluding any other code it calls • Inclusive is the current scope and any code it calls Exclusive vs Inclusive
  17. Proprietary and Confidential • Call Count • How many times

    the function is called • [Inclusive] Wall Time (wt) • How long a function took in real time • [Inclusive] CPU Usage/CPU Time (cpu) • How much time the CPU spent running the function • [Inclusive] Memory Usage (mu) • How much memory is currently being used by the function • [Inclusive] Peak Memory Usage (pmu) • The peak memory usage used by the function Glossary
  18. Proprietary and Confidential • Exclusive Wall Time (ewt) • Exclusive

    CPU Time (ecpu) • Exclusive Memory Usage (emu) • Exclusive Peak Memory Usage (epmu) Exclusive
  19. Proprietary and Confidential • Recent — The most recent runs

    (paged) • Longest wall time — Order by the slowest runs based on actual (wall) clock time • Most CPU — Order by the most CPU time • Most Memory — Order by the most memory usage • Custom View — Perform custom queries against the mongo DB • Watch Functions — Mark functions that should appear at the top of the run for review • Waterfall — An experimental view for reviewing how concurrent requests impact each other
  20. Proprietary and Confidential • mysql_(.*) for ext/mysql • mysqli(.*) for

    ext/mysqli • pdo(.*) for PDO (which will work for all PDO-based database interactions) Watch all MySQL DB Activity
  21. Proprietary and Confidential Feedback & Questions: Feedback: https://joind.in/
 Twitter: @dshafik

    Email: [email protected] Slides: http://daveyshafik.com/slides 13110 Xhprof: http://pecl.php.net Xhgui: http://github.com/perftools/xhgui Resources