在Linux環境中對Node.js進行性能測試,可以使用多種工具和方法。以下是一些常用的工具和步驟:
ab -n 1000 -c 10 http://localhost:3000/
wrk -t12 -c400 -d30s http://localhost:3000
const { performance } = require('perf_hooks');
const start = performance.now();
// 執行測試代碼
const end = performance.now();
console.log(`Execution time: ${end - start} ms`);
const profiler = require('v8-profiler-node8');
const profile = profiler.takeSnapshot();
profile.export((error, result) => {
if (error) throw error;
require('fs').writeFileSync('profile.json', JSON.stringify(result));
// 使用speedscope等工具分析profile.json
});
sysbench cpu --threads=4 --events=10000 --time=0 run
./Run
通過上述工具和方法,可以有效地對Linux環境下的Node.js進行性能測試和調優,確保應用程序在高負載下仍能保持高效運行。