You are here

protected function BenchmarkCommand::execute in Devel 8.2

Same name and namespace in other branches
  1. 8.3 webprofiler/src/Command/BenchmarkCommand.php \Drupal\webprofiler\Command\BenchmarkCommand::execute()
  2. 8 webprofiler/src/Command/BenchmarkCommand.php \Drupal\webprofiler\Command\BenchmarkCommand::execute()
  3. 4.x webprofiler/src/Command/BenchmarkCommand.php \Drupal\webprofiler\Command\BenchmarkCommand::execute()

File

webprofiler/src/Command/BenchmarkCommand.php, line 47

Class

BenchmarkCommand
Class BenchmarkCommand

Namespace

Drupal\webprofiler\Command

Code

protected function execute(InputInterface $input, OutputInterface $output) {
  $runs = $input
    ->getOption('runs');
  $file = $input
    ->getOption('file');
  $cache_rebuild = $input
    ->getOption('cache-rebuild');

  // http://username:password@hostname/
  $url = $input
    ->getArgument('url');
  $url_components = parse_url($url);
  $login = isset($url_components['user']) && isset($url_components['pass']);
  $steps = 3;
  if ($cache_rebuild) {
    $steps++;
  }
  if ($login) {
    $steps++;
  }

  /** @var \Drupal\Core\Http\Client $client */
  $client = $this->container
    ->get('http_client');
  $progress = new ProgressBar($output, $runs + $steps);
  $progress
    ->setFormat(' %current%/%max% [%bar%] %percent:3s%% %message%');
  if ($cache_rebuild) {
    $progress
      ->setMessage($this
      ->trans('commands.webprofiler.benchmark.progress.cache_rebuild'));
    $this
      ->RebuildCache();
    $progress
      ->advance();
  }
  if ($login) {
    $progress
      ->setMessage($this
      ->trans('commands.webprofiler.benchmark.progress.login'));
    $login_url = "{$url_components['scheme']}://{$url_components['host']}/user/login";

    // Enable cookies storage.
    $cookieJar = new CookieJar();
    $client
      ->setDefaultOption('cookies', $cookieJar);

    // Retrieve a form_build_id using the DomCrawler component.
    $response = $client
      ->get($login_url)
      ->getBody()
      ->getContents();
    $crawler = new Crawler($response);
    $form_build_id = $crawler
      ->filter('#user-login-form input[name=form_build_id]')
      ->attr('value');
    $op = $crawler
      ->filter('#user-login-form input[name=op]')
      ->attr('value');

    // Login a user.
    $response = $client
      ->post($login_url, [
      'body' => [
        'name' => $url_components['user'],
        'pass' => $url_components['pass'],
        'form_build_id' => $form_build_id,
        'form_id' => 'user_login_form',
        'op' => $op,
      ],
    ]);
    $progress
      ->advance();
    if ($response
      ->getStatusCode() != 200) {
      throw new \Exception($this
        ->trans('commands.webprofiler.benchmark.messages.error_login'));
    }
  }
  $datas = [];
  for ($i = 0; $i < $runs; $i++) {
    $progress
      ->setMessage($this
      ->trans('commands.webprofiler.benchmark.progress.get'));
    $datas[] = $this
      ->getData($client, $url);
    $progress
      ->advance();
  }
  $progress
    ->setMessage($this
    ->trans('commands.webprofiler.benchmark.progress.compute_avg'));
  $avg = $this
    ->computeAvg($datas);
  $progress
    ->advance();
  $progress
    ->setMessage($this
    ->trans('commands.webprofiler.benchmark.progress.compute_median'));
  $median = $this
    ->computePercentile($datas, 50);
  $progress
    ->advance();
  $progress
    ->setMessage($this
    ->trans('commands.webprofiler.benchmark.progress.compute_95percentile'));
  $percentile95 = $this
    ->computePercentile($datas, 95);
  $progress
    ->advance();
  $progress
    ->setMessage($this
    ->trans('commands.webprofiler.benchmark.progress.git_hash'));
  $gitHash = $this
    ->getGitHash();
  $progress
    ->advance();
  $progress
    ->setMessage($this
    ->trans('commands.webprofiler.benchmark.progress.yaml'));
  $yaml = $this
    ->generateYaml($gitHash, $runs, $url, $avg, $median, $percentile95);
  $progress
    ->advance();
  $progress
    ->setMessage($this
    ->trans('commands.webprofiler.benchmark.progress.done'));
  $progress
    ->finish();
  $output
    ->writeln('');
  if ($file) {
    file_put_contents($file, $yaml);
  }
  else {
    $output
      ->writeln($yaml);
  }
}