Benchmark Asynchronous Code (Benchmark.js, Node.js)

I would like to use the Benchmark.js module to test asynchronous code written in node.js. In particular, I want to run ~ 10,000 requests to two servers (one of them is written in node, one is written in PHP) and keep track of how long it takes for each server to complete all the requests.

I planned to write a simple node script to run these queries using Benchmark, but I'm a bit confused about how to use it with asynchronous code. Usually in node modules there is some kind of callback that you call when your asynchronous code is completed, or a promise is returned from a function, etc. But with Benchmark, from everything I read in the docs, t seems to generally handle asynchronous mode.

Does anyone know what I should do or watch? I can write the test manually if necessary; it just looks like a fairly common case where Benchmark or others may have already implemented it in their professional-grade test libraries.

Thanks for any direction, ~ Nate

+4
source share
1

, 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()
+4

Source: https://habr.com/ru/post/1599624/


All Articles