欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

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

一: 需求

今天接到一個(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)文章

最新評(píng)論