import requests
from numpy import *
from multiprocessing import Process
from multiprocessing import Queue
class BenchMark(Process):
def __init__(self, web_address, num, queue):
super(Process, self).__init__()
self.address = web_address
self.num = num
self.queue = queue
def run(self):
for i in range(self.num):
print("request {0}/{1}".format(i, self.num))
r_time = requests.get(self.address).elapsed.total_seconds()
ms_time = float(r_time)*1000
self.queue.put(ms_time)
print("process done")
class Daemon():
def __init__(self, thread_num, address, request_num, queue):
self.thread_num = thread_num
self.address = address
self.request_num = request_num
self.queue = queue
def work(self):
thread_pool = []
for i in range(self.thread_num):
thread_pool.append(BenchMark(self.address, self.request_num, self.queue))
for each in thread_pool:
each.start()
for each in thread_pool:
each.join()
if __name__ == "__main__":
address = "http://www.baidu.com"
thread_num = 10
request_num = 100
q = Queue()
d = Daemon(thread_num, address, request_num, q)
d.work()
response_time = []
while not q.empty():
response_time.append(q.get())
response_time.sort()
avg_response = mean(response_time)
index_95 = int(thread_num * request_num * 0.95)
print("avg time = {0}ms".format(avg_response))
print("95% response time = {0}ms".format(response_time[index_95]))
评论