Locust性能測試工具的安裝及實際應用

1、安裝Locustnode

安裝Locust以前先安裝的庫:
gevent庫:第三方庫,gevent爲python提供了比較完善的協程支持。使用gevent,能夠得到極高的併發性能。python

pip install gevent==1.1.2web

flask庫:Flask是一個使用 Python 編寫的輕量級 Web 應用框架。json

pip install flask==0.10.1flask

requests庫:用python寫過接口測試的朋友應該都不陌生,Requests 是用Python語言編寫,基於 urllib,採用 Apache2 Licensed 開源協議的 HTTP 庫。它比 urllib 更加方便,能夠節約咱們大量的工做,徹底知足 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官網下載: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
若是還報錯再執行:
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

安裝完成後,執行:locust–help
看到以下locust相關命令介紹,說明安裝成功。

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

 

 

2、使用Locust測試淘寶首頁

 1 #locust_taobao.py
 2 
 3 #!/usr/bin/env python
 4 #-*-coding:utf-8-*-
 5 #author:@TT
 6 
 7 from locust import HttpLocust, TaskSet, task
 8 #定義用戶行爲
 9 class UserBehavior(TaskSet):
10 
11 @task
12 def taobao_page(self): #定義一個方法訪問淘寶首頁
13 self.client.get("/")
14 
15 class WebsiteUser(HttpLocust):
16 task_set = UserBehavior
17 min_wait = 3000 #用戶等待時間下限
18 max_wait = 5000 #用戶等待時間上限

 

 

執行:
locust -f locust_taobao.py --host=https://www.taobao.com

提示:Starting web monitor at *.8089
           Starting Locust 0.7.5

說明已經啓動成功,瀏覽器輸入:http://120.76.139.13:8089便可看到設置頁面,並監控

Number of users to simulate:設置須要模擬的用戶數量,此次只是爲了演示,設置爲10;

Hatch rate:每秒須要啓動的用戶數量,此次只是爲了演示,設置爲2;

點擊『start swarming』,開始了性能測試:

 

Type:請求的類型;

Name:請求的url或者自定義的統計分組名字;

requests:當前請求的數量;

fails:當前請求失敗的數量;

Median:中間值,一半的服務器響應時間高於該值,而另外一半的服務器響應時間低於該值(毫秒);

Average:全部請求的平均響應時間(毫秒);

Min:請求最小響應時間(毫秒);

Max:請求最大響應時間(毫秒);

Content Size:單個請求的大小(字節);

reqs/sec:每秒請求的個數;

今天就先整理到這裏,若是按照以上內容操做,對Locust的安裝和性能測試的執行有了大概的瞭解。後續會不斷更新……

 


 

                                                               瞭解更多請關注微信公衆號:測試架構師

                                                         

相關文章
相關標籤/搜索