首页 > 编程语言 >用python写一个并发测试工具

用python写一个并发测试工具

时间:2024-02-08 10:22:27浏览次数:21  
标签:body 并发 python label url json 测试工具 type headers

工作中会有一些需要并发测试的场景,例如:两人同时操作一条数据,此时需要验证结果是否符合预期

 

最初是借助jmeter来进行并发测试,建2个线程组,每个线程组中各放一个接口,启动时会同时执行个线程组中的接口,从而实现并发测试的目的

但是每次都要打开jmeter,用起来不太方便,所以就尝试用python来实现接口并发测试,然后再写个前端页面

所以本次实现以下功能:

1、后端在django框架下,基于python实现接口并发测试

2、前端页面基于百度amis低代码平台实现

一、后端逻辑实现

用python自带的库concurrent.futures,它提供ThreadPoolExecutor,使之能够并发执行函数(来自ChatGPT)

ChatGPT给出的示例代码如下

上述代码中,make_request()函数是需要并发执行的函数,ThreadPoolExecutor设置并发请求,示例中传入的参数urls是一个list

根据自己的实际需求改造一下示例代码:

首先我的目标是对2个接口请求进行并发测试,所以会从前端传入2个接口的请求url、headers、请求参数,把前端参数组合成list,大致如下:列表包裹字典,一个字典是一组参数,标记好url、headers、payload

    param_data = [
        {"url": url_1, "headers": headers_1, "payload": payload_1},
        {"url": url_2, "headers": headers_2, "payload": payload_2}
    ]

新建一个concurrency_test.py文件

make_request()函数改为如下形式,它接收的参数data就是一组字典参数

from concurrent.futures import ThreadPoolExecutor
import requests


def make_request(data):
    r = requests.post(url=data["url"], headers=data["headers"], json=data["payload"], verify=False)
    # print(r.json())
    return r.json()

然后再定义一个主函数

def multi_processing(data):
    with ThreadPoolExecutor(max_workers=2) as executor:
        results = list(executor.map(make_request, data))
    # print(results)
    res = []
    for i, result in zip(data, results):
        res.append(
            result["message"] + ";traceId=" + result["traceId"]
        )
    print(res)

    return res

max_workers控制并发数量(线程数),map()中接收并发函数以及它所需要的参数

 

再新建一个视图函数concurrency_test_views.py

from django.http import JsonResponse,HttpResponse
import json
from app.api.concurrency_test import *
from django.views.decorators.http import require_http_methods


def concurrent(request):
    url_1 = request.GET.get("url_1")

    headers_1 = json.loads(request.GET.get("header_1"))
    payload_1 = json.loads(request.GET.get("body_1"))

    url_2 = request.GET.get("url_2")
    headers_2 = json.loads(request.GET.get("header_2"))
    payload_2 = json.loads(request.GET.get("body_2"))

    param_data = [
        {"url": url_1, "headers": headers_1, "payload": payload_1},
        {"url": url_2, "headers": headers_2, "payload": payload_2}
    ]

    res = multi_processing(param_data)
    dict_res = {
        "res1": res[0],
        "res2": res[1]
    }
    return JsonResponse(dict_res, json_dumps_params={'ensure_ascii': False})
    # return HttpResponse(res)

最后配置好路由,这个接口就写好了

----------------------------------------------

接下来开始写前端,前端布局大致如下

这个页面是直接用amis写的,这部分是用的现成的组件,不再细说,贴下代码

前端amis代码
<template>
    <div ref="box" style="text-align: center; font-size: 24px; font-weight: 700; margin-bottom: 20px">
      基本信息
    </div>
  </template>
  <script>
  export default {
    mounted() {
      const amis = amisRequire('amis/embed')
      let url = this.COMMON.test_url
      let amisJSON = {
        "type": "page",
        "title": "工具类",
        "remark": "",

        "body": [
            {
                "type": "tabs",
                "tabs": [
                    {
                        "title": "接口并发测试",
                        "hash": "tab1",
                        "body": [
                            
                            {
                                "type": "grid",
                                "columns": [

                                    {
                                        // "type": "wrapper",
                                        // "style": {
                                        //     "width": "600px"
                                        // },

                                        "body": [
                                            // {
                                            //     "type": "wrapper",
                                            //     // "title": "面板标题",
                                            //     "body": "面板内容"
                                            // },
                                            
                                            {
                                            "type": "form",
                                            "style": {
                                            
                                                },
                                            "title": "并发测试工具",
                                            "mode": "horizontal",
                                            "autoFocus": false,
                                            "horizontal": {
                                                "leftFixed": "md"
                                            },


                                            "body": [

                                                {
                                                    "type": "group",
                                                    "label": "Request URL",
                                                    "body": [
                                                        {
                                                            "type": "input-text",
                                                            "size": "",  //控制输入框大小
                                                            "placeholder": "请输入第一个接口url",
                                                            "clearable": true,  //设置可清除输入内容
                                                            "required": true,
                                                            // "showCounter": true,
                                                            // "maxLength": 11,  //限制最大字数
                                                            "name": "url_1",
                                                            "value": "",
                                                            "label": false
                                                        },
                                                        {
                                                            "type": "input-text",
                                                            "size": "",  //控制输入框大小
                                                            "placeholder": "请输入第二个接口url",
                                                            "clearable": true,  //设置可清除输入内容
                                                            "required": true,
                                                            // "showCounter": true,
                                                            // "maxLength": 11,  //限制最大字数
                                                            "name": "url_2",
                                                            "value": "",
                                                            "label": false
                                                        }
                                                    ]
                                                },
                                                {
                                                    "type": "group",
                                                    "label": "Headers",
                                                    "body": [
                                                        {
                                                            "type": "textarea",
                                                            "size": "",
                                                            "placeholder": "请输入第一个接口的headers",
                                                            "required": true,
                                                            "clearable": true, 
                                                            "name": "header_1",
                                                            "value": "",
                                                            "label": false
                                                        },
                                                        {
                                                            "type": "textarea",
                                                            "size": "",
                                                            "placeholder": "请输入第二个接口的headers",
                                                            "required": true,
                                                            "clearable": true, 
                                                            "name": "header_2",
                                                            "value": "",
                                                            "label": false
                                                        }
                                                    ]
                                                },
                                                {
                                                    "type": "group",
                                                    "label": "Payload",
                                                    "body": [
                                                        {
                                                            "type": "textarea",
                                                            "size": "",
                                                            "placeholder": "请输入第一个接口的请求参数",
                                                            "required": true,
                                                            "clearable": true, 
                                                            "name": "body_1",
                                                            "value": "",
                                                            "label": false
                                                        },
                                                        {
                                                            "type": "textarea",
                                                            "size": "",
                                                            "placeholder": "请输入第二个接口的请求参数",
                                                            "required": true,
                                                            "clearable": true, 
                                                            "name": "body_2",
                                                            "value": "",
                                                            "label": false
                                                        }
                                                    ]
                                                },

                                                // json展示返回结果
                                                {
                                                    "type": "control",
                                                    "css": {
                                                    ".myClass": {
                                                        "color": "black",
                                                        "font-size": "15px",
                                                        "font-weight": "normal",
                                                        "text-align": "left",
                                                        // "display": "inline-block"
                                                        }
                                                    },
                                                    "name": "res",
                                                    "label": "接口返回结果",
                                                    "body": {
                                                        "type": "json",
                                                        "levelExpand": 100,
                                                        "className": "myClass"
                                                    }
                                                }
                                                // {
                                                //     "name": "res",
                                                //     "type": "static",
                                                //     "label": "返回结果",
                                                //     "value": ""
                                                // }
                                            ],
                                            "actions": [
                                                {
                                                    "//": "type为submit时, 表示该按钮是一个行为按钮, 点击可以提交请求",
                                                    "type": "submit",

                                                    "label": "提交",

                                                    "//": "level配置按钮颜色, https://aisuda.bce.baidu.com/amis/zh-CN/components/action?page=1#%E4%B8%BB%E9%A2%98",
                                                    "level": "primary",

                                                    "api": {
                                                        "method": "get",
                                                        "url": url+"data_factory/concurrent",
                                                        "data": {
                                                            "url_1": "${url_1}",
                                                            "header_1": "${header_1}",
                                                            "body_1": "${body_1}",
                                                            "url_2": "${url_2}",
                                                            "header_2": "${header_2}",
                                                            "body_2": "${body_2}",
                                                        },
                                                        "adaptor": "return {\n data: {\n res: response.data\n}\n}"
                                                    }
                                                },

                                                {
                                                    "type": "reset",
                                                    "label": "重置"
                                                }
                                            ]
                                        }
                                        ]
                                    }
                                ]
                            },
                            
                            {
                                "type": "grid",
                                "columns": [
                                {
                                            "type": "page",
                                            "title": "使用说明",
                                            "css": {
                                                ".myClass": {
                                                "color": "black",
                                                "font-size": "15px",
                                                "font-weight": "normal",
                                                "text-align": "left",
                                                "display": "inline-block"
                                                }
                                            },
                                            "body": [{
                                                "type": "tpl",
                                                "tpl": "<ul><li>支持对接口进行并发测试</li><li>输入2个接口的请求url、headers、入参</li><li>【请求头】和【请求参数】均为json格式,示例如下</li></ul>",
                                                "className": "myClass"
                                            },
                                            {
                                                    "type": "group",
                                                    "label": "Headers + Payload",
                                                    "body": [
                                                        {
                                                            "type": "textarea",
                                                            "value": '{\n    "Content-Type":"application/json",\n    "Cookie":"",\n    "":"",\n    "param": ""\n}',
                                                            "label": false
                                                        },
                                                        {
                                                            "type": "textarea",
                                                            "value": '{\n    "Content-Type":"application/json;",\n    "Cookie":"",\n    "param":"",\n    "param": ""\n}',
                                                            "label": false
                                                        },
                                                        {
                                                            "type": "textarea",
                                                            "value": '{\n    "Content-Type":"application/json; charset=UTF-8",\n    "Token":"",\n    "param":"",\n}\n',
                                                            "label": false
                                                        },
                                                        {
                                                            "type": "textarea",
                                                            "value": '{\n    "param":"",\n    "param":""\n}\n \n',
                                                            "label": false
                                                        }
                                                    ]
                                                },
                                        ]
                                        }
                                ]
                            }
                        ]
                    },

                    {
                        "title": "其他",
                        "hash": "tab5",
                        "body": 
                        "开发中"
                    }
                ]
            }

        ]
    }
      const amisScoped = amis.embed(this.$refs.box, amisJSON)
    }
  }
  </script>

 

标签:body,并发,python,label,url,json,测试工具,type,headers
From: https://www.cnblogs.com/hanmk/p/17998896

相关文章

  • Python监控服务器
    Python代码如下: importpsutilimporttimeimportpymysqldb=pymysql.connect(user="root",passwd="root",db="test",host="127.0.0.1")db.autocommit(True)cur=db.cursor()defgetinfo():mem=psutil.virtua......
  • Ubuntu源码安装Python
    Ubuntu源码安装Python目前Ubuntu电脑需要升级Python,但根据源来升级,会出现报错,故记录用源码升级的方法。从官方链接下载源码:https://www.python.org/downloads/source/根据自己需求下载对应版本即可。这里笔者下载的是Python-3.10.0rc2.tgz解压:tar-zxvfPython-3.10.0rc......
  • Python实现软件设计模式9:组合模式 Composite Pattern
    动机如何将容器和叶子进行递归组合,使得用户在使用时无须对它们进行区分,可以一致地对待容器和叶子?典型案例如:文件系统,在树形目录结构中,包含文件和文件夹两类不同的元素;在文件夹中可以继续包含文件或子文件夹,在文件中不能再包含子文件或者子文件夹。概念组合多个对象形成树形......
  • C++多线程 第四章 同步并发操作
    第四章同步并发操作等待事件设想一个情景:你正坐在一辆从哈尔滨驶向郴州的绿皮火车上,这趟车需要耗时2天2夜,合计3000公里的路程.于是在这里,我们将你和司机视作为两个线程.你的任务是在目的地下车,司机的任务是将车开到目的地.假设你和司机坐在同一个车厢内,并且你是个不说......
  • jacoco覆盖率测试工具
    简介jacoco是一个能跑覆盖率的工具,可以把覆盖率结果生成报告,和IDEA自带的覆盖率测试工具类似,eclipse是没有自带覆盖率测试功能的,jacoco可以在maven执行test周期的时候生成数据,可以作为eclipse覆盖率测试工具,jacoco生成的报告可以和sonaqube,jenkin,gitlab等工具联动,实现代码门禁的......
  • 扒开源安卓性能测试工具moblieperf源码——开发属于你自己的性能稳定性测试工具
    moblieperf下载和使用moblieperf由阿里巴巴开源的Android性能测试工具下载:官方源码地址mobileperfgithub使用:使用pycharm打开下载的项目使用只需要修改配置文件config.conf即可运行采集:a.mac、linux在mobileperf工具根目录下执行shrun.sh;b.windows双击run.bat配置......
  • 【视频】小甲鱼零基础入门学习Python(全96集)
    视频下载地址:https://pan.quark.cn/s/c17e3da33a76目录1.第一讲:我和Python的第一次亲密接触2.第二讲:用Python设计第一个游戏3.第三讲:小插曲之变量和字符串4.第四讲:改进我们的小游戏5.第五讲:Python的数据类型6.第六讲:常用的操作符7.第七-九讲:了不起的分支和循环8.第十讲:一个......
  • [python3]: python --【class】类变量(类属性)
    [python3]: python --【class】类变量(类属性)    一、说明: 1、类变量:类变量,定义在【类内】且【函数外】。1classobject:23#class_variable4icount=0567def__init__(self):8#usingclass_vari......
  • Python 获取相对路径
    想要获取当前文件的路径,通常我的做法是os.path.abspath(__file__)如果想要获取当前文件的所在文件夹,通常的做法是os.path.dirname(__file__)但是更多的时候,我想获取当前所在文件的父目录的父目录,做法可以是os.path.dirname(os.path.diranme(__file__))或path=os.path......
  • 02 在vscode中使用python
    安装插件需要先安装python这个插件安装完成后,创建一个文件夹,用于工程的创建。使用vscode打开这个文件夹,之后新建一个.py文件。编写第一个程序:print("hello")a=3b=4print(a+b)box="gogogo"name="lili"print("byby"+name)配置相关信息选择这个:......