JThree
JThree

Reputation: 2496

Measuring page speed and network time with Python like HTML5 Navigation Timing does?

I'm currently working on a client, which should determine the health of a server by it's actually response time (and the time one the network separately). I came across a implementation of a new HTML5 feature, which gives me basically all the metrics, I'm interested in:

Visual presentation of Navigation Timing API performance object

I know that I could use Selenium and PhantomJS and just parse the output, but for performance and scalability reasons I would prefer a solution urllib or requests, which breaks it down to the DNS-request, the time on the network and the actual time taken for the server to respond.

Upvotes: 1

Views: 1788

Answers (1)

johntellsall
johntellsall

Reputation: 15170

To build on references given by @Andre Daniel, this code fetches a domain IP via DNS, and prints out the time elapsed. It then fetches a URL, prints the time, and prints the elapsed time after removing the DNS lookup time for the same URL.

source

import socket, time, urllib, urlparse

URL = 'http://example.com/123'

print URL, 'timing:'

urlinfo = urlparse.urlparse(URL)

start = time.time()
ip = socket.gethostbyname( urlinfo.netloc )
dns_tm = time.time()-start
print 'DNS:\t\t{:.3f} seconds'.format( dns_tm )

start = time.time()
_data = urllib.urlopen(URL).read()
load_tm = time.time()-start
print 'load:\t\t{:.3f} seconds'.format( load_tm )
print 'w/o DNS:\t{:.3f} seconds'.format( load_tm-dns_tm )

output

http://example.com/123 timing:
DNS:        0.054 seconds
load:       0.104 seconds
w/o DNS:    0.050 seconds

Upvotes: 4

Related Questions