跳到主要内容

k6

https://github.com/grafana/k6 https://k6.io/ https://grafana.com/docs/k6/latest/

概述

  • 开源免费版不支持分布式运行
  • Scripting in JavaScript ES2015/ES6
  • the tool itself is written in Go, embedding a JavaScript runtime allowing for easy test scripting

运行

k6 run --vus 10 --duration 30s script.js

For a test to run, you need to have init code, which prepares the test, and VU code, which makes requests.

Code in the init context defines functions and configures the test options (like duration).

Every test also has a default function, which defines the VU logic

// init

export default function () {
// vu code: do things here...
}

Init code runs first and is called only once per VU. The default code runs as many times or as long as is configured in the test options

Metric

https://k6.io/docs/using-k6/metrics/

By default, k6 automatically collects built-in metrics. Besides built-ins, you can also make custom metrics

To filter metrics, you can use Tags and groups. You can also export metrics in various summary and granular formats, as documented in Results output

  • requests: measure traffic (in requests)
  • errors: availability (in error rate)
  • duration: latency (in request duration)

Built-in metrics

  • vus
  • vus_max
  • iterations
  • iteration_duration
  • dropped_iterations
  • http_reqs
  • http_req_duration
  • http_req_failed
  • grpc_req_duration
  • grpc_streams
  • grpc_streams_msgs_sent
  • grpc_streams_msgs_received

Create custom metrics

import http from 'k6/http';
import { Trend } from 'k6/metrics';

const myTrend = new Trend('waiting_time');

export default function () {
const r = http.get('https://httpbin.test.k6.io');
myTrend.add(r.timings.waiting);
console.log(myTrend.name); // waiting_time
}

协议

HTTP

https://k6.io/docs/using-k6/http-requests/

gRPC

https://k6.io/blog/performance-testing-grpc-services/

import grpc from 'k6/net/grpc';
import { check, sleep } from 'k6';

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);
};

Testing guides

https://grafana.com/docs/k6/latest/testing-guides/

Load test types

https://grafana.com/docs/k6/latest/testing-guides/test-types/

  • Smoke tests
  • Average-load test
  • Stress tests
  • Soak tests
  • Spike tests
  • Breakpoint tests