Hana90
Hana90

Reputation: 925

how to measure response time for both loading and search time for a website ? selenium

I need to measure response time for website using Selenium and Selenide.

or this?

driver.get("url");
        WebElement ele = $(By.tagName("body"));
        // get the  page load time
        Long loadtime = (Long)((JavascriptExecutor)driver).executeScript(
            "return performance.timing.loadEventEnd - performance.timing.navigationStart;");        
        System.out.println(loadtime);

and last way the following:

StopWatch pageLoad = new StopWatch();
            pageLoad.start();
            //Open your web app (In my case, I opened facebook)
            driver.get("url");
            // Wait for the required any element (I am waiting for Login button in fb)
            WebDriverWait wait = new WebDriverWait(driver, 10);
            wait.until(ExpectedConditions.presenceOfElementLocated(By.tagName("body")));

            pageLoad.stop();
            //Get the time
            long pageLoadTime_ms = pageLoad.getTime();
            long pageLoadTime_Seconds = pageLoadTime_ms / 1000;
            System.out.println("Total Page Load Time: " + pageLoadTime_ms + " milliseconds");

Upvotes: 10

Views: 30770

Answers (7)

Fefar Ravi
Fefar Ravi

Reputation: 939

from selenium import webdriver

url = "https://devnote.in"
self.get(url)

navigationStart = self.execute_script("return window.performance.timing.navigationStart")
domContentLoadedEventEnd = self.execute_script("return window.performance.timing.domContentLoadedEventEnd")
loadEventEnd = self.execute_script("return window.performance.timing.loadEventEnd")

backendPerformance = (domContentLoadedEventEnd - navigationStart)/1000
frontendPerformance = (loadEventEnd - navigationStart)/1000

print (backendPerformance)
print (frontendPerformance)

Output:
3.554 #DOMContentLoaded
5.294 #Load

enter image description here

Upvotes: 0

Manouchehr Rasouli
Manouchehr Rasouli

Reputation: 550

You can use selenium web driver to gather the timing data about your web page. Just do this:

from selenium import webdriver

source = "http://www.example.com/"
driver = webdriver.Chrome()
driver.get(source)

navigationStart = driver.execute_script("return window.performance.timing.navigationStart")
responseStart = driver.execute_script("return window.performance.timing.responseStart")
domComplete = driver.execute_script("return window.performance.timing.domComplete")

backendPerformance = responseStart - navigationStart
frontendPerformance = domComplete - responseStart

print "Back End: %s" % backendPerformance
print "Front End: %s" % frontendPerformance

driver.quit()

Upvotes: 8

Vijay sadhu
Vijay sadhu

Reputation: 500

function abc(){
    var start = window.performance.now();
      //Perform task 
    var end = window.performance.now();
    console.log('start = ',start,' ','end = ',end);
}

Upvotes: -1

M Navneet Krishna
M Navneet Krishna

Reputation: 694

There are 2 parts to your question.

  1. Page Load Times from a front end perspective.
  2. Load Time of the search functionality from an E2E perspective.

The approach for each of these cases would vary a bit.


In the first case. you could use the second and third approach specified (the one with the javascript and java). However, as rightly mentioned, it would vary from client to client. As the page loading is completely dependent on the browser rendering engine, each browser would give a different value.

Additionally, if in case the load time takes more than 10 seconds, you might not be able to identify the absolute value as you have specified a timeout of 10 in your WebDriverWait method.

The first approach might not provide what you need as you are waiting for a specific component within the page. This might not be the right way as modern browsers render progressively and do not wait for the entire response to arrive before starting to load the page.


In the second case, you could tweak up the same approaches a lil bit and obtain the load time of the search functionality from an E2E perspective (inclusive of all variations considered in the first case).


If in case you are looking for other tools which would give much more specific and accurate details, then you could try YSLOW and WebPageTest. YSLOW has an integration with Phantom JS which will help in automating the process and WebPageTest has APIs which can be leveraged to automate the same.

WebPageTest Reference Link : https://sites.google.com/a/webpagetest.org/docs/advanced-features/webpagetest-restful-apis

YSLOW Reference Link : http://yslow.org/phantomjs/

Upvotes: 2

Florent B.
Florent B.

Reputation: 42518

You can get the timing-related performance information for a given page with the performance.timing API:

// launch the browser
WebDriver driver = new FirefoxDriver();

// visit a page
driver.get("http://stackoverflow.com");

// get the  page load time
Long loadtime = (Long)((JavascriptExecutor)driver).executeScript(
    "return performance.timing.loadEventEnd - performance.timing.navigationStart;");

The doc :

https://developer.mozilla.org/en-US/docs/Web/API/PerformanceTiming

Upvotes: 6

Mobrockers
Mobrockers

Reputation: 2148

Website load testing always depends on the client used. There are specialized tools for testing your website load time such as Apache JMeter, however it does not execute Javascript or render the HTML page (and the load times of those actions are very client specific), it only collects the time it takes to receive the website, and cases like your second question could be easier to test in selenium than JMeter, depending on how the search is executed.

As for your second question, you would have a WebDriverWait until a search result is found, in the same way as your example.

Upvotes: 1

RemcoW
RemcoW

Reputation: 4326

Selenium doesn't provide a better way to do this. The way you are using it is the best way possible as far as I know. For the search method you can use the same way as you used for the page loading. Except you'd be doing some different actions between the start and stop time.

Also take a look at this question as I believe it answers your question. How can we get exact time to load a page using Selenium WebDriver?

Upvotes: 2

Related Questions