大爱

Taffy集成Locust性能测试框架使用说明

2019-01-16

1、简介

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

官网:https://locust.io/

主要特点如下:

使用普通的Python脚本用户测试场景

分布式和可扩展,支持成千上万的用户

基于Web的用户界面,用户可以实时监控脚本运行状态

几乎可以测试任何系统,除了web http接口外,还可自定义clients测试其他类型系统

2、安装

1
pip install locustio

3、使用

taffy集成locust的基本流程如下:

3.1 配置 config/locust.yml

YAML是对人友好的数据序列化标准,可适用所有的编程语言。

与Json数据在线转换网站:https://www.json2yaml.com/

可以使用pip安装PyYAML:

1
pip install PyYAML

locust.yml主要配置项如下:

1) mode 运行模式

为0表示单例模式

为1表示分布式,使用可选参数 slaves_num, master_port

2) no-web 是否以no-web模式运行

为0表示普通模式,使用可选参数port,运行后需要先手工在浏览器打开locust 页面,填入并发用户数及每秒请求数后再执行测试

为1表示no-web模式,使用可选参数csv,c,r,run_time

3) min_wait及max_wait,可选参数,表示任务执行之间最小及最大等待时间(默认值分别为100/1000,单位ms)

4) task为测试任务配置:必填参数file,class,function分别代表测试文件,类及方法;可选参数weight(默认值1)

特别注意:使用nose框架Test generators方法编写的Tests,转换为locustfile后Locust无法正常执行性能测试(运行结果为空),所以这里填写的class/function暂不支持Test generators

locust.yml示例如下:

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
46
47
---
#mode 运行模式(默认为0) 0:单例模式; 1:分布式
#no-web 是否以no-web模式运行(默认为0) 0:否; 1:是
#min_wait/max_wait 任务执行之间的最小、最大等待时间(默认为10/1000ms)

#只有mode为1时,params中如下参数才有效:slaves_num,master_port
#slaves_num slaves数目(默认为当前机器cpu核数)
#master_port master绑定端口号(默认5557)

#只有no-web为0时,params中如下参数才有效:port
#port web端口号,默认8089

#只有no-web为1时,params中如下参数才有效:csv,c,r,run_time
#csv 运行结果文件名
#c 并发用户数
#r 每秒请求数
#run_time 运行时间
mode: 1
no_web: 0
min_wait: 10
max_wait: 100
params:
slaves_num: 4
master_port: 5557
port: 8089
csv: locust
c: 10
r: 10
run_time: 5m
#task 性能测试任务
task:
#file 测试文件名,支持相对路径如test_xxx/text_xxx_file.py
#class 测试类
#function 测试方法
#weight 任务选择的概率权重(默认1)
- file: test_demo.py
class: test_demo
function: test_httpbin_get
weight: 2
- file: test_demo.py
class: test_demo
function: test_httpbin_post
weight: 1
- file: test_demo.py
class: test_demo
function: test_webservice
weight: 1

3.2 运行locust.py

根据配置文件locust.yml,读取模板生成locustfile文件,然后运行locust执行性能测试,命令如下:

1
2
$ cd Taffy\Tests
$ python test_locust.py

3.3 与jmeter性能测试结果对比

针对百度首页搜索接口,分别使用jmeter及locust进行了10路并发性能测试(时间为5min)

1) jmeter及locust总体请求数分别为1173及1248
2) 平均响应时间分别为2.539s及2.390s,每秒请求数分别为3.9及4.2。

可以得出结论:locust与jmeter性能基本一致。

使用支付宝打赏
使用微信打赏

若你觉得我的文章对你有帮助,欢迎点击上方按钮对我打赏

扫描二维码,分享此文章