Locust使用点滴

安利一篇我翻译的国外大牛的神经网络入门文章

locust的基本使用方法可以直接参考官方文档
这里主要记下一些使用时候遇到的细节需要注意的地方

1. --no-web的使用

参考官方文档,写了一个简单测试,但是运行时候总是无法发起请求:

(locust) liujinliu@liujinliu-OptiPlex-7010:~/code/servertest/locust$ locust --host=http://{ip}:{port} --print-stats
/home/liujinliu/venv/locust/local/lib/python2.7/site-packages/locust/rpc/__init__.py:6: UserWarning: WARNING: Using pure Python socket RPC implementation instead of zmq. If running in distributed mode, this could cause a performance decrease. We recommend you to install the pyzmq python package when running in distributed mode.
  warnings.warn("WARNING: Using pure Python socket RPC implementation instead of zmq. If running in distributed mode, this could cause a performance decrease. We recommend you to install the pyzmq python package when running in distributed mode.")
[2017-01-14 18:23:46,358] liujinliu-OptiPlex-7010/INFO/locust.main: Starting web monitor at *:8089
[2017-01-14 18:23:46,358] liujinliu-OptiPlex-7010/INFO/locust.main: Starting Locust 0.7.5
 Name                                                          # reqs      # fails     Avg     Min     Max  |  Median   req/s
--------------------------------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------------------------------
 Total                                                              0     0(0.00%)                                       0.00

 Name                                                          # reqs      # fails     Avg     Min     Max  |  Median   req/s
--------------------------------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------------------------------
 Total                                                              0     0(0.00%)                                       0.00

 Name                                                          # reqs      # fails     Avg     Min     Max  |  Median   req/s
--------------------------------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------------------------------
 Total                                                              0     0(0.00%)                                       0.00

 Name                                                          # reqs      # fails     Avg     Min     Max  |  Median   req/s
--------------------------------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------------------------------
 Total                                                              0     0(0.00%)                                       0.00

^C[2017-01-14 18:23:53,095] liujinliu-OptiPlex-7010/ERROR/stderr: KeyboardInterrupt
[2017-01-14 18:23:53,095] liujinliu-OptiPlex-7010/INFO/locust.main: Shutting down (exit code 0), bye.
 Name                                                          # reqs      # fails     Avg     Min     Max  |  Median   req/s
--------------------------------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------------------------------
 Total                                                              0     0(0.00%)                                       0.00

Percentage of the requests completed within given times
 Name                                                           # reqs    50%    66%    75%    80%    90%    95%    98%    99%   100%
--------------------------------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------------------------------

在最后加上--no-web的参数就好了
即正确写法是这样:

locust --host=http://{ip}:{port} --print-stats --no-web
Locust是一款用Python编写的开源负载测试工具,支持分布式测试,采用事件驱动方式,在单台机器上可模拟数千甚至上万的并发用户,与传统基于线程的工具相比,资源消耗更低,扩展性更强[^3]。以下是Locust使用指南: ### 环境准备 Locust支持的Python版本有2.7、3.4、3.5、3.6、3.7和3.8,需提前配置好Python运行环境 [^1]。 ### 安装Locust 使用pip3进行安装,并可通过`locust -V`查看版本: ```bash pip3 install locust locust -V ``` [^4] ### 编写Locustfile Locustfile是一个普通的Python文件,里面必须含有一个User类,代表一个用户或者一个locust集群,Locust会为每个用户生成一个实例,User类里可添加自定义函数 [^2]。 #### 示例1:直接在HttpUser中编写(代码量少的情况) ```python from locust import HttpUser, constant, between, task, TaskSet, SequentialTaskSet class Login(HttpUser): host = 'http://1.72.24.40:23' wait_time = between(1, 3) @task def login(self): url = '/login' json = { "password": "45", "username": "34", } header = {"Content-Type": "application/json;charset=UTF-8"} self.client.request(method='POST', url=url, json=json, headers=header, name='登录1') ``` [^4] #### 示例2:使用任务集编写 ```python import os from locust import task, TaskSet, HttpUser, constant # 任务集 用户行为脚本 class UserBehavior(TaskSet): @task(1) def getBasicProfile(self): headers = {'content-type': 'application/x-www-form-urlencoded', 'ua': 'xxx', 'Cookie': 'xxx'} url = "http://xxx/profile/getBasicProfile.do" res = self.client.post(url, headers=headers) print(res.json()) wait_time = constant(1) class WebsiteUser(HttpUser): tasks = [UserBehavior] min_wait = 500 max_wait = 1000 host = "http://localhost:8089" if __name__ == "__main__": os.system("locust -f %s " % __file__) ``` [^5] ### 运行Locust 在命令行中,进入包含Locustfile的目录,运行以下命令启动Locust: ```bash locust -f locustfile.py ``` 启动后,在浏览器中访问`http://localhost:8089`,配置要模拟的用户数、每秒启动的用户数和目标URL,点击“Start swarming”开始测试 [^2]。 ### wait_time设置 User类中包含可选项`wait_time`,用于设置在每个task运行之后的等待时间,若不设置,会马上执行下一个task。它有三个内置参数: - `constant`:固定时长 - `between`:在区间内随机取值 例如: ```python wait_time = constant(3) # 固定等待3秒 wait_time = between(1, 3) # 在1到3秒之间随机等待 ``` [^2]
评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值