在Linux中進行Node.js項目的性能測試,可以使用多種工具和方法。以下是一些常用的性能測試工具和方法:
top
、htop
、vmstat
、iostat
等實時監控系統資源情況。wrk -t12 -c400 -d30s http://localhost:3000/
這條命令會使用12個線程,模擬400個并發連接,持續30秒對 http://localhost:3000
進行壓力測試。
const { performance } = require('perf_hooks');
const start = performance.now(); // 記錄開始時間
// 執行一些操作
for (let i = 0; i < 1000000; i++) {
// 模擬一些計算密集型操作
}
const end = performance.now(); // 記錄結束時間
console.log(`Time taken: ${end - start} milliseconds`);
通過上述工具和方法,可以對Node.js應用在Linux系統上進行全面的性能測試和分析,從而找出性能瓶頸并進行優化。