在Ubuntu中进行Node.js性能测试,可以使用多种工具和方法。以下是一些常用的性能测试工具和方法:
ab -c 100 -d 30s http://localhost:3000
这个命令会对位于http://localhost:3000
的服务器进行100个并发连接,持续30秒的负载测试。
wrk -t12 -c400 -d30s http://localhost:3000
这个命令会使用12个线程,对位于http://localhost:3000
的服务器进行400个并发连接,持续30秒的负载测试。
const { performance } = require('perf_hooks');
// 记录开始时间
const startTime = performance.now();
// 执行一些操作
for (let i = 0; i < 1000000; i++) {
// 模拟一些计算密集型操作
}
// 记录结束时间
const endTime = performance.now();
// 计算并输出执行时间
console.log(`Execution time: ${endTime - startTime} ms`);
// test.js
const add = (a, b) => a + b;
module.exports = { add };
node test.js
这个命令会运行test.js
文件中的测试用例,并输出测试结果。
通过这些工具和方法,可以对Node.js应用程序进行全面的性能测试和分析,从而找出性能瓶颈并进行优化。