# k6（软件）

**k6**是一款开源负载测试工具，使用Go语言编写，以[JavaScript](https://www.edgepedia.org/zh/javascript)编写脚本，通过虚拟用户（VU）模拟生产流量，对API、微服务和网站施加负载。它于2017年由Load Impact开源发布，专为高负载性能测试而设计，如尖峰测试、压力测试和浸泡测试，且资源消耗极低。<sup>[1](https://grafana.com/blog/k6-vs-jmeter-comparison/)</sup><sup> • </sup><sup>[2](https://grafana.com/docs/k6/latest/)</sup><sup> • </sup><sup>[3](https://fenix.tecnico.ulisboa.pt/downloadFile/1407770020547929/Dissertation.pdf)</sup>

| 关键事实 | 详情 |
|---|---|
| 首次发布 | 代码仓库创建于2016年3月21日；2017年由Load Impact开源发布<sup>[4](https://github.com/grafana/k6)</sup><sup> • </sup><sup>[1](https://grafana.com/blog/k6-vs-jmeter-comparison/)</sup> |
| 架构 | Go引擎内嵌JavaScript引擎；“Go的性能，JavaScript脚本编写的熟悉感”<sup>[4](https://github.com/grafana/k6)</sup> |
| 并发模型 | 每个虚拟用户对应一个goroutine，由Go运行时调度，而非每个用户一个线程<sup>[3](https://fenix.tecnico.ulisboa.pt/downloadFile/1407770020547929/Dissertation.pdf)</sup> |
| 单机容量 | 单实例可承载30,000–40,000个VU，每秒生成超过300,000次请求<sup>[1](https://grafana.com/blog/k6-vs-jmeter-comparison/)</sup> |
| 协议 | HTTP/1.1、HTTP/2、WebSockets、gRPC和浏览器测试，更多协议可通过xk6扩展获得<sup>[1](https://grafana.com/blog/k6-vs-jmeter-comparison/)</sup> |
| 执行模式 | 本地、分布式（通过k6 Operator在Kubernetes上）和云端（Grafana Cloud k6）<sup>[2](https://grafana.com/docs/k6/latest/)</sup> |
| 许可证 | GNU Affero通用公共许可证v3.0（AGPL-3.0）<sup>[4](https://github.com/grafana/k6)</sup> |
| 采用情况 | 截至检索时，GitHub上有31,911个星标和1,682个复刻<sup>[4](https://github.com/grafana/k6)</sup> |

## 架构与工作原理

一个k6测试是一个JavaScript ES6模块，由Go引擎通过内嵌的JavaScript运行时执行。每个虚拟用户本质上是一个并行的`while(true)`循环，运行脚本的迭代；虚拟用户越多，模拟的流量就越大。<sup>[2](https://grafana.com/docs/k6/latest/)</sup>该引擎的设计目标是结合Go的吞吐量与开发者和测试人员熟悉的脚本语言。<sup>[4](https://github.com/grafana/k6)</sup>

**是goroutine，不是线程。** JMeter为每个虚拟用户分配一个操作系统线程，在等待响应时线程处于空闲状态；而k6将每个虚拟用户分配给一个goroutine。goroutine由Go调度器管理，调度器会复用空闲线程，这是k6每用户内存开销更低的机制基础。<sup>[3](https://fenix.tecnico.ulisboa.pt/downloadFile/1407770020547929/Dissertation.pdf)</sup>所引用的对比报告显示，k6在其测试中使用的内存少于JMeter。<sup>[1](https://grafana.com/blog/k6-vs-jmeter-comparison/)</sup>

每个k6测试经历四个生命周期阶段：`init`阶段，每个虚拟用户运行一次，用于加载文件、导入模块和定义测试函数；可选的`setup`函数，运行一次以准备测试环境和生成数据；VU代码，每次迭代运行一次；以及可选的`teardown`函数，在结束时运行一次。<sup>[2](https://grafana.com/docs/k6/latest/)</sup>

测试以三种执行模式之一运行：在单机、容器或CI服务器上本地运行；通过k6-testrun资源在[Kubernetes](https://www.edgepedia.org/zh/kubernetes)集群上分布式运行；或在Grafana Cloud k6上云端运行。<sup>[2](https://grafana.com/docs/k6/latest/)</sup>

## 测试编写：场景、检查和阈值

**场景**允许一个脚本运行多个独立执行的JavaScript函数，每个函数有自己的VU和迭代调度模式，由专门构建的执行器驱动。执行器配置测试运行时长、流量是保持恒定还是随时间变化，以及工作负载是按虚拟用户建模还是按到达率建模，即封闭式或开放式工作负载模型。场景并行运行，但可以通过`startTime`设置使其表现为顺序运行。<sup>[2](https://grafana.com/docs/k6/latest/)</sup>外部控制的执行器更进一步，在运行时管理`vus`、`maxVUs`和`duration`参数，可通过CLI或REST API动态调整。<sup>[3](https://fenix.tecnico.ulisboa.pt/downloadFile/1407770020547929/Dissertation.pdf)</sup>

**阈值**是基于指标的条件集合，用于判定测试通过或失败，例如`avg<100`或`p(95)<200`，或断言99%的请求在3000毫秒内完成。由于阈值失败会产生失败的测试结果，阈值可用于门控CI/CD流水线：k6与CI/CD及自动化工具集成，使性能标准在开发和发布周期中作为一部分被强制执行。<sup>[2](https://grafana.com/docs/k6/latest/)</sup><sup> • </sup><sup>[4](https://github.com/grafana/k6)</sup><sup> • </sup><sup>[1](https://grafana.com/blog/k6-vs-jmeter-comparison/)</sup>

## 指标与结果

k6自动收集内置指标，并支持四种类型的自定义指标：计数器、瞬时值、速率和趋势。<sup>[2](https://grafana.com/docs/k6/latest/)</sup>内置HTTP指标直接映射到站点可靠性工程实践：`http_reqs`以请求数衡量流量，`http_req_failed`衡量请求失败率，`http_req_duration`衡量延迟。这是SRE四个黄金信号中的三个。<sup>[2](https://grafana.com/docs/k6/latest/)</sup>

趋势指标报告通过`summaryTrendStats`配置的汇总统计：平均值、中位数、最小值、最大值、计数，以及任意百分位数如p(95)、p(99)和p(99.99)。`summary`选项可将测试结束报告保存为机器可读的JSON，供下游处理。<sup>[2](https://grafana.com/docs/k6/latest/)</sup>

在比较工具时，有一个测量注意事项很重要。k6的默认`http_req_duration`等于`http_req_sending + http_req_waiting + http_req_receiving`，因此不包括初始DNS查找和连接时间。在OctoPerf一项禁用连接复用的基准测试中，k6报告的平均响应时间为0.01959秒，而JMeter为0.081秒，Gatling为0.071秒，Locust为0.150秒，各自约32,000次请求；将k6的连接和TLS握手指标与持续时间合并后得到0.109秒，与其他工具处于同一量级，尽管在该组合中DNS时间仍无法测量。<sup>[5](https://blog.octoperf.com/open-source-load-testing-tools-comparative-study/)</sup>同一研究发现，JMeter和Gatling报告的计时接近浏览器计时，而Locust报告的数值显著偏高，k6则报告偏低，除非合并指标。<sup>[5](https://blog.octoperf.com/open-source-load-testing-tools-comparative-study/)</sup>

对于调优，将`responseType`设置为`none`可减少测试机所需的内存和垃圾回收负载，这往往能产生更可靠的结果。k6还暴露pprof性能分析端点用于CPU和内存分析，以及[Prometheus](https://www.edgepedia.org/zh/prometheus) `/metrics`端点用于Go运行时指标。<sup>[2](https://grafana.com/docs/k6/latest/)</sup>

## 协议与可扩展性

k6原生支持HTTP/1.1、HTTP/2、WebSockets和gRPC，可测试SOAP/REST Web服务，更多协议可通过用Go编写的xk6社区扩展获得。<sup>[1](https://grafana.com/blog/k6-vs-jmeter-comparison/)</sup>通过k6浏览器API，测试可以驱动真实浏览器并收集浏览器指标，以识别仅在浏览器层出现的性能问题。<sup>[2](https://grafana.com/docs/k6/latest/)</sup>

## 数据一览

单个k6实例通常足以生成30,000至40,000个并发虚拟用户的负载，每秒可产生超过300,000次请求，具体取决于目标和机器。<sup>[1](https://grafana.com/blog/k6-vs-jmeter-comparison/)</sup>在Grafana Cloud k6中，每个负载生成器实例承载300个VU，按负载生成器设置的选项会随实例数成倍增加：配置为1000个VU的测试会启动4个负载生成器实例。<sup>[2](https://grafana.com/docs/k6/latest/)</sup>GitHub仓库创建于2016年3月21日，截至检索时有31,911个星标、1,682个复刻和777个未解决问题。<sup>[4](https://github.com/grafana/k6)</sup>

## 与JMeter、Locust和Gatling的比较

这些工具最显著的差异在于脚本模型和资源使用。JMeter提供GUI和庞大的社区，但基于Java且更重；k6用JavaScript编写脚本，运行在Go引擎上。一篇比较它们的论文指出，与JMeter相比，k6是相对较新的工具，JMeter受益于强大的社区支持和GUI，但消耗更多计算资源。<sup>[3](https://fenix.tecnico.ulisboa.pt/downloadFile/1407770020547929/Dissertation.pdf)</sup>在Rafaela Azevedo的一项比较中，JMeter运行一个测试消耗760 MB内存，而k6仅用256 MB。<sup>[1](https://grafana.com/blog/k6-vs-jmeter-comparison/)</sup>

容量数据因来源而异。Grafana的比较指出，JMeter在单个负载生成器上平均可运行约一千个虚拟用户，之后需要分布式执行，而在相同资源下单个k6实例可运行数万个。<sup>[1](https://grafana.com/blog/k6-vs-jmeter-comparison/)</sup>一项独立的QAInsights基准测试发现，JMeter在单个m4实例上无法从1000个虚拟用户生成负载，而k6和Locust均成功；在同一基准测试中，k6每分钟产生约30M字节的网络流量，略优于Locust，而JMeter无论使用1 GB还是4 GB堆内存，每分钟都仅传输12M字节，落后于另外两款工具。<sup>[6](https://qainsights.com/performance-tools-benchmarking/)</sup>

ICTSS上的一项2025年同行评审研究评估了五个开源工具——[Apache JMeter](https://www.edgepedia.org/zh/apache-jmeter)、Grafana k6、Gatling、Locust和[Artillery](https://www.edgepedia.org/zh/artillery)——集成到基于Jenkins的CI/CD流水线中，用于[Spring Boot](https://www.edgepedia.org/zh/spring-boot)应用，测量在代表性、最大和尖峰负载下的CPU和内存使用及响应时间。统计分析（Kruskal-Wallis和Mann-Whitney U检验，经Bonferroni校正）发现工具间的差异在p < 0.05水平上显著，Grafana k6在所有测试场景中始终表现出最高的资源效率。<sup>[7](https://conf.researchr.org/details/ictss-2025/ictss-2025-general-track/12/Automating-Performance-Testing-in-CI-CD-Tools-Evaluation)</sup>这些工具层面的差异符合负载测试研究的总体框架，该框架将实践组织为设计适当的负载、执行测试和分析结果。<sup>[8](https://dl.acm.org/doi/10.1109/TSE.2015.2445340)</sup>

有一处已发表的分歧值得注意：QAInsights的一篇文章称，与JMeter和Locust不同，k6在开源版本中不支持分布式执行，云端分布式执行需要商业版本。<sup>[6](https://qainsights.com/performance-tools-benchmarking/)</sup>相比之下，k6官方文档将跨Kubernetes集群的分布式执行列为k6三种执行模式之一，Grafana记录了k6 Operator用于在集群中创建k6自定义资源。<sup>[2](https://grafana.com/docs/k6/latest/)</sup><sup> • </sup><sup>[1](https://grafana.com/blog/k6-vs-jmeter-comparison/)</sup>

## 云产品与许可

除开源CLI之外，k6产品线还包括Grafana Cloud k6——一种SaaS产品，用于测试执行、指标关联和数据分析，并与该工具原生集成<sup>[4](https://github.com/grafana/k6)</sup>——以及k6 Operator，它通过在Kubernetes集群中创建k6自定义资源对象来实现分布式负载测试。<sup>[1](https://grafana.com/blog/k6-vs-jmeter-comparison/)</sup>

k6在[GNU Affero通用公共许可证](https://www.edgepedia.org/zh/gnu-affero-general-public-license)v3.0下分发。<sup>[4](https://github.com/grafana/k6)</sup>AWS的性能测试指南指出，AGPL-3.0在定制容器镜像以直接包含k6时提出了要求，这是将工具嵌入自身发行版的组织需要考虑的事项。<sup>[9](https://docs.aws.amazon.com/solutions/latest/performance-testing/choosing-your-testing-tool.html)</sup>

## 近期发展

v0.5x版本使脚本编写体验现代化。k6 v0.52增加了[TypeScript](https://www.edgepedia.org/zh/typescript)支持以及ES6+特性，如可选链、对象展开和私有类字段；截至v0.53，这些ES6+特性在默认兼容模式下可用。<sup>[10](https://www.infoq.com/news/2024/09/grafana-k6-releases/)</sup>

**浏览器测试走向成熟。** 浏览器模块于2021年引入，在v0.52中变为完全异步，与Playwright风格的async/await API保持一致，并从实验性的`k6/experimental/browser`路径升级为稳定的`k6/browser`模块。<sup>[10](https://www.infoq.com/news/2024/09/grafana-k6-releases/)</sup>v2.2.0版本更进一步，允许浏览器模块通过Chrome DevTools协议附加到现有的基于Chromium的浏览器，模仿Playwright的`browserType.connectOverCDP()`；k6管理连接并在迭代结束时自动关闭。<sup>[11](https://github.com/grafana/k6/releases)</sup>

在v0.51中，双向gRPC流集成到稳定的`k6/net/grpc`模块中，该模块还获得了返回Promise的非阻塞`client.asyncInvoke`方法，并新增了Stream模块，以小分块读取大文件，防止在大数据集负载测试期间出现内存溢出错误。<sup>[10](https://www.infoq.com/news/2024/09/grafana-k6-releases/)</sup>v2.x系列增加了`--scenario`标志，无需编辑脚本即可运行命名场景的子集，回应了社区长期以来的请求。<sup>[11](https://github.com/grafana/k6/releases)</sup>

## References

1. [Comparing k6 and JMeter for load testing, Grafana Labs](https://grafana.com/blog/k6-vs-jmeter-comparison/)
2. [Grafana k6 documentation](https://grafana.com/docs/k6/latest/)
3. [Closed-loop and distributed load testing of Web applications using Kubernetes, Instituto Superior Técnico dissertation](https://fenix.tecnico.ulisboa.pt/downloadFile/1407770020547929/Dissertation.pdf)
4. [grafana/k6, official GitHub repository](https://github.com/grafana/k6)
5. [Open source Load Testing tools comparative study, OctoPerf](https://blog.octoperf.com/open-source-load-testing-tools-comparative-study/)
6. [Performance Tools Benchmarking, QAInsights](https://qainsights.com/performance-tools-benchmarking/)
7. [Automating Performance Testing in CI/CD, Tools Evaluation, ICTSS 2025](https://conf.researchr.org/details/ictss-2025/ictss-2025-general-track/12/Automating-Performance-Testing-in-CI-CD-Tools-Evaluation)
8. [A Survey on Load Testing of Large-Scale Software Systems](https://dl.acm.org/doi/10.1109/TSE.2015.2445340)
9. [Choosing Your Testing Tool, Performance Testing on AWS](https://docs.aws.amazon.com/solutions/latest/performance-testing/choosing-your-testing-tool.html)
10. [Grafana K6 Releases: Enhancements in TypeScript, ECMAScript, Browser Testing, and More, InfoQ](https://www.infoq.com/news/2024/09/grafana-k6-releases/)
11. [Releases, grafana/k6](https://github.com/grafana/k6/releases)

---
*Initially written Oct 15, 2026*

Topic: 技术与人造世界 › 计算技术与数字系统 › 软件与编程 › Software engineering and development process › Software testing and quality › Test infrastructure and tooling

Copyright 2026 Edgepedia

License: Edgepedia Community License 1.0, https://www.edgepedia.org/edgepedia/license

Source: https://www.edgepedia.org/zh/k6-software

English original: https://www.edgepedia.org/k6-software
