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

Profiling PHP @ PHPers Warszawa #12

Profiling PHP @ PHPers Warszawa #12

Sebastian Grodzicki

October 10, 2016
Tweet

More Decks by Sebastian Grodzicki

Other Decks in Technology

Transcript

  1. PROFILING PHP INCLUSIVE VS EXCLUSIVE TIME function foo()
 {
 $str

    = bar(); return $str; } function foo()
 {
 $str = bar(); return $str; }
  2. 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);
 }
  3. 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);
 }
  4. PROFILING PHP ASSERTIONS tests: "Application should never hit the DB":

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

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

    "/.*" assertions: - "main.wall_time < 100ms"
  7. BLACKFIRE PHP SDK ▸ composer require blackfire/php-sdk ▸ $blackfire =

    new \Blackfire\Client(); ▸ $probe = $blackfire->createProbe(); ▸ $profile = $blackfire->endProbe($probe);
  8. <?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')
 ;
 }
 }