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合并Excel表(多sheet)的實(shí)現(xiàn)
這篇文章主要介紹了Python合并Excel表(多sheet)的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-04-04
使用Python對(duì)EXCEL數(shù)據(jù)的預(yù)處理
這篇文章主要介紹了使用Python處理EXCEL基礎(chǔ)操作篇2,如何使用Python對(duì)EXCEL數(shù)據(jù)的預(yù)處理,文中提供了解決思路和部分實(shí)現(xiàn)代碼,一起來看看吧2023-03-03
利用Python裁切tiff圖像且讀取tiff,shp文件的實(shí)例
這篇文章主要介紹了利用Python裁切tiff圖像且讀取tiff,shp文件的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-03-03
python實(shí)現(xiàn)數(shù)據(jù)清洗(缺失值與異常值處理)
今天小編就為大家分享一篇python實(shí)現(xiàn)數(shù)據(jù)清洗(缺失值與異常值處理),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-12-12
Python基于list的append和pop方法實(shí)現(xiàn)堆棧與隊(duì)列功能示例
這篇文章主要介紹了Python基于list的append和pop方法實(shí)現(xiàn)堆棧與隊(duì)列功能,結(jié)合實(shí)例形式分析了Python使用list定義及使用隊(duì)列的相關(guān)操作技巧,需要的朋友可以參考下2017-07-07
基于Python Playwright進(jìn)行前端性能測(cè)試的腳本實(shí)現(xiàn)
在當(dāng)今Web應(yīng)用開發(fā)中,性能優(yōu)化是提升用戶體驗(yàn)的關(guān)鍵因素之一,本文將介紹如何使用Playwright構(gòu)建一個(gè)自動(dòng)化性能測(cè)試工具,希望對(duì)大家有所幫助2025-08-08
Python中PyQt5/PySide2的按鈕控件使用實(shí)例
這篇文章主要介紹了PyQt5/PySide2的按鈕控件使用實(shí)例,代碼簡(jiǎn)單易懂,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-08-08
Python深度學(xué)習(xí)pytorch神經(jīng)網(wǎng)絡(luò)Dropout應(yīng)用詳解解
這篇文章主要為大家介紹了Python深度學(xué)習(xí)中關(guān)于pytorch神經(jīng)網(wǎng)絡(luò)Dropout的應(yīng)用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步2021-10-10
Python文件操作指南解鎖三個(gè)txt文件合并技術(shù)
本文將深入介紹如何利用Python編寫腳本,將三個(gè)文本文件中指定的列數(shù)據(jù)合并成一個(gè)新文件,通過豐富的示例代碼和詳細(xì)解釋,幫助掌握這一實(shí)用而靈活的數(shù)據(jù)處理技巧2024-01-01

