最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Python實(shí)現(xiàn)多線程并發(fā)請求測試的腳本

 更新時間:2023年06月20日 09:46:20   作者:一夜奈何梁山  
這篇文章主要為大家分享了一個Python實(shí)現(xiàn)多線程并發(fā)請求測試的腳本,文中的示例代碼簡潔易懂,具有一定的借鑒價值,需要的小伙伴可以了解一下

一: 需求

今天接到一個需求, 要對線上環(huán)境進(jìn)行并發(fā)請求測試。 請求方式可以是兩種一種是發(fā)送HTTP請求, 一種是發(fā)送MESH請求。

測試達(dá)到的效果

1: 通過測試檢測網(wǎng)關(guān), 引擎的內(nèi)存, CPU消耗, 負(fù)載等。

2: 通過批量測試, 檢測引擎規(guī)則是否有異常。

3: 通過測試, 發(fā)現(xiàn)單請求最短耗時和最長耗時。

二:測試腳本

#!/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ā)測試"""
    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ù)"""
    # 讀取測試需要用的CSV文件內(nèi)容, test_num限制測試數(shù)據(jù)數(shù)量
    res_datas = read_csv(file_name=file_name, test_num=test_num)
    # 進(jìn)行并發(fā)請求測試
    out_put_datas = threading_test(input_datas=res_datas, pool_num=pool_num, req_type=req_type)
    # 測試結(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ā)請求測試的腳本的文章就介紹到這了,更多相關(guān)Python多線程并發(fā)測試內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論

务川| 攀枝花市| 夏河县| 池州市| 平遥县| 江门市| 桂东县| 潼南县| 平舆县| 惠东县| 西青区| 双牌县| 宁陕县| 祁阳县| 崇义县| 广州市| 金阳县| 普兰县| 延安市| 双江| 乌兰县| 洛浦县| 楚雄市| 长兴县| 乐陵市| 汝南县| 高碑店市| 哈巴河县| 栖霞市| 乌兰察布市| 浪卡子县| 北海市| 房产| 收藏| 永定县| 曲水县| 新安县| 马尔康县| 兴海县| 黑龙江省| 珠海市|