You are here

public function MarkdownBenchmarkAverages::getAverage in Markdown 3.0.x

Retrieves the averaged time from all benchmarks of a certain type.

Parameters

string $type: The type of benchmark to retrieve, can be one of:

  • parsed
  • rendered
  • total (default)

bool $render: Flag indicating whether to render the build array.

Return value

array|\Drupal\Component\Render\MarkupInterface A renderable array containing the averaged time.

1 call to MarkdownBenchmarkAverages::getAverage()
MarkdownBenchmarkAverages::build in src/MarkdownBenchmarkAverages.php

File

src/MarkdownBenchmarkAverages.php, line 174

Class

MarkdownBenchmarkAverages
Class MarkdownBenchmarkAverages.

Namespace

Drupal\markdown

Code

public function getAverage($type = 'total', $render = FALSE) {
  $build = [
    '#theme' => 'item_list__markdown_benchmark_average',
    '#items' => [],
    '#attributes' => [
      'class' => [
        'markdown-benchmark-average',
        "markdown-benchmark-average--{$type}",
      ],
    ],
    '#context' => [
      'type' => $type,
    ],
  ];
  if ($type === 'all') {
    $build['#items'] = [
      [
        'data' => $this
          ->getAverage('parsed', 'all'),
      ],
      [
        'data' => $this
          ->getAverage('rendered', 'all'),
      ],
      [
        'data' => $this
          ->getAverage('total', 'all'),
      ],
    ];
    return $build;
  }
  $benchmarks = $this
    ->getBenchmarks($type);
  if (!$benchmarks) {
    return [];
  }
  $last = array_slice($benchmarks, -1, 1)[0];
  $result = $last
    ->getResult();
  if (count($benchmarks) === 1) {
    $start = $last
      ->getStart();
    $end = $last
      ->getEnd();
  }
  else {
    $ms = array_map(function ($benchmark) {

      /** @var \Drupal\markdown\MarkdownBenchmark $benchmark */
      return $benchmark
        ->getMilliseconds(FALSE);
    }, $benchmarks);
    $averaged_ms = array_sum($ms) / count($ms);
    $start = microtime(TRUE);
    $end = $start + $averaged_ms / 1000;
  }
  $average = MarkdownBenchmark::create('average', $start, $end, $result)
    ->build();
  if ($render === 'all') {
    return $average;
  }
  $build['#items'][] = [
    'data' => $average,
  ];
  return $render ? $this
    ->renderer()
    ->renderPlain($build) : $build;
}