Capturing page-performance metrics

Measuring and optimizing the client-side performance is essential for a seamless user experience, and this is critical for Web 2.0 applications using AJAX.

Capturing vital information, such as the time taken for page load, rendering of the elements, and the JavaScript code execution, will help in identifying the areas where performance is slow and optimizes the overall client-side performance.

Navigation Timing is a W3C-Standard JavaScript API to measure performance on the web. The API provides a simple way to get accurate and detailed timing statistics natively for page navigation and load events. It is available on Internet Explorer 9, Google Chrome, Firefox, and WebKit-based browsers.

The API is accessed via the properties of the timing interface of the window.performance object using JavaScript. We will capture the page-load time every time we navigate to a page. This can be done by using JavaScriptExecutor to call winodw.performance in the afterNavigateTo() method in IAmTheEventListener2.java, as shown in the following code snippet:

@Override
public void afterNavigateTo(String to, WebDriver driver) {
try {

JavascriptExecutor jsExecutor = (JavascriptExecutor) driver;

// Get the Load Event End
long loadEventEnd = (Long) jsExecutor.executeScript("return window.performance.timing.loadEventEnd;");
// Get the Navigation Event Start
long navigationStart = (Long) jsExecutor.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.")
;

} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

As discussed in the previous code, the window.performance object provides us with the performance metric that is available within the Browser Window object. We need to use JavaScript to retrieve this metric. 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.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.138.122.195