gulp-bench

2019-05-16 admin

gulp-bench是什么

什么是gulp-bench,Run performance benchmark tests

gulp-bench使用教程帮助文档

gulp-bench Build Status

Run performance Benchmark tests. Ported from grunt-benchmark.

Install

$ npm install --save-dev gulp-bench

Usage

The usage syntax below is the same as for grunt-benchmark.

Basic

Setup your benchmark test, e.g. in test.js:

var fibonacci = function(n) {
  return n < 2 ? n : fibonacci(n - 1) + fibonacci(n - 2);
};

module.exports = function() {
  fibonacci(10);
};

In your gulpfile:

var gulp = require('gulp');
var benchmark = require('gulp-bench');

gulp.task('default', function () {
	return gulp.src('test.js', {read: false})
		.pipe(benchmark());
});

Run it:

$ gulp
[16:11:01] Running benchmark basic [./test-data/basic.js]...
[16:11:11]    basic x 1,107,255 ops/sec ±0.74% (96 runs sampled)

As well as outputting to console output the plugin stream returns a single file containing the test results in JSON form:

gulp.task('default', function () {
    return gulp.src('test.js', {read: false})
        .pipe(benchmark())
        .pipe(gulp.dest('.'));  /* writes a results file to current folder */   
});

You can modify the results filename and format by supplying plugin options.

Test options

Tests can be configured through futher options:

module.exports = {
  name: 'Timeout (asynchronous)',
  maxTime: 2, /* test should run for max. this no. of seconds */
  defer: true, /* indicates that test is asynchronous */
  onComplete: function() {
    console.log('Hooray!');
  },
  fn: function(deferred) {
    setTimeout(function() {
      deferred.resolve(); 
    }, 500);
  }
};

Test suites

You can compare implementations by constructing a test suite:

var Timer = require('clockmaker').Timer;

module.exports = {
  name: 'Timeout Showdown',
  maxTime: 2,
  tests: {
    'Return immediately (synchronous)': function() {
      return;
    },
    'Timeout: 50ms (asynchronous)': {
      defer: true,
      fn: function(deferred) {
        Timer(deferred.resolve, 50, { this: deferred }).start();
      }
    },
    'Timeout: 100ms (asynchronous)': {
      defer: true,
      fn: function(deferred) {
        Timer(deferred.resolve, 100, { this: deferred }).start();
      }
    }
  }
};

The expected console output for the above test suite will look similar to:

[16:11:01] Running suite Timeout Showdown [./test-data/compare.js]...
[16:11:11]    Sync x 69,826,905 ops/sec ±4.27% (37 runs sampled)
[16:11:26]    Async-50 x 19.64 ops/sec ±0.25% (40 runs sampled)
[16:11:38]    Async-100 x 9.91 ops/sec ±0.17% (23 runs sampled)
[16:11:40] Fastest test is Sync at 3,555,514.2x faster than Async-50

The tests within a suite can also be specified as an array:

var Timer = require('clockmaker').Timer;

module.exports = {
  name: 'Timeout Showdown',
  maxTime: 2,
  tests: [
    {
        name: 'Return immediately (synchronous)',
        fn: function() {
            return;
        }
    },
    {
        name: 'Timeout 50ms (asynchronous)',
        defer: true,
        fn: function(deferred) {
            Timer(deferred.resolve, 50, { this: deferred }).start();
        }
    },
    {
        name: 'Timeout 100ms (asynchronous)',
        defer: true,
        fn: function(deferred) {
            Timer(deferred.resolve, 100, { this: deferred }).start();
        }
    },
  ]
};

Plugin Options

output

Type: String Default: benchmark-results.json

Specifies the name of the file in which to write test results.

outputFormat

Type: String Default: json Values: csv, json

Specifies the format for the output file.

License

MIT - see LICENSE.md

你可能感兴趣的文章:

本站文章除注明转载外,均为本站原创或编译。欢迎任何形式的转载,但请务必注明出处。

转载请注明:文章转载自 JavaScript中文网 [https://www.javascriptcn.com]

本文地址:https://www.javascriptcn.com/read-65097.html

文章标题:gulp-bench

回到顶部