在了解locust基本代码结构之后,我们再来详细分析各个模块。
首先是locust的程序入口,main.py模块
主要功能如下:
- 命令行参数解析
- 查找和载入配置文件
- 根据命令行参数执行不同的功能
相关函数如下:
1,函数parse_options()
主要功能:使用外部模块optparse,添加命令行参数,并返回命令行解析后的三元组(解析器本身、参数、参数对应的值)
def parse_options():
"""
使用optparse.OptionParser解析命令行,返回元组:parser, opts, args
Return list of arguments, largely for use in `parse_arguments`.
"""
# 初始化parser解析器
parser = OptionParser(usage="locust [options] [LocustClass [LocustClass2 ... ]]")
# -H或--host参数表示压测服务的地址,即URL的host地址,例如:压测目标URL是http://www.baidu.com/ask/,可以设置host地址为http://www.baidu.com
parser.add_option(
'-H', '--host',
dest="host",
default=None,
help="Host to load test in the following format: http://10.21.32.33"
)
# --web-host参数表示的是本地Web界面服务的地址,默认为''
parser.add_option(
'--web-host',
dest="web_host",
default="",
help="Host to bind the web interface to. Defaults to '' (all interfaces)"
)
# -P、--port或--web-port用于指定本地Web界面服务的端口,默认为8089
parser.add_option(
'-P', '--port', '--web-port',
type="int",
dest="port",
default=8089,
help="Port on which to run web host"
)
# -f或者--locustfile用于指定压测文件,默认为locustfile.py
parser.add_option(
'-f', '--locustfile',
dest='locustfile',
default='locustfile',
help="Python module file to import, e.g. '../other.py'. Default: locustfile"
)
# --csv或者--csv-base-name可以用于指定一个CSV文件用于存储压测过程中当前的状态
parser.add_option(
'--csv', '--csv-base-name',
action='store',
type='str',
dest='csvfilebase',
default=None,
help="Store current request stats to files in CSV format.",
)
# --master在需要进行分布式压测时,可以用于设置为master节点
parser.add_option(
'--master',
action='store_true',
dest='master',
default=False,
help="Set locust to run in distributed mode with this process as master"
)
# --slave在需要进行分布式压测时,可以用于设置为slave节点
parser.add_option(
'--slave',
action='store_true',
dest='slave',
default=False,
help="Set locust to run in distributed mode with this process as slave"
)
# --master-host用于设置master节点的IP,和--slave一起配合使用时需要设置该参数。默认为127.0.0.1。
parser.add_option(
'--master-host',
action='store',
type='str',
dest='master_host',
default="127.0.0.1",
help="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节点的端口,仅限于和--slave配合使用时设置该参数。默认为5557
parser.add_option(
'--master-port',
action='store',
type='int',
dest='master_port',
default=5557,
help="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节点绑定host地址,默认绑定master节点所有网卡。需要和--master参数一起配合使用
parser.add_option(
'--master-bind-host',
action='store',
type='str',
dest='master_bind_host',
default="*",
help="Interfaces (hostname, ip) that locust master should bind to. Only