Skip to content

Commit 715f509

Browse files
authored
Create ab.py
1 parent fde2adb commit 715f509

1 file changed

Lines changed: 110 additions & 0 deletions

File tree

ab.py

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
#coding:gbk
2+
3+
"""
4+
"""
5+
__author__ = "Roger Liu([email protected])"
6+
__version__ = "$v1.0"
7+
__date__ = "$Date: 2012/12/15 15:06"
8+
9+
import sys
10+
import urllib2
11+
import threading
12+
import Queue
13+
import time
14+
from optparse import OptionParser
15+
16+
class ThreadPool(object):
17+
def __init__(self, urlpth, req_number, thread_num):
18+
self.work_queue = Queue.Queue()
19+
self.threads = []
20+
self.__init_work_queue(req_number, urlpth)
21+
self.__init_thread_pool(thread_num)
22+
"""
23+
initialize threads
24+
"""
25+
def __init_thread_pool(self, thread_num):
26+
for i in range(thread_num):
27+
self.threads.append(MyThread(self.work_queue))
28+
29+
"""
30+
initialize work queue
31+
"""
32+
def __init_work_queue(self, req_number, urlpth):
33+
for i in range(req_number):
34+
self.add_job(do_job, urlpth)
35+
36+
"""
37+
add a job to the queue
38+
"""
39+
def add_job(self, func, args):
40+
self.work_queue.put((func, args))
41+
42+
"""
43+
wait for all the threads to be completed
44+
"""
45+
def wait_all_complete(self):
46+
for item in self.threads:
47+
if item.isAlive():
48+
item.join()
49+
50+
class MyThread(threading.Thread):
51+
def __init__(self, work_queue):
52+
threading.Thread.__init__(self)
53+
self.work_queue = work_queue
54+
self.start()
55+
56+
def run(self):
57+
while True:
58+
try:
59+
do, args = self.work_queue.get(block=False)
60+
do(args)
61+
self.work_queue.task_done()#notify the completement of the job
62+
except:
63+
break
64+
65+
ERROR_NUM = 0
66+
67+
def do_job(args):
68+
try:
69+
html = urllib2.urlopen(args)
70+
except Exception, e:
71+
print e
72+
global ERROR_NUM
73+
ERROR_NUM += 1
74+
75+
76+
def parse():
77+
"""parse the args"""
78+
parser = OptionParser(description="The scripte is used to simulate apache benchmark(sending requests and testing the server)")
79+
parser.add_option("-n", "--number", dest="num_of_req", action="store", help="Number of requests you want to send", default=1)
80+
parser.add_option("-c", "--concurrent", dest="con_req", action="store", help="Number of concurrent requests you set", default=1)
81+
parser.add_option("-u", "--url", dest="urlpth", action="store", help="The url of server you want to send to")
82+
(options, args) = parser.parse_args()
83+
return options
84+
85+
def main():
86+
"""main function"""
87+
start = time.time()
88+
options = parse()
89+
90+
if not options.urlpth:
91+
print 'Need to specify the parameter option "-u"!'
92+
if '-h' in sys.argv or '--help' in sys.argv:
93+
print __doc__
94+
95+
tp = ThreadPool(options.urlpth, int(options.num_of_req), int(options.con_req))
96+
tp.wait_all_complete()
97+
end = time.time()
98+
99+
print "==============================================="
100+
print "URL: ", options.urlpth
101+
print "Total Requests Number: ", options.num_of_req
102+
print "Concurrent Requests Number: ", options.con_req
103+
print "Total Time Cost(seconds): ", (end-start)
104+
print "Average Time Per Request: ", (end-start)/int(options.num_of_req)
105+
print "Average Requests Number Per Second: ", int(options.num_of_req)/(end-start)
106+
print "Total Error Number: ", ERROR_NUM
107+
108+
109+
if __name__ == '__main__':
110+
main()

0 commit comments

Comments
 (0)