nodejs await 併發效能測試/負載測試
阿新 • • 發佈:2019-02-15
利用Grinder對使用await的web服務做負載測試,並以只使用callback的web服務作為對照.
- 測試環境: Linux Centos虛擬機器 單核
- 測試引數:
- grinder.processes=7
- grinder.thread = 3
- grinder.runs=600
- 測試結果:callback和await在併發效能上並無顯著差異
-
callback await 平均響應時間毫秒 34.8 34.4 平均TPS 519 568 峰值TPS 637 685 CPU峰值 23% 23% 測試總次數 12600 12600
原始碼如下:
server-with-callback.js
(function() { var http, sleep; http = require('http'); sleep = function(callback) { return setTimeout(function() { return callback('wakeup'); }, 30); }; http.createServer(function(req, res) { sleep(function(msg){ res.end(msg); }); }).listen(8080); }).call(this);
server-with-await.js
(function() { var aaa, http, sleep, wrapper; http = require('http'); sleep = function(callback) { return setTimeout(function() { return callback('wakeup'); }, 30); }; http.createServer(async function(req, res) { var msg = await new Promise(function(resolve, reject){ sleep(function(msg) { resolve(msg); }); }); res.end(msg); }).listen(8080); }).call(this);