【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 - 博客园