性能测试
一句话解释
性能测试测量速度和响应性——加载时间、延迟、FPS——确保应用满足性能要求。
用大白话说
性能测试测量应用响应速度。与负载测试(在流量下测试)不同,性能测试关注单个用户体验到的速度。关键指标:1. 页面加载时间——从导航到完全加载的时间
- 可交互时间(TTI)——页面变为可交互的时刻
- 首次内容绘制(FCP)——首个内容出现的时刻
- API 延迟——单个 API 调用的响应时间
- 每秒帧数(FPS)——动画/渲染流畅度
- Core Web Vitals——LCP、FID、CLS(Google 关键指标)
类型:1. 前端性能——页面加载、渲染、JavaScript 执行
- 后端性能——API 响应时间、数据库查询速度
- 网络性能——带宽、延迟、CDN 效果工具:Lighthouse、WebPageTest、Chrome DevTools、k6、Artillery。流程:测量基线 → 设定目标(如 LCP < 2.5s)→ 优化 → 重新测量 → 迭代。
示例
javascript
// Lighthouse performance audit (Node.js)
import lighthouse from 'lighthouse';
import chromeLauncher from 'chrome-launcher';
async function runPerfAudit(url) {
const chrome = await chromeLauncher.launch();
const result = await lighthouse(url, {
port: chrome.port,
onlyCategories: ['performance'],
});
await chrome.kill();
const metrics = result.lhr.audits;
console.log({
score: result.lhr.categories.performance.score * 100,
FCP: metrics['first-contentful-paint'].numericValue, // ms
LCP: metrics['largest-contentful-paint'].numericValue, // ms
TTI: metrics['interactive'].numericValue, // ms
CLS: metrics['cumulative-layout-shift'].numericValue,
speedIndex: metrics['speed-index'].numericValue, // ms
});
// Assert thresholds
expect(metrics['largest-contentful-paint'].numericValue).toBeLessThan(2500);
expect(metrics['cumulative-layout-shift'].numericValue).toBeLessThan(0.1);
}Lighthouse 测量 Core Web Vitals:LCP(最大内容绘制 < 2.5s)、CLS(布局偏移 < 0.1)、FID(首次输入延迟 < 100ms)。这些直接影响用户体验和 SEO 排名。
常见混淆
容易混淆的对象: load testing
关键区别: 性能测试测量单个用户的速度和响应性——页面加载时间、API 延迟、渲染 FPS。负载测试测量系统处理多并发用户的能力。性能测试关于速度;负载测试关于容量。两者都需要:一个用户快且多用户快。
相关术语
相关资源
相关课程
- Performance Testing
性能测试概述。
解读错误信息
用大白话解释常见错误——什么意思、为什么发生、如何修复。