locust --hellp
1. Locust简介
Locust是使用Python语言编写实现的开源性能测试工具,简洁、轻量、高效,并发机制基于gevent协程,可以实现单机模拟生成较高的并发压力。
主要特点如下:
1) 使用普通的Python脚本用户测试场景
2) 分布式和可扩展,支持成千上万的用户
3) 基于Web的用户界面,用户可以实时监控脚本运行状态
4) 几乎可以测试任何系统,除了web http接口外,还可自定义clients测试其他类型系统
2. 安装
使用pip或easy_install,可以方便安装Locust
|
1
|
pip install locustio |
安装完成后,可以在shell或cmd中运行locust命令,如查看可用的选项:
|
1
|
locust --help |
Locust主要由下面的几个库构成:
1) gevent
gevent是一种基于协程的Python网络库,它用到Greenlet提供的,封装了libevent事件循环的高层同步API。
2) flask
Python编写的轻量级Web应用框架。
3) requests
Python Http库
4) msgpack-python
MessagePack是一种快速、紧凑的二进制序列化格式,适用于类似JSON的数据格式。msgpack-python主要提供MessagePack数据序列化及反序列化的方法。
5) six
Python2和3兼容库,用来封装Python2和Python3之间的差异性
6) pyzmq
pyzmq是zeromq(一种通信队列)的Python绑定,主要用来实现Locust的分布式模式运行
3. 快速入门
Usage: locust [options] [LocustClass [LocustClass2 ... ]] Options:
-h, --help show this help message and exit
-H HOST, --host=HOST Host to load test in the following format:
http://10.21.32.33
--web-host=WEB_HOST Host to bind the web interface to. Defaults to '' (all
interfaces)
-P PORT, --port=PORT, --web-port=PORT
Port on which to run web host
-f LOCUSTFILE, --locustfile=LOCUSTFILE
Python module file to import, e.g. '../other.py'.
Default: locustfile
--csv=CSVFILEBASE, --csv-base-name=CSVFILEBASE
Store current request stats to files in CSV format.
--master Set locust to run in distributed mode with this
process as master
--slave Set locust to run in distributed mode with this
process as slave
--master-host=MASTER_HOST
Host or IP address of locust master for distributed
load testing. Only used when running with --slave.
Defaults to 127.0.0.1.
--master-port=MASTER_PORT
The port to connect to that is used by the locust
master for distributed load testing. Only used when
running with --slave. Defaults to 5557. Note that
slaves will also connect to the master node on this
port + 1.
--master-bind-host=MASTER_BIND_HOST
Interfaces (hostname, ip) that locust master should
bind to. Only used when running with --master.
Defaults to * (all available interfaces).
--master-bind-port=MASTER_BIND_PORT
Port that locust master should bind to. Only used when
running with --master. Defaults to 5557. Note that
Locust will also use this port + 1, so by default the
master node will bind to 5557 and 5558.
--heartbeat-liveness=HEARTBEAT_LIVENESS
set number of seconds before failed heartbeat from
slave
--heartbeat-interval=HEARTBEAT_INTERVAL
set number of seconds delay between slave heartbeats
to master
--expect-slaves=EXPECT_SLAVES
How many slaves master should expect to connect before
starting the test (only when --no-web used).
--no-web Disable the web interface, and instead start running
the test immediately. Requires -c and -r to be
specified.
-c NUM_CLIENTS, --clients=NUM_CLIENTS
Number of concurrent Locust users. Only used together
with --no-web
-r HATCH_RATE, --hatch-rate=HATCH_RATE
The rate per second in which clients are spawned. Only
used together with --no-web
-t RUN_TIME, --run-time=RUN_TIME
Stop after the specified amount of time, e.g. (300s,
20m, 3h, 1h30m, etc.). Only used together with --no-
web
-L LOGLEVEL, --loglevel=LOGLEVEL
Choose between DEBUG/INFO/WARNING/ERROR/CRITICAL.
Default is INFO.
--logfile=LOGFILE Path to log file. If not set, log will go to
stdout/stderr
--print-stats Print stats in the console
--only-summary Only print the summary stats
--no-reset-stats [DEPRECATED] Do not reset statistics once hatching has
been completed. This is now the default behavior. See
--reset-stats to disable
--reset-stats Reset statistics once hatching has been completed.
Should be set on both master and slaves when running
in distributed mode
-l, --list Show list of possible locust classes and exit
--show-task-ratio print table of the locust classes' task execution
ratio
--show-task-ratio-json
print json data of the locust classes' task execution
ratio
-V, --version show program's version number and exit
3.1 示例
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
#coding:utf-8from locust import HttpLocust, TaskSet, taskclass UserBehavior(TaskSet): def on_start(self): """ on_start is called when a Locust start before any task is scheduled """ self.login() def login(self): self.client.post("/login", {"username":"ellen_key", "password":"education"}) @task(2) def index(self): self.client.get("/") @task(1) def profile(self): self.client.get("/profile")class WebsiteUser(HttpLocust): task_set = UserBehavior host='http://example.com' min_wait = 5000 max_wait = 9000 |
上面是官方的示例demo,定义了针对http://example.com网站的测试场景:先模拟用户登录系统,然后随机地访问index(/)和profile页面(/profile/),请求比例为2:1;并且,在测试过程中,两次请求的间隔时间为5~9秒间的随机值。
3.2 运行
要运行上述locust脚本,如果文件名为locustfile.py且在当前目录下,可以这样运行:
|
1
|
locust |
如果locust脚本文件目录不同或名称不同,需要使用-f指定文件(--host用来指定测试主机地址):
|
1
|
locust -f locust_files/my_locust_file.py --host=http://example.com |
要运行分布在多个进程上的locust,我们需要使用--master启动主进程:
|
1
|
locust -f locust_files/my_locust_file.py --master --host=http://example.com |
之后使用--slave启动任意数量的从进程:
|
1
|
locust -f locust_files/my_locust_file.py --slave --host=http://example.com |
如果在多台机器上分布式运行locust,我们需要在启动从进程时指定master-host(默认为127.0.0.1):
|
1
|
locust -f locust_files/my_locust_file.py --slave --master-host=192.168.0.100 --host=http://example.com |
3.3 Locust web模式
Locust默认使用该方式启动,启动后在本机打开http://localhost:8089/,可以看到Locust WEB页面,设置并发用户数及每秒请求数后即可开始性能测试。
3.4 Locust no-web模式
Locust也可使用no-web模式,使用命令如下:
|
1
|
locust -f locust_files/my_locust_file.py --no-web --csv=locust -c10 -r2 --run-time 1h30m |
其中--no-web表示使用no-web模式,--csv表示运行结果文件名,-c 并发用户数,-r 每秒请求数,--run_time 运行时间
4. locustfile详解
locustfile中测试场景均是继承自Locust和TaskSet的子类,下面分别介绍Locust和TaskSet两个类。
4.1 Locust类
Locust类的client属性对应虚拟用户作为客户端的请求方法。在使用Locust时,需要先继承Locust类,然后在继承子类中的client属性中绑定客户端的实现类。
对于常见的HTTP(S)协议,Locust已经实现了HttpLocust类,其client属性绑定了HttpSession类,而HttpSession又继承自requests.Session。因此在测试HTTP(S)的Locust脚本中,我们可以通过client属性来使用Python requests库的所有方法,包括GET/POST/HEAD/PUT/DELETE/PATCH等,调用方式也与requests完全一致。另外,由于requests.Session的使用,因此client的方法调用之间就自动具有了状态记忆的功能。常见的场景就是,在登录系统后可以维持登录状态的Session,从而后续HTTP请求操作都能带上登录状态。
而对于HTTP(S)以外的协议,我们同样可以使用Locust进行测试,只是需要我们自行实现客户端。在客户端的具体实现上,可通过注册事件的方式,在请求成功时触发events.request_success,在请求失败时触发events.request_failure即可。然后创建一个继承自Locust类的类,对其设置一个client属性并与我们实现的客户端进行绑定。主要,我们就可以像使用HttpLocust类一样,测试其它协议类型的系统了。
在Locust类中,除了client属性,还有几个属性需要关注下:
task_set: 指向一个TaskSet类,TaskSet类定义了用户的任务信息,该属性为必填;
max_wait/min_wait: 每个用户执行两个任务间隔时间的上下限(毫秒),具体数值在上下限中随机取值,若不指定则默认间隔时间固定为1秒;
host:被测系统的host,当在终端中启动locust时没有指定--host参数时才会用到;
weight:同时运行多个Locust类时会用到,用于控制不同类型任务的执行权重。
测试开始后,每个虚拟用户(Locust实例)的运行逻辑都会遵循如下规律:
先执行WebsiteTasks中的on_start(只执行一次),作为初始化;
从WebsiteTasks中随机挑选(如果定义了任务间的权重关系,那么就是按照权重关系随机挑选)一个任务执行;
根据Locust类中min_wait和max_wait定义的间隔时间范围(如果TaskSet类中也定义了min_wait或者max_wait,以TaskSet中的优先),在时间范围中随机取一个值,休眠等待;
重复2~3步骤,直至测试任务终止。
4.2 Taskset类
TaskSet类实现了虚拟用户所执行任务的调度算法,包括规划任务执行顺序(schedule_task)、挑选下一个任务(execute_next_task)、执行任务(execute_task)、休眠等待(wait)、中断控制(interrupt)等等。在此基础上,我们就可以在TaskSet子类中采用非常简洁的方式来描述虚拟用户的业务测试场景,对虚拟用户的所有行为(任务)进行组织和描述,并可以对不同任务的权重进行配置。
在TaskSet子类中定义任务信息时,可以采取两种方式,@task装饰器和tasks属性。
采用@task装饰器定义任务信息时,描述形式如下:
|
1
2
3
4
5
6
7
8
|
from locust import TaskSet, taskclass UserBehavior(TaskSet): @task(1) def test_job1(self): self.client.get('/job1') @task(2) def test_job2(self): self.client.get('/job2') |
采用tasks属性定义任务信息时,描述形式如下:
|
1
2
3
4
5
6
7
8
|
from locust import TaskSetdef test_job1(obj): obj.client.get('/job1')def test_job2(obj): obj.client.get('/job2')class UserBehavior(TaskSet): tasks = {test_job1:1, test_job2:2} # tasks = [(test_job1,1), (test_job1,2)] # 两种方式等价 |
在如上两种定义任务信息的方式中,均设置了权重属性,即执行test_job2的频率是test_job1的两倍。
在TaskSet子类中除了定义任务信息,还有一个是经常用到的是on_start函数。这个和LoadRunner中的vuser_init功能相同,在正式执行测试前执行一次,主要用于完成一些初始化的工作。例如,当测试某个搜索功能,而该搜索功能又要求必须为登录态的时候,就可以先在on_start中进行登录操作;前面也提到,HttpLocust使用到了requests.Session,因此后续所有任务执行过程中就都具有登录状态了。
5. 自定义客户端测试其他系统
虽然,locust主要是为了测试HTTP而生。然而,它可以很容易地扩展到测试任何基于请求/响应的系统,只需要编写一个触发request_success和request_failure事件自定义客户端即可。
官网提供了详细的示例,我们简单修改下就可以用来对任意系统进行性能测试:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
|
import timefrom locust import Locust, TaskSet, events, taskimport requestsclass TestHttpbin(object): def status(self): try: r = requests.get('http://httpbin.org/status/200') status_code = r.status_code print status_code assert status_code == 200, 'Test Index Error: {0}'.format(status_code) except Exception as e: print eclass CustomClient(object): def test_custom(self): start_time = time.time() try: # add your custom test function here TestHttpbin().status() name = TestHttpbin().status.__name__ except Exception as e: total_time = int((time.time() - start_time) * 1000) events.request_failure.fire(request_type="Custom", name=name, response_time=total_time, exception=e) else: total_time = int((time.time() - start_time) * 1000) events.request_success.fire(request_type="Custom", name=name, response_time=total_time, response_length=0)class CustomLocust(Locust): def __init__(self, *args, **kwargs): super(CustomLocust, self).__init__(*args, **kwargs) self.client = CustomClient()class ApiUser(CustomLocust): min_wait = 100 max_wait = 1000 class task_set(TaskSet): @task(1) def test_custom(self): self.client.test_custom() |
上述脚本里,我们自定义一个测试类TestHttpbin,其中status方法用来校验接口返回码;我们只需要在CustomClient类的test_custom方法中添加自定义的测试方法TestHttpbin().status(),即可使用locust对该方法进行负载测试。
taffy框架就是这样做实现了一份代码同时进行功能自动化及性能测试。
6. 参考资料
1. https://docs.locust.io/en/latest/
2. http://debugtalk.com/post/head-first-locust-user-guide/
locust --hellp的更多相关文章
- 性能测试工具Locust
An open source load testing tool. 一个开源性能测试工具. define user behaviour with python code, and swarm your ...
- python httprequest, locust
r = self.client.get("/orders", headers = {"Cookie": self.get_user_cookie(user[0] ...
- Python Locust对指定网站“一键压测”
[本文出自天外归云的博客园] 前篇 前篇:Python Locust性能测试框架实践 本篇 承上——归纳过程 在前篇的基础上,我们可以利用Locust性能测试框架编写python脚本对指定网站或者接口 ...
- linux安装locust
linux安装locust 1. 安装epel扩展源(目的是为了在安装Pip时不出现一堆乱七八糟的错误信息) EPEL(http://fedoraproject.org/wiki/EPEL) 是由 F ...
- Python Locust性能测试框架实践
[本文出自天外归云的博客园] Locust的介绍 Locust是一个python的性能测试工具,你可以通过写python脚本的方式来对web接口进行负载测试. Locust的安装 首先你要安装pyth ...
- 性能测试框架Locust初学笔记
Locust初探 Locust是一款类似于Jmeter开源负载测试工具,所不同的是它是用python实现,并支持python脚本. locust提供web ui界面,能够方便用户实时监控脚本运行状态. ...
- 初遇locust
大概有四个月没有用过PYTHON的我. 今天差点都不知道怎么运行了. 说起来真是丢人呐. 幸好还是存留着一点点印象,再加上看了一下以前写的几篇文章, 还是比较快的想起来了.不然真的是要崩溃了. 刚开始 ...
- Locust性能测试工具的安装及实际应用
一.安装Locust 安装Locust之前先安装的库:gevent库:第三方库,gevent为python提供了比较完善的协程支持.使用gevent,可以获得极高的并发性能. pip install ...
- Locust性能测试框架,从入门到精通
1. Locust简介 Locust是使用Python语言编写实现的开源性能测试工具,简洁.轻量.高效,并发机制基于gevent协程,可以实现单机模拟生成较高的并发压力. 主要特点如下: 使用普通的P ...
随机推荐
- 虚拟机kali Linux 的网络配置
首先打开虚拟机中的kali 发现是没有办法连网的 然后我们将kaili关闭 然后打开 然后看黄色区域选择桥接模式然后重新启动即可 https://jingyan.baidu.com/article/7 ...
- Maven - skiptest
1. 概述 maven install 中尝试跳过 单元测试 2. 背景 从别处找来的工程 改了改发现测试过不了 编译都过不了 但又要着急继续调整 就是懒 这种事本来不提倡, 但是生产里数不胜数 Ma ...
- PHP基础学习笔记5
一.连接MYSQL 1.1 MySQLi - 面向对象 <?php $servername = "localhost"; $username = "username ...
- 常用的vi的命令模式下的快捷命令
知识点 在VI命令模式下:y 表示拷贝, d 表示删除,p标识粘贴 1.删除 dw 表示删除从当前光标到光标所在单词结尾的内容. d0 表示删除从当前光标到光标所在行首的内容. d$ 表示删除从当前光 ...
- php和redis怎么实现消息队列
把瞬间服务器的请求处理换成异步处理,缓解服务器的压力,实现数据顺序排列获取.本文主要和大家分享php和redis如何实现消息队列,希望能帮助到大家. redis实现消息队列步骤如下: 1).redis ...
- vue中watch和computed为什么能监听到数据的改变以及不同之处
先来个流程图,水平有限,凑活看吧-_-|| 首先在创建一个Vue应用时: var app = new Vue({ el: '#app', data: { message: 'Hello Vue!' } ...
- hackinglab 种族歧视
首先打开题目 发现是禁止访问的然后打开后台 发现后台也没有什么有用的信息所以用bp抓包 然后修改一下国家语言
- Linux 服务器作为Nginx web服务器常见优化参数
内核参数调整cat /etc/sysctl.conf# sysctl settings are defined through files in # /usr/lib/sysctl.d/, /run/ ...
- 树莓派安装ubuntu_meta并配置开发环境
1.烧录系统 首先准备好我们要烧录的ubuntu_meta系统,可以在树莓派官网中下载https://www.raspberrypi.org/downloads/ 这里我们选择 Raspberry P ...
- 抽象方法(abstract)
抽象方法:父类当中的方法,不确定如何进行 {} 方法体实现,那么这就是一个抽象方法 例如:动物类:叫{} // 叫方法就是抽象方法,不确定以何种声音叫 格式: 抽象方法:加上 abstract ...