写了一个记录请求时间的装饰器
#@说明: 统计一次get请求的平均时间
import time
import requests
def get_file(logfile = 'out1.log'):
def get_1(func):
def req():
start = time.time() #当前的时间戳
b = func()
end = time.time()
# print(round((end - start) / b))
time2 = (round(((end - start) * 1000) / b , 4))
tim = f'请求{b}次得到的平均请求时间为{time2}ms' #统计毫秒,并保留4位小数
with open(logfile,'a',encoding="GBK") as f:
f.write(tim + '\n')
return req
return get_1
@get_file()
def time1():
a = int(input('请输入想要请求的次数:'))
for i in range(a):
requests.get('http://www.baidu.com/')
return a
time1()