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

Profiling PHP @ SymfonyCon Berlin 2016

Profiling PHP @ SymfonyCon Berlin 2016

You cannot improve what you cannot measure. That's why profiling applications should always be the first step before trying to improve its performance. Learn how to spot your applications' bottlenecks and how to adopt profiling into your developer pipeline.

9a328142924c93e5e148c75356ba6d42?s=128

Sebastian Grodzicki

December 01, 2016
Tweet

More Decks by Sebastian Grodzicki

Other Decks in Programming

Transcript

  1. PROFILING PHP SEBASTIAN GRODZICKI

  2. WHO USES PHP?

  3. WHO USES PHP 7?

  4. WHO USES PHP 7.1?

  5. WHO USES PHPUNIT?

  6. WHO USES XDEBUG?

  7. WHO USES BLACKFIRE?

  8. SEBASTIAN GRODZICKI CTO @ SHOWROOM

  9. 40% OF USERS
 ABANDON A WEBSITE
 THAT TAKES MORE THAN


    3 SECONDS TO LOAD Akamai STATISTICS
  10. 500 MS DELAY CAUSED 20% TRAFFIC DECREASE Google STATISTICS

  11. 100 MS DELAY CAUSED 1% DROP IN REVENUE Amazon STATISTICS

  12. PROFILING IS A WAY TO MEASURE WHERE A PROGRAM SPENDS

    TIME MathWorks DEFINITION
  13. PROFILING IS ABOUT GATHERING DATA Fabien Potencier DEFINITION

  14. MICROTIME

  15. <?php
 
 $start = microtime(true);
 
 // do something
 


    $end = microtime(true);
 
 echo $end - $start;
  16. <?php
 
 // do something
 
 $time = microtime(true);
 $time

    -= $_SERVER['REQUEST_TIME_FLOAT'];
 
 echo $time;
  17. log_format foobar '$upstream_response_time';
 access_log /var/log/nginx/access.log foobar;

  18. MEMORY_GET_USAGE

  19. <?php
 
 $start = memory_get_usage(true);
 
 // do something
 


    $end = memory_get_usage(true);
 
 echo $end - $start;
  20. PROS & CONS MICROTIME & MEMORY_GET_USAGE ▸ built in PHP

    ▸ no training required ▸ data without context ▸ require code change
  21. Xdebug

  22. xdebug.profiler_enable_trigger = 1 http://url?XDEBUG_PROFILE=1

  23. KCACHEGRIND

  24. WEBGRIND

  25. PROS & CONS XDEBUG PROFILER ▸ zero code change ▸

    cachegrind compatible file format ▸ extension required ▸ huge overhead ▸ unsuitable for production
  26. XHProf

  27. <?php
 
 xhprof_enable();
 
 // do something
 
 $data =

    xhprof_disable();
  28. # jns/xhprof-bundle
 jns_xhprof:
 location_web: http://xhprof.localhost
 enabled: true

  29. XHPROF

  30. PROS & CONS XHPROF ▸ suitable for production ▸ diff

    reports ▸ no longer maintained ▸ missing PHP 7 support
  31. None
  32. None
  33. PROFILING PHP TIME Wall time I/O CPU Disk I/O Network

    = = + +
  34. PROFILING PHP INCLUSIVE VS EXCLUSIVE TIME function foo()
 {
 $str

    = bar(); return $str; } function foo()
 {
 $str = bar(); return $str; }
  35. BLACKFIRE CONTINUOUS PERFORMANCE TESTING

  36. BLACKFIRE CALLGRAPHS

  37. BLACKFIRE HOT PATHS

  38. DEMO CHROME COMPANION

  39. None
  40. None
  41. public function getVersion() { $process = new Process($this->getPath() . '

    --version'); $process->run(); if (!$process->isSuccessful()) { throw new \RuntimeException($process->getErrorOutput()); } $version = substr($process->getOutput(), 12); return trim($version); }
  42. public function getVersion() { static $version; if (null !== $version)

    { return trim($version); } $process = new Process($this->getPath() . ' --version'); $process->run(); if (!$process->isSuccessful()) { throw new \RuntimeException($process->getErrorOutput()); } $version = substr($process->getOutput(), 12); return trim($version); }
  43. None
  44. DEMO BLACKFIRE CURL

  45. None
  46. None
  47. DEMO BLACKFIRE RUN

  48. None
  49. None
  50. ASSERTIONS

  51. PROFILING PHP ASSERTIONS tests: "Application should never hit the DB":

    path: "/.*" assertions: - "metrics.sql.queries.count == 0"
  52. PROFILING PHP ASSERTIONS tests: "Homepage should never call the API":

    path: "/" assertions: - "metrics.http.requests.count == 0"
  53. PROFILING PHP ASSERTIONS tests: "Pages should be fast enough": path:

    "/.*" assertions: - "main.wall_time < 100ms"
  54. None
  55. BUILDS

  56. None
  57. None
  58. None
  59. PHP SDK

  60. BLACKFIRE PHP SDK ▸ composer require blackfire/php-sdk ▸ $blackfire =

    new \Blackfire\Client(); ▸ $probe = $blackfire->createProbe(); ▸ $profile = $blackfire->endProbe($probe);
  61. PHPUNIT

  62. <?php
 
 use Blackfire\Bridge\PhpUnit\TestCaseTrait;
 use Blackfire\Profile;
 
 class IntegrationTest extends

    \PHPUnit_Framework_TestCase {
 use TestCaseTrait;
 /**
 * @requires extension blackfire
 */
 public function testSomething() {
 $config = new Profile\Configuration();
 $config
 ->assert('metrics.sql.queries.count < 5', 'SQL queries')
 ->assert('metrics.twig.render.count < 3', 'Rendered Twig templates')
 ->assert('metrics.twig.compile.count == 0', 'Twig compilation')
 ;
 }
 }
  63. medium.com/@sgrodzicki

  64. blackfire.io/docs/24-days

  65. NEW RELIC

  66. NEW RELIC

  67. TIDEWAYS

  68. TIDEWAYS

  69. QUESTIONS?

  70. THANK YOU! https://speakerdeck.com/sgrodzicki https://twitter.com/sebgrodzicki