http://www.cnblogs.com/fnng/p/6081798.html
https://blog.csdn.net/gogoboi_jin/article/details/79229570
1.安裝locust
1唯蝶、安裝Python:
Python3
2疤苹、安裝Locuse
2.1菇爪, 通過pip命令安裝?/>?pip install locustio
2.2魏烫, 通過GitHub上克隆項目安裝(Python3推薦):https://github.com/locustio/locust
3靡馁、安裝?pyzmq
? ??If you intend to run Locust distributed across multiple processes/machines, we recommend you to also install pyzmq.
如果你打算運行Locust?分布在多個進程/機器,我們建議你也安裝pyzmq.
通過pip命令安裝野芒。?/>?pip install pyzmq
4赖阻、安裝成功,CMD敲入命令驗證浦楣。?/> locust --help
編寫簡單的性能測試腳本 創(chuàng)建py文件
import gevent
from geventimport monkey
gevent.monkey.patch_all()
import requests
from locustimport HttpLocust, TaskSet, task
# locust -f locust_test.py --host=https://httpbin.org
class UserBehavior(TaskSet):
@task(1)
def test_get(self):
params = {'show_envs':'1'}
self.client.get("/get",params=params)
# url = "https://httpbin.org/get"
# r = requests.get(url, params)
# print(r.json())
? @task(1)? ?#并發(fā)事件
def test_post(self):
json = {
"info": {"code":1, "sex":"男", "id":1900, "name":"喬巴"},
? ? ? ? "code":1,
? ? ? ? "name":"喬巴", "sex":"女",
? ? ? ? "id":1990
? ? ? }
self.client.post("/post",data=json)
class WebsiteUser(HttpLocust):
task_set = UserBehavior
min_wait =3000
? max_wait =6000
“”“”“”“”
創(chuàng)建UserBehavior()類繼承TaskSet類袖肥,為用戶行為。
創(chuàng)建baidu()?方法表示一個行為振劳,訪問百度首頁椎组。用@task()?裝飾該方法為一個任務。1表示一個Locust實例被挑選執(zhí)行的權重历恐,數值越大寸癌,執(zhí)行頻率越高。在當前UserBehavior()行為下只有一個baidu()任務弱贼,所以蒸苇,這里的權重設置為幾,并無影響吮旅。
WebsiteUser()類用于設置性能測試溪烤。
task_set:指向一個定義了的用戶行為類。
min_wait:用戶執(zhí)行任務之間等待時間的下界庇勃,單位:毫秒檬嘀。
max_wait:用戶執(zhí)行任務之間等待時間的上界,單位:毫秒责嚷。
運行性能測試