import type { BenchmarkReport, BenchmarkOptions } from './types.js'; import type { Logger, ILogObj } from 'tslog'; export type BenchmarkFunction = (arg?: unknown) => unknown | Promise; /** * A simple benchmarking library for JavaScript. * * The `Benchmarker` class is responsible for running a benchmark on a given function and collecting * statistical data about its performance. It handles warm-up iterations, adaptive inner iterations, * outlier removal, and provides methods to calculate various performance metrics. */ export declare class Benchmarker { #private; /** * The name of the benchmark. * This is used to identify the benchmark in reports and logs. */ protected name: string; /** * Holds the timing results of the benchmark. * Each value in this array represents the average time (in milliseconds) taken for one iteration * of the benchmarked function during a single measurement. */ protected results: number[]; /** * The function to be benchmarked. * This can be either a synchronous function (`() => void`) or an asynchronous function * (`() => Promise`). */ protected fn: BenchmarkFunction; /** * The maximum execution time for the benchmark in milliseconds. * The benchmark will stop after this time has elapsed, even if the maximum number of * iterations (`maxIterations`) has not been reached. */ protected maxExecutionTime: number; /** * The number of warmup iterations. * Warmup iterations are run before the actual measurements to allow the JavaScript engine to * optimize the code. These iterations are not included in the final results. */ protected warmupIterations: number; /** * The initial number of inner iterations to run per benchmark iteration. * The actual number of inner iterations might be adjusted adaptively based on the `timeThreshold`. */ protected innerIterations: number; /** * The maximum value that the adaptive innerIterations can reach. * This prevents the inner loop from becoming too large for very fast functions. */ protected maxInnerIterations: number; /** * The target minimum time (in milliseconds) for the inner loop to execute. * If the inner loop completes faster than this threshold, the number of inner iterations * is doubled, up to `maxInnerIterations`, to improve measurement accuracy. */ protected timeThreshold: number; /** * The minimum number of samples to keep after removing outliers. * This ensures that there are enough samples for statistical analysis. */ protected minSamples: number; /** * The maximum number of iterations to run. * This is used to accurately measure the performance of the function. * The benchmark will try to run the function for this number of iterations. * If the function is too slow, the `maxExecutionTime` will end the benchmark after the set time. */ protected maxIterations: number; /** * Logger instance for logging messages. * This is used to log warnings and errors during the benchmark process. */ protected logger: Logger; /** * Creates a new Benchmarker instance. * * @param name - The name of the benchmark. * @param fn - The function to benchmark. * @param opts - The benchmark run options. * @param logger - The logger to use. */ constructor(name: string, fn: () => unknown | Promise, opts?: BenchmarkOptions, logger?: Logger); /** * Runs the benchmark. * * This method performs the following steps: * 1. **Warm-up:** Runs the benchmark function a specified number of times (`warmupIterations`) to * allow the JavaScript engine to optimize the code. * 2. **Adaptive Inner Iterations:** Determines a suitable number of `innerIterations` to ensure that * each measurement takes at least `timeThreshold` milliseconds. This is done to reduce the * impact of timer resolution and overhead. * 3. **Main Benchmark Loop:** Runs the benchmark for a fixed number of iterations (`maxIterations`) * or until the `maxExecutionTime` is reached. In each iteration, the benchmark function is run * `innerIterations` times, and the average time per iteration is recorded. * 4. **Outlier Removal:** Removes outliers from the collected results using the IQR method. * * @param passValue A value to be passed to the benchmark function. It is primarily used with the Suite * integration, were the before/before group/before group benchmark function are called and can potentially * prepare a value for the execute benchmark function. * @throws Will throw an error if the benchmark function throws an error during any iteration. */ run(passValue?: unknown): Promise; /** * Removes outliers from the results using the IQR (Interquartile Range) method. * * Outliers are values that fall below Q1 - 1.5 * IQR or above Q3 + 1.5 * IQR, where Q1 and Q3 * are the first and third quartiles, respectively, and IQR is the interquartile range (Q3 - Q1). * * This method is called automatically at the end of the `run()` method. */ protected removeOutliers(): void; /** * Calculates the average time per iteration. * * Each captured result is the average time taken for one iteration of the benchmarked function: * * ```typescript * const iterationTime = (endTime - startTime) / this.innerIterations * ``` * * Therefore, `iterationTime` represents the average time (in milliseconds) taken to execute the benchmark function * once during that particular iteration of the main loop. * * The `iterationTime` is captured in the `this.results` array during the main benchmark loop. * * @returns The average time per iteration in milliseconds. */ protected getAverageTime(): number; /** * Calculates the standard deviation of the iteration times. * * @returns The standard deviation of the iteration times in milliseconds. */ protected getStandardDeviation(): number; /** * Calculates the number of operations per second. * * @returns The number of operations per second. */ protected getOperationsPerSecond(): number; /** * Gets the relative margin of error (RME). * * The RME is calculated as the margin of error divided by the average time. * This value indicates the precision of the benchmark results. * A lower RME indicates more precise results. * * @returns The relative margin of error (RME) as an absolute value (not a percentage). * @see {@link https://en.wikipedia.org/wiki/Relative_margin_of_error} */ protected getRME(): number; /** * Gets the number of samples. * * @returns The number of samples. */ protected getSampleSize(): number; /** * Gets the name of the benchmark. * * @returns The name of the benchmark. */ protected getName(): string; /** * Gets the results of the benchmark. * * @returns The results of the benchmark. */ protected getResults(): number[]; /** * Gets the margin of error. * * @returns The margin of error. */ protected getMarginOfError(): number; /** * Gets the standard error of the mean. * * @returns The standard error of the mean. */ protected getStandardErrorOfTheMean(): number; /** * Gets the sample variance. * * @returns The sample variance. */ protected getSampleVariance(): number; /** * Gets the median of the benchmark results. * * @returns The median of the benchmark results. */ protected getMedian(): number; /** * Generates a report with the benchmark results. * * @returns The benchmark report. */ getReport(): BenchmarkReport; } //# sourceMappingURL=benchmark.d.ts.map