Gathering detailed insights and metrics for buffalo-bench
Gathering detailed insights and metrics for buffalo-bench
A benchmarking library that supports async hooks and benchmarks by default.
npm install buffalo-bench
Typescript
Module System
Node Version
NPM Version
73.7
Supply Chain
99
Quality
75.5
Maintenance
100
Vulnerability
100
License
TypeScript (77.43%)
JavaScript (22.57%)
Total Downloads
35,960
Last Day
16
Last Week
24
Last Month
163
Last Year
13,575
5 Stars
14 Commits
3 Watching
6 Branches
1 Contributors
Latest Version
2.0.0
Package Id
buffalo-bench@2.0.0
Unpacked Size
117.11 kB
Size
30.00 kB
File Count
10
NPM Version
8.12.1
Node Version
18.4.0
Cumulative downloads
Total Downloads
Last day
1,500%
16
Compared to previous day
Last week
100%
24
Compared to previous week
Last month
-36.6%
163
Compared to previous month
Last year
15.9%
13,575
Compared to previous year
A benchmarking library that supports async hooks and benchmarks by default.
This library comes from the problem of handling async hooks in a way that is compatible with benchmarking.
The problem is that async hooks are not supported by Benchmark.js. See the foot notes.
For example, the following code will not work as expected:
1 new Benchmark('test', async () => { 2 await doSomething(); 3 }, { 4 async: true, 5 async onStart() { 6 console.log(1); 7 await new Promise(resolve => setTimeout(resolve, 1000)); 8 console.log(2); 9 } 10 })
The previous code will log 1
and then run the benchmark, and the log 2
could be logged before the benchmark is finished or couldn't be logged at all.
This problem prevent us to create an async onStart and/or onComplete for a benchmark like an api call that requires opening a db connection, creating a collection, adding a document and launching a server to handle the request. And after the benchmark finishes clear the databese, close the connection and stop the server.
So, BuffaloBench solves this problem by providing a way to create a benchmarks with all the hooks handled as async by default. (Also works with sync functions)
1const { Benchmark } = require('buffalo-bench'); 2 3// Create a benchmark only with name and function 4const bench = new Benchmark("name", async () => {}); 5 6// Create a benchmark with name, function and options 7const bench = new Benchmark("name", async () => {}, options); 8 9// Create a benchmark with name and options 10const bench = new Benchmark("name", {fn: async () => {}, ...options}); 11 12// Run the benchmark 13await bench.run();
1const { Benchmark } = require('buffalo-bench'); 2 3// Create a benchmark with all the options 4const bench = new Benchmark('myBenchmark', { 5 maxTime: 5, // In seconds 6 minSamples: 1, 7 beforeEach: async () => { 8 await doSomething(); 9 }, 10 afterEach: async () => { 11 await doSomething(); 12 }, 13 before: async () => { 14 await doSomething(); 15 }, 16 after: async () => { 17 await doSomething(); 18 }, 19 onError: async (error) => { 20 await doSomething(); 21 }, 22 fn: async () => { 23 await doSomething(); 24 } 25}); 26 27// Run the benchmark 28await bench.run();
1const { Benchmark } = require('buffalo-bench'); 2 3let suite = new Benchmark.Suite("String comparison", { 4 beforeEach(benchmark) { 5 console.log(`${this.name}: ${benchmark.name}: Start`); 6 }, 7 afterEach(benchmark) { 8 console.log(`${this.name}: ${benchmark.name}: End`); 9 } 10}); 11suite.add("Direct comparison", () => "Hello World!" === "Hello World!"); 12suite.add("Regexp comparison", () => new RegExp("Hello World!").test("Hello World!")); 13suite.add("IndexOf comparison", () => "Hello World!".indexOf("Hello World!")); 14suite.add("Complex comparison", () => { 15 let str = "Hello World!"; 16 let str2 = "Hello World!"; 17 let l = str.length; 18 str.length === str2.length && str[0] === str2[0] && str[l - 1] === str2[l - 1] && str === str2; 19}); 20 21await suite.run(); 22 23// String comparison: Direct comparison: Start 24// String comparison: Direct comparison: End 25// String comparison: Regexp comparison: Start 26// String comparison: Regexp comparison: End 27// String comparison: IndexOf comparison: Start 28// String comparison: IndexOf comparison: End 29// String comparison: Complex comparison: Start 30// String comparison: Complex comparison: End 31 32let result = suite.compareFastestWithSlowest('percent'); 33console.log(result.fastest.name + " is faster than " + result.slowest.name + " by " + result.by + "%"); 34// Direct comparison is faster than Regexp comparison by 281.47%
You can get this library as a Node.js module available through the npm registry:
1// With npm 2$ npm install buffalo-bench 3// With yarn 4$ yarn add buffalo-bench
Or you can use it standalone in the browser with:
<script src="https://cdn.jsdelivr.net/npm/buffalo-bench"></script>
The Benchmark
constructor takes an options
object argument with the following properties:
maxTime
: The maximum time in seconds that a benchmark can take including hooks.minSamples
: The minimum number of samples that must be taken.beforeEach
: A function to be run once before each benchmark loop, does not count for run time.afterEach
: A function to be run once after each benchmark loop, does not count for run time.before
: A function to be run once before the benchmark loop starts, does not count for run time.after
: A function to be run once after the benchmark loop finishes, does not count for run time.onError
: A function to be run if an error occurs.fn
: The function to be run.The Benchmark
instance has the following properties:
name
: The name of the benchmark.error
: The error object if an error occurred.cycles
: The number of cycles performed.samples
: The number of samples taken.hz
: The number of cycles per second.meanTime
: The meanTime time per cycle.medianTime
: The medianTime time per cycle.standardDeviation
: The standard deviation.maxTime
: The maximum time.minTime
: The minimum time.times
: An array of times for each cycle.options
: The options object passed to the constructor.stamp
: A timestamp representing when the benchmark was created.runTime
: The total time taken to run the benchmark, this does not include beforeEach, afterEach, before and after hooks.totalTime
: The total time taken to run the benchmark including beforeEach, afterEach, before and after hooks.The Benchmark
instance has the following methods:
run()
: Async method that runs the benchmark.toJSON()
: Return a JSON representation of the benchmark.compareWith(other: Benchmark, compareBy: CompareBy)
: Compare this benchmark to the other benchmark and return a number representing the difference of the CompareBy
metric between the two benchmarks.The Benchmark
class has the following static properties:
version
: A string containing the library version.defaults
: An object containing the default options.Suite
: A class that represents a suite of benchmarks.The Suite
constructor takes a name
and an options
object argument with the following properties:
maxTime
: The maximum time in seconds that a benchmark can take including hooks.minSamples
: The minimum number of samples that must be taken.beforeEach
: A function to be run once before each benchmark run, does not count for run time.afterEach
: A function to be run once after each benchmark run, does not count for run time.before
: A function to be run once before the benchmark run starts, does not count for run time.after
: A function to be run once after the benchmark run finishes, does not count for run time.onError
: A function to be run if an error occurs.The Suite
instance has the following properties:
name
: The name of the suite.error
: The error object if an error occurred.options
: The options object passed to the constructor.stamp
: A timestamp representing when the suite was created.runTime
: The total time taken to run the suite, this does not include beforeEach, afterEach, before and after hooks.totalTime
: The total time taken to run the suite including beforeEach, afterEach, before and after hooks.benchmarks
: An array of the benchmarks in the suite.The Suite
instance has the following methods:
add(name: string, optionsOrFn: BenchmarkOptions | Function, options?: BenchmarkOptions)
: Add a benchmark to the suite.getSortedBenchmarksBy(sortedBy: CompareBy)
: Get the benchmarks sorted by a given CompareBy
metric.getFastest(sortedBy: CompareBy)
: Get the fastest benchmark in the suite sorting by the given CompareBy
metric.getSlowest(sortedBy: CompareBy)
: Get the slowest benchmark in the suite sorting by the given CompareBy
metric.compareFastestWithSlowest(compareBy: CompareBy)
: Compare the fastest benchmark with the slowest benchmark sorting by the given CompareBy
metric.run
: Async method that runs the suite.toJSON
: Return a JSON representation of the suite.The CompareBy
enum has the following values:
meanTime
: Compare by the mean time per cycle.medianTime
: Compare by the median time per cycle.standardDeviation
: Compare by the standard deviation.maxTime
: Compare by the maximum time.minTime
: Compare by the minimum time.hz
: Compare by the number of cycles per second.runTime
: Compare by the total time taken to run the suite.cycles
: Compare by the number of cycles.percent
: Compare by the percentage of cycles that were slower than the fastest benchmark.If the beforeEach
afterEach
before
after
onError
returns a Promise, the benchmark will wait for the promise to resolve before continuing.
If the beforeEach
function throws an error, the benchmark will stop and emit an BeforeEachError
event.
If the afterEach
function throws an error, the benchmark will stop and emit an AfterEachError
event.
If the fn
function throws an error, the benchmark will stop and emit an RunError
event.
If the after
function throws an error, the benchmark will stop and emit an AfterError
event.
If the before
function throws an error, the benchmark will stop and emit an BeforeError
event.
If the onError
function throws an error, the benchmark will stop and emit an FatalError
event.
This errors will be found in the error
property of the benchmark instance.
When converting to JSON, the errorMessage
property will be a string containing the error message.
If you want to write your benchmarks with typescript, you must install the ts-node
library and require in your project the ts-node/register
file.
Example:
1require('ts-node/register'); 2require('./my-benchmark.ts');
1import { Benchmark } from 'buffalo-bench/lib'; 2 3const bench = new Benchmark('myBenchmark', () => {}); 4(async () => { 5 await bench.run(); 6 console.log(bench.toJSON()); 7})();
yarn dev
to watch and compile the library on every change to it running the index.ts benchmark in the tests folder.yarn build
to build the library.yarn commit
to commit your changes.Author: Masquerade Circus. License Apache-2.0
No vulnerabilities found.
No security vulnerabilities found.