Python3中使用locust 2.15.1压测grpc接口
[本文出自天外归云的博客园]
首先安装grpc、日志、locust相关依赖库:
google protobuf grpcio grpcio-tools grpc_interceptor loguru locust==2.15.1
然后创建一个grpc_user.py文件,内容如下:
import time from typing import Any, Callable import grpc import grpc.experimental.gevent as grpc_gevent from grpc_interceptor import ClientInterceptor from locust import User from locust.exception import LocustError from loguru import logger # patch grpc so that it uses gevent instead of asyncio grpc_gevent.init_gevent() class LocustInterceptor(ClientInterceptor): def __init__(self, environment, *args, **kwargs): super().__init__(*args, **kwargs) self.env = environment def intercept( self, method: Callable, request_or_iterator: Any, call_details: grpc.ClientCallDetails, ): response = None exception = None start_perf_counter = time.perf_counter() response_length = 0 try: response = method(request_or_iterator, call_details) response_result = response.result() if response_result.code != 0: exception = Exception("code is not 0") else: # logger.debug(response_result) logger.debug("success") response_length = response.result().ByteSize() except grpc.RpcError as e: exception = e self.env.events.request.fire( request_type="grpc", name=call_details.method, response_time=(time.perf_counter() - start_perf_counter) * 1000, response_length=response_length, response=response, context=None, exception=exception, ) return response class GrpcUser(User): abstract = True stub_class = None def __init__(self, environment): super().__init__(environment) for attr_value, attr_name in ( (self.host, "host"), (self.stub_class, "stub_class"), ): if attr_value is None: raise LocustError(f"You must specify the {attr_name}.") self._channel = grpc.insecure_channel(self.host) interceptor = LocustInterceptor(environment=environment) self._channel = grpc.intercept_channel(self._channel, interceptor) self.stub = self.stub_class(self._channel)
最后写压测文件(引用grpc_user.py),这里文件名假设为xxx.py。内容如下:
from locust import task import xx_pb2, xx_pb2_grpc import grpc_user class MyGrpcUser(grpc_user.GrpcUser): host = "ip:port" stub_class = xx_pb2_grpc.XxxStub @task def get_charging_gun_count(self): req = xx_pb2.Req() self.stub.XxxMethod(req)
之后就可以运行压测了:
locust -f xxx.py
本文来自博客园,作者:天外归云,转载请注明原文链接:https://www.cnblogs.com/LanTianYou/p/17269747.html
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
2019-03-29 基于Vue element-ui实现支持多级纵向动态表头的仿表格布局
2018-03-29 Python3在指定路径下递归定位文件中出现的字符串