我们知道Locust默认的HTTP客户端实现用的是python自有模块requests.它满足了我们发送http请求的需求。但是如果是要进行大规模的压测时,这种默认方式不是最节省资源的。Locust官方给出了faster HTTP client的实现,它使用了geventhttpclient代替了requests. 居官方给出的消息,它能提升5-6倍的并发量。
为了验证使用这种方式带来的效果,以一个实验案例为验证locust对测试机器的CPU资源来判断。
实验场景:模拟100虚拟用户打开一篇博客文章。Locust web UI设置如下:
1)使用Locust默认HttpUser来实现场景,并执行测试。
脚本:
from locust import task,between,HttpUser
class MyUser(HttpUser):
wait_time = between(3,5)
@task
def open_blog(self):
with self.client.get("/13734261/2538763",catch_response=True) as res:
if res.status_code == 200:
res.success()
else:
res.failure()
执行结果如下:
对测试机CPU消耗: (CPU使用率为13.5%)
2)使用FastHttpUser来实现场景,并执行测试。
脚本:
from locust import task,between
from locust.contrib.fasthttp import FastHttpUser
class MyUser(FastHttpUser):
wait_time = between(3,5)
@task
def open_blog(self):
with self.client.get("/13734261/2538763",catch_response=True) as res:
if res.status_code == 200:
res.success()
else:
res.failure()
执行结果如下:
对测试机CPU消耗: (CPU使用率为8.8%)
我们的这个对比测试是跑在2019款 Mac Pro(2.3 GHz, i5)的机器上,但是不管怎样,从这样一个简单的对比测试的结果来看,利用FastHttpUser是比较减少资源的消耗的。个人也比较推荐FastHttpUser这种方式。