Skip to main content

Basic gRPC Request

k6 supports testing gRPC services using the k6/net/grpc module:
import grpc from 'k6/net/grpc';
import { check } from "k6";

// to run this sample, you need to start the grpc server first.
// to start the grpc server, run the following command in k6 repository's root:
// go run -mod=mod examples/grpc_server/*.go
// (golang should be installed)
const GRPC_ADDR = __ENV.GRPC_ADDR || '127.0.0.1:10000';
const GRPC_PROTO_PATH = __ENV.GRPC_PROTO_PATH || '../internal/lib/testutils/grpcservice/route_guide.proto';

let client = new grpc.Client();

client.load([], GRPC_PROTO_PATH);

export default () => {
  client.connect(GRPC_ADDR, { plaintext: true });

    const response = client.invoke("main.FeatureExplorer/GetFeature", {
        latitude: 410248224,
        longitude: -747127767
    })

    check(response, { "status is OK": (r) => r && r.status === grpc.StatusOK });
    console.log(JSON.stringify(response.message))

    client.close()
}
You need a running gRPC server to test against. The example uses a test server from the k6 repository.

Setting up gRPC Client

1

Create client and load proto file

import grpc from 'k6/net/grpc';

const client = new grpc.Client();
client.load([], 'path/to/service.proto');
2

Connect to server

client.connect('localhost:50051', {
    plaintext: true,  // Use plaintext connection (no TLS)
});
3

Make requests

const response = client.invoke('package.Service/Method', {
    field1: 'value1',
    field2: 123
});
4

Close connection

client.close();

Client Streaming

Test gRPC client streaming with k6:
import { Client, Stream } from 'k6/net/grpc';
import { sleep } from 'k6';

const COORD_FACTOR = 1e7;
const GRPC_ADDR = __ENV.GRPC_ADDR || '127.0.0.1:10000';
const GRPC_PROTO_PATH = __ENV.GRPC_PROTO_PATH || '../lib/testutils/grpcservice/route_guide.proto';

let client = new Client();
client.load([], GRPC_PROTO_PATH);

// a sample DB of points
const DB = [
  {
    location: { latitude: 407838351, longitude: -746143763 },
    name: 'Patriots Path, Mendham, NJ 07945, USA',
  },
  {
    location: { latitude: 408122808, longitude: -743999179 },
    name: '101 New Jersey 10, Whippany, NJ 07981, USA',
  },
  {
    location: { latitude: 413628156, longitude: -749015468 },
    name: 'U.S. 6, Shohola, PA 18458, USA',
  },
];

export default () => {
  if (__ITER == 0) {
    client.connect(GRPC_ADDR, { plaintext: true });
  }

  const stream = new Stream(client, 'main.RouteGuide/RecordRoute');

  stream.on('data', (stats) => {
    console.log('Finished trip with', stats.pointCount, 'points');
    console.log('Passed', stats.featureCount, 'features');
    console.log('Travelled', stats.distance, 'meters');
    console.log('It took', stats.elapsedTime, 'seconds');
  });

  stream.on('error', (err) => {
    console.log('Stream Error: ' + JSON.stringify(err));
  });

  stream.on('end', () => {
    client.close();
    console.log('All done');
  })

  // send 5 random items
  for (var i = 0; i < 5; i++) {
    let point = DB[Math.floor(Math.random() * DB.length)];
    pointSender(stream, point);
  }

  // close the client stream
  stream.end();
};

const pointSender = (stream, point) => {
  console.log(
    'Visiting point ' +
      point.name +
      ' ' +
      point.location.latitude / COORD_FACTOR +
      ', ' +
      point.location.longitude / COORD_FACTOR
  );

  // send the location to the server
  stream.write(point.location);

  sleep(0.5);
};

Stream Event Handlers

gRPC streams support event-driven programming:
stream.on('data', (message) => {
    console.log('Received from server:', message);
});

Configuration Options

Customize gRPC client behavior:
import grpc from 'k6/net/grpc';

const client = new grpc.Client();
client.load([], 'service.proto');

client.connect('localhost:50051', {
    plaintext: true,           // Disable TLS
    timeout: '10s',            // Request timeout
    maxReceiveSize: 4194304,   // Max message size (4MB)
    maxSendSize: 4194304,      // Max message size (4MB)
});
gRPC testing requires the protocol buffer definition file (.proto) for the service you’re testing.

Build docs developers (and LLMs) love