压力测试
一句话解释
压力测试将负载增加到超预期限制,找到系统何时以及如何崩溃——以及是否恢复。
用大白话说
压力测试回答:「超过容量时会发生什么?」它故意将系统推超设计限制来找到断点。压力测试揭示什么:1. 断点——系统失败的确切负载
- 失败模式——如何失败(优雅降级 vs 崩溃)
- 恢复——负载降低后是否恢复?4. 瓶颈——什么先失败(数据库连接、内存、CPU)
- 错误处理——错误是否被优雅处理?流程:1. 从预期负载开始
- 逐步增加到超预期(2x、5x、10x)
- 持续直到系统崩溃
- 观察失败行为和错误消息
- 降低负载并检查系统是否恢复常见发现:
- 数据库连接池耗尽
- 内存溢出错误
- 线程池饱和
- 级联故障
- 超时级联工具:k6、JMeter、Locust、Gatling。
示例
javascript
// k6 stress test — push beyond limits
import http from 'k6/http';
import { check } from 'k6';
export const options = {
stages: [
{ duration: '1m', target: 500 }, // 5x normal load
{ duration: '2m', target: 1000 }, // 10x normal load
{ duration: '2m', target: 2000 }, // 20x normal load
{ duration: '1m', target: 5000 }, // 50x normal load — expect failure
{ duration: '3m', target: 0 }, // ramp down — check recovery
],
};
export default function () {
const res = http.get('https://api.example.com/products');
check(res, {
'status is 200': (r) => r.status === 200,
'status is 503': (r) => r.status === 503, // expected at high load
});
}
// Run: k6 run stress-test.js
// Watch for:
// - When does error rate spike?
// - What's the last successful throughput?
// - Does the system recover when load drops?压力测试将负载增加到 50 倍正常。观察错误何时开始、系统如何失败(503 优雅 vs 崩溃)、以及负载降低时是否恢复。
常见混淆
容易混淆的对象: load testing
关键区别: 负载测试验证系统处理预期流量——测试正常条件。压力测试推超预期限制找断点——测试故障条件。负载测试关注满足要求;压力测试关注理解限制和故障行为。
相关术语
相关资源
相关课程
- Performance Testing
压力测试。
解读错误信息
用大白话解释常见错误——什么意思、为什么发生、如何修复。