极客大学架构师训练营 0 期 week 07 作业
发布于: 2020 年 07 月 22 日
性能压测的时候,随着并发压力的增加,系统响应时间和吞吐量如何变化,为什么?
答:
随着并发压力的增加:
系统的响应时间,如图,在并发用户数为50并发(例)以内系统的响应时间基本不变,在50~100并发时(例)内响应时间略微增加;在100~500(例)即c点之后,系统响应时间急剧增大;自d点后系统呈现出即将失去响应的走势响应时间无限增加,系统不可用。
系统的TPS/QPS均匀增大,直到达到一个点“b”的时候,TPS/QPS增加的速率逐渐减小,并在点“c”的时候TPS/QPS达到峰值,过了点“c”以后TPS/QPS开始减少。点"d"时系统资源耗尽,系统处于不可用状态。
用你熟悉的编程语言写一个 web 性能压测工具,输入参数:URL,请求总次数,并发数。输出参数:平均响应时间,95% 响应时间。用这个测试工具以 10 并发、100 次请求压测 www.baidu.com
代码:
#!/usr/bin/env python3import osimport concurrent.futuresimport requestsimport timeimport sysimport getopt# 用你熟悉的编程语言写一个 web 性能压测工具,输入参数:URL,# 请求总次数,并发数。输出参数:平均响应时间,95% 响应时间。# 用这个测试工具以 10 并发、100 次请求压测 www.baidu.com# 命令行参数举例如下:## test_runner.py -t 10 -n 100 -r www.baidu.comdef test(url, thread_nums=10,request_nums= 100): max_time = float('-inf'); mean_time = 0; min_time = float('inf'); time_95 = 0; time_90 = 0; cost_times = [] total_costs = 0 with concurrent.futures.ThreadPoolExecutor(thread_nums) as executor: future_to_url = {executor.submit(request_for_url, url) for i in range(request_nums)} for future in concurrent.futures.as_completed(future_to_url): # print('##### request start #####') # print('############' + str(future.result())) cost_time = future.result() if cost_time > max_time: max_time = cost_time if cost_time < min_time: min_time = cost_time total_costs += cost_time cost_times.append(cost_time) # print('##### request ends costs #####' + str(future.result())) mean_time = total_costs / request_nums cost_times.sort() time_95 = cost_times[int(request_nums * 0.95)] time_90 = cost_times[int(request_nums * 0.9)] print(f'max_time:{max_time}') print(f'mean_time:{mean_time}') print(f'min_time:{min_time}') print(f'time_95:{time_95}') print(f'time_90:{time_90}')def request_for_url(url): # print('*************') start_time = time.time() # print('**** start ******' + str(time.time())) response = requests.get('https://www.baidu.com/') # print('***** end ******' + str(time.time())) return time.time() - start_timedef main(argv): try: opts, _ = getopt.getopt(argv, "t: n: r:") except getopt.GetoptError as e: print(e) print('test_runner.py -t <parallel thread num> -n <request num> -r <request url>') sys.exit(2) t_val = None n_val = None r_val = None flag = True try: for opt, arg in opts: if opt == '-t': t_val = int(arg) if t_val < 1: flag = False elif opt == '-n': n_val = int(arg) if n_val < 1: flag = False elif opt == '-r': r_val = arg except Exception as e: print(e) print('test_runner.py -t <parallel thread num> -n <request num> -r <request url>') sys.exit(2) if not flag: print('test_runner.py -t <parallel thread num> -n <request num> -r <request url>') sys.exit(2) print('\n################################ RESULT BEGIN #####################################') # print('valid tcp port:') test(r_val, t_val, n_val) print('################################ RESULT END #####################################\n')if __name__ == "__main__": main(sys.argv[1:]) # thread_nums = 10 # test('www.baidu.com', thread_nums,request_nums)
测试结果:
################################ RESULT BEGIN #####################################max_time:0.2521350383758545mean_time:0.09389840841293334min_time:0.05919384956359863time_95:0.1565380096435547time_90:0.13916993141174316################################ RESULT END #####################################
划线
评论
复制
发布于: 2020 年 07 月 22 日阅读数: 53
chun1123
关注
还未添加个人签名 2018.03.09 加入
还未添加个人简介
评论