Code Monkey home page Code Monkey logo

leizm-benchmark's Introduction

@leizm/benchmark

简单性能测试框架,支持预热和多进程执行(针对异步任务)。

安装

npm i @leizm/benchmark -S

用法

import Benchmark from "@leizm/benchmark";

const b = new Benchmark({ title: "Benchmark Example" });
b.addAsync("async task", async () => await sleep(0))
  .addCallback("callback task", done => sleep(0).then(done))
  .addSync("sync task", () => 1 + 1)
  .addSyncFaster("sync faster task", count => {
    for (let i = 0; i < count; i++) {
      // ... 针对性能损坏极小的同步任务,使用 for 来循环 count 次
    }
  })
  .runAndPrint();

function sleep(ms: number): Promise<number> {
  return new Promise(resolve => {
    setTimeout(resolve, ms);
  });
}

支持四种任务类型:

  • addAsync(title, async () => void) 添加 async function,需要返回 Promise
  • addCallback(title, (callback) => void) 添加异步回调函数
  • addSync(title, () => void) 添加阻塞函数
  • addSyncFaster(title, (count) => void) 添加阻塞函数,函数内部通过 for 循环运算

注意:addAsync()addCallback() 中的任务必须是真正的异步函数,否则可能造成进程卡死

选项

export interface Options {
  /** 标题 */
  title: string;
  /** 每个任务执行时长(秒) */
  seconds: number;
  /** 并发数量(异步任务时有效) */
  concurrent: number;
  /** 每个任务间隔(秒) */
  delay: number;
  /** 预热时间(秒) */
  preheat: number;
  /** 是否开启多进程,默认不开启 */
  clusterMode: boolean;
  /** 子进程数量,默认为CPU总数 */
  clusterCount: number;
}

License

MIT License

Copyright (c) 2018 Zongmin Lei <[email protected]>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

leizm-benchmark's People

Contributors

leizongmin avatar renovate-bot avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

leizm-benchmark's Issues

建议增加预热功能

如果函数执行前没有进行预热(包括连接池连接的预热,或者函数JIT的预热,会对结果产生影响)

见下图:

image

preheatget without rest 执行的代码是一致的。但是结果差距有点大,可能是一开始 Redis 连接没有建立等原因导致

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.