實現場景:所有併發虛擬使用者共享同乙份測試資料,並且保證虛擬使用者使用的資料不重複。
例如,模擬10使用者併發註冊賬號,總共有100個手機號,要求註冊賬號不重複,註冊完畢後結束測試
虛擬使用者
locust1
locust2
locust3
locust4
locust5
locust6
locust7
locust8
locust9
locust10
共享資料
tel1
tel2
tel3
tel4
tel5
tel6
tel7
......
tel99
tel100
虛擬使用者數,可以在啟動的時候設定,這裡先裝備好註冊需要用到的手機號,可以用list生成
# 生成測試手機號
demo = 13812120000
teldatas = [str(demo+i) for i in range(100)]
print(teldatas)
將測試資料加到佇列
import queue
# 生成測試手機號
demo = 13812120000
teldatas = [str(demo+i) for i in range(100)]
# print(teldatas)
# 新增到佇列
telqueue = queue.queue()
for i in teldatas:
telqueue.put_nowait(i)
以下是乙個簡單的demo模型,具體的註冊介面替換過去就可以了
# 儲存為 locustfile4.py
tel = self.locust.telqueue.get() # 獲取佇列裡的資料
print(tel)
except queue.empty: # 佇列取空後,直接退出
print("no data exist")
exit(0)
print("當前註冊手機號:%s" % tel)
# body =
# self.client.post("/register", data=body) # post方法傳送請求
# 生成測試手機號
teldatas = [str(13812120000+i) for i in range(100)]
# 新增到佇列
telqueue = queue.queue()
for i in teldatas:
telqueue.put_nowait(i)
if __name__ == "__main__":
import os
os.system("locust -f locustfile4.py")
cmd命令列啟動
$ locust -f locustfile4.py開啟web頁面,輸入10個使用者, 可以看到控制台的列印
[2018-09-21 00:00:58,013] desktop-hj487c8/info/stdout: 當前註冊手機號:13812120096
[2018-09-21 00:00:58,013] desktop-hj487c8/info/stdout:
[2018-09-21 00:00:58,015] desktop-hj487c8/info/stdout: 13812120097
[2018-09-21 00:00:58,015] desktop-hj487c8/info/stdout:
[2018-09-21 00:00:58,015] desktop-hj487c8/info/stdout: 當前註冊手機號:13812120097
[2018-09-21 00:00:58,015] desktop-hj487c8/info/stdout:
[2018-09-21 00:00:58,015] desktop-hj487c8/info/stdout: 13812120098
[2018-09-21 00:00:58,015] desktop-hj487c8/info/stdout:
[2018-09-21 00:00:58,015] desktop-hj487c8/info/stdout: 當前註冊手機號:13812120098
[2018-09-21 00:00:58,015] desktop-hj487c8/info/stdout:
[2018-09-21 00:00:58,017] desktop-hj487c8/info/stdout: 13812120099
[2018-09-21 00:00:58,017] desktop-hj487c8/info/stdout:
[2018-09-21 00:00:58,017] desktop-hj487c8/info/stdout: 當前註冊手機號:13812120099
[2018-09-21 00:00:58,017] desktop-hj487c8/info/stdout:
locust 效能測試
作業系統最小執行單位是程序,而程序最小執行單位是執行緒 協程是微小的執行緒,完全由程式所控制 在使用者態執行 好處是效能大幅度的提公升 locust 是一款分布式負載測試工具,完全基於事件,即乙個locust節點也可以在乙個程序中支援數千併發使用者,不使用 通過 gevent 使用輕量級過程 即在自...
5 locust 引數化3 使用
1 指令碼 新建任務集 from locust import taskset,task from test.test get list queue1 import csv to list from test.test get params1 import get random param class...
效能測試之locust
2 在本地儲存檔中新建立乙個python3資料夾 4 點選勾選add python 3.9 5 然後點選customize installa進入optional features,然後點選next 6 點選next進入advanced options,然後點選browse選擇python的安裝位置,...