返回顶部

Benchmark 简介

Benchmark

基准测试

安装

npm install benchmark

github

API

Benchmark.Suite

基准测试套件

每个套件内部可以使用包装后的lodash方法,例如 each/forEach, indexOf, map, and reduce

语法

Benchmark.Suite(name, [, options={}])
  • name

    suite(套件)名称

  • options

    配置项,每一项都是一个事件监听器

示例

var suite = new Benchmark.Suite('foo', {
    // called when the suite starts running
    'onStart': onStart,

    // called between running benchmarks
    'onCycle': onCycle,

    // called when aborted
    'onAbort': onAbort,

    // called when a test errors
    'onError': onError,

    // called when reset
    'onReset': onReset,

    // called when the suite completes running
    'onComplete': onComplete
});

options

export interface Options {
    async?: boolean;
    defer?: boolean;
    delay?: number;
    id?: string;
    initCount?: number;
    maxTime?: number;
    minSamples?: number;
    minTime?: number;
    name?: string;
    onAbort?: Function;
    onComplete?: Function;
    onCycle?: Function;
    onError?: Function;
    onReset?: Function;
    onStart?: Function;
    setup?: Function | string;
    teardown?: Function | string;
    fn?: Function | string;
    queued?: boolean;
}

#prototype.add

为基准测试套件添加测试用例

语法

add(name, fn [, options={}])
  • name

    测试用例名称

  • fn

测试用例

示例

var suite = new Benchmark.Suite;

// basic usage
suite.add(fn);

// or using a name first
suite.add('foo', fn);

// or with options
suite.add('foo', fn, {
  'onCycle': onCycle,
  'onComplete': onComplete
});

// or name and options
suite.add('foo', {
  'fn': fn,
  'onCycle': onCycle,
  'onComplete': onComplete
});

// or options only
suite.add({
  'name': 'foo',
  'fn': fn,
  'onCycle': onCycle,
  'onComplete': onComplete
});

#prototype.on

在指定事件上注册监听器,并返回Suite实例

语法

on(type, listener)
  • type

    事件类型,包括 cyclestart cyclecomplete

  • listener

    监听器

示例

// register a listener for an event type
suite.on('cycle', listener);

// register a listener for multiple event types
suite.on('start cycle', listener);

#prototype.run

运行基准测试,并返回Suite实例

语法

run([options={}])

应用

'use strict'

const Benchmark = require('benchmark');
const suite = new Benchmark.Suite;

suite
    .add('let store = {}', function () {
    let store = {}
    store.key = 'value'
})
    .add('let store = new Map()', function () {
    let store = new Map()
    store.set('key', 'value')
})
    .add('let store = Object.create(null)', function () {
    let store = Object.create(null)
    store.key = 'value'
})
// 每个测试跑完后,输出信息
    .on('cycle', function (event) {
    console.log(String(event.target));
})
    .on('complete', function () {
    console.log('Fastest is ' + this.filter('fastest').map('name'));
})
// run async
    .run({ 'async': true });

/**
测试用例名称 / 每秒调用次数 / 方差 / 抽样数
let store = {} x 743,774,795 ops/sec ±21.96% (85 runs sampled)
let store = new Map() x 22,559,544 ops/sec ±0.70% (95 runs sampled)
let store = Object.create(null) x 28,966,959 ops/sec ±1.27% (88 runs sampled)
Fastest is let store = {}
*/
posted @ 2021-04-18 11:23  usmile  阅读(583)  评论(0编辑  收藏  举报