2015-11-11 17 views
0

当组合针包async.auto,当有大量并发的性能会降低要求当组合针包async.auto,性能会时有很多并发请求的

鉴于码降级下面:

router.get('/test', function(req, res) { 
    async.auto({ 
     step1: function(cb) { 
      console.log('step1'); 
      cb(); 
     }, 
     step2: ['step1', function(cb) { 
      needle.get('https://www.google.com', function(err, response, body) { 
       console.log('needle return'); 
      }); 
      cb(); 
     }] 
    }, function(err) { 
     res.json({ 
      'return': 'here' 
     }); 
    }); 
}); 

我使用下面的命令:

AB -n 100 -c 25 https://test.server.com/test

(注:我的测试服务器是唯一的单核CPU,1GB RAM,运行的Node.js和nginx的)

大部分的API响应时间是500毫秒 - 900毫秒

但是,如果我已经改变了代码:

router.get('/test', function(req, res) { 
    async.series([ 
     function(cb) { 
      console.log('step a'); 
      cb(); 
     }, 
     function(cb) { 
      needle.get('https://www.google.com', function(err, response, body) { 
       console.log('needle return'); 
      }); 
      console.log('step b'); 
      cb(); 
      res.json({ 
       'return': 'here' 
      }); 
     } 
    ]); 
}); 

,或者如果我已经改变了代码:

router.get('/test', function(req, res) { 
    async.auto({ 
     step1: function(cb) { 
      console.log('step1'); 
      cb(); 
     }, 
     step2: ['step1', function(cb) { 
      request('https://www.google.com', function(err, response, body) { 
       console.log('http request return'); 
      }); 
      cb(); 
     }] 
    }, function(err) { 
     res.json({ 
      'return': 'here' 
     }); 
    }); 
}); 

大部分的API响应时间10ms的是 - 50ms的,偶尔有些要求是100毫秒 - 150毫秒

确实注意到了性能上的差异仅仅是显而易见的,当有大量并发请求到node.js的(因此AB基准)

不知道async.auto并不意味着与针一起工作? 还是有其他我忽略的问题?

我使用:

+0

为什么你立即调用'cb()',而不是'needle'完成时? – Bergi

+0

在我的应用程序中,我只需要将数据发布到另一台服务器,我不需要等待来自http响应的数据。但即使我在针尖完成时放置cb(),问题仍然是一样的。 – forestclown

回答

相关问题