基准asynchronous代码(Benchmark.js,Node.js)

我想使用Benchmark.js模块来testing在node.js中编写的一些asynchronous代码。 具体来说,我想向两台服务器(一个用节点编写,一个用PHP编写)发送大约10,000个请求,并跟踪每台服务器完成所有请求所需的时间。

我打算编写一个简单的节点脚本来使用Benchmark来触发这些请求,但是我对如何在asynchronous代码中使用它有点困惑。 通常在节点模块中,当你的asynchronous代码完成时,你会调用一些callback函数,或者从函数中返回Promise等。但是对于基准testing,从我在文档中读到的所有东西,来处理asynchronous。

有谁知道我应该做什么或看什么? 如果需要,我可以手动编写基准; 它似乎是一个常见的用例,Benchmark或其他人可能已经在其专业级testing库中实现了它。

谢谢你的任何指示,〜Nate

这不是很好的文件,但这是一个PoC:

var Benchmark = require('benchmark'); var suite = new Benchmark.Suite(); suite.add(new Benchmark('foo', { // a flag to indicate the benchmark is deferred defer : true, // benchmark test function fn : function(deferred) { setTimeout(function() { deferred.resolve(); }, 200); } })).on('complete', function() { console.log(this[0].stats); }).run(); 

Benchmark.js v2会稍微改变语法:

 var Benchmark = require('benchmark'); var suite = new Benchmark.Suite; suite.add('foo', { defer: true, fn: function (deferred) { setTimeout(function() { deferred.resolve(); }, 200); } }).on('complete', function () { console.log(this[0].stats) }).run()