初涉Locust

Locust官网地址:https://docs.locust.io/en/latest/index.html

参考:

https://github.com/HttpRunner

深入浅出开源性能测试工具Locust(脚本增强)

http://debugtalk.com/post/head-first-locust-user-guide/

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

http://debugtalk.com/post/head-first-locust-advanced-script/

【LocustPlus序】漫谈服务端性能测试

http://debugtalk.com/post/locustplus-talk-about-performance-test/

应用 Locust 快速上手写压测

http://www.moye.me/2017/06/24/locust-load-testing/

~~~~~

locust命令

ptions:
  -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

  

举个栗子

from locust import HttpLocust, TaskSet, task
import json
import queue

class WebsiteTasks(TaskSet):

    # 初始化函数,在正式执行前被调用一次
    def on_start(self):

        # 接口请求头
        self.headers = {
        'Accept-Encoding': 'identity',
        'Content-Type': 'application/json'
     巴拉巴拉等…… } # task 装饰器,index函数 函数调用概率为about函数的2倍 # task 默认不定义权重,则函数调用概率 1:1 @task def testexamle(self): # 使用队列取参数,保证测试数据唯一性 try: data = self.locust.params_queue.get() except queue.Empty: print('when queue is empty, test ended.') exit(0) # 参数化实现 url = '/api/**/**?os='+data['os']+'&gamecode='+data['gamecode']+'&version='+data['version']+'&channelId='+data['channelId'] # print('URL:'+url) res = self.client.request(method = 'GET',url = url,headers = self.headers) # 保证并发测试数据唯一性,循环取数据,当前代码如果注释,则不再循环取数据,数据取值结束,则代码运行结束 self.locust.params_queue.put_nowait(data) assert res['Code'] == 0,'Code error, the acutal is '+res['Code']+'; the res is '+res # print('RESPONSE:'+str(res.json())) class WebsiteUser(HttpLocust): # 被测系统的host,当在终端中启动locust时没有指定--host参数时才会用到; host = "https://rmsyssvc.comfun.com" task_set = WebsiteTasks # max_wait/min_wait: 每个用户执行两个任务间隔时间的上下限(毫秒) # 具体数值在上下限中随机取值,若不指定则默认间隔时间固定为1秒; # min_wait = 1000 # max_wait = 5000 # 生成测试数据 params_queue = queue.Queue() data_init = { 'os': '1', 'gamecode': 'comf', 'version': '1.0.20180912', 'channelId': '1000000003', } params_queue.put_nowait(data_init) for i in range(3): data = { 'os':'1%d' % i, 'gamecode': 'comf%d' % i, 'version': '1.0.20180912%d' % i, 'channelId': '1000000003%d' % i, } params_queue.put_nowait(data)

后续待补充~

原文地址:https://www.cnblogs.com/wonderful0714/p/9705133.html