京寵展信息指南
709
2025-04-04
前言
不同的壓力工具在參數化的實現邏輯上也會不同,但是參數化必須依賴業務邏輯,而不是工具中能做到什么功能。所以在參數化之前,我們必須分析真實業務邏輯中如何使用數據,再在工具中選擇相對應的組合參數的方式去實現。
參數化
在 locust 工具中有怎么使用參數化完成工作,在開展工作開始前,先了解 Python 中的一個 Queue 類,queue它是一個隊列數據結構是先進先出的數據結構,具體原理大家自己查詢即可。
Queue種類:
FIFO: Queue.Queue(maxsize=0) FIFO即First in First Out,先進先出。Queue提供了一個基本的FIFO容器,使用方法很簡單,maxsize是個整數,指明了隊列中能存放的數據個數的上限。一旦達到上限,插入會導致阻塞,直到隊列中的數據被消費掉。如果maxsize小于或者等于0,隊列大小沒有限制。 LIFO Queue.LifoQueue(maxsize=0) LIFO即Last in First Out,后進先出。與棧的類似,使用也很簡單,maxsize用法同上。 priority class Queue.PriorityQueue(maxsize=0) 構造一個優先隊列。maxsize用法同上。
基本方法:
Queue.Queue(maxsize=0) FIFO, 如果maxsize小于1就表示隊列長度無限
Queue.LifoQueue(maxsize=0) LIFO, 如果maxsize小于1就表示隊列長度無限
Queue.qsize() 返回隊列的大小
Queue.empty() 如果隊列為空,返回True,反之False
Queue.full() 如果隊列滿了,返回True,反之False
Queue.get([block[, timeout]]) 讀隊列,timeout等待時間
Queue.put(item, [block[, timeout]]) 寫隊列,timeout等待時間
Queue.queue.clear() 清空隊列
隊列 queue 多應用在多線程應用中,多線程訪問共享變量。對于多線程而言,訪問共享變量時,隊列queue是線程安全的。
有以上簡單基礎知識后,直接上 locust 腳本怎么編寫參數化,直接仿照即可寫出參數腳本,下面是簡單一個參數化代碼。
import csv import os, requests import queue from locust import TaskSet, task, HttpUser from requests.packages.urllib3.exceptions import InsecureRequestWarning # 禁用安全請求警告 requests.packages.urllib3.disable_warnings(InsecureRequestWarning) def fnReadData(): f = open("uuid.text", "r") #讀取參數文件 data = [] #聲明空列表 data = csv.reader(f) #通過 csv讀取文件內容 s = queue.Queue() #實例化一個queue對象 for each in data: #循環讀取open里面的數據 for key in each: try: s.put_nowait(key) #put到隊列中 except queue.Full: print("Queue overflow") f.close() return s class MyBlogs(TaskSet): # 訪問我的博客首頁 @task(1) def get_blog(self): # 定義請求頭 header = { "User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36"} data = self.user.queueData.get() req = self.client.get("/357712148/%s" % data, headers=header, verify=False) if req.status_code == 200: print("success") else: print("fails") class httpGet(HttpUser): tasks = [MyBlogs] min_wait = 3000 # 單位為毫秒 max_wait = 6000 # 單位為毫秒 queueData = fnReadData() # 隊列實例化 if __name__ == "__main__": #通過好 os.system("locust -f lcome.py --host=https://blog.com --headless -u 1 -r 1 -t 1s")
參數化文件:
運行結果:
[2021-04-25 13:39:51,536] liwen.local/INFO/locust.main: Run time limit set to 1 seconds [2021-04-25 13:39:51,536] liwen.local/INFO/locust.main: Starting Locust 1.4.4 [2021-04-25 13:39:51,536] liwen.local/INFO/locust.runners: Spawning 1 users at the rate 1 users/s (0 users already running)... [2021-04-25 13:39:51,536] liwen.local/INFO/locust.runners: All users spawned: httpGet: 1 (1 total running) [2021-04-25 13:39:51,537] liwen.local/INFO/root: Terminal was not a tty. Keyboard input disabled Name # reqs # fails | Avg Min Max Median | req/s failures/s -------------------------------------------------------------------------------------------------------------------------------------------- -------------------------------------------------------------------------------------------------------------------------------------------- Aggregated 0 0(0.00%) | 0 0 0 0 | 0.00 0.00 success success success [2021-04-25 13:39:52,536] liwen.local/INFO/locust.main: Time limit reached. Stopping Locust. [2021-04-25 13:39:52,537] liwen.local/INFO/locust.runners: Stopping 1 users [2021-04-25 13:39:52,537] liwen.local/INFO/locust.runners: 1 Users have been stopped, 0 still running [2021-04-25 13:39:52,537] liwen.local/INFO/locust.main: Running teardowns... [2021-04-25 13:39:52,537] liwen.local/INFO/locust.main: Shutting down (exit code 0), bye. [2021-04-25 13:39:52,537] liwen.local/INFO/locust.main: Cleaning up runner... Name # reqs # fails | Avg Min Max Median | req/s failures/s -------------------------------------------------------------------------------------------------------------------------------------------- GET /357712148/2525651 1 0(0.00%) | 238 238 238 238 | 1.30 0.00 GET /357712148/2532241 1 0(0.00%) | 210 210 210 210 | 1.30 0.00 GET /357712148/2562906 1 0(0.00%) | 320 320 320 320 | 1.30 0.00 -------------------------------------------------------------------------------------------------------------------------------------------- Aggregated 3 0(0.00%) | 256 210 320 240 | 3.89 0.00 Response time percentiles (approximated) Type Name 50% 66% 75% 80% 90% 95% 98% 99% 99.9% 99.99% 100% # reqs --------|------------------------------------------------------------|---------|------|------|------|------|------|------|------|------|------|------|------| GET /357712148/2525651 240 240 240 240 240 240 240 240 240 240 240 1 GET /357712148/2532241 210 210 210 210 210 210 210 210 210 210 210 1 GET /357712148/2562906 320 320 320 320 320 320 320 320 320 320 320 1 --------|------------------------------------------------------------|---------|------|------|------|------|------|------|------|------|------|------|------| None Aggregated 240 240 320 320 320 320 320 320 320 320 320 3
關聯
相對于關聯都是獲取響應結果來做入參,關聯理論請參考關聯和斷言:一動一靜,核心都是在取數據 這里面理論已經講解很清楚,現在看看 locust 怎么關聯數據。
打開項目工程 關鍵內容如下:
/** * 需要關聯的數據 * * @param id * @return */ @GetMapping("/associated/{id}") @ResponseBody public R associated(@PathVariable Integer id) { HashMap
locust 腳本參考:
def get_param(self): '''獲取參數''' response = self.client.get("/associated/1") print("Response json:", type(response.json())) # 判斷類型 print("Response json:", response.json()) res = response.json() # 轉換字典 return res['data']['rew'] # 獲取參數 @task(1) def request_param(self): '''關聯參數請求''' id = self.get_param() response = self.client.get("/associated/data/%s" % id) print("Response json:", type(response.json())) # 判斷類型 print("Response json:", response.json())
結果:
[2021-04-25 22:52:25,837] liwen.local/INFO/locust.main: Run time limit set to 1 seconds [2021-04-25 22:52:25,837] liwen.local/INFO/locust.main: Starting Locust 1.4.4 [2021-04-25 22:52:25,837] liwen.local/INFO/locust.runners: Spawning 1 users at the rate 1 users/s (0 users already running)... [2021-04-25 22:52:25,838] liwen.local/INFO/locust.runners: All users spawned: webTestDunShan: 1 (1 total running) [2021-04-25 22:52:25,838] liwen.local/INFO/root: Terminal was not a tty. Keyboard input disabled Name # reqs # fails | Avg Min Max Median | req/s failures/s -------------------------------------------------------------------------------------------------------------------------------------------- -------------------------------------------------------------------------------------------------------------------------------------------- Aggregated 0 0(0.00%) | 0 0 0 0 | 0.00 0.00 Response json:
總結
Locust 的參數化與關聯需要自己寫相應代碼才能完成,但對性能工程師來說學習 Python 代碼是不可繞過去的事情。上面是簡單的參數化與關聯代碼希望對大家有幫助,
Python 任務調度 壓力測試
版權聲明:本文內容由網絡用戶投稿,版權歸原作者所有,本站不擁有其著作權,亦不承擔相應法律責任。如果您發現本站中有涉嫌抄襲或描述失實的內容,請聯系我們jiasou666@gmail.com 處理,核實后本網站將在24小時內刪除侵權內容。
版權聲明:本文內容由網絡用戶投稿,版權歸原作者所有,本站不擁有其著作權,亦不承擔相應法律責任。如果您發現本站中有涉嫌抄襲或描述失實的內容,請聯系我們jiasou666@gmail.com 處理,核實后本網站將在24小時內刪除侵權內容。