Debian下进行JS性能测试的方法和工具如下:
perf_hooks:精确测量代码执行时间、延迟等指标,如performance.now()。Benchmark.js:评估代码性能,支持自定义测试场景。wrk/Autocannon:模拟高并发HTTP请求,测试服务器性能。heapdump/v8-profiler:分析内存使用,定位泄漏问题。pm2:监控应用进程状态、CPU/内存占用。NetData:实时监控系统性能指标。# 安装常用工具
sudo apt install nodejs npm apache2-utils wrk
npm install benchmark heapdump
perf_hooks测量函数执行时间:const { performance } = require('perf_hooks');
const start = performance.now();
// 待测试代码
const end = performance.now();
console.log(`耗时:${end - start}ms`);
wrk):wrk -t12 -c400 -d30s http://localhost:3000 # 12线程,400并发,持续30秒
Benchmark.js):node benchmark.js
参考来源: