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

前端 未结 2 592
感情败类
感情败类 2021-01-17 17:36

I\'d like to use the Benchmark.js module to test some asynchronous code written in node.js. Specifically, I want to fire ~10,000 requests to two servers (one written in nod

2条回答
  •  借酒劲吻你
    2021-01-17 18:17

    It's not very well documented, but here's a 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 slightly changes the syntax:

    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()
    

提交回复
热议问题