Reputation: 2496
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:
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
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.
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 )
http://example.com/123 timing:
DNS: 0.054 seconds
load: 0.104 seconds
w/o DNS: 0.050 seconds
Upvotes: 4