Skip to content

Instantly share code, notes, and snippets.

@MKorostoff
Created June 25, 2015 16:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save MKorostoff/119261280da381402237 to your computer and use it in GitHub Desktop.
Save MKorostoff/119261280da381402237 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(
'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
Copy link

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