Skip to main content

Overview

Load testing gRPC services requires specialized clients that can serialize and send Protocol Buffer messages. With Locust and the GrpcUser from locust-plugins, you can simulate high-throughput gRPC calls and measure performance under load.

Locust Test Script (locust.py)

# locust.py
import os
import sys
from grpc_tools import protoc
from locust import task, between
from locust_plugins.users import GrpcUser

# Inline .proto definition
proto = '''syntax = "proto3";
package helloworld;

message HelloRequest { string name = 1; }
message HelloReply { string message = 1; }

service Greeter { rpc SayHello (HelloRequest) returns (HelloReply); }
'''
# Write & compile stubs at runtime
with open('helloworld.proto', 'w') as f:
    f.write(proto)
protoc.main(['', '--proto_path=.', '--python_out=.', '--grpc_python_out=.', 'helloworld.proto'])
# Ensure current dir is importable
sys.path.insert(0, os.getcwd())
import helloworld_pb2

class HelloWorldUser(GrpcUser):
    host = "grpc.server.com:50051"
    wait_time = between(1, 3)

    @task
    def say_hello(self):
        # Prepare request
        request = helloworld_pb2.HelloRequest(name="LoadForge")
        # Call the service
        self.call(request, "/helloworld.Greeter/SayHello")

Notes:
  • Install dependencies: locust-plugins, grpcio, grpcio-tools if you wish to test locally.
I