Skip to main content
k6 provides comprehensive support for testing gRPC services through the k6/net/grpc module, including unary calls, client streaming, server streaming, bidirectional streaming, and server reflection.

Getting Started

The gRPC client requires loading protocol buffer definitions before making requests.
The load() method must be called in the init context (outside the default function). The reflect option allows the client to discover services without proto files.

Loading Protocol Buffers

Load .proto files with optional import paths:

Connection Options

Configure gRPC connections with various options:

Unary Calls

Make single request-response RPC calls:

Server Streaming

Receive multiple messages from the server:

Client Streaming

Send multiple messages to the server:

Bidirectional Streaming

Send and receive messages simultaneously:

Metadata and Headers

Send metadata (headers) with gRPC requests:

Status Codes

Check gRPC status codes in responses:

Available Status Codes

  • grpc.StatusOK - Success (0)
  • grpc.StatusCanceled - Operation cancelled (1)
  • grpc.StatusUnknown - Unknown error (2)
  • grpc.StatusInvalidArgument - Invalid argument (3)
  • grpc.StatusDeadlineExceeded - Deadline exceeded (4)
  • grpc.StatusNotFound - Not found (5)
  • grpc.StatusAlreadyExists - Already exists (6)
  • grpc.StatusPermissionDenied - Permission denied (7)
  • grpc.StatusResourceExhausted - Resource exhausted (8)
  • grpc.StatusFailedPrecondition - Failed precondition (9)
  • grpc.StatusAborted - Aborted (10)
  • grpc.StatusOutOfRange - Out of range (11)
  • grpc.StatusUnimplemented - Unimplemented (12)
  • grpc.StatusInternal - Internal error (13)
  • grpc.StatusUnavailable - Unavailable (14)
  • grpc.StatusDataLoss - Data loss (15)
  • grpc.StatusUnauthenticated - Unauthenticated (16)

Response Object

The response object contains:

Load Testing Example

Always call client.close() to properly clean up gRPC connections. Failure to close connections may lead to resource leaks.

Metrics

k6 automatically collects gRPC-specific metrics:
  • grpc_req_duration - Request duration
  • grpc_streams - Number of streams
  • grpc_streams_msgs_received - Messages received per stream
  • grpc_streams_msgs_sent - Messages sent per stream

Best Practices

  1. Load proto files in init context to avoid recompilation on each iteration
  2. Reuse client connections across iterations when possible
  3. Always close clients to prevent resource leaks
  4. Use reflection for development but prefer proto files for production
  5. Handle all status codes for robust error handling
  6. Set appropriate timeouts to prevent hanging requests
  7. Use metadata for authentication tokens and request tracking
  8. Monitor stream lifecycle events for debugging