Python + Jmeter 实现自动化性能压测

Step01: Python脚本开发

文件路径:D://wl//testproject//Fone-grpc//project1//test_client.py

Python 脚本作用:

1.通过 grpc 调用底层 c++ 的接口,做数据库的数据插入与查询操作,然后将返回的结果进行拼接与输出。

2.代码里面将每一次调用后返回的内容进行拼接后,并做了成功信息的统计,输出成功的次数为3,输出后会转给 jmeter 里面的脚本获取。

python 复制代码
from __future__ import print_function
import logging
import sys
import grpc
import Storage_pb2      # Storage_pb2.py which contains our generated request and response classes
import StorageService_pb2_grpc # StorageService_pb2_grpc.py which contains our generated client and server classes.

host="10.10.1.117:50066"

def run():
    result=""
    with grpc.insecure_channel(host) as channel:
        resp0=StorageService_pb2_grpc.FOneStorageStub(channel).InsertKVS(Storage_pb2.PUpsertKVS(
            DBName="MergeLog",TableName="MergeLog",KeyValues=[{"Key":b'',"Key":b''}],Upsert=True,Transaction=True
        ))
        result+=str(resp0)

    with grpc.insecure_channel(host) as channel:
        stud1=StorageService_pb2_grpc.FOneStorageStub(channel)
        resp1=stud1.InsertV(Storage_pb2.PInsertV(DBName="MergeLog", TableName="MergeLog", Value=b"1"))
        result += str(resp1)
     
    with grpc.insecure_channel(host) as channel:
        stud2=StorageService_pb2_grpc.FOneStorageStub(channel)
        resp2=stud2.FindOne(Storage_pb2.PFindK(DBName="MergeLog", TableName="MergeLog", Key="1"))
        result += str(resp2)

    result=result.replace("\n", " ")
    result=result.count("Successful return: 0")
    print(result)


if __name__ == '__main__':
    logging.basicConfig()
    run()

PS:在本地调试好脚本,确保符合预期。

Step02: Jmeter 创建 beanshell 取样器

脚本内容如下:

python 复制代码
import java.io.BufferedReader;
import java.io.InputStreamReader;

log.info("----------------------------start to exe");
String command = "cmd /c python D://wl//testproject//Fone-grpc//project1//test_client.py"; //定义要执行的python文件路径
String var;
Runtime rt = Runtime.getRuntime();
Process pr = rt.exec(command);  //执行上面的python脚本
pr.waitFor()
BufferedReader b = new BufferedReader(new InputStreamReader(pr.getInputStream())); // 转换执行脚本后的返回响应
String line = "";
StringBuilder response = new StringBuilder();
while ((line = b.readLine()) != null) {
     response.append(line);
} 
String response_data = response.toString();
log.info(response_data);
if(response_data.equals("3")){ // 判断响应的内容是否有3次,如果不是3次就表示执行失败了
 log.info("---------------------------success end exe");
}else{
 log.error("--------------------------failed end exe"); //这里的输出一次会记录一次失败次数
}
b.close();

Step03: 执行效果

通过 jmeter 工具进行并发操作,对后端底层接口进行压力测试,效果不错,效果图如下所示。

Step04: Jmeter日志输出

python 复制代码
2023-02-22 17:26:08,539 INFO o.a.j.t.JMeterThread: Stopping because end time detected by thread: 线程组--python 1-33
2023-02-22 17:26:08,539 INFO o.a.j.t.JMeterThread: Thread finished: 线程组--python 1-33
2023-02-22 17:26:08,597 INFO o.a.j.u.BeanShellTestElement: 3
2023-02-22 17:26:08,597 INFO o.a.j.u.BeanShellTestElement: ---------------------------success end exe
2023-02-22 17:26:08,598 INFO o.a.j.t.JMeterThread: Stopping because end time detected by thread: 线程组--python 1-82
2023-02-22 17:26:08,598 INFO o.a.j.t.JMeterThread: Thread finished: 线程组--python 1-82
2023-02-22 17:26:08,608 INFO o.a.j.u.BeanShellTestElement: 2
2023-02-22 17:26:08,608 ERROR o.a.j.u.BeanShellTestElement: --------------------------failed end exe

如果今天的分享对你有帮助的话,请毫不犹豫:关注、分享、点赞、在看、收藏呀~

最后感谢每一个认真阅读我文章的人,礼尚往来总是要有的,这些资料,对于【软件测试】的朋友来说应该是最全面最完整的备战仓库,虽然不是什么很值钱的东西,如果你用得到的话可以直接拿走:

这些资料,对于【软件测试】的朋友来说应该是最全面最完整的备战仓库,这个仓库也陪伴上万个测试工程师们走过最艰难的路程,希望也能帮助到你!

相关推荐
Jay Kay31 分钟前
TensorFlow源码深度阅读指南
人工智能·python·tensorflow
会的全对٩(ˊᗜˋ*)و44 分钟前
【数据挖掘】数据挖掘综合案例—银行精准营销
人工智能·经验分享·python·数据挖掘
___波子 Pro Max.1 小时前
GitHub Actions配置python flake8和black
python·black·flake8
阿蒙Amon2 小时前
【Python小工具】使用 OpenCV 获取视频时长的详细指南
python·opencv·音视频
橘子编程3 小时前
Python-Word文档、PPT、PDF以及Pillow处理图像详解
开发语言·python
蓝婷儿3 小时前
Python 机器学习核心入门与实战进阶 Day 2 - KNN(K-近邻算法)分类实战与调参
python·机器学习·近邻算法
之歆4 小时前
Python-封装和解构-set及操作-字典及操作-解析式生成器-内建函数迭代器-学习笔记
笔记·python·学习
天天爱吃肉82184 小时前
ZigBee通信技术全解析:从协议栈到底层实现,全方位解读物联网核心无线技术
python·嵌入式硬件·物联网·servlet
旧曲重听15 小时前
最快实现的前端灰度方案
前端·程序人生·状态模式
Allen_LVyingbo5 小时前
Python常用医疗AI库以及案例解析(2025年版、上)
开发语言·人工智能·python·学习·健康医疗