基准异步代码(Benchmark.js,Node.js)

ope*_*kie 8 javascript asynchronous node.js benchmark.js

我想使用Benchmark.js模块来测试一些用node.js编写的异步代码。具体来说,我想向两台服务器(一个用节点编写,一个用PHP编写)发出约10,000个请求,并跟踪每个服务器完成所有请求所花费的时间。

我打算编写一个简单的节点脚本以使用Benchmark触发这些请求,但是我对如何将其与异步代码一起使用感到有些困惑。通常在节点模块中,当异步代码完成或从函数等返回Promise时,会调用某种回调。但是使用Benchmark,从我在文档中读取的所有内容来看,似乎没有处理异步。

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

谢谢你的指示,〜Nate

rob*_*lep 8

它的文档记录不是很好,但是这里有一个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();
Run Code Online (Sandbox Code Playgroud)

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()
Run Code Online (Sandbox Code Playgroud)