1.0.0-alpha.3 • Published 1 year ago

funcbench v1.0.0-alpha.3

Weekly downloads
-
License
MIT
Repository
github
Last release
1 year ago

funcbench

funcbench is a TypeScript library that helps you measure the performance of functions by executing them with different inputs and sample sizes. The main goal of thid library is to provide developers with a tool for identifying performance bottlenecks and understanding the performance characteristics of their code, and to do while providing type safety for the entire lifespan.

Features

  • Profile multiple functions with different input arguments
  • Customize the number of samples per function and input
  • Calculate mean execution time, variance, and standard deviation
  • Choose between multiple time units (e.g., seconds, milliseconds and nanoseconds)
  • Chainable builder pattern for easy configuration

Installation

To use the funcbench benchmark, first install the package from npm

npm install funcbench

Usage

First import the benchmark function from the library:

import { benchmark } from 'funcbench';

To create a new benchmark, call the benchmark function and chain the builder methods to configure it:

const addNumbers = (a: number, b: number) => a + b

const myBenchmark = benchmark<[number, number]>()
    .addFuncs(addNumbers)
    .addInputs({ name: '0 and 1', args: [0, 1] })
    .addSamples(100)
    .setUnits('ms');

You can also pass an optional configuration object to the benchmark function for initial settings:

const myBenchmark = benchmark<[number, number]>({
    functions: [addNumbers],
    inputs: [{ name: '0 and 1', args: [0, 1] }],
    samples: [100],
    units: 's'
});

Then, run the benchmark to get the results:

const results = myBenchmark.run();

The results variable will contain an array of performance statistics for each function, input, and sample size combination.

API Reference

benchmark<Args>(options?: BuilderOptions<Args>)

Creates a new Benchmark object. The optional BuilderOptions object can be used to set the initial configuration:

BuilderOptions<Args>

Property NameTypeDescription
functionsArray<Function>The functions to profile.
inputsArray<{ name: string, args: Args }>The input arguments for each function. Each input must be given a name. This will appear in the output to make the results more human readable.
samplesArray<number>The number of samples to run for each function and input combination.
units'ns' | 'ms' | 's'The time units to use for the results (e.g., 'ms' for milliseconds). Defaults to 'ms'

BenchmarkBuilder

The builder builder exposes several chainable methods for configuring your test:

Method NameParametersDescription
addFuncsfuncs: Function \| Function[]Add one or more functions to the benchmark.
addInputsinputs: Input<Args> \| Input<Args>[]Add one or more input argument sets to the benchmark.
addSamplessamples: number \| number[]Add one or more sample sizes to the benchmark.
removeFuncsindices: number \| number[]Remove one or more functions from the benchmark by their indices.
removeInputsindices: number \| number[]Remove one or more input argument sets from the benchmark by their indices.
removeSamplesindices: number \| number[]Remove one or more sample sizes from the benchmark by their indices.
run{ rank: boolean \| undefined, testCallback: (stats: Stats) => unknown \| unknown }Runs the benchmark and returns the results as a BenchmarkResult object. If rank is true, then the resulting stats will be sorted with priority in the following order: sample size ascending, input name alphabetic sorting and then mean execution time.
setFuncsfuncs: Function[]Set the functions to profile.
setInputsinputs: Input<Args>[]Set the input arguments for each function.
setSamplessamples: number[]Set the number of samples to run for each function and input combination.
setUnitsunits: 'ns' | 'ms' | 's'Set the time units to use for the results (e.g., 'ms' for milliseconds).

Once you have set up the benchmark how you want it to be, just call the run function and handle the result.

BenchmarkResult

An object that contains an array of Stats objects (one for each function/input/sample combination). A Stats has the following properties.

Property NameTypeDescription
funcNamestringThe name of the function passed in. If you passed in an anonymous function, this will be an empty string "".
inputNamestringThe string representing the name of the input passed in during the build process.
samplesnumberThe sample size of this dataset.
meannumberThe mean of all runs of the functions in the sample set for a given input.
sigmanumberThe standard deviation of all runs of the functions in the sample set for a given input.
sigmaSquarednumberThe variance of all runs of the functions in the sample set for a given input.

Contact

If you have any questions for me, feel free to contact me through my website.

1.0.0-alpha.3

1 year ago

1.0.0-alpha.2

1 year ago

1.0.0-alpha.1

1 year ago

0.3.1

1 year ago

0.3.0

1 year ago

0.2.0

1 year ago

0.1.3

1 year ago

0.1.2

1 year ago

0.1.1

1 year ago

0.1.0

1 year ago

0.0.1

1 year ago