Python實(shí)現(xiàn)多線程并發(fā)請(qǐng)求測(cè)試的腳本
一: 需求
今天接到一個(gè)需求, 要對(duì)線上環(huán)境進(jìn)行并發(fā)請(qǐng)求測(cè)試。 請(qǐng)求方式可以是兩種一種是發(fā)送HTTP請(qǐng)求, 一種是發(fā)送MESH請(qǐng)求。
測(cè)試達(dá)到的效果
1: 通過測(cè)試檢測(cè)網(wǎng)關(guān), 引擎的內(nèi)存, CPU消耗, 負(fù)載等。
2: 通過批量測(cè)試, 檢測(cè)引擎規(guī)則是否有異常。
3: 通過測(cè)試, 發(fā)現(xiàn)單請(qǐng)求最短耗時(shí)和最長(zhǎng)耗時(shí)。
二:測(cè)試腳本
#!/usr/bin/env python # -*- coding: utf-8 -*- # @Time : 2022/12/5 10:57 AM import json import time import requests import click import pandas as pd from concurrent.futures import ThreadPoolExecutor from xylib.lib.http import mesh_http_path mesh_appid = "XXXXXXX" mesh_request_path = "XXXXXXXX" http_request_url = "http://127.0.0.1:8888/xxxxx/xxxx" def read_csv(file_name, test_num): """讀取CSV文件""" res_datas = [] df = pd.read_csv(file_name) index, column = df.shape for idx in range(0, index): params = df.loc[idx].to_dict() res_datas.append(params) return res_datas[:test_num] def send_request(input_msg, request_type): request_data = { "xxxxx": 2, "xxxxxx": "oooooooo", "xxxxxxx": { "xxxxxxxx": str(input_msg.get("aaaa", "0000")), "xxxxxx": int(input_msg.get("bbbbb", 50)), "xxxxx": int(input_msg.get("ccccc", 48)), "xxxxxxxxxx": str(input_msg.get("dddddd", "")), } } if request_type == "http": res = requests.post(http_request_url, data=json.dumps(request_data)) else: res = mesh_http_path( mesh_appid, mesh_appid, mesh_request_path, 'POST', data=json.dumps(request_data) ) result = dict() try: result = json.loads(res) except Exception as e: print("error is {}".format(e.message)) input_msg["xdxaxaxa"] = result.get("xaxx", {}).get("xaxsaxs", {}).get("xaxaxsx", "") input_msg["xaxaxs"] = result.get("xaxsxs").get("xaxsaxs", {}).get("xsaxsaxsax", "") return input_msg def threading_test(input_datas, pool_num, req_type): """多線程并發(fā)測(cè)試""" out_put_datas = [] futures = [] start_time = time.time() try: with ThreadPoolExecutor(max_workers=pool_num) as executor: for input_data in input_datas: futures.append(executor.submit(send_request, (input_data, req_type))) for future in futures: out_put_datas.append(future.result()) except Exception as e: print("error is {}".format(e.message)) finally: end_time = time.time() print("cost time is %s" % str(end_time - start_time)) return out_put_datas def write_to_csv(out_put_datas, out_file_name): """寫入到csv文件中""" rdf = pd.DataFrame(out_put_datas) rdf.to_csv(out_file_name) @click.command() @click.option('--req_type', default="http", help='You need input http or mesh') @click.option('--pool_num', default=80, help='You need input a num') @click.option('--test_num', default=1000000, help='You need input a num') @click.option('--file_name', default="hy.csv", help='You need input a file name') @click.option('--out_file_name', default="result.csv", help='You need input a file name') def run(req_type, pool_num, test_num, file_name, out_file_name): """主運(yùn)行函數(shù)""" # 讀取測(cè)試需要用的CSV文件內(nèi)容, test_num限制測(cè)試數(shù)據(jù)數(shù)量 res_datas = read_csv(file_name=file_name, test_num=test_num) # 進(jìn)行并發(fā)請(qǐng)求測(cè)試 out_put_datas = threading_test(input_datas=res_datas, pool_num=pool_num, req_type=req_type) # 測(cè)試結(jié)果寫入到CSV文件中 write_to_csv(out_put_datas=out_put_datas, out_file_name=out_file_name) if __name__ == '__main__': run()
到此這篇關(guān)于Python實(shí)現(xiàn)多線程并發(fā)請(qǐng)求測(cè)試的腳本的文章就介紹到這了,更多相關(guān)Python多線程并發(fā)測(cè)試內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
python利用paramiko實(shí)現(xiàn)交換機(jī)巡檢的示例
這篇文章主要介紹了python利用paramiko實(shí)現(xiàn)交換機(jī)巡檢,幫助大家更好的理解和使用python,感興趣的朋友可以了解下2020-09-09詳解python如何提取瀏覽器中保存的網(wǎng)站登錄用戶名密碼
很多瀏覽器都貼心地提供了保存用戶密碼功能,用戶一旦開啟,就不需要每次都輸入用戶名、密碼,非常方便,作為python腳本,能否拿到用戶提前保存在瀏覽器中的用戶名密碼,用以自動(dòng)登錄呢,下面我們就來看看吧2023-08-08selenium2.0中常用的python函數(shù)匯總
這篇文章主要介紹了selenium2.0中常用的python函數(shù),總結(jié)分析了selenium2.0中常用的python函數(shù)的功能、原理與基本用法,需要的朋友可以參考下2019-08-08利用Opencv中Houghline方法實(shí)現(xiàn)直線檢測(cè)
這篇文章主要為大家詳細(xì)介紹了利用Opencv中的Houghline方法進(jìn)行直線檢測(cè),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-02-02使用BeeWare實(shí)現(xiàn)iOS調(diào)用Python方式
這篇文章主要介紹了使用BeeWare實(shí)現(xiàn)iOS調(diào)用Python方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-12-12flask中主動(dòng)拋出異常及統(tǒng)一異常處理代碼示例
這篇文章主要介紹了flask中主動(dòng)拋出異常及統(tǒng)一異常處理代碼示例,具有一定借鑒價(jià)值,需要的朋友可以參考下2018-01-01Python正則表達(dá)式實(shí)現(xiàn)截取成對(duì)括號(hào)的方法
這篇文章主要介紹了Python正則表達(dá)式實(shí)現(xiàn)截取成對(duì)括號(hào)的方法,涉及Python正則匹配相關(guān)操作技巧,需要的朋友可以參考下2017-01-01