性能工具之 Locust 工具關聯與參數化

      網友投稿 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 工具關聯與參數化

      相對于關聯都是獲取響應結果來做入參,關聯理論請參考關聯和斷言:一動一靜,核心都是在取數據 這里面理論已經講解很清楚,現在看看 locust 怎么關聯數據。

      打開項目工程 關鍵內容如下:

      /** * 需要關聯的數據 * * @param id * @return */ @GetMapping("/associated/{id}") @ResponseBody public R associated(@PathVariable Integer id) { HashMap map = new HashMap<>(); if (StringUtils.isEmpty(id)) { return R.error(); } else if (id == 1) { HttpHeaders headers = new HttpHeaders(); //根據自己的需要動態添加你想要的content type headers.add(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE); map.put("des", "關聯數據"); map.put("rew", 6666); map.put("header", headers); return R.ok().put("data", map); } else { return R.error().put("data", map); } } /** * 需要關聯的數據 * * @param id * @return */ @GetMapping("/associated/data/{id}") @ResponseBody public R associatedData(@PathVariable Integer id) { HashMap map = new HashMap<>(); if (StringUtils.isEmpty(id)) { return R.error(); } else if (id == 6666) { map.put("des", "關聯數據成功"); map.put("rew", 8888); return R.ok().put("data", map); } else { return R.error().put("data", map); } }

      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: Response json: {'msg': 'success', 'code': 0, 'data': {'des': '關聯數據', 'rew': 6666, 'header': {'Content-Type': ['application/json']}}} Response json: Response json: {'msg': 'success', 'code': 0, 'data': {'des': '關聯數據成功', 'rew': 8888}} ..... 略 .... Response json: {'msg': 'success', 'code': 0, 'data': {'des': '關聯數據成功', 'rew': 8888}} Response json: Response json: {'msg': 'success', 'code': 0, 'data': {'des': '關聯數據', 'rew': 6666, 'header': {'Content-Type': ['application/json']}}} [2021-04-25 22:52:26,705] liwen.local/INFO/locust.main: Time limit reached. Stopping Locust. [2021-04-25 22:52:26,705] liwen.local/INFO/locust.runners: Stopping 1 users [2021-04-25 22:52:26,705] liwen.local/INFO/locust.runners: 1 Users have been stopped, 0 still running [2021-04-25 22:52:26,705] liwen.local/INFO/locust.main: Running teardowns... [2021-04-25 22:52:26,705] liwen.local/INFO/locust.main: Shutting down (exit code 0), bye. [2021-04-25 22:52:26,705] liwen.local/INFO/locust.main: Cleaning up runner... Name # reqs # fails | Avg Min Max Median | req/s failures/s -------------------------------------------------------------------------------------------------------------------------------------------- GET /associated/1 406 0(0.00%) | 0 0 5 1 | 468.35 0.00 GET /associated/data/6666 405 0(0.00%) | 1 0 7 1 | 467.20 0.00 -------------------------------------------------------------------------------------------------------------------------------------------- Aggregated 811 0(0.00%) | 0 0 7 1 | 935.55 0.00 Response time percentiles (approximated) Type Name 50% 66% 75% 80% 90% 95% 98% 99% 99.9% 99.99% 100% # reqs --------|------------------------------------------------------------|---------|------|------|------|------|------|------|------|------|------|------|------| GET /associated/1 1 1 1 1 1 1 1 1 5 5 5 406 GET /associated/data/6666 1 1 1 1 1 1 1 2 7 7 7 405 --------|------------------------------------------------------------|---------|------|------|------|------|------|------|------|------|------|------|------| None Aggregated

      總結

      Locust 的參數化與關聯需要自己寫相應代碼才能完成,但對性能工程師來說學習 Python 代碼是不可繞過去的事情。上面是簡單的參數化與關聯代碼希望對大家有幫助,

      Python 任務調度 壓力測試

      版權聲明:本文內容由網絡用戶投稿,版權歸原作者所有,本站不擁有其著作權,亦不承擔相應法律責任。如果您發現本站中有涉嫌抄襲或描述失實的內容,請聯系我們jiasou666@gmail.com 處理,核實后本網站將在24小時內刪除侵權內容。

      版權聲明:本文內容由網絡用戶投稿,版權歸原作者所有,本站不擁有其著作權,亦不承擔相應法律責任。如果您發現本站中有涉嫌抄襲或描述失實的內容,請聯系我們jiasou666@gmail.com 處理,核實后本網站將在24小時內刪除侵權內容。

      上一篇:表格填好怎么保存(表格填完怎么保存)
      下一篇:Word2010如何編輯表格邊框(怎么設置word表格邊框)
      相關文章
      亚洲日本人成中文字幕| 精品亚洲456在线播放| 日本亚洲欧美色视频在线播放| 亚洲国产精品久久丫| 亚洲精品第一国产综合精品| 亚洲综合久久综合激情久久| 亚洲成人在线网站| 亚洲成a人片77777老司机| 国产成A人亚洲精V品无码| 亚洲日韩精品一区二区三区| 亚洲欧洲无码AV电影在线观看| 国产成人精品日本亚洲专区61| 在线亚洲97se亚洲综合在线| 自拍偷自拍亚洲精品第1页| 亚洲一区二区三区偷拍女厕| 久久夜色精品国产亚洲| 久久精品国产亚洲AV麻豆王友容| 久久夜色精品国产嚕嚕亚洲av| 久久亚洲国产视频| 久久亚洲国产成人精品性色| 亚洲综合激情另类小说区| 亚洲免费电影网站| 在线综合亚洲中文精品| 亚洲色大18成人网站WWW在线播放| 亚洲国产美女精品久久久| 豆国产96在线|亚洲| 亚洲国产精品无码久久久久久曰| 国产精品亚洲二区在线观看| 亚洲日本一区二区三区在线| 亚洲国产香蕉碰碰人人| 亚洲国产精品成人综合色在线婷婷 | 亚洲系列国产精品制服丝袜第| 亚洲电影唐人社一区二区| 亚洲国产精品一区二区久| 学生妹亚洲一区二区| 亚洲av日韩aⅴ无码色老头| 亚洲国产综合久久天堂| 亚洲人成人77777网站| 色噜噜综合亚洲av中文无码| 亚洲乱码一二三四区麻豆| 亚洲乱色伦图片区小说|