1. Locust简介

Locust是使用Python语言编写实现的开源性能测试工具,简洁、轻量、高效,并发机制基于gevent协程,可以实现单机模拟生成较高的并发压力。

官网:https://locust.io/

主要特点如下:

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 '' (allinterfaces)-P PORT, --port=PORT, --web-port=PORTPort on which to run web host-f LOCUSTFILE, --locustfile=LOCUSTFILEPython module file to import, e.g. '../other.py'.Default: locustfile--csv=CSVFILEBASE, --csv-base-name=CSVFILEBASEStore current request stats to files in CSV format.--master              Set locust to run in distributed mode with thisprocess as master--slave               Set locust to run in distributed mode with thisprocess as slave--master-host=MASTER_HOSTHost or IP address of locust master for distributedload testing. Only used when running with --slave.Defaults to 127.0.0.1.--master-port=MASTER_PORTThe port to connect to that is used by the locustmaster for distributed load testing. Only used whenrunning with --slave. Defaults to 5557. Note thatslaves will also connect to the master node on thisport + 1.--master-bind-host=MASTER_BIND_HOSTInterfaces (hostname, ip) that locust master shouldbind to. Only used when running with --master.Defaults to * (all available interfaces).--master-bind-port=MASTER_BIND_PORTPort that locust master should bind to. Only used whenrunning with --master. Defaults to 5557. Note thatLocust will also use this port + 1, so by default themaster node will bind to 5557 and 5558.--heartbeat-liveness=HEARTBEAT_LIVENESSset number of seconds before failed heartbeat fromslave--heartbeat-interval=HEARTBEAT_INTERVALset number of seconds delay between slave heartbeatsto master--expect-slaves=EXPECT_SLAVESHow many slaves master should expect to connect beforestarting the test (only when --no-web used).--no-web              Disable the web interface, and instead start runningthe test immediately. Requires -c and -r to bespecified.-c NUM_CLIENTS, --clients=NUM_CLIENTSNumber of concurrent Locust users. Only used togetherwith --no-web-r HATCH_RATE, --hatch-rate=HATCH_RATEThe rate per second in which clients are spawned. Onlyused together with --no-web-t RUN_TIME, --run-time=RUN_TIMEStop after the specified amount of time, e.g. (300s,20m, 3h, 1h30m, etc.). Only used together with --no-web-L LOGLEVEL, --loglevel=LOGLEVELChoose between DEBUG/INFO/WARNING/ERROR/CRITICAL.Default is INFO.--logfile=LOGFILE     Path to log file. If not set, log will go tostdout/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 hasbeen 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 runningin distributed mode-l, --list            Show list of possible locust classes and exit--show-task-ratio     print table of the locust classes' task executionratio--show-task-ratio-jsonprint json data of the locust classes' task executionratio-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-8
from locust import HttpLocust, TaskSet, task
class 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, task
class 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 TaskSet
def 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 time
from locust import Locust, TaskSet, events, task
import requests
class 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 e
class 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/

转载于:https://www.cnblogs.com/SunshineKimi/p/11218646.html

locust --hellp相关推荐

  1. 正式压力测试:locust进阶,超简单搭建生产级locust集群

    locust进阶,搭建生产级locust集群 本教程基于k8s集群搭建,使用helm作为包管理工具 通过helm安装locust

  2. locust入门:单机使用locust运行压力测试

    locust的官方文档在这里: http://docs.locust.io/en/stable/ 前置:locust的测试脚本使用为python(若未安装python环境,可以搜索安装python) ...

  3. 基于python的压测工具_Python Locust性能测试简介及框架实践

    Locust(俗称 蝗虫), 一个轻量级的开源压测工具,用Python编写.使用 Python 代码定义用户行为,也可以仿真百万个用户: Locust 非常简单易用,是分布式,用户负载测试工具.Loc ...

  4. 深入浅出开源性能测试工具 Locust (使用篇 1)

    在<[LocustPlus序]漫谈服务端性能测试>中,我对服务端性能测试的基础概念和性能测试工具的基本原理进行了介绍,并且重点推荐了Locust这一款开源性能测试工具.然而,当前在网络上针 ...

  5. 【转载】locust性能测试3

    #coding=utf-8 from locust import HttpLocust,TaskSet,task import queue import randomclass test_taskse ...

  6. locust压测工具:测试信息输出与分布式模式

    locust压测环境描述 本文环境python3.5.2 locust版本0.9.0 locust测试信息输出与分布式模式 本文将主要分析两个方面的内容: 1.locust在启动运行在跑测试用例的时候 ...

  7. locust压测工具:http测试过程与定时停止

    locust压测环境描述 本文环境python3.5.2 locust版本0.9.0 locust示例的执行过程 上文大概描述了locust的启动了流程,本文主要是接着上文继续分析,示例代码中的htt ...

  8. mysql的ab测试工具_轻量级性能测试工具ab / wrk / locust 分析 对比

    很早前,介绍过4款压力测试工具 今天再推荐下两款更强大的. 1. ab 是目前最常见的压力测试工具.其典型用法如下: shell> ab -k -n 1000 -c 10 -t 100 http ...

  9. 8s 接口压力测试_Python Locust 基于Robot Framework实现关键字驱动接口性能测试

    上一篇文章Python Locust 基于Robot Framework实现关键字驱动接口性能测试中,我们分享了如何利用Locust库结合Robot Framework实现接口的性能指标的持续性比较, ...

最新文章

  1. linux中的apachectl是什么命令
  2. Android拖动和缩放图片
  3. Hacker's Browser
  4. Python对图像的基本操作
  5. python输出第几行第几个_python怎么PRINT出第几行
  6. 图片马可以直接连接吗_商标买来可以直接使用吗?
  7. python求组合数_求组合数的算法_Cppowboy's Blog - SegmentFault 思否
  8. 被裁的第50天,我终于拿到心仪公司Offer
  9. 安装sqlyog和使用注册码
  10. vue中computer和watch的区别和使用
  11. ps有一款比较好用的插件也就是调色插件用过吗
  12. win10 更新计算机时间,win10更新时间太长怎么回事_windows10更新时间太久解决教程...
  13. java汉字获取拼音的方法
  14. 如何让多个word文档合并成一个
  15. NEON 常用函数及其执行结果
  16. [转]AJAX基础教程
  17. gitblit+jenkins本地服务
  18. P2P原理 和技术演变(概念性质)
  19. oracle动态菜单,本地动态SQL(Open for等)如何获取SQL的定义属性,包括栏位名称和数据类型...
  20. python如何输入一个数_Python 数据的输入

热门文章

  1. 无尽的永恒什么用 dnf大转移无尽的永恒有什么用 怎么得 会涨价吗
  2. 使用简洁明了的语言和排版风格,可以提高公文的可读性
  3. 联盟链或成政策暖风最大受益者,国产公链纷纷加入DeFi热潮
  4. 数据仓库数据同步策略
  5. 用Java写微信支付Jsp进行下单操作
  6. 戴尔不再提供电脑和手机互联功能:Mobile Connect应用将终止服务(学习资料)
  7. vue-cli3、swiper、swiper.animate配合使用
  8. Credit Card Approval Predict w/ Towhee
  9. AIGC 与 Neuralink 脑机接口的融合
  10. PHP对象转换成数组