一、如何编写一个locust脚本
只要编写的python脚本至少申明一个类,并且继承Locust。
二、执行locust
-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.
--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
基本指令如下:locust -f yourlocustfile.py --host=https://www.baidu.com
三、Locust class
每个locust代表一个或者一群虚拟用户,locust类会对每个虚拟用户实例化,实例对象属性如下:
1、task_set
task_set需指向TaskSet类,定义用户场景或操作
2、max_wait和min_wait
单位,ms,默认值1000ms,不指定max_wait和min_wait每个task执行将间隔1s,如果指定max_wait和min_wait每个task将间隔max_wait和min_wait之间的时间执行。
以下代码每个task执行间隔为5~9s
from locust import Locust, TaskSet, task
class MyTask(TaskSet):
@task(2)
def index(self):
print('The first page')
pass
class MyLocust(Locust):
max_wait = 9000
min_wait = 5000
task_set = MyTask
host = ‘https://www.baidu.com’
pass
3、weight
在locust中指定weight属性时,weight越大locust执行的概率就越大,如下FirstLocust的执行概率是SecondLocust的5倍。
from locust import Locust
class FirstLocust(Locust):
weight = 5
pass
class SecondLocust(Locust):
weight = 1
pass
4、host
host属性,请求访问的域名,一般在commandline使用--host指定,如果在locust中指定则运行时将访问指定的域名。