Quick and dirty benchmarking with Python
I needed to benchmark the HTTP request time of a list of URL's. I've been hacking around with some Python lately so figured it was up for the task. Below is the code. I'm sure it is ugly as hell by Python coding standards but it seems to work.
Simply add a text file containing the URL's you wish to test, 1 per line and let a rip!
# # Quick and dirty URL benchmarking script # # Author: ngmaloney@gmail.com # Date: 7/8/09 # Description: # Used to benchmark URLS from a textfile # # Usage: # Requires a textfile in the same dir names 'sites.txt' containing # 1 URL per line. # # python benchmarker.py # from timeit import Timer from urlparse import urlparse import urllib import operator sites_file = "sites.txt" results = [] buffer = open(sites_file,'rU').readlines() # Retrieve HTTP def testURL(url): resp = urllib.urlopen(url) data = resp.read(); if data: return data else: return False # Benchmark results from http grab def benchmark(url): s = "testURL('" + url + "')" #Timer needs command as str t = Timer(s,"from __main__ import testURL") return t.timeit(1) print 'Running benchmarks please wait...' #Iterate through urls and benchmark for line in buffer: url = line.rstrip() #Ghetto validation u = urlparse(url) if u.scheme: row = {'url':url, 'time': benchmark(url)} results.append(row) #Sort Results results.sort(key=operator.itemgetter('time')) results.reverse() #Output Results for row in results: print row['url'] + ' ' + str(row['time']) + ' seconds'
- Login to post comments