python locust性能测试

/ 测试开发 / 0 条评论 / 701浏览
gevent + Locust
 对于I/O密集型任务,gevent还是能对性能做很大提升的,由于协程的创建、调度开销都比线程小的多,所以可以看到不论使用gevent的Spawn模式还是Pool模式,性能差距不大 
Locust完全是事件驱动的,因此在单台机器上能够支持几千并发用户访问。与其它许多基于事件的应用相比,Locust并不使用回调,而是使用gevent,而gevent是基于协程的,可以用同步的方式来编写异步执行的代码。每个用户实际上运行在自己的greenlet中。

locust
pip install locustio
如果你打算运行Locust 分布在多个进程/机器,我们建议你也安装pyzmq.
 通过pip命令安装。 /> pip install pyzmq
https://www.locust.io/
https://github.com/yileye/locust/

from locust import HttpLocust, TaskSet, taskclass WebsiteTasks(TaskSet): def on_start(self): self.client.post("/login", { "username": "test_user", "password": "" }) @task def index(self): self.client.get("/") @task def about(self): self.client.get("/about/")class WebsiteUser(HttpLocust): task_set = WebsiteTasks min_wait = 5000 max_wait = 15000
“-f”指定了要执行的python文件路径,“--host”指定了模拟用户请求接口的host名
切换到性能测试脚本所在的目录,启动性能测试:
------------------------------------------------------------------
.../> locust -f load_test.py --host=https://www.baidu.com
[2016-11-19 22:38:16,967] fnngj-PC/INFO/locust.main: Starting web monitor at *:8089
 [2016-11-19 22:38:16,967] fnngj-PC/INFO/locust.main: Starting Locust 0.7.5
注意:
如果遇到下面的错误,注意[Errorno 10048]那行,可以看出端口8089被占用导致Locust项目启动失败,这里我们需要找到对应占用了8089端口的进程并杀掉

无界面模式:
 --no-web是用来选择无浏览器模式,-c后面接的是模拟用户数,-r后面接的每秒模拟用户并发数,-n后面接的是模拟请求数
locust -f .\locust_test_1.py --host='http://api.winyyg.com' --no-web -c 1000 -r 10 -n 1000
===========================================================================
分布式

指定port(defaults to 5557)

https://docs.locust.io/en/latest/running-locust-distributed.html

测试结果写csv文件()

 关闭locust
 如果要终止分布式Locust的运行,直接在master机器上Ctrl + C关闭即可,slave机器会接收到停止信号自动停止运行。
=====================================
ice(RPC框架)
  ICE是ZEROC的开源通信协议产品
使我们能够以最小的代价构建分布式应用程序。ICE使我们专注于应用逻辑的开发,它来处理所有底层的网络接口编程,这样我们就不用去考虑这样的细节