zoukankan      html  css  js  c++  java
  • 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 = {}
    */
    
  • 相关阅读:
    成本直降50% | 阿里云发布云原生网关,开启下一代网关新进程
    阿里云容器服务全面升级为 ACK Anywhere,让云的边界拓展至企业需要的每个场景
    云拨测助力节卡机器人 全面优化海外网站性能
    如何加速云原生数据应用?这个开源项目备受关注
    课程升级 | 极速构建知识体系,即学即用 Serverless
    ECS 选款利器!PTS助您快速上云!
    Morphling:云原生部署 AI , 如何把降本做到极致?
    报名领奖|云栖大会,10月19-22日杭州不见不散!
    Dubbo3.0|阿里巴巴服务框架三位一体的选择与实践
    告别Kafka Stream,让轻量级流处理更加简单
  • 原文地址:https://www.cnblogs.com/usmile/p/14780187.html
Copyright © 2011-2022 走看看