@0j3/bench-js 中文文档教程
bench.js
nodejs 中的基准测试工具
Installing
yarn add @0j3/bench-js
Usage
要使用 bench.js,首先要初始化它:
const benchjs = new (require("bench-js"))(true); // The first argument in the constructor of bench.js (the "true") specifies, if benchjs.finish (see below) should output anything
然后,首先创建一个“任务”,您的程序在其中执行您想要进行基准测试的事情(您可以有无限的任务,并且您应该做任何需要大量时间的事情时间有一个任务),通过
const task = benchjs.createTask("Sample Task Name");
在你的代码开头使用。
要启动任务(指定它何时开始执行),请使用
task.begin();
重要提示:不要调用 Task.start
,因为它是一个指定何时开始的整数!
要在任务完成时告诉 Bench.JS,请使用
task.finish();
要告诉 Bench.JS 任务出错,请使用
task.error(new Error("Error Message Here"));
在代码末尾,您必须添加
benchjs.finish(0);
0 是可选的,它指定退出代码。
当运行完成时,bench.js 将退出你的程序,一旦它完成输出基准数据,所以在这之后你没有任何其他运行是很重要的。
Bench.JS Sample Output
取自 测试文件
===== BENCHMARK FINISHED =====
Task Node (#0) took 0.10054s
Task App (#1) took 0.02517s
Task Sample Task 1 (#2) took 0.00005s
Task Sample Task 2 (#3) took 0.00005s
Task Sample Task 3 (#4) took 0.00004s
Task Sample Task 4 (#5) DNF | Error (See C:\PATH/TO/BENCHJS/errors/21.9.2020-18.33.5/Sample-Task-4-id-5.err.yml)
Task Sample Task 5 (#6) DNF
Task Finish writing to error files (#7) took 0.0146s
==============================
Extending Bench.JS
如果您想扩展 bench.js,请使用
const BenchOriginal = require("benchjs");
class Bench extends BenchOriginal {
constructor(outputResults = true) {
super.constructor(outputResults); // call original constructor
}
}
并添加您想要的任何内容。
如果您还想扩展任务类,
const BenchOriginal = require("benchjs");
class Task extends BenchOriginal.Task {
constructor(taskName, id, parent) {
super.constructor(taskName, id, parent); // call original constructor
}
}
class Bench extends BenchOriginal {
constructor(outputResults = true) {
super.constructor(outputResults); // call original constructor
super.TaskClass = Task;
}
}
请从那里使用和添加您想要的任何内容。
bench.js
a benchmarking tool in nodejs
Installing
yarn add @0j3/bench-js
Usage
To use bench.js, start by initializing it:
const benchjs = new (require("bench-js"))(true); // The first argument in the constructor of bench.js (the "true") specifies, if benchjs.finish (see below) should output anything
Then, start by creating a "Task" where your program does something you want to benchmark (you can have infinite tasks, and you should make anything that takes lots of time have a task), by using
const task = benchjs.createTask("Sample Task Name");
at the beginning of you code.
To start the task (specify when it's execution starts), use
task.begin();
IMPORTANT: DO NOT CALL Task.start
AS THAT IS AN INTEGER SPECIFYING WHEN IT STARTED!
To tell Bench.JS when a task is finished, use
task.finish();
To tell Bench.JS that a task has errored, use
task.error(new Error("Error Message Here"));
At the end of your code, you must add
benchjs.finish(0);
The 0
is optional, and specifies the exit code.
When running finish, bench.js will exit your program, once its done outputting the benchmark data, so it is important that you have nothing else running after this.
Bench.JS Sample Output
Taken from the Tests File
===== BENCHMARK FINISHED =====
Task Node (#0) took 0.10054s
Task App (#1) took 0.02517s
Task Sample Task 1 (#2) took 0.00005s
Task Sample Task 2 (#3) took 0.00005s
Task Sample Task 3 (#4) took 0.00004s
Task Sample Task 4 (#5) DNF | Error (See C:\PATH/TO/BENCHJS/errors/21.9.2020-18.33.5/Sample-Task-4-id-5.err.yml)
Task Sample Task 5 (#6) DNF
Task Finish writing to error files (#7) took 0.0146s
==============================
Extending Bench.JS
If you want to extend bench.js, use
const BenchOriginal = require("benchjs");
class Bench extends BenchOriginal {
constructor(outputResults = true) {
super.constructor(outputResults); // call original constructor
}
}
and add whatever you want from there.
If you want to extend the task class aswell, use
const BenchOriginal = require("benchjs");
class Task extends BenchOriginal.Task {
constructor(taskName, id, parent) {
super.constructor(taskName, id, parent); // call original constructor
}
}
class Bench extends BenchOriginal {
constructor(outputResults = true) {
super.constructor(outputResults); // call original constructor
super.TaskClass = Task;
}
}
and add whatever you want from there.