k6是GitHub上提供的开源负载测试工具。使用Go编写,并运行用JavaScript编写的测试脚本

先决条件

概念

  • vu:虚拟用户
  • duration:持续时长

入门

  1. import http from 'k6/http';
  2. import { sleep } from 'k6';
  3. export default function() {
  4. http.get('http://localhost:5000/api/values');
  5. }

k6 run ValuesApiK6.js

多Vu和指定时长

k6 run --vus 10 --duration 10s ValuesApiK6.js

使用配置选项

  1. import http from "k6/http";
  2. import { sleep,check } from "k6";
  3. export let options = {
  4. vus: 10,
  5. duration: '10s',
  6. };
  7. // 脚本至少必须包含一个default函数,这定义了VU的入口点,类似于许多其他语言中的main()函数
  8. export default function() {
  9. let res = http.get("http://localhost:5000/api/values");
  10. check(res, { 'status was 200': r => r.status == 200 });
  11. sleep(1);
  12. }

测试过程中设置阶段使VU级别上升和下降

  1. import http from "k6/http";
  2. import { sleep, check } from "k6";
  3. export let options = {
  4. // 阶段负载
  5. stages: [
  6. { duration: "3s", target: 2 },
  7. { duration: "5s", target: 3 },
  8. { duration: "6s", target: 10 },
  9. ],
  10. };
  11. // 脚本至少必须包含一个default函数,这定义了VU的入口点,类似于许多其他语言中的main()函数
  12. export default function () {
  13. let res = http.get("http://localhost:5000/api/values");
  14. check(res, { "status was 200": (r) => r.status == 200 });
  15. sleep(1);
  16. }

可能出现的异常:using multiple execution config shortcuts (durationandstages) simultaneously is not allowed 不允许同时使用多个执行配置快捷方式(“duration”和“stages”)

断言

需要import { sleep, check } from "k6"; 引入check模块

check(res, { "status was 200": (r) => r.status == 200 });

测试结果

  1. execution: local-
  2. script: ValuesApiK6.js
  3. output: -
  4. scenarios: (100.00%) 1 executors, 10 max VUs, 44s max duration (incl. graceful stop):
  5. * default: Up to 10 looping VUs for 14s over 3 stages (gracefulRampDown: 30s, gracefulStop: 30s)
  6. I
  7. running (16.0s), 00/10 VUs, 38 complete and 0 interrupted iterations
  8. default [======================================] 00/10 VUs 14s9
  9. status was 200
  10. checks.....................: 100.00% 38 0
  11. data_received..............: 176 MB 11 MB/s
  12. data_sent..................: 3.3 kB 208 B/s
  13. http_req_blocked...........: avg=79.1µs min=0s med=0s max=2ms p(90)=0s p(95)=149.71µs
  14. http_req_connecting........: avg=52.85µs min=0s med=0s max=1.01ms p(90)=0s p(95)=149.71µs
  15. http_req_duration..........: avg=682.14ms min=332.4ms med=516.59ms max=1.66s p(90)=1.22s p(95)=1.44s
  16. http_req_receiving.........: avg=10.39ms min=3.98ms med=8.97ms max=37.89ms p(90)=16.85ms p(95)=23.37ms
  17. http_req_sending...........: avg=0s min=0s med=0s max=0s p(90)=0s p(95)=0s
  18. http_req_tls_handshaking...: avg=0s min=0s med=0s max=0s p(90)=0s p(95)=0s
  19. http_req_waiting...........: avg=671.75ms min=323.42ms med=508.61ms max=1.65s p(90)=1.21s p(95)=1.43s
  20. http_reqs..................: 38 2.374567/s
  21. iteration_duration.........: avg=1.68s min=1.33s med=1.51s max=2.66s p(90)=2.22s p(95)=2.44s
  22. iterations.................: 38 2.374567/s
  23. vus........................: 0 min=0 max=9
  24. vus_max....................: 10 min=10 max=10

测试细节

  • execution: local k6执行模式(本地或云)
  • output: -测试结果的输出。默认值为stdout
  • script: script.js 显示正在执行的脚本的名称
  • duration: 测试运行时间
  • iterations: -VU 迭代的总数
  • vus: 100 测试将开始运行的VU的初始数量
  • max: 100 测试将扩展的VU的最大数量

测试指标分析参考:https://k6.io/docs/using-k6/metrics

测试结果输出第三方平台

参考:https://k6.io/docs/getting-started/results-output#output-plugins

输出Json格式

k6 run --summary-export=export.json test.js

输出JSON格式详细信息

使用—out/-o选项使k6输出JSON格式的详细统计信息

k6 run --out json=my_test_result.json test.js

使用tags给请求打标签分组

参考:https://k6.io/docs/using-k6/http-requests#url-grouping

K6内置指标

参考:https://k6.io/docs/using-k6/metrics#custom-metrics

HTTP内置指标

参考:https://k6.io/docs/using-k6/metrics#metric-types

阈值

阈值是通过/失败标准,用于指定被测系统的性能期望

预期示例(阈值):

  • 系统产生的错误不超过1%
  • 95%的请求的响应时间应小于200ms
  • 99%的请求的响应时间应低于400毫秒
  • 特定端点必须始终在300毫秒内响应
  • 阈值分析性能指标并确定最终测试结果(通过/失败)。阈值对于负载测试自动化至关重要

参考:https://k6.io/docs/using-k6/thresholds

分析结果可视化

influxdb

  1. 安装目录下运行启动influxdb
  2. 启动客户端输入命令开始操作 influx
  3. k6 run --out influxdb=http://localhost:8080/test ValuesApiK6.js

Grafana

image.png