import { type BenchmarkFunction } from './benchmark.js'; import type { Reporter } from './reporters/reporter.js'; import type { BenchmarkOptions, BenchmarkReport, SuiteReport, SuiteInit } from './types.js'; import type { Logger, ILogObj } from 'tslog'; import { SuiteConfig } from './suite_config.js'; interface BenchmarkSuiteEventMap { 'before-run': CustomEvent<{ name: string; }>; 'after-run': CustomEvent<{ name: string; report: BenchmarkReport; }>; 'error': CustomEvent<{ name: string; error: Error; }>; } interface BenchmarkSuiteEntry { /** * The type of the function to run: * * - `benchmark`: A benchmark function that will be executed * - `setup`: A setup function that will be executed before the benchmark */ type: 'benchmark'; /** * Optional group name for the benchmark. */ group?: string; /** * The name of the benchmark function. */ name: string; /** * The function to run. */ fn: BenchmarkFunction; /** * Optional benchmark options applied to this benchmark only. * When set it overrides the options set in the constructor. */ options?: BenchmarkOptions; /** * The benchmark setup function to execute before the benchmark. */ setup?: BenchmarkFunction; } /** * Defines when a reporter should be executed. */ export type ReporterExecutionTiming = 'after-each' | 'after-all'; interface SuiteReporterEntry { reporter: Reporter; } /** * Represents a suite of benchmarks. * * ## Execution flow * * When the `run()` method is called, the following steps are executed: * * 1. The reporters are initialized. * 1. The `before-run` event is dispatched. * 1. The group suite setup function is executed (if any, only for the first benchmark). * 1. The group benchmark setup function is executed (if any). * 1. The benchmark's own setup function is executed (if any). * 1. The benchmark function is executed. * 1. The report is generated. * 1. The benchmark teardown function is executed (if any). * 1. The group benchmark teardown function is executed (if any). * 1. The group suite teardown function is executed (if any, only for the last benchmark). * 1. The `after-each` reporters are called. * * @fires BenchmarkSuite#before-run - Dispatched before each benchmark is run. * @fires BenchmarkSuite#after-run - Dispatched after each benchmark has completed. * @fires BenchmarkSuite#error - Dispatched if an error occurs during a benchmark run. */ export declare class Suite extends EventTarget { /** * The benchmark execution queue. */ protected benchmarks: BenchmarkSuiteEntry[]; protected reports: BenchmarkReport[]; protected options: SuiteConfig; /** * Registered reporters for the suite. */ protected reporters: Map; /** * The global setup function to be queued in the execution queue. * The author has to call the `setup()` method to add it to the execution queue. */ protected benchmarkSetup: BenchmarkFunction | null; /** * A list of groups and their corresponding benchmark setup functions. * Keys are group names, values are setup functions. */ protected groupBenchmarkSetup: Map; /** * A list of groups and their corresponding benchmark teardown functions. * Keys are group names, values are teardown functions. */ protected groupBenchmarkTeardown: Map; /** * A list of groups and their corresponding suite setup functions. * Keys are group names, values are setup functions. */ protected groupSuiteSetup: Map; /** * A list of groups and their corresponding suite teardown functions. * Keys are group names, values are teardown functions. */ protected groupSuiteTeardown: Map; /** * The name of the benchmark suite. * This is used for logging and reporting purposes. */ protected name: string; /** * Logger instance for logging messages. * This is used to log warnings and errors during the benchmark process. */ protected logger: Logger; /** * When set to true, the benchmark will run in debug mode. * This mode is useful for debugging and development purposes. * It may slow down the benchmark execution. */ protected debug: boolean; /** * Keeps track of the first and last benchmark index for each group. */ protected groupIndex: Map; /** * It is a map of group names and their corresponding values created by the group setup function. * The values are passed to the benchmark setup function as the only argument. */ protected groupGlobalSetupValues: Map; /** * When the `setup()` method is called, the setup function is hold in here. * After the next benchmark is added, the setup function is then added as a property to the benchmark. */ protected pendingSetup?: BenchmarkFunction; /** * A flag to indicate if the configuration has been loaded. * This is used to prevent loading the configuration multiple times. */ protected configLoaded: boolean; /** * Creates a new benchmark suite. * * @param name - The name of the benchmark suite. * @param options - Options for the benchmark suite. * * @example * ```typescript * const suite = new Suite('My Suite', { maxExecutionTime: 5000 }); * ``` */ constructor(name: string, options?: SuiteInit | SuiteConfig); /** * Creates a new benchmark suite. * * @param options - Options for the benchmark suite. * * @example * ```typescript * const suite = new Suite({ maxExecutionTime: 5000 }); * ``` */ constructor(options?: SuiteInit | SuiteConfig); /** * Initializes the configuration of the suite. * This step is optional and called automatically when the suite is run. * However, if not called, the debug mode will not be enabled until the suite is run. * * @example * ```typescript * const suite = new Suite('My Suite', { maxExecutionTime: 5000 }); * await suite.load(); * ``` * @returns A promise that resolves when the configuration is loaded. */ load(): Promise; /** * Adds a benchmark to the suite. * * @param name - The name of the benchmark. * @param fn - The function to benchmark. * @param options - Optional benchmark options applied to this benchmark only. * When set it overrides the options set in the constructor. */ add(name: string, fn: BenchmarkFunction, options?: BenchmarkOptions): this; /** * Adds a benchmark to a specific group within the suite. * @param groupName - The name of the group. * @param name - The name of the benchmark. * @param fn - The function to benchmark. * @param options - Optional benchmark options applied to this benchmark only. * When set it overrides the options set in the constructor. */ group(groupName: string, name: string, fn: BenchmarkFunction, options?: BenchmarkOptions): this; /** * Adds a reporter to the suite. * @param reporter - The reporter to add. * @param timing - When the reporter should be executed. */ addReporter(reporter: Reporter, timing: ReporterExecutionTiming): this; /** * Sets the setup function for the suite. * The setup function is then called when adding the `setup()` to the execution queue. * * @example * ```typescript * suite.setSetup(() => { * // Setup code here * }); * suite * .setup() * .add('My Benchmark', () => { * // Benchmark code here * }) * .setup() * .add('My Benchmark', () => { * // Benchmark code here * }) * .run(); * ``` * @param fn - The setup function. */ setSetup(fn: BenchmarkFunction): this; /** * Adds the setup function to the execution queue. * The setup function will be executed before any benchmark. * * @param fn - Optional setup function to add to the execution queue. * If not provided, the previously set setup function will be used. * @returns The current instance of the suite for chaining. * @throws Error if no setup function is defined. * @example * ```typescript * suite * .setSetup(() => { * // Setup code here * }) * .setup() * .add('My Benchmark', () => { * // Benchmark code here * }) * .setup(() => { * // Setup for the next benchmark only * }) * .add('My Benchmark', () => { * // Benchmark code here * }) * .run(); * ``` */ setup(fn?: BenchmarkFunction | null): this; /** * Sets the suite setup function for a specific group. * * This function is executed only once before the first benchmark in the group. * It is useful for setting up resources or configurations that are shared across all benchmarks in the group. * * The value returned by this function is passed to the benchmark's group setup function as the only argument. * If the benchmark's group setup function is not defined, the value is passed to the benchmark's setup function * as the only argument. * If none of the functions are defined, the value is passed to the benchmark function as the only argument. * * @param groupName - The name of the group. * @param fn - The suite setup function. * @returns The current instance of the suite for chaining. * @throws Error if a suite setup function for the group already exists. */ setGroupSuiteSetup(groupName: string, fn: BenchmarkFunction): this; setGroupSuiteTeardown(groupName: string, fn: BenchmarkFunction): this; /** * Sets the benchmark setup function for a specific group. * * The setup function will be executed before each benchmark in the group starts and before * the benchmark's `setup()` function. * * @param groupName - The name of the group. * @param fn - The teardown function. */ setGroupBenchmarkSetup(groupName: string, fn: BenchmarkFunction): this; /** * Sets the benchmark teardown function for a specific group. * * The teardown function will be executed after each benchmark in the group have completed. * * @param groupName - The name of the group. * @param fn - The teardown function. */ setGroupBenchmarkTeardown(groupName: string, fn: BenchmarkFunction): this; /** * Runs all benchmarks in the suite. * @returns A promise that resolves when all benchmarks have completed. */ run(): Promise; protected initializeReporters(): Promise; /** * Runs the reporters for the specified timing. * @param timing - The timing for which to run the reporters. * @param data - The data to pass to the reporters. */ protected runReporters(timing: ReporterExecutionTiming, data: BenchmarkReport | SuiteReport): Promise; /** * Dispatches an event to all listeners. * @param event - The event name. * @param data - The event data. */ dispatchEvent(event: BenchmarkSuiteEventMap[K]): boolean; /** * Gets all reports. * @returns All reports. */ getReport(): SuiteReport; } export {}; //# sourceMappingURL=suite.d.ts.map