#! /usr/bin/env python """ This script emulates the QALoad HTTP functions. Use the GLoad class to create a set of thread for web-stress purpose. Version 0.9 for SIForge.org (c) 2002-2003 Giovanni Giorgi, www.siforge.org """ import urllib import string from threading import * class Client(Thread): """ Simulate a Web Client """ def __init__(self,url,silent): # We must call the father: Thread.__init__(self) self.url=url self.silent=silent def run(self): self.error=0 self.text="" if not self.silent: print self,"[Running]" try: f = urllib.urlopen(self.url) self.text=f.read() f.close() if not self.silent: print self,"[END]", len(self.text) except IOError: self.error=1 if not self.silent: print self,"[ERR]" def isOk(self): if(self.error==1): return 0 return 1 def isResultMatch(self,textExpected): return self.text == textExpected def getResult(self): return self.text class GLoad: def __init__(self, url='http://localhost/', load=100): tlist=[] print print "Test for ",url," X ",load," Getting expected result..." tmp=Client(url,1) tmp.start() while tmp.isAlive(): pass self.expectedResult=tmp.getResult() print "Expected Result Size:",len(self.expectedResult) #print self.expectedResult print "OK [Loading Threads:",load,"]" for i in range(load): # For silent setup put 1 tlist.append(Client(url,0)) print "[Starting]" for i in range(load): tlist[i].start() print "All Threads Launched" for i in range(load): while tlist[i].isAlive(): pass print "All Threads ends" failedResponse=0 noerrors=0 # Collects stats: for i in range(load): if tlist[i].isOk(): noerrors=noerrors+1 if not tlist[i].isResultMatch(self.expectedResult): failedResponse=failedResponse+1 else: pass #print tlist[i].getResult() print " -----------------------------" print "No errors:", noerrors, " Failed:", (load-noerrors), " (connected but failed resp:",failedResponse, ")" print "Success%", ((noerrors/float(load))*100) print " -----------------------------" testurl='http://127.0.0.1' print "Running 5 incremental tests...." for t in range(15,20): test=GLoad(url=testurl, load=t) # test=GLoad(url=testurl, load=20) # test=GLoad(url=testurl, load=30)