Skip to content

Instantly share code, notes, and snippets.

@victorpavlov
Forked from MKorostoff/gist:119261280da381402237
Last active August 29, 2015 14:23
Show Gist options
  • Save victorpavlov/d4520dfd27966d3bed3f to your computer and use it in GitHub Desktop.
Save victorpavlov/d4520dfd27966d3bed3f to your computer and use it in GitHub Desktop.
<?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";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment