Client.connect(address [,params])
Opens a connection to a gRPC server; will block until a connection is made or a connection error is thrown. Cannot be called during the
init phase.
See Client.close() to close the connection.
ConnectParams
TLS
TLS settings of the connection. If not defined, the main TLS config from options will be used.
Examples
import grpc from 'k6/net/grpc';
const client = new grpc.Client();
export default () => {
client.connect('localhost:8080');
};import grpc from 'k6/net/grpc';
const client = new grpc.Client();
export default () => {
client.connect('localhost:8080', { plaintext: true });
};import grpc from 'k6/net/grpc';
import { check } from 'k6';
import { SharedArray } from 'k6/data';
import exec from 'k6/execution';
// note: the services in this example don't exist. If you would like
// to run this example, make sure to replace the URLs, and
// the cacerts, cert, key, and password variables.
const grpcArgs = new SharedArray('grpc', () => {
// Using SharedArray here so that not every VU gets a copy of every certificate a key
return [
{
host: 'foo1.grpc-quickpizza.grafana.com:443',
plaintext: false,
params: {
tls: {
cacerts: [open('cacerts0.pem')],
cert: open('cert0.pem'),
key: open('key0.pem'),
},
},
},
{
host: 'foo2.grpc-quickpizza.grafana.com:443',
params: {
plaintext: false,
tls: {
cacerts: open('cacerts1.pem'),
cert: open('cert1.pem'),
key: open('key1.pem'),
password: 'cert1-passphrase',
},
},
},
];
});
const client = new grpc.Client(null, 'quickpizza.proto');
export default () => {
if (__ITER === 0) {
// Take one config and use it for this one VU
const grpcArg = grpcArgs[exec.vu.idInTest % grpcArgs.length];
client.connect(grpcArg.host, grpcArg.params);
}
const response = client.invoke('quickpizza.GRPC/Status');
check(response, {
'status is OK': (r) => r && r.status === grpc.StatusOK,
});
console.log(JSON.stringify(response.message));
};

