import time import urllib import sys import thread def stress(url, numhits, numthreads): start = time.time() locks = [] for nt in xrange(numthreads): lock = thread.allocate_lock() locks.append(lock) lock.acquire() def run(lock): for nh in xrange(numhits): urllib.urlopen(url).read() lock.release() thread.start_new_thread(run, (lock,)) for lock in locks: lock.acquire() lock.release() return time.time() - start if __name__ == '__main__': import sys if len(sys.argv) not in (3, 4): print 'usage: %s []' print 'where is the url to stress, the number of hits' print '*per thread*, and the number of threads to run' print '(default 10)' sys.exit(1) url = sys.argv[1] numhits = int(sys.argv[2]) numthreads = 10 if len(sys.argv) == 4: numthreads = int(sys.argv[3]) print 'calling url %s' % (url,) print '%s hits in each of %s threads' % (numhits, numthreads) print 'total %s hits' % (numhits * numthreads,) t = stress(sys.argv[1], numhits, numthreads) print 'time spent: %s seconds' % (t,) print '(%s hits per second)' % ((numhits * numthreads) / t,)