Scripts2026年4月12日·1 分钟阅读

k6 — Modern Load Testing Tool Using Go and JavaScript

k6 is a modern load testing tool built by Grafana Labs. Write test scripts in JavaScript, run them in a high-performance Go runtime. Developer-centric with CLI-first workflow, CI/CD integration, and Grafana Cloud for result analysis.

SC
Script Depot · Community
快速使用

先拿来用,再决定要不要深挖

这里应该同时让用户和 Agent 知道第一步该复制什么、安装什么、落到哪里。

brew install k6                             # macOS
sudo apt install k6                         # Debian/Ubuntu
docker run --rm -i grafana/k6 run - < script.js
// load-test.js
import http from "k6/http";
import { check, sleep } from "k6";

export const options = {
  stages: [
    { duration: "30s", target: 50 },   // Ramp up to 50 VUs
    { duration: "1m",  target: 50 },   // Stay at 50
    { duration: "10s", target: 0 },    // Ramp down
  ],
  thresholds: {
    http_req_duration: ["p(95)<500"],   // 95th percentile < 500ms
    http_req_failed: ["rate<0.01"],     // Error rate < 1%
  },
};

export default function () {
  const res = http.get("https://api.tokrepo.com/assets");
  check(res, {
    "status is 200": (r) => r.status === 200,
    "body has assets": (r) => r.json().length > 0,
  });
  sleep(1);
}
k6 run load-test.js
k6 run --vus 100 --duration 2m load-test.js
介绍

k6 is a modern load testing tool built by Grafana Labs. Write test scripts in JavaScript (ES6), execute them in a high-performance Go runtime (not Node.js). CLI-first, developer-friendly, with built-in metrics, thresholds, and CI/CD integration.

What k6 Does

  • JavaScript scripting — ES6 modules for test logic
  • Go runtime — high performance, low overhead
  • Virtual Users — simulate concurrent users
  • Scenarios — constant, ramping, arrival-rate
  • Thresholds — pass/fail criteria for CI
  • Checks — response assertions
  • Metrics — request duration, throughput, errors
  • Protocols — HTTP, WebSocket, gRPC, Redis
  • Extensions — xk6 for custom protocols
  • Output — JSON, CSV, Grafana Cloud, Prometheus, InfluxDB

Comparison

Tool Scripts Runtime Distributed
k6 JavaScript Go k6-operator
Locust Python Python Built-in
JMeter XML/GUI Java Built-in
Gatling Scala JVM Paid
Artillery YAML/JS Node.js Paid

常见问题 FAQ

Q: 为什么不用 Node.js? A: k6 的 JS 引擎是嵌入 Go 的 goja(不是 V8/Node)。这样每个 VU 开销很小,单机可以模拟几千 VU。

Q: 和 Grafana 集成? A: 输出到 Prometheus Remote Write 或 Grafana Cloud k6,然后在 Grafana 面板看实时负载测试指标。

来源与致谢 Sources

讨论

登录后参与讨论。
还没有评论,来写第一条吧。

相关资产