初涉Locust

Locust官網地址:https://docs.locust.io/en/latest/index.htmlhtml

參考:node

https://github.com/HttpRunnerpython

深刻淺出開源性能測試工具Locust(腳本加強)git

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

深刻淺出開源性能測試工具Locust(使用篇)web

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

【LocustPlus序】漫談服務端性能測試api

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

應用 Locust 快速上手寫壓測app

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)

 

後續待補充~

相關文章
相關標籤/搜索