写点什么

架构师系列之 8:python 网站压测工具

用户头像
桃花原记
关注
发布于: 2020 年 12 月 05 日

使用 request 后的 response 中时间来计算平均相应时间, 95%响应时间:

关于 elapsed 中的时间差解释:https://www.cnblogs.com/landhu/p/5442069.html

class MessageFetcher:    def __init__(self, url, total_number, parallel_number):        self.total_number = total_number        self.parallel_number = parallel_number        self.url = url
def get_message_data(self): req_list = [] for i in range(self.parallel_number): req_list.append(grequests.get(self.url)) elapsed_time_ms_list = [] try: for batch_number in range(int(self.total_number/self.parallel_number)): print("Send request in batch: {0}".format(batch_number)) res_list = grequests.map(req_list) print(res_list) for res_item in res_list: elapsed_time_ms_list.append(res_item.elapsed.microseconds) except Exception as e: print(e) elapsed_time_ms_list.sort() print(elapsed_time_ms_list) print(len(elapsed_time_ms_list)) print("95% 响应时间:{0} ms".format(elapsed_time_ms_list[94]/1000)) time_total = 0 for t in elapsed_time_ms_list: time_total += t print("平均响应时间:{0} ms".format(time_total / (1000*100)))
if __name__ == "__main__": MessageFetcher("http://www.baidu.com", 100, 10).get_message_data()
复制代码

Send request in batch: 0

[<Response [200]>, <Response [200]>, <Response [200]>, <Response [200]>, <Response [200]>, <Response [200]>, <Response [200]>, <Response [200]>, <Response [200]>, <Response [200]>]

Send request in batch: 1

[<Response [200]>, <Response [200]>, <Response [200]>, <Response [200]>, <Response [200]>, <Response [200]>, <Response [200]>, <Response [200]>, <Response [200]>, <Response [200]>]

Send request in batch: 2

[<Response [200]>, <Response [200]>, <Response [200]>, <Response [200]>, <Response [200]>, <Response [200]>, <Response [200]>, <Response [200]>, <Response [200]>, <Response [200]>]

Send request in batch: 3

[<Response [200]>, <Response [200]>, <Response [200]>, <Response [200]>, <Response [200]>, <Response [200]>, <Response [200]>, <Response [200]>, <Response [200]>, <Response [200]>]

Send request in batch: 4

[<Response [200]>, <Response [200]>, <Response [200]>, <Response [200]>, <Response [200]>, <Response [200]>, <Response [200]>, <Response [200]>, <Response [200]>, <Response [200]>]

Send request in batch: 5

[<Response [200]>, <Response [200]>, <Response [200]>, <Response [200]>, <Response [200]>, <Response [200]>, <Response [200]>, <Response [200]>, <Response [200]>, <Response [200]>]

Send request in batch: 6

[<Response [200]>, <Response [200]>, <Response [200]>, <Response [200]>, <Response [200]>, <Response [200]>, <Response [200]>, <Response [200]>, <Response [200]>, <Response [200]>]

Send request in batch: 7

[<Response [200]>, <Response [200]>, <Response [200]>, <Response [200]>, <Response [200]>, <Response [200]>, <Response [200]>, <Response [200]>, <Response [200]>, <Response [200]>]

Send request in batch: 8

[<Response [200]>, <Response [200]>, <Response [200]>, <Response [200]>, <Response [200]>, <Response [200]>, <Response [200]>, <Response [200]>, <Response [200]>, <Response [200]>]

Send request in batch: 9

[<Response [200]>, <Response [200]>, <Response [200]>, <Response [200]>, <Response [200]>, <Response [200]>, <Response [200]>, <Response [200]>, <Response [200]>, <Response [200]>]

[17445, 18356, 18615, 18803, 19253, 19296, 19675, 19725, 19932, 19934, 20026, 20185, 20199, 20213, 20268, 20270, 20585, 20595, 20608, 20762, 21015, 21078, 21177, 21217, 21234, 21256, 21259, 21320, 21354, 21383, 21530, 21563, 21635, 21657, 21676, 21724, 21728, 21735, 21755, 21840, 21916, 22060, 22087, 22115, 22218, 22250, 22277, 22380, 22396, 22431, 22465, 22531, 22534, 22661, 22666, 22840, 22873, 22907, 23070, 23105, 23147, 23208, 23240, 23274, 23279, 23332, 23373, 23438, 23634, 23739, 23868, 23979, 24001, 24055, 24129, 24217, 24344, 24364, 24629, 24642, 24686, 24817, 25180, 25306, 25590, 25722, 25774, 25830, 26384, 26487, 26492, 26724, 26767, 26938, 27036, 27431, 27959, 28335, 30206, 31448]

100

95% 响应时间:27.036 ms

平均响应时间:22.84667 ms


发布于: 2020 年 12 月 05 日阅读数: 28
用户头像

桃花原记

关注

还未添加个人签名 2018.11.24 加入

还未添加个人简介

评论

发布
暂无评论
架构师系列之8:python网站压测工具