Important: This documentation is about an older version. It's relevant only to the release noted, many of the features and functions have been updated or replaced. Please view the current version.
Client
Client
is a gRPC client that can interact with a gRPC server.
Method | Description |
---|---|
Client.load(importPaths, …protoFiles) | Loads and parses the given protocol buffer definitions to be made available for RPC requests. |
Client.loadProtoset(protosetPath) | Loads and parses the given protoset file to be made available for RPC requests. |
Client.connect(address [,params]) | Opens a connection to the given gRPC server. |
Client.invoke(url, request [,params]) | Makes a unary RPC for the given service/method and returns a Response. |
Client.close() | Close the connection to the gRPC service. |
Examples
import grpc from 'k6/experimental/grpc';
const client = new grpc.Client();
// Download addsvc.proto for https://grpcbin.test.k6.io/, located at:
// https://raw.githubusercontent.com/moul/pb/master/addsvc/addsvc.proto
// and put it in the same folder as this script.
client.load(null, 'addsvc.proto');
export default () => {
client.connect('grpcbin.test.k6.io:9001', { timeout: '5s' });
const response = client.invoke('addsvc.Add/Sum', {
a: 1,
b: 2,
});
console.log(response.message.v); // should print 3
client.close();
};
import grpc from 'k6/experimental/grpc';
import { check } from 'k6';
const client = new grpc.Client();
client.load([], 'authorization.proto', 'route_guide.proto');
export function setup() {
client.connect('auth.googleapis.com:443');
const resp = client.invoke('google.cloud.authorization.v1.AuthService/GetAccessToken', {
username: 'john.smith@k6.io',
password: 'its-a-secret',
});
client.close();
return resp.message.accessToken;
}
export default (token) => {
client.connect('route.googleapis.com:443');
const metadata = {
authorization: `bearer ${token}`,
};
const response = client.invoke(
'google.cloud.route.v1.RoutingService/GetFeature',
{
latitude: 410248224,
longitude: -747127767,
},
{ metadata }
);
check(response, { 'status is OK': (r) => r && r.status === grpc.StatusOK });
client.close();
};
import grpc from 'k6/experimental/grpc';
import { check } from 'k6';
const client = new grpc.Client();
client.load([], 'language_service.proto');
export default () => {
if (__ITER == 0) {
client.connect('language.googleapis.com:443');
}
const response = client.invoke('google.cloud.language.v1.LanguageService/AnalyzeSentiment', {});
check(response, { 'status is OK': (r) => r && r.status === grpc.StatusOK });
// Do NOT close the client
};