在Debian下对JS项目进行性能测试,可按以下步骤使用工具和方法:
# 安装Node.js和npm
curl -fsSL https://deb.nodesource.com/setup_16.x | sudo -E bash -
sudo apt-get install -y nodejs
# 安装性能测试工具
sudo apt install apache2-utils wrk # ApacheBench和wrk
npm install -g benchmark.js artillery # Benchmark.js和Artillery
const { performance } = require('perf_hooks');
const start = performance.now();
// 待测试代码
const end = performance.now();
console.log(`耗时:${end - start}ms`);
wrk -t12 -c400 -d30s http://localhost:3000/api # 12线程,400并发,持续30秒
artillery run --output report.json test-scenario.yml
npm install heapdump
# 在代码中触发内存快照
require('heapdump').writeSnapshot('./heapdump.heapsnapshot');
--inspect标志启动应用,配合Chrome DevTools分析CPU/内存。node --inspect server.js
通过Jenkins集成性能测试,实现自动化运行和结果报告。
以上工具和方法可根据项目需求组合使用,优先通过轻量级工具定位问题,再逐步深入分析。