Week7- 作业
发布于: 2020 年 12 月 06 日
1.性能压测的时候,随着并发压力的增加,系统响应时间和吞吐量如何变化,为什么?
答:
A.系统响应时间变化为:先是不变,然后变慢,最后无响应。首先是不变的状态是因为并发压力不够大,此时系统有足够的CPU和内存资源支撑,慢慢变慢,则是随着压力增大,CPU和内存等资源开始不足,需要切换一部分内存到硬盘上,所以会变慢,最后无响应,则是系统被压垮了。
B.吞吐量变化为:先上升,到顶峰后,开始往下掉,最后变为0。CPU和内存、磁盘足够时,吞吐量会上升,到顶峰是CPU和内存利用到极致了,下掉是因为需要如果增加更多的线程和进程,则需要把内存置换到硬盘上,来回切换拖延了响应时间,吞吐量也就会下掉,变为0则是因为系统被压垮了。
2.用你熟悉的编程语言写一个 Web 性能压测工具,输入参数:URL,请求总次数,并发数。输出参数:平均响应时间,95% 响应时间。用这个测试工具以 10 并发、100 次请求压测 www.baidu.com。
import timefrom loguru import loggerimport requestsfrom concurrent.futures import ProcessPoolExecutor, as_completed, ThreadPoolExecutorimport sysimport numpy as nphandlers = [ {"sink": sys.stdout, "level": "INFO", "enqueue": True, "format": "<green>{time:YYYY-MM-DD HH:mm:ss.SSS}</green> | " "<bold>{level}</bold> | " "<cyan>{process.id}</cyan>:<cyan>{thread}</cyan> | " "<blue>{function}</blue>:<blue>{module}</blue>:<blue>{line}</blue> | " "{message}"}]logger.configure(handlers=handlers)con_current_num = 10request_num = 10def ab_test(): start = time.time() response = requests.get("http://www.baidu.com") end = time.time() - start return response.status_code, enddef run_process(): result = [] with ThreadPoolExecutor(max_workers=request_num) as executor: p_futures = {executor.submit(ab_test): n for n in range(request_num)} for p_f in as_completed(p_futures): try: result.append(p_f.result()[1]) # logger.info(p_f.result()) except Exception as e: logger.exception(str(e)) return resultif __name__ == '__main__': every_request_take_time = [] time_begin = time.time() with ProcessPoolExecutor(max_workers=con_current_num) as executor: futures = {executor.submit(run_process): n for n in range(con_current_num)} for f in as_completed(futures): try: every_request_take_time.extend(f.result()) except Exception as e: logger.exception(str(e)) total_take_time = time.time() - time_begin logger.info("测试程序运行耗时:{}秒", total_take_time) logger.info("总共测试次数: {}", len(every_request_take_time)) logger.info(every_request_take_time) logger.info("平均耗时: {}秒", sum(every_request_take_time) / (con_current_num * request_num)) logger.info("95%响应时间 {}秒", np.percentile(every_request_take_time, 95))
结果
INFO | 6645:4795579840 | <module>:test-request:56 | 测试程序运行耗时:1.4330978393554688秒INFO | 6645:4795579840 | <module>:test-request:57 | 总共测试次数: 100INFO | 6645:4795579840 | <module>:test-request:58 | [0.07397007942199707, 0.07367491722106934, 0.07253289222717285, 0.07085704803466797, 0.06812071800231934, 0.0771629810333252, 0.0786900520324707, 0.07571792602539062, 0.08804583549499512, 0.07916712760925293, 0.07902002334594727, 0.08790016174316406, 0.08948802947998047, 0.08926987648010254, 0.08170890808105469, 0.09229373931884766, 0.09064602851867676, 0.09532999992370605, 0.08845901489257812, 0.10013389587402344, 0.05608391761779785, 0.07138490676879883, 0.052525997161865234, 0.07371902465820312, 0.05608391761779785, 0.061721086502075195, 0.0776371955871582, 0.07477807998657227, 0.05423307418823242, 0.08046698570251465, 0.06822514533996582, 0.06823110580444336, 0.07303714752197266, 0.07215070724487305, 0.07045722007751465, 0.07430815696716309, 0.05661582946777344, 0.05515694618225098, 0.05676078796386719, 0.05880403518676758, 0.04926419258117676, 0.11653900146484375, 0.12441396713256836, 0.12197113037109375, 0.1275947093963623, 0.1432950496673584, 0.13350319862365723, 0.13819384574890137, 0.144287109375, 0.18158388137817383, 0.06884908676147461, 0.10759902000427246, 0.11798691749572754, 0.12537288665771484, 0.11878299713134766, 0.1281108856201172, 0.13505196571350098, 0.12995314598083496, 0.1382598876953125, 0.1824939250946045, 0.14472317695617676, 0.1448650360107422, 0.14327287673950195, 0.1425321102142334, 0.14401626586914062, 0.16845321655273438, 0.4662899971008301, 0.4723069667816162, 0.46994924545288086, 0.49394989013671875, 0.134138822555542, 0.1459190845489502, 0.15198993682861328, 0.16389203071594238, 0.1823749542236328, 0.18753719329833984, 0.1873321533203125, 0.19466495513916016, 0.5325460433959961, 0.5492517948150635, 0.13053107261657715, 0.13301587104797363, 0.13348603248596191, 0.1559441089630127, 0.1611769199371338, 0.16404104232788086, 0.17393207550048828, 0.1958320140838623, 0.5367898941040039, 0.5413310527801514, 0.12419295310974121, 0.1298508644104004, 0.14293599128723145, 0.1507248878479004, 0.1375737190246582, 0.14210200309753418, 0.15798091888427734, 0.17752695083618164, 0.5561628341674805, 0.5496010780334473]INFO | 6645:4795579840 | <module>:test-request:59 | 平均耗时: 0.1531241536140442秒INFO | 6645:4795579840 | <module>:test-request:60 | 95%响应时间 0.5327582359313965秒
划线
评论
复制
发布于: 2020 年 12 月 06 日阅读数: 29
lggl
关注
还未添加个人签名 2018.08.28 加入
还未添加个人简介
评论