Javascript 将针头包与async.auto结合使用时,当存在大量并发请求时,性能会降低
将针头包与async.auto结合使用时,当存在大量并发请求时,性能会降低 鉴于以下代码:Javascript 将针头包与async.auto结合使用时,当存在大量并发请求时,性能会降低,javascript,node.js,asynchronous,Javascript,Node.js,Asynchronous,将针头包与async.auto结合使用时,当存在大量并发请求时,性能会降低 鉴于以下代码: router.get('/test', function(req, res) { async.auto({ step1: function(cb) { console.log('step1'); cb(); }, step2: ['step1', function(cb) { n
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
(注意:我的测试服务器只有单核cpu、1GB RAM、运行node.js和nginx)
大多数API响应时间为500ms-900ms
但如果我将代码更改为:
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'
});
});
});
或者如果我已将代码更改为:
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,偶尔一些请求为100ms-150ms
请注意,只有当对node.js有大量并发请求时,性能差异才会明显(因此采用ab基准测试)
不确定async.auto是否不打算与针一起工作?
还是我忽略了其他一些问题
我正在使用:
- node.js v0.10.37
- Ubuntu Linux(14.x)
- 这是针头包装作者的回答
为什么要立即调用
cb()
,而不是在针完成时调用???在我的应用程序中,我只需要将数据发布到另一台服务器,不需要等待来自http响应的数据。但即使我在打完针后放入cb(),问题仍然是一样的。