写点什么

架构师训练营第七周命题作业

用户头像
whiter
关注
发布于: 2020 年 07 月 22 日
  • 性能压测的时候,随着并发压力的增加,系统响应时间和吞吐量如何变化,为什么?

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

性能压测曲线分析

当对一个系统进行测试时,随着并发数的增加,系统吞吐量(由每秒事务数TPS表示)的变化曲线如下:

在并发数较低时,系统吞吐量趋于线性增长,此时系统运行良好。随着并发数继续增加,系统吞吐量增长开始放缓,此时系统中有些模块已经出现资源不足性能下降的情况。在此基础上继续增加并发数,此时系统中会开始出现更多资源不足的情况,并且系统吞吐量开始下降。最后并发数达到临界值,系统崩溃。



相应的,并发数与响应时间的变化曲线如下:

在并发数较低时,响应时间几乎保持不变,对应吞吐量线性增加。随着并发数继续增加,响应时间开始缓慢增加,对应吞吐量增幅放缓。继续增加并发数,响应时间快速增加,此时对应吞吐量下降。最后并发数达到临界值,系统崩溃,停止响应。

Web性能压测工具

代码如下:

import datetime
import requests
from math import floor
from time import sleep
from threading import Thread
def pressureTest(url, repeatTimes, concurrentRequests):
results = []
def sendRequest(url):
req = requests.get(url)
results.append(req.elapsed)
print(req.ok, req.elapsed)
def runTest(url, repeatTimes, concurrentRequests):
for _ in range(repeatTimes):
workingThreads = []
for _ in range(concurrentRequests):
workingThread = Thread(target=sendRequest, args=(url,))
workingThreads.append(workingThread)
for thread in workingThreads:
thread.start()
for thread in workingThreads:
thread.join()
sleep(0.1)
runTest(url, repeatTimes, concurrentRequests)
results.sort()
summation = sum(results, datetime.timedelta())
partialCount = floor(len(results) * 0.95)
partialSummation = datetime.timedelta()
for i in range(partialCount):
partialSummation += results[i]
averageResponseTime = summation / partialCount
averagePartialResponseTime = partialSummation / len(results)
print('Test URL: ', url)
print('Test Repeat Times: ', repeatTimes)
print('Test Concurrency: ', concurrentRequests)
print('Average response time: ', averageResponseTime.microseconds / 1000, 'ms')
print('95% response time: ', averagePartialResponseTime.microseconds / 1000, 'ms')
print('Success connections: ', len(results))
url = 'https://www.baidu.com'
repeatTimes = 100
concurrentRequests = 10
pressureTest(url, repeatTimes, concurrentRequests)



结果如下:

Test URL: https://www.baidu.com
Test Repeat Times: 100
Test Concurrency: 10
Average response time: 437.34 ms
95% response time: 389.615 ms
Success connections: 1000

Test URL: https://www.google.com
Test Repeat Times: 100
Test Concurrency: 10
Average response time: 159.844 ms
95% response time: 141.151 ms
Success connections: 1000

Test URL: https://www.taobao.com
Test Repeat Times: 100
Test Concurrency: 10
Average response time: 505.383 ms
95% response time: 422.499 ms
Success connections: 998

Test URL: https://www.amazon.com
Test Repeat Times: 100
Test Concurrency: 10
Average response time: 240.279 ms
95% response time: 210.155 ms
Success connections: 1000

尝试了百度、谷歌、淘宝和亚马逊这四个网站。百度的响应时间要低于淘宝,这个结果比较合理,因为淘宝的页面显然比百度复杂。同理也能说明谷歌的相应时间低于亚马逊。比较有趣的是在测试淘宝的时候在一千次访问中有两次失败,重复了几次都有这种情况,不知原因为何。通过这次的测试也可以看出跨太平洋的网络延迟大概在 200ms 以上。

发布于: 2020 年 07 月 22 日阅读数: 51
用户头像

whiter

关注

还未添加个人签名 2020.05.29 加入

还未添加个人简介

评论

发布
暂无评论
架构师训练营第七周命题作业