Implement code timings

5 (100%) 1 vote

Problem

The easiest way to get some performance related metrics is to implement timings in your testcode. This will work in every browser which is the big advantage. The disadvantage is probably that we have to enrich the testcode with timers. A lot of websites will use AJAX or other JavaScript functionality, these days. It can be interesting to see how long it takes before an element appears in the HTML DOM. In this recipe we will see how we can implement a timing strategy in the testcode itself.

Prerequisites

Think of the objectives you want to measure, because the outcome really depends on the implementation.

Solution

The script within a testmethod will be executed hierarchically, from top till down. The moment the script hits the line with long startTime, the start-time will be stored in a local variable. The moment the script hits the line with long endTime, the end-time will be stored in a local variable. After that the time difference is calculated, with long totalTime = endTime – startTime;.

<code lang="java">
@Test
public void codeTimings() {
    driver.get("/");
    long startTime = System.currentTimeMillis();
    WebElement search = driver.findElement(By.id("q"));
    search.sendKeys("Selenium");
    search.submit();
    assertTrue(driver.findElement(By.tagName("body")).getText()
                .contains("Selenium"));
    long endTime = System.currentTimeMillis();
    long totalTime = endTime - startTime;
    System.out.println("Totaltime: " + totalTime + "milliseconds");
    WebElement result = driver.findElement(By.linkText("Selenium"));
    result.click();
    assertTrue(driver.findElement(By.tagName("body")).getText()
                .contains("Selenium"));
    
}
</code>

There’s more…

We have to enrich our testscripts with a lot of extra code, following the above principles. What will happen if the way we calculated the time difference will change or we want to store the time difference in a database? We have to adjust the code on multiple places. A solution for this challenge is to extract the code to a separate class. We can use the class below to calculate the timings.

<code lang="java">
public class Timer {
    private long startTime = 0;
    private long endTime = 0;

    public void start() {
        this.startTime = System.currentTimeMillis();
    }

    public void end() {
        this.endTime = System.currentTimeMillis();
    }

    public long getStartTime() {
        return this.startTime;
    }

    public long getEndTime() {
        return this.endTime;
    }

    public long getTotalTime() {
        return this.endTime - this.startTime;
    }
}
</code>

We have to initialize a new object if we want to use the functions. We can use the code below for implementing the timer class.

<code lang="java">
@Test
public void measureWebTimings() {
    driver.get("/");
    Timer timer = new Timer();
    timer.start();
    WebElement search = driver.findElement(By.id("q"));
    search.sendKeys("Selenium");
    search.submit();
    assertTrue(driver.findElement(By.tagName("body")).getText()
                .contains("Selenium"));
    timer.end();
    System.out.println("Totaltime: " + timer.getTotalTime()
                + " milliseconds");
    WebElement result = driver.findElement(By.linkText("Selenium"));
    result.click();
    assertTrue(driver.findElement(By.tagName("body")).getText()
                .contains("Selenium"));
}
</code>

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.