You are here

trait MarkdownParserBenchmarkTrait in Markdown 3.0.x

Hierarchy

4 files declare their use of MarkdownParserBenchmarkTrait
LeagueCommonMark.php in src/Plugin/Markdown/LeagueCommonMark.php
Parsedown.php in src/Plugin/Markdown/Parsedown.php
PeclCmark.php in src/Plugin/Markdown/PeclCmark.php
PhpMarkdown.php in src/Plugin/Markdown/PhpMarkdown.php

File

src/Traits/MarkdownParserBenchmarkTrait.php, line 10

Namespace

Drupal\markdown\Traits
View source
trait MarkdownParserBenchmarkTrait {

  /**
   * Flag indicating whether this is currently in the process of a benchmark.
   *
   * @var bool
   */
  protected static $benchmark = FALSE;

  /**
   * {@inheritdoc}
   *
   * @see \Drupal\markdown\Plugin\Markdown\MarkdownParserBenchmarkInterface::benchmark()
   *
   * @return \Drupal\markdown\MarkdownBenchmark[]
   */
  public function benchmark($markdown, $format = NULL) {

    // Start.
    $parsed = $this
      ->benchmarkParse($markdown);
    $rendered = $this
      ->benchmarkRender($markdown, $format);
    $total = MarkdownBenchmark::create('total', $parsed
      ->getStart(), $rendered
      ->getEnd(), $rendered
      ->getResult());

    // Stop
    static::$benchmark = FALSE;

    // Return parsed, rendered, total.
    return [
      $parsed,
      $rendered,
      $total,
    ];
  }

  /**
   * {@inheritdoc}
   *
   * @see \Drupal\markdown\Plugin\Markdown\MarkdownParserBenchmarkInterface::benchmarkAverages()
   *
   * @return \Drupal\markdown\MarkdownBenchmarkAverages
   */
  public function benchmarkAverages($markdown, $format = NULL, $iterations = 10) {
    return MarkdownBenchmarkAverages::create($iterations)
      ->iterate([
      $this,
      'benchmark',
    ], [
      $markdown,
      $format,
    ]);
  }

  /**
   * {@inheritdoc}
   *
   * @see \Drupal\markdown\Plugin\Markdown\MarkdownParserBenchmarkInterface::benchmarkParse()
   *
   * @return \Drupal\markdown\MarkdownBenchmark
   */
  public function benchmarkParse($markdown) {
    $start = microtime(TRUE);
    $result = $this
      ->convertToHtml($markdown);
    $end = microtime(TRUE);
    return MarkdownBenchmark::create('parsed', $start, $end, static::$benchmark = $result);
  }

  /**
   * {@inheritdoc}
   *
   * @see \Drupal\markdown\Plugin\Markdown\MarkdownParserBenchmarkInterface::benchmarkRender()
   *
   * @return \Drupal\markdown\MarkdownBenchmark
   */
  public function benchmarkRender($markdown, $format = NULL) {
    if ($format === NULL) {

      /** @var \Drupal\Core\Render\RendererInterface $renderer */
      $renderer = \Drupal::service('renderer');
      $start = microtime(TRUE);
      $build = [
        '#markup' => $this
          ->parse($markdown),
      ];
      $result = $renderer
        ->renderPlain($build);
      $end = microtime(TRUE);
    }
    else {
      $start = microtime(TRUE);
      $result = check_markup($markdown, $format);
      $end = microtime(TRUE);
    }
    return MarkdownBenchmark::create('rendered', $start, $end, $result);
  }

  /**
   * {@inheritdoc}
   *
   * @return \Drupal\markdown\ParsedMarkdownInterface
   */
  public function parse($markdown, LanguageInterface $language = NULL) {
    return ParsedMarkdown::create($markdown, static::$benchmark ?: $this
      ->convertToHtml($markdown, $language), $language);
  }

}

Members