為什么用locust做壓測??
1.因為locust可以完美兼容python
2.locust采用的是協程,LoadRunner 和 Jmeter 這類采用進程和線程的測試工具,都很難在單機上模擬出較高的並發壓力。
Locust 的並發機制摒棄了進程和線程,采用協程(gevent)的機制。協程避免了系統級資源調度,由此可以大幅提高單機的並發能力
3.設置集合點需要自己寫代碼控制
4.並發數量多的情況下要用分布式,locust完美支持分布式還有無界面形式設置master主節點和slave子節點。
5.使用Locust進行性能測試時,當一台單機不足以模擬所需的用戶數量的時候,可以在多台機器上分布式的執行性能測試
缺點:沒有像jmeter可以設置並發數量的概念,locust只要啟動會一直不斷的發送請求。
代碼: #coding=utf-8 import requests from locust import HttpLocust,TaskSet,task,seq_task import os from locust import events from gevent._semaphore import Semaphore all_locusts_spawned = Semaphore() all_locusts_spawned.acquire() def on_hatch_complete(**kwargs): all_locusts_spawned.release() #創建鈎子方法 events.hatch_complete += on_hatch_complete #掛載到locust鈎子函數(所有的Locust實例產生完成時觸發) class Cms(TaskSet): # 訪問cms后台首頁 @seq_task(1) def cms_login(self): # 定義請求頭 header = { "User-Agent": "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.67 Safari/537.36" } req = self.client.get("/cms/manage/loginJump.do?userAccount=admin&loginPwd=123456", headers=header, verify=False) if req.status_code == 200: print("success") else: print("fails") @seq_task(2) def cms_queryUserList(self): header = { "User-Agent":"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.67 Safari/537.36" } queryUserList_data = { "startCreateDate": "", "endCreateDate": "", "searchValue": "", "page": "1" } req = self.client.post("/cms/manage/queryUserList.do",data=queryUserList_data) queryUserList_url = 'http://192.168.23.132:8080/cms/manage/queryUserList.do' if req.status_code == 200: #u'查詢用戶成功!') print("success") else: print("fails") def on_start(self): """ on_start is called when a Locust start before any task is scheduled """ # self.cms_login() # self.cms_queryUserList() all_locusts_spawned.wait() #限制在所有用戶准備完成前處於等待狀態 class websitUser(HttpLocust): task_set = Cms min_wait = 1000 # 單位為毫秒 max_wait = 1000 # 單位為毫秒 if __name__ == "__main__": import os os.system("locust -f lesson8.py --host=http://192.168.23.134:8080") ''' semaphore是一個內置的計數器: 每當調用acquire()時,內置計數器-1 每當調用release()時,內置計數器+1 計數器不能小於0,當計數器為0時,acquire()將阻塞線程直到其他線程調用release() '''
其他參考資料介紹:
https://blog.csdn.net/JOJOY_tester/article/details/77926470
https://blog.csdn.net/sg619262284/article/details/81074278
http://www.testclass.net/locust --locust詳細介紹
https://testerhome.com/topics/16766
https://testerhome.com/topics/19000 -- 自動化測試平台
https://www.missshi.cn/api/view/blog/59a6b53fe519f50d04000103 --locust性能測試工具深度剖析
https://testerhome.com/topics/17068 --wrk ab jmeter locust性能壓測工具對比
https://blog.debugtalk.com/categories/3-Testing/性能測試/ --tearshome社區