Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
<?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(
'new Date().getTime() - window.performance.timing.requestStart'
);
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";
}
}
@victorpavlov

This comment has been minimized.

Copy link

victorpavlov commented Jun 26, 2015

We can just use window.performance.now() instead of new Date().getTime() - window.performance.timing.requestStart

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.