# 性能测试

**性能测试**是一种软件测试实践，用于确定软件应用在承受称为工作负载的并发服务请求流时的响应能力、可靠性、可扩展性和资源消耗。<sup>[1](https://alm.parasoft.com/hubfs/whitepaper-Performance-Testing-Best-Practices-Guide.pdf)</sup> 待测应用部署在模拟生产的环境中，测试工具对其生成合成工作负载。<sup>[1](https://alm.parasoft.com/hubfs/whitepaper-Performance-Testing-Best-Practices-Guide.pdf)</sup> 在 [ISO/IEC 25010](https://www.edgepedia.org/zh/iso-iec-25010) 软件质量模型（ISO/IEC 25010 software quality model）中，这类测试评估的属性是性能效率，定义为在既定条件下相对于所用资源量的性能，其子特性包括时间行为、资源利用率和容量。<sup>[2](https://www.iso.org/obp/ui/?_escaped_fragment_=iso%3Astd%3Aiso-iec%3A25010%3Aed-1%3Av1%3Aen)</sup>

| 关键事实 | 详情 |
|---|---|
| 测量内容 | 并发请求工作负载下的响应能力、可靠性、可扩展性和资源消耗<sup>[1](https://alm.parasoft.com/hubfs/whitepaper-Performance-Testing-Best-Practices-Guide.pdf)</sup> |
| 质量标准 | ISO/IEC 25010 的性能效率涵盖时间行为、资源利用率和容量<sup>[2](https://www.iso.org/obp/ui/?_escaped_fragment_=iso%3Astd%3Aiso-iec%3A25010%3Aed-1%3Av1%3Aen)</sup> |
| 常用指标 | 响应时间、吞吐量、错误率、资源利用率（CPU、内存、磁盘）和网络延迟<sup>[3](https://docs.aws.amazon.com/pdfs/prescriptive-guidance/latest/performance-engineering-aws/performance-engineering-aws.pdf)</sup> |
| 主要测试类型 | 负载测试、压力测试、尖峰测试和耐久性测试，各自揭示不同的失效模式<sup>[4](https://learn.microsoft.com/en-us/azure/well-architected/performance-efficiency/performance-test)</sup> |
| 工作负载模型 | 开放模型控制用户的到达率；封闭模型限制并发用户数<sup>[5](https://docs.gatling.io/testing-concepts/workload-models/)</sup> |
| 典型成本 | 一次负载测试通常运行 3 到 15 分钟或更久，因此把性能测试加入 CI/CD 会延长发布流程<sup>[6](https://grafana.com/docs/k6/latest/testing-guides/automated-performance-testing/)</sup> |
| 广泛使用的工具 | 开源工具包括 JMeter（JMeter）、Locust（Locust）、Gatling（Gatling）和 k6（k6）<sup>[7](https://blog.octoperf.com/open-source-load-testing-tools-comparative-study/)</sup> |

## 测试类型

不同的测试类型从不同角度验证工作负载，每种类型都有各自的目的和环境。<sup>[4](https://learn.microsoft.com/en-us/azure/well-architected/performance-efficiency/performance-test)</sup>

- **负载测试**验证系统在正常和峰值使用情况下能否处理预期的用户量。它建立基线性能、容量限制和扩展有效性，微软（Microsoft）建议尽早开始并频繁运行。<sup>[4](https://learn.microsoft.com/en-us/azure/well-architected/performance-efficiency/performance-test)</sup>
- **压力测试**在生产就绪之前寻找系统的极限和崩溃点，揭示最大容量、失效模式和恢复行为。<sup>[4](https://learn.microsoft.com/en-us/azure/well-architected/performance-efficiency/performance-test)</sup>
- **尖峰测试**检查系统对突发流量激增的响应，这对面向公众的应用尤其重要。它暴露自动扩缩容的响应能力、队列处理和优雅降级。<sup>[4](https://learn.microsoft.com/en-us/azure/well-architected/performance-efficiency/performance-test)</sup>
- **耐久性测试**（也称浸泡测试）在初始负载测试通过后运行，检测只在长时间运行后才出现的问题，例如内存泄漏、资源耗尽和连接池问题。<sup>[4](https://learn.microsoft.com/en-us/azure/well-architected/performance-efficiency/performance-test)</sup>

微软的指导是先从基础负载测试开始建立基线，然后随着风险被识别，逐步扩展到压力测试、尖峰测试，最终到耐久性测试，因为覆盖所有测试类型需要大量时间和基础设施投入。<sup>[4](https://learn.microsoft.com/en-us/azure/well-architected/performance-efficiency/performance-test)</sup> 同样的区分也出现在支付系统实践中：尖峰测试分析对流量快速异常激增的响应，而耐久性测试验证一个组件或系统能否在较长时间内处理预期工作负载。<sup>[8](https://www.paragonedge.com/hubfs/2024%20Resources/Performance%20Testing%20-%20Best%20Practice%20Guide/Paragon%20Best%20Practice%20Guide%20-%20Performance%20Testing%20for%20Payment%20Systems.pdf?hsCtaAttrib=181639973295)</sup>

## 指标与通过标准

常见的性能指标是响应时间、吞吐量、错误率、资源利用率（CPU、内存、磁盘）和网络延迟。<sup>[3](https://docs.aws.amazon.com/pdfs/prescriptive-guidance/latest/performance-engineering-aws/performance-engineering-aws.pdf)</sup> IBM（IBM）的指南将同一组指标视为可接受的性能标准，此外还包括带宽检查，以确认数据传输速率能够处理工作负载流量。<sup>[9](https://www.ibm.com/think/topics/performance-testing)</sup>

在 Grafana k6（Grafana k6）这一负载测试工具中，指标分为四类：计数器把数值相加；仪表记录最小值、最大值和最近值；比率记录非零值出现的频率；趋势计算均值或百分位数等汇总统计量。<sup>[10](https://grafana.com/docs/k6/latest/using-k6/metrics/)</sup> 对于不确定从何入手的团队，k6 推荐 RED 方法（RED method）的指标——请求数、错误数和持续时间：`http_reqs`、`http_req_failed` 和 `http_req_duration`，分别对应流量、可用性和延迟。<sup>[10](https://grafana.com/docs/k6/latest/using-k6/metrics/)</sup> 阈值把指标转化为通过标准；一个典型例子要求 90% 的请求在 400 毫秒内完成，且总体错误率保持在 1% 以下。<sup>[11](https://grafana.com/docs/k6/latest/using-k6/thresholds/?pg=load-testing-your-api-with-postman&plcmt=in-text)</sup>

## 过程

IBM 将性能测试过程描述为六个步骤。<sup>[9](https://www.ibm.com/think/topics/performance-testing)</sup>

1. **定义性能标准和需求**，为响应时间、吞吐量、资源利用率和错误率设定目标和可接受标准，并识别关键性能指标。<sup>[9](https://www.ibm.com/think/topics/performance-testing)</sup>
2. **规划并设计测试**，选择工具、测试脚本和技术，并设置基于代码的质量门禁，以允许或拒绝进入后续生产步骤。<sup>[9](https://www.ibm.com/think/topics/performance-testing)</sup>
3. **建立测试环境**，准确模拟真实生产环境，将待测应用部署到那里，并集成监控工具以捕获指标。<sup>[9](https://www.ibm.com/think/topics/performance-testing)</sup>
4. **执行测试**，运行选定的场景，同时实时监控吞吐量、响应时间和资源使用情况，以发现瓶颈。<sup>[9](https://www.ibm.com/think/topics/performance-testing)</sup>
5. **研究结果**，将收集到的数据与第一步定义的基准进行比较，找出偏差和可能的瓶颈。<sup>[9](https://www.ibm.com/think/topics/performance-testing)</sup>
6. **优化、测试、重复**，应用代码优化、资源升级或配置更改，并重新运行该流程，直到结果与基准一致。<sup>[9](https://www.ibm.com/think/topics/performance-testing)</sup>

微软在这一循环中增加了两种做法。测试应使用真实世界的使用模式和数据特征，包括多样的数据集、输入随机化，以及大型数据载荷、复杂查询和高并发等边缘情况。它还建议采用假设驱动的实验，例如对同一组件的两个版本运行相同的负载测试，并对照各层的预算检查其结果，以区分缓慢的 API 响应、缓慢的数据库查询或错误激增。<sup>[4](https://learn.microsoft.com/en-us/azure/well-architected/performance-efficiency/performance-test)</sup>

## 工作负载模型

负载本身的设计很重要。Gatling 的文档区分了两种工作负载模型：在封闭系统中，并发用户数有上限，因此新用户只有在另一用户退出时才能进入，就像所有话务员都忙的呼叫中心，或在达到容量上限时让用户排队的票务网站；在开放系统中没有这种控制，即使应用难以为用户提供服务，用户仍会不断到达，就像大多数网站那样。<sup>[5](https://docs.gatling.io/testing-concepts/workload-models/)</sup> 对实际上属于开放系统的系统使用封闭工作负载模型会产生误导性结果：当系统变慢时，响应时间和行程时间增加，并发用户数上升，虚拟用户注入速度减慢以匹配那个想象出来的上限，因此测试测量的是真实系统并不具备的行为。<sup>[5](https://docs.gatling.io/testing-concepts/workload-models/)</sup> 当测试目标是模拟已知的请求速率时，到达率执行器能更直接地表达这一速率。<sup>[12](https://grafana.com/blog/how-to-generate-real-world-load-tests-using-grafana-cloud-k6-and-production-telemetry/)</sup>

## 自动化与环境

自动化把性能测试从被动活动转变为可重复的习惯，提高覆盖率，并在软件开发生命周期中更早发现问题。<sup>[6](https://grafana.com/docs/k6/latest/testing-guides/automated-performance-testing/)</sup> 并非所有性能测试都属于 CI/CD 流水线：完整策略可以结合 CI/CD 测试、基于 cron 的测试、云调度和手动测试。<sup>[6](https://grafana.com/docs/k6/latest/testing-guides/automated-performance-testing/)</sup> 由于负载测试通常运行 3 到 15 分钟或更久，把它们加入流水线会显著延长发布过程，Grafana 建议在专门的预发布环境中规划一天或数天的性能测试，而不是在部署流水线中运行更大的测试。<sup>[6](https://grafana.com/docs/k6/latest/testing-guides/automated-performance-testing/)</sup>

环境选择取决于测试类型：负载测试使用预发布或类生产环境，压力测试使用专门的性能测试环境，耐久性测试使用具有完整资源分配的类生产环境。<sup>[4](https://learn.microsoft.com/en-us/azure/well-architected/performance-efficiency/performance-test)</sup> 在生产环境中测试能提供其他环境无法提供的真实洞察，但需要谨慎处理测试数据并避免影响真实用户。<sup>[6](https://grafana.com/docs/k6/latest/testing-guides/automated-performance-testing/)</sup> Grafana 还警告说，当测试脚本、服务级目标或标准有误时，通过/失败质量门禁可能制造虚假的安心感，并建议在验证过程成熟之前，把门禁结果视为触发更深入调查的警告。<sup>[6](https://grafana.com/docs/k6/latest/testing-guides/automated-performance-testing/)</sup>

## 工具

负载生成工具在脚本模型和测量方式上各不相同；对 JMeter、Locust、Gatling 和 k6 运行同一负载测试进行比较，重点在于理解每个工具测量什么、结果意味着什么，而不是给它们排名。<sup>[7](https://blog.octoperf.com/open-source-load-testing-tools-comparative-study/)</sup> 性能测试也以更小的规模存在于单元测试框架中：Apple（Apple）的 XCTest（XCTest）提供性能测试方法，其默认 `measure(_:)` 行为以秒为单位记录被测代码所花费的时间，并在低于可接受基线时报告回归，同时给出最近值的图表以及每个记录指标的均值。<sup>[13](https://developer.apple.com/documentation/xcode/writing-and-running-performance-tests)</sup>

## References

1. [Performance Testing Best Practices Guide, Paragon Application Systems](https://alm.parasoft.com/hubfs/whitepaper-Performance-Testing-Best-Practices-Guide.pdf)
2. [ISO/IEC 25010:2011, Systems and software Quality Requirements and Evaluation (SQuaRE), ISO Online Browsing Platform](https://www.iso.org/obp/ui/?_escaped_fragment_=iso%3Astd%3Aiso-iec%3A25010%3Aed-1%3Av1%3Aen)
3. [A phased approach for performance engineering in the AWS Cloud, AWS Prescriptive Guidance](https://docs.aws.amazon.com/pdfs/prescriptive-guidance/latest/performance-engineering-aws/performance-engineering-aws.pdf)
4. [Architecture Strategies for Performance Testing, Microsoft Azure Well-Architected Framework](https://learn.microsoft.com/en-us/azure/well-architected/performance-efficiency/performance-test)
5. [Workload models: open and closed workload models for load testing, Gatling documentation](https://docs.gatling.io/testing-concepts/workload-models/)
6. [Automated performance testing, Grafana k6 documentation](https://grafana.com/docs/k6/latest/testing-guides/automated-performance-testing/)
7. [Open source Load Testing tools comparative study, OctoPerf](https://blog.octoperf.com/open-source-load-testing-tools-comparative-study/)
8. [Performance Testing Best Practice Guide for Payment Systems, Paragon Application Systems](https://www.paragonedge.com/hubfs/2024%20Resources/Performance%20Testing%20-%20Best%20Practice%20Guide/Paragon%20Best%20Practice%20Guide%20-%20Performance%20Testing%20for%20Payment%20Systems.pdf?hsCtaAttrib=181639973295)
9. [What is Performance Testing?, IBM](https://www.ibm.com/think/topics/performance-testing)
10. [Metrics, Grafana k6 documentation](https://grafana.com/docs/k6/latest/using-k6/metrics/)
11. [Thresholds, Grafana k6 documentation](https://grafana.com/docs/k6/latest/using-k6/thresholds/?pg=load-testing-your-api-with-postman&plcmt=in-text)
12. [How to generate real-world load tests using Grafana Cloud k6 and production telemetry, Grafana Labs](https://grafana.com/blog/how-to-generate-real-world-load-tests-using-grafana-cloud-k6-and-production-telemetry/)
13. [Writing and running performance tests, Apple Developer Documentation](https://developer.apple.com/documentation/xcode/writing-and-running-performance-tests)

---
*Initially written Oct 15, 2026*

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

Copyright 2026 Edgepedia

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

Source: https://www.edgepedia.org/zh/performance-testing

English original: https://www.edgepedia.org/performance-testing
