victorpavlov
6/26/2015 - 9:01 AM

gistfile1.php

<?php
use Behat\Behat\Context\SnippetAcceptingContext;
use Drupal\DrupalExtension\Context\RawDrupalContext;
class LoadTimeContext extends RawDrupalContext implements SnippetAcceptingContext {
  /**
   * @Then /^time to first byte should be less than "([^"]*)" seconds$/
   */
  public function timeToFirstByteShouldBeLessThanSeconds($max_seconds)
  {
    $session = $this->getSession();
    $time_to_first_byte = $session->evaluateScript(
      'window.performance.timing.responseStart - window.performance.timing.requestStart'
    );
    if ($time_to_first_byte > $max_seconds * 1000) {
      throw new \Exception("Time to first byte was " . $time_to_first_byte / 1000 . " seconds.");
    }
  }
  /**
   * @Then /^time to last byte should be less than "([^"]*)" seconds$/
   */
  public function timeToLastByteShouldBeLessThanSeconds($max_seconds)
  {
    $session = $this->getSession();
    $time_to_last_byte = $session->evaluateScript(
      'window.performance.timing.domComplete - window.performance.timing.requestStart'
    );
    if ($time_to_last_byte > $max_seconds * 1000) {
      throw new \Exception("Time to last byte was " . $time_to_last_byte / 1000 . " seconds.");
    }
    print "Time to last byte was " . $time_to_last_byte / 1000 . " seconds";
  }
  /**
   * @Then /^Time to completion of javascript should be less than "([^"]*)" seconds$/
   */
  public function timeToCompletionOfJavascriptShouldBeLessThanSeconds($max_seconds) {
    $session = $this->getSession();
    $time_to_completion_of_javascript = $session->evaluateScript(
      'window.performance.now()'
    );
    if ($time_to_completion_of_javascript > $max_seconds * 1000) {
      throw new \Exception("Time to completion of javascript was " . $time_to_completion_of_javascript / 1000 . " seconds.");
    }
    print "Time to completion of javascript was " . $time_to_completion_of_javascript / 1000 . " seconds";
  }
}