nodejs await 并发性能测试/负载测试

利用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);

你可能感兴趣的:(nodejs,await,async,压力测试)