Week 07 作业

用户头像
鱼_XueTr
关注
发布于: 2020 年 07 月 22 日
Week 07 作业

作业二:

用你熟悉的编程语言写一个 web 性能压测工具,输入参数:URL,请求总次数,并发数。输出参数:平均响应时间,95% 响应时间。用这个测试工具以 10 并发、100 次请求压测 www.baidu.com



import hashlib
import threading
from time import *
from datetime import datetime
import requests
import json
THREAD_NUM = 10
ONE_WORKER_NUM = 10
sum_time = 0.00
success_count = 0
url ="https://www.baidu.com/"
login_response = requests.post(url, data)
c = login_response.cookies
def request():  
global c  
global sum_time  
global success_count 
start_time = time()
url1 = "http://www.baidu.com/"
make_response = requests.post(url, data = form_data1,cookies = c)
res = make_response.text
assert res == "success"
print("请求成功")
end_time = time()
res_time = end_time - start_time
with open("~/res.txt", "a") as f:
f.write("请求成功的响应时间:{}\n".format(str(res_time)))
sum_time = sum_time + res_time
success_count = success_count + 1
def worker():  
global ONE_WORKER_NUM  
for i in range(0,ONE_WORKER_NUM):     
request()
def main():  
global THREAD_NUM  
Threads = []  
for i in range(THREAD_NUM):    
t = threading.Thread(target=worker, name="T"+str(i))    
t.setDaemon(True)    
Threads.append(t)  
for t in Threads:    
t.start()  
for t in Threads:    
t.join()
if __name__ == "__main__":
main()
with open("~/res.txt", "a") as f: 
f.write("并发数:" + str(THREAD_NUM * ONE_WORKER_NUM) + "\n")  
f.write("成功数:" + str(success_count) + "\n") 
f.write("订单成功率:" + str(success_count / (THREAD_NUM * ONE_WORKER_NUM) * 100) + "%" + "\n") 
f.write("成功总响应时间:" + str(sum_time) + "\n")  
f.write("成功平均响应时间:" + str(sum_time / success_count) + "\n")  
f.write("tps:" + str((success_count) / (sum_time / success_count)) + "\n")  
f.clo



用户头像

鱼_XueTr

关注

还未添加个人签名 2019.04.19 加入

还未添加个人简介

评论

发布
暂无评论
Week 07 作业