【Locust】实现grpc接口性能测试
一、locust
https://www.locust.io/
二、准备测试服务
1、下载测试服务
https://github.com/grpc/grpc.git
2、使用编辑器或者IDE打开
3、proto文件位置
4、创建测试代码
安装相关库
pip install locust grpcio grpcio-tools
python -m grpc_tools.protoc -I=/path/to/proto --python_out=/path/to/proto --grpc_python_out=/path/to/proto /path/to/proto/helloworld.proto
三、编写测试脚本
可以将脚本放入pb2文件同一目录,创建test.py
import sys import grpc import inspect import time import gevent from locust.contrib.fasthttp import FastHttpUser from locust import task, events, constant from locust.runners import STATE_STOPPING, STATE_STOPPED, STATE_CLEANUP, WorkerRunner import helloworld_pb2 import helloworld_pb2_grpc def stopwatch(func): def wrapper(*args, **kwargs): previous_frame = inspect.currentframe().f_back _, _, task_name, _, _ = inspect.getframeinfo(previous_frame) start = time.time() result = None try: result = func(*args, **kwargs) except Exception as e: total = int((time.time() - start) * 1000) events.request.fire(request_type="TYPE", name=task_name, response_time=total, response_length=0, exception=e) else: total = int((time.time() - start) * 1000) events.request.fire(request_type="TYPE", name=task_name, response_time=total, response_length=0) return result return wrapper class GRPCMyLocust(FastHttpUser): host = 'http://127.0.0.1:50051' wait_time = constant(0) def on_start(self): pass def on_stop(self): pass @task @stopwatch def grpc_client_task(self): try: with grpc.insecure_channel('127.0.0.1:50051') as channel: stub = helloworld_pb2_grpc.GreeterStub(channel) response = stub.SayHello(helloworld_pb2.HelloRequest(name='criss')) print(response) except (KeyboardInterrupt, SystemExit): sys.exit(0)
四、启动grpc服务端
五、启动测试脚本
六、执行测试
1、打开UI界面
2、检查服务端输出
3、检查UI界面显示
到此,我们简单完成了使用locust对grpc服务的测试
参考链接:
Locust完成gRPC协议的性能测试-腾讯云开发者社区-腾讯云
locust压测grpc locust压测sdk_mob6454cc64e36b的技术博客_51CTO博客
Locust对gRPC协议进行压测 - DaisyLinux - 博客园
分类:
Python-Basic
, Software Test
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
2021-07-22 【mac】./gradlew: command not found错误处理