locust --hellp

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 '' (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-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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值