一杨何、安裝Locust
安裝Locust之前先安裝的庫:
gevent庫:第三方庫垫言,gevent為python提供了比較完善的協(xié)程支持堕伪。使用gevent,可以獲得極高的并發(fā)性能纺弊。
pip install gevent==1.1.2
flask庫:Flask是一個使用 Python 編寫的輕量級 Web 應用框架圆恤。
pip install flask==0.10.1
requests庫:用python寫過接口測試的朋友應該都不陌生,Requests 是用Python語言編寫队伟,基于 urllib穴吹,采用 Apache2 Licensed 開源協(xié)議的 HTTP 庫。它比 urllib 更加方便嗜侮,可以節(jié)約我們大量的工作港令,完全滿足 HTTP 測試需求。
pip install requests==2.10.0
msgpack-python庫:比JSON快10倍的序列化包锈颗。
pip install msgpack-python==0.4.2
six庫:six 提供了一些簡單的工具用來封裝 Python 2 和 Python 3 之間的差異性顷霹。
pip install six==1.10.0
pyzmq庫:Pyzmq是zeromq的Python綁定。
pip install pyzmq==15.2.0
安裝庫的過程中可能會遇到一些報錯信息:
ReadTimeoutError: HTTPSConnectionPool(host='pypi.python.org', port=443): Read timed out.
解決方案:
更新pip版本到pip官網(wǎng)下載:https://pypi.python.org/simple/pip/
比如下載pip-9.0.1-py2.py3-none-any.whl
pip install pip-9.0.1-py2.py3-none-any.whl
如果還報錯再執(zhí)行:
pip --default-timeout=100 install -U pip
其他第三方庫都安裝完成后击吱,接下來再安裝locust:
pip install locustio
安裝locustio庫時可能會報錯:
error:command 'gcc' failed with exit status 1
解決方案:yum install gcc python-devel
安裝完成后淋淀,執(zhí)行:locust--help
看到如下locust相關(guān)命令介紹,說明安裝成功覆醇。
Usage: locust [options] [LocustClass [LocustClass2 ... ]]
Options:
-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
--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.
--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 clients. 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
-n NUM_REQUESTS, --num-request=NUM_REQUESTS
Number of requests to perform. 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
-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測試淘寶首頁
#locust_taobao.py
#!/usr/bin/env python
#-*-coding:utf-8-*-
#author:@TT
from locust import HttpLocust, TaskSet, task
#定義用戶行為
class UserBehavior(TaskSet):
@task
def taobao_page(self): #定義一個方法訪問淘寶首頁
self.client.get("/")
class WebsiteUser(HttpLocust):
task_set = UserBehavior
min_wait = 3000 #用戶等待時間下限
max_wait = 5000 #用戶等待時間上限
執(zhí)行:
locust -f locust_taobao.py --host=https://www.taobao.com
提示:Starting web monitor at *.8089
Starting Locust 0.7.5
說明已經(jīng)啟動成功炭臭,瀏覽器輸入:http://120.76.139.13:8089即可看到設(shè)置頁面,并監(jiān)控
Number of users to simulate:設(shè)置需要模擬的用戶數(shù)量,這次只是為了演示袍辞,設(shè)置為10鞋仍;
Hatch rate:每秒需要啟動的用戶數(shù)量,這次只是為了演示搅吁,設(shè)置為2威创;
點擊『start swarming』,開始了性能測試:
Type:請求的類型谎懦;
Name:請求的url或者自定義的統(tǒng)計分組名字肚豺;
requests:當前請求的數(shù)量;
fails:當前請求失敗的數(shù)量界拦;
Median:中間值吸申,一半的服務器響應時間高于該值,而另一半的服務器響應時間低于該值(毫秒)寞奸;
Average:所有請求的平均響應時間(毫秒)呛谜;
Min:請求最小響應時間(毫秒)在跳;
Max:請求最大響應時間(毫秒)枪萄;
Content Size:單個請求的大小(字節(jié))猫妙;
reqs/sec:每秒請求的個數(shù)瓷翻;