Load Testing gRPC Services

Guide on load testing gRPC APIs with LoadForge using Locust.

LoadForge can record your browser, graphically build tests, scan your site with a wizard and more. Sign up now to run your first test.

Sign up now


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.

Ready to run your test?
LoadForge is cloud-based locust.io testing.