IMLC.ME
/k6/k6 gRPC/

k6 gRPC

没什么用的提示:自 (v0.29.0)[https://github.com/loadimpact/k6/releases/tag/v0.29.0] 起,k6 提供了 gRPC 的支持。

没什么用的提示2:本文默认你知道 gRPC 是什么玩意了,直接上正文。

import grpc from 'k6/net/grpc';

const client = new grpc.Client();

client.load(['definitions'], 'hello.proto');

export default () => {
  client.connect('grpcbin.test.k6.io:9001', {
    // plaintext: false
  });

  const data = { greeting: 'Bert' };
  const response = client.invoke('hello.HelloService/SayHello', data);

  check(response, {
    'status is OK': (r) => r && r.status === grpc.StatusOK,
  });

  console.log(JSON.stringify(response.message));

  client.close();
  sleep(1);
};
k6 run test.js


          /\      |‾‾| /‾‾/   /‾‾/
     /\  /  \     |  |/  /   /  /
    /  \/    \    |     (   /   ‾‾\
   /          \   |  |\  \ |  ()  |
  / __________ \  |__| \__\ \_____/ .io

  execution: local
     script: /Users/simme/code/grpc/test.js
     output: -

  scenarios: (100.00%) 1 scenario, 1 max VUs, 10m30s max duration (incl. graceful stop):
           * default: 1 iterations for each of 1 VUs (maxDuration: 10m0s, gracefulStop: 30s)

INFO[0000] {"reply":"hello Bert"}                        source=console

running (00m01.4s), 0/1 VUs, 1 complete and 0 interrupted iterations
default ✓ [======================================] 1 VUs  00m01.4s/10m0s  1/1 iters, 1 per VU

    ✓ status is OK

    checks...............: 100.00% ✓ 10
    data_received........: 3.0 kB  2.1 kB/s
    data_sent............: 731 B   522 B/s
    grpc_req_duration....: avg=48.44ms min=48.44ms med=48.44ms max=48.44ms p(90)=48.44ms p(95)=48.44ms
    iteration_duration...: avg=1.37s   min=1.37s   med=1.37s   max=1.37s   p(90)=1.37s   p(95)=1.37s
    iterations...........: 1       0.714536/s
    vus..................: 1       min=1 max=1
    vus_max..............: 1       min=1 max=1

参考文献

Performance testing gRPC services