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.

Sebastian Grodzicki

December 01, 2016
Tweet

More Decks by Sebastian Grodzicki

Other Decks in Programming

Transcript

  1. PROFILING PHP
    SEBASTIAN GRODZICKI

    View Slide

  2. WHO USES
    PHP?

    View Slide

  3. WHO USES
    PHP 7?

    View Slide

  4. WHO USES
    PHP 7.1?

    View Slide

  5. WHO USES
    PHPUNIT?

    View Slide

  6. WHO USES
    XDEBUG?

    View Slide

  7. WHO USES
    BLACKFIRE?

    View Slide

  8. SEBASTIAN
    GRODZICKI
    CTO @ SHOWROOM

    View Slide

  9. 40% OF USERS

    ABANDON A WEBSITE

    THAT TAKES MORE THAN

    3 SECONDS TO LOAD
    Akamai
    STATISTICS

    View Slide

  10. 500 MS DELAY CAUSED
    20% TRAFFIC DECREASE
    Google
    STATISTICS

    View Slide

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

    View Slide

  12. PROFILING IS A WAY TO
    MEASURE WHERE A
    PROGRAM SPENDS TIME
    MathWorks
    DEFINITION

    View Slide

  13. PROFILING IS ABOUT
    GATHERING DATA
    Fabien Potencier
    DEFINITION

    View Slide

  14. MICROTIME

    View Slide


  15. $start = microtime(true);


    // do something


    $end = microtime(true);


    echo $end - $start;

    View Slide


  16. // do something


    $time = microtime(true);

    $time -= $_SERVER['REQUEST_TIME_FLOAT'];


    echo $time;

    View Slide

  17. log_format foobar '$upstream_response_time';

    access_log /var/log/nginx/access.log foobar;

    View Slide

  18. MEMORY_GET_USAGE

    View Slide


  19. $start = memory_get_usage(true);


    // do something


    $end = memory_get_usage(true);


    echo $end - $start;

    View Slide

  20. PROS & CONS
    MICROTIME & MEMORY_GET_USAGE
    ▸ built in PHP
    ▸ no training required
    ▸ data without context
    ▸ require code change

    View Slide

  21. Xdebug

    View Slide

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

    View Slide

  23. KCACHEGRIND

    View Slide

  24. WEBGRIND

    View Slide

  25. PROS & CONS
    XDEBUG PROFILER
    ▸ zero code change
    ▸ cachegrind compatible file format
    ▸ extension required
    ▸ huge overhead
    ▸ unsuitable for production

    View Slide

  26. XHProf

    View Slide


  27. xhprof_enable();


    // do something


    $data = xhprof_disable();

    View Slide

  28. # jns/xhprof-bundle

    jns_xhprof:

    location_web: http://xhprof.localhost

    enabled: true

    View Slide

  29. XHPROF

    View Slide

  30. PROS & CONS
    XHPROF
    ▸ suitable for production
    ▸ diff reports
    ▸ no longer maintained
    ▸ missing PHP 7 support

    View Slide

  31. View Slide

  32. View Slide

  33. PROFILING PHP
    TIME
    Wall time I/O
    CPU
    Disk
    I/O Network
    =
    =
    +
    +

    View Slide

  34. PROFILING PHP
    INCLUSIVE VS EXCLUSIVE TIME
    function foo()

    {

    $str = bar();
    return $str;
    }
    function foo()

    {

    $str = bar();
    return $str;
    }

    View Slide

  35. BLACKFIRE
    CONTINUOUS PERFORMANCE TESTING

    View Slide

  36. BLACKFIRE
    CALLGRAPHS

    View Slide

  37. BLACKFIRE
    HOT PATHS

    View Slide

  38. DEMO
    CHROME COMPANION

    View Slide

  39. View Slide

  40. View Slide

  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);
    }

    View Slide

  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);
    }

    View Slide

  43. View Slide

  44. DEMO
    BLACKFIRE CURL

    View Slide

  45. View Slide

  46. View Slide

  47. DEMO
    BLACKFIRE RUN

    View Slide

  48. View Slide

  49. View Slide

  50. ASSERTIONS

    View Slide

  51. PROFILING PHP
    ASSERTIONS
    tests:
    "Application should never hit the DB":
    path: "/.*"
    assertions:
    - "metrics.sql.queries.count == 0"

    View Slide

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

    View Slide

  53. PROFILING PHP
    ASSERTIONS
    tests:
    "Pages should be fast enough":
    path: "/.*"
    assertions:
    - "main.wall_time < 100ms"

    View Slide

  54. View Slide

  55. BUILDS

    View Slide

  56. View Slide

  57. View Slide

  58. View Slide

  59. PHP SDK

    View Slide

  60. BLACKFIRE
    PHP SDK
    ▸ composer require blackfire/php-sdk
    ▸ $blackfire = new \Blackfire\Client();
    ▸ $probe = $blackfire->createProbe();
    ▸ $profile = $blackfire->endProbe($probe);

    View Slide

  61. PHPUNIT

    View Slide


  62. 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')

    ;

    }

    }

    View Slide

  63. medium.com/@sgrodzicki

    View Slide

  64. blackfire.io/docs/24-days

    View Slide

  65. NEW RELIC

    View Slide

  66. NEW RELIC

    View Slide

  67. TIDEWAYS

    View Slide

  68. TIDEWAYS

    View Slide

  69. QUESTIONS?

    View Slide

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

    View Slide