Measuring performance with the Navigation Timing API

11 April, 2014
Navigation Timing is a W3C Standard JavaScript API for measuring performance on the Web.The API provides a simple way to get accurate and detailed timing statistics natively for page navigation and load events. The API is accessed via the properties of the timing interface of the window.performance object using JavaScript.

Each performance.timing attribute shows the time of a navigation event when the page was requested or when the page load event was measured in milliseconds since midnight of January 1, 1970 (UTC). A zero value means that an event did not occur.


import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class PerformanceTest {
	public static void main(String[] args) {
		WebDriver driver = new FirefoxDriver();
		// Open the Application
		driver.get("http://money.rediff.com/");
		
		JavascriptExecutor js = (JavascriptExecutor) driver;
		
		// Get the Load Event End
		long loadEventEnd = (Long) js.executeScript("return window.performance.timing.loadEventEnd;");
		
		// Get the Navigation Event Start
		long navigationStart = (Long) js.executeScript("return window.performance.timing.navigationStart;");
		
		// Difference between Load Event End and Navigation Event Start is Page Load Time
		System.out.println("Page Load Time is " + (loadEventEnd - navigationStart)/1000 + " seconds.");
		
	}
}

Here we are collecting the loadEventEnd time and the navigationEventStart time and calculating the difference between them, which will give us the page load time.

No comments:

Post a Comment