安裝
locust?官方介紹https://locust.io/
① 可以直接通過pip安裝,安裝命令
pip install locustio
可能出現(xiàn)的問題:可能出現(xiàn)gcc編譯失敗等問題,可以先pip安裝zmq
② 通過源碼安裝
備注:在gcc 4.8+以上版本使用源碼安裝。下載locust-master代碼
安裝順序:
python setup.py build?
python setup.py install
如果locust有依賴安裝失敗,可用pip安裝缺失依賴。例:pip安裝zmq,pip install zmq
驗(yàn)證是否安裝成功:locust --help
③ 安裝的Q&A
· 注意locust 的版本和python版本是否匹配,可以查看locust的setup.py 中的說明。目前已知0.11 /0.9支持python2.7

· locust可以啟動(dòng)壓測腳本,web頁面不能操作、沒有趨勢圖。排查flask是否安裝成功:import flask
啟動(dòng)方式
單進(jìn)程運(yùn)行
no_web方式
可以使用Locust的單進(jìn)程no_web運(yùn)行模式進(jìn)行本地腳本調(diào)試。啟動(dòng)方式:locust -f test.py --no-web -c 1
參數(shù)講解:-f?啟動(dòng)的文件, --no-web?非web方式啟動(dòng) -c?指定并發(fā)數(shù) -n?總執(zhí)行次數(shù)
web方式
如果采用web形式,,則通常情況下無需指定其它額外參數(shù),Locust默認(rèn)采用8089端口啟動(dòng)web。如果使用其他端口,需要使用如下參數(shù)進(jìn)行指定
-P, --port:指定web端口,默認(rèn)為8089
locust -f test.py? -P 8098 --web-host=127.0.0.1 --only-summary
啟動(dòng)的時(shí)候注意指定--web-host,防止出現(xiàn)綁定異常
按照如上方式啟動(dòng)后,case并沒有執(zhí)行,通過暴露的web服務(wù)127.0.0.1:8098?訪問webUI,配置參數(shù)
Number of users to simulate: 設(shè)置并發(fā)用戶數(shù),對應(yīng)中no_web模式的-c, --clients參數(shù);
Hatch rate (users spawned/second): 啟動(dòng)虛擬用戶的速率,對應(yīng)著no_web模式的-r, --hatch-rate參數(shù)。
多進(jìn)程分布式運(yùn)行
當(dāng)并發(fā)壓力要求較高時(shí),就需要用到Locust的多進(jìn)程分布式運(yùn)行模式。從字面意思上看,第一反應(yīng)就是多臺(tái)壓力機(jī)同時(shí)運(yùn)行,每臺(tái)壓力機(jī)分擔(dān)負(fù)載一部分的壓力生成。的確,Locust支持任意多臺(tái)壓力機(jī)(一主多從)的分布式運(yùn)行模式,但這里說到的多進(jìn)程分布式運(yùn)行模式還有另外一種情況,就是在同一臺(tái)壓力機(jī)上開啟多個(gè)slave的情況。這是因?yàn)楫?dāng)前階段大多數(shù)計(jì)算機(jī)的CPU都是多處理器(multiple processor cores),單進(jìn)程運(yùn)行模式下只能用到一個(gè)處理器的能力,而通過在一臺(tái)壓力機(jī)上運(yùn)行多個(gè)slave,就能調(diào)用多個(gè)處理器的能力了。比較好的做法是,如果一臺(tái)壓力機(jī)有N個(gè)處理器內(nèi)核,那么就在這臺(tái)壓力機(jī)上啟動(dòng)一個(gè)master,N個(gè)slave。當(dāng)然,我們也可以啟動(dòng)N的倍數(shù)個(gè)slave,但是根據(jù)試驗(yàn)數(shù)據(jù),效果跟N個(gè)差不多,因此只需要啟動(dòng)N個(gè)slave即可。
master啟動(dòng)方式
locust -f test.py? -P 8098 --web-host=10.10.10.10? --master :?沒有特殊指定--master-bind-port默認(rèn)為5557
slave啟動(dòng)方式
locust -f test.py --slave --master-host=10.10.10.10 :沒有特殊指定?--master-port?默認(rèn)為5557
master和slave都啟動(dòng)完畢后,就可以在瀏覽器中通過http://locust_machine_ip:8089進(jìn)入Locust的Web管理頁面了。使用方式跟單進(jìn)程web形式完全相同,只是此時(shí)是通過多進(jìn)程負(fù)載來生成并發(fā)壓力,在web管理界面中也能看到實(shí)際的slave數(shù)量。
locust相關(guān)
Locust
可以理解為用戶,用戶的行為由taskset指定。大家可以基于locust基類實(shí)現(xiàn)繼承類的用戶,比如:httpLocust就是基于Locust派生而來,并指定了它的client =?httpSession
屬性介紹:
max_wait= 1000? :?執(zhí)行task最大等待時(shí)間
min_wait= 1000 :執(zhí)行task最小等待時(shí)間
task_set= None :?locust執(zhí)行的task類
weight= 10:locust被挑選的權(quán)重
HttpLocust
基于Locust派生的http?user,底層的client為httpSession(基于python.requests實(shí)現(xiàn))。HTTP的類壓測可直接用HttpLocust
TaskSet
Locust?User執(zhí)行的task列表,屬性解釋:
max_wait= None :task最大執(zhí)行間隔,默認(rèn)不設(shè)置,如果設(shè)置了將覆蓋locust中的max_wait
min_wait= None:task最小執(zhí)行間隔,默認(rèn)不設(shè)置,如果設(shè)置了將覆蓋locust中的max_wait
tasks =?[] :task列表
on_start:方法,執(zhí)行tasks之前執(zhí)行的方法,只執(zhí)行一次,可以作為初始化使用
如何寫TaskSet
①?通過tasks指定執(zhí)行權(quán)重
from?locust?import?HttpLocust, TaskSet
deflogin(l):
????l.client.post("/login", {"username":"ellen_key",?"password":"education"})
def index(l):
????l.client.get("/")
def profile(l):
????l.client.get("/profile")
class UserBehavior(TaskSet):
????tasks?={index:?2, profile:?1}#注意這一行,后面數(shù)字為執(zhí)行比例
????def on_start(self):
????????login(self)
②?通過裝飾器權(quán)重、執(zhí)行順序
from?locust?import?HttpLocust, TaskSet
def login(l):
????l.client.post("/login", {"username":"ellen_key",?"password":"education"})
class UserBehavior(TaskSet):
????def on_start(self):
????????login(self)
@task(2)?
@seq_task(1)
def index(self):
? ? self.client.get("/")
@task(1)
@seq_task(2)
def profile(l):
????l.client.get("/profile")
@task?是權(quán)重裝飾器,index:profile的執(zhí)行比例是2:1
@seq_task為順序裝飾器,tasks按照如上先index、后profile的順序進(jìn)行
Event?hooks
為了進(jìn)行數(shù)據(jù)統(tǒng)計(jì),用戶可以自己定義成功、失敗、異常相關(guān)的事件統(tǒng)計(jì)。目前支持的可用hooks見https://docs.locust.io/en/stable/api.html#available-hooks
以request_success?為例進(jìn)行說明如何使用
request_success= <locust.events.EventHook object>
request_success?is fired when a request is completed successfully.
Listeners should take the following arguments:
request_type: Request type method used
name: Path to the URL that was called (or override name if it was used in the call to the client)
response_time: Response time in milliseconds
response_length: Content-length of the response
當(dāng)你認(rèn)為task中某個(gè)請求成功,可以直接通過events.request_success.fire進(jìn)行記錄,參數(shù)即為如上描述。
由于Locust類和TaskSet類有多種setup/teardown相互依賴,下面是這些類的執(zhí)行順序,方便理解:
Locust setup
TaskSet setup
TaskSet on_start
TaskSet tasks…
TaskSet on_stop
TaskSet teardown
Locust teardown
附錄
深入淺出開源性能測試工具 Locust? ??https://debugtalk.com/post/head-first-locust-user-guide/?
locust官方文檔https://docs.locust.io/en/stable/
gevent?猴子補(bǔ)丁http://www.gevent.org/api/gevent.monkey.html
dumeter locust的幫助文檔Locust測試