Use Python + Selenium to Automate Web Timing

I've been hearing a lot recently about the Navigation Timing spec, which sets a multitude of timing events as javascript properties. There are numerous events in the flow, from the very first navigation event, which could be when the user clicks a search result in Google, to DNS timing to Dom parsing etc. See the MDN PerformanceTiming API documentation for more details.


I wanted a way to be able to automate the capturing of these timings, what better way than to use Selenium. Selenium can execute javascript and return the results to your script, so it is relatively straight-forward.

Here's a basic script which will fetch the page and calculate two timings. The back-end performance which is from when the user starts the navigation to when the first response starts. The second timing, you could probably guess, is front-end performance, which is from when the user starts receiving the first response until the DOM is complete.

Use Selenium to Measure Web Timing
Performance Timing Events flow
navigationStart -> redirectStart -> redirectEnd -> fetchStart -> domainLookupStart -> domainLookupEnd
-> connectStart -> connectEnd -> requestStart -> responseStart -> responseEnd
-> domLoading -> domInteractive -> domContentLoaded -> domComplete -> loadEventStart -> loadEventEnd

from selenium import webdriver

source = ""
driver = webdriver.Chrome()

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


You will have to install the Selenium drivers, which if you use pip should be a straight-forward
$ pip install selenium

This is a basic Python example, the same execute_script command is available in your language of choice.