//#region src/modules/performance.d.ts /** * 性能测试结果接口 * * @public */ interface PerformanceResult { /** * 执行时间(毫秒) */ duration: number; /** * 内存使用情况(如果可用) */ memory?: { /** 使用的JS堆大小(字节) */usedJSHeapSize: number; /** 总的JS堆大小(字节) */ totalJSHeapSize: number; /** JS堆大小限制(字节) */ jsHeapSizeLimit: number; }; /** * 函数返回值 */ result: unknown; /** * 执行次数 */ iterations: number; } /** * 性能测试选项接口 * * @public */ interface PerformanceTestOptions { /** * 执行次数 * @defaultValue 1 */ iterations?: number; /** * 是否收集内存信息 * @defaultValue true */ collectMemory?: boolean; /** * 预热次数(在正式测试前执行的次数,用于JIT优化) * @defaultValue 0 */ warmupIterations?: number; /** * 是否在每次迭代前强制垃圾回收(仅在支持的环境中有效) * @defaultValue false */ forceGC?: boolean; /** * 时间限制(毫秒) * 当设置时间限制时,测试将在达到时间限制或完成所有迭代时停止 * @defaultValue undefined */ timeLimit?: number; } /** * 性能检测器类 * * 提供测试JavaScript代码片段或方法执行性能的功能,支持多次迭代测试、 * 内存使用监控、预热执行等高级特性。 * * @example * ```typescript * const perf = new PerformanceMonitor() * * // 测试函数性能 * const result = await perf.measureFunction(() => { * return Array.from({ length: 1000 }, (_, i) => i * 2) * }) * console.log(`执行时间: ${result.duration}ms`) * * // 测试异步函数性能 * const asyncResult = await perf.measureFunction(async () => { * await new Promise(resolve => setTimeout(resolve, 100)) * return 'done' * }) * * // 多次迭代测试 * const iterResult = await perf.measureFunction( * () => Math.random(), * { iterations: 1000, warmupIterations: 100 } * ) * * // 比较两个函数的性能 * const comparison = await perf.compare( * () => [1, 2, 3].map(x => x * 2), * () => [1, 2, 3].forEach((x, i, arr) => arr[i] = x * 2), * { iterations: 10000 } * ) * ``` * * @public * @since 1.0.0 */ declare class PerformanceMonitor { /** * 测试函数执行性能 * * @param fn - 要测试的函数 * @param options - 测试选项 * @returns - 性能测试结果 * * @example * ```typescript * const perf = new PerformanceMonitor() * * // 基本测试 * const result = await perf.measureFunction(() => { * return Array.from({ length: 1000 }, (_, i) => i * 2) * }) * * // 带选项的测试 * const result2 = await perf.measureFunction( * () => someExpensiveOperation(), * { * iterations: 100, * warmupIterations: 10, * collectMemory: true, * forceGC: true * } * ) * ``` */ measureFunction(fn: () => T | Promise, options?: PerformanceTestOptions): Promise; /** * 比较两个函数的性能 * * @param fn1 - 第一个函数 * @param fn2 - 第二个函数 * @param options - 测试选项 * @returns - 包含两个函数性能结果的对象 * * @example * ```typescript * const perf = new PerformanceMonitor() * * const comparison = await perf.compare( * () => [1, 2, 3].map(x => x * 2), * () => { * const result = [] * for (const x of [1, 2, 3]) { * result.push(x * 2) * } * return result * }, * { iterations: 10000 } * ) * * console.log(`函数1: ${comparison.fn1.duration}ms`) * console.log(`函数2: ${comparison.fn2.duration}ms`) * console.log(`性能差异: ${comparison.ratio.toFixed(2)}x`) * ``` */ compare(fn1: () => T1 | Promise, fn2: () => T2 | Promise, options?: PerformanceTestOptions): Promise<{ fn1: PerformanceResult; fn2: PerformanceResult; ratio: number; faster: 'fn1' | 'fn2'; }>; /** * 批量测试多个函数的性能 * * @param functions - 要测试的函数数组 * @param options - 测试选项 * @returns - 所有函数的性能结果数组 * * @example * ```typescript * const perf = new PerformanceMonitor() * * const results = await perf.benchmark([ * () => [1, 2, 3].map(x => x * 2), * () => [1, 2, 3].forEach((x, i, arr) => arr[i] = x * 2), * () => { * const result = [] * for (const x of [1, 2, 3]) { * result.push(x * 2) * } * return result * } * ], { iterations: 10000 }) * * results.forEach((result, index) => { * console.log(`函数${index + 1}: ${result.duration}ms`) * }) * ``` */ benchmark(functions: Array<() => T | Promise>, options?: PerformanceTestOptions): Promise; /** * 测试代码字符串的性能 * * @param code - 要测试的代码字符串 * @param options - 测试选项 * @returns - 性能测试结果 * * @example * ```typescript * const perf = new PerformanceMonitor() * * const result = await perf.measureCode(` * const arr = Array.from({ length: 1000 }, (_, i) => i) * return arr.reduce((sum, x) => sum + x, 0) * `, { iterations: 100 }) * ``` */ measureCode(code: string, options?: PerformanceTestOptions): Promise; /** * 获取内存使用信息 * * @returns - 内存信息对象,如果不支持则返回undefined * * @internal */ private getMemoryInfo; /** * 格式化性能结果为可读字符串 * * @param result - 性能测试结果 * @returns - 格式化的字符串 * * @example * ```typescript * const perf = new PerformanceMonitor() * const result = await perf.measureFunction(() => someFunction()) * console.log(perf.formatResult(result)) * ``` */ formatResult(result: PerformanceResult): string; /** * 创建性能报告 * * @param results - 性能测试结果数组 * @param labels - 可选的标签数组 * @returns - 格式化的性能报告 * * @example * ```typescript * const perf = new PerformanceMonitor() * const results = await perf.benchmark([fn1, fn2, fn3]) * const report = perf.createReport(results, ['Map', 'ForEach', 'For Loop']) * console.log(report) * ``` */ createReport(results: PerformanceResult[], labels?: string[]): string; } /** * 创建性能监控器实例的便捷函数 * * @returns - 新的性能监控器实例 * * @example * ```typescript * const perf = createPerformanceMonitor() * const result = await perf.measureFunction(() => someFunction()) * ``` * * @public */ declare function createPerformanceMonitor(): PerformanceMonitor; /** * 快速测试函数性能的便捷函数 * * @param fn - 要测试的函数 * @param options - 测试选项 * @returns - 性能测试结果 * * @example * ```typescript * const result = await measurePerformance(() => { * return Array.from({ length: 1000 }, (_, i) => i * 2) * }, { iterations: 100 }) * console.log(`执行时间: ${result.duration}ms`) * ``` * * @public */ declare function measurePerformance(fn: () => T | Promise, options?: PerformanceTestOptions): Promise; /** * 快速比较两个函数性能的便捷函数 * * @param fn1 - 第一个函数 * @param fn2 - 第二个函数 * @param options - 测试选项 * @returns - 比较结果 * * @example * ```typescript * const comparison = await comparePerformance( * () => [1, 2, 3].map(x => x * 2), * () => [1, 2, 3].forEach((x, i, arr) => arr[i] = x * 2) * ) * console.log(`${comparison.faster === 'fn1' ? '第一个' : '第二个'}函数更快`) * ``` * * @public */ declare function comparePerformance(fn1: () => T1 | Promise, fn2: () => T2 | Promise, options?: PerformanceTestOptions): Promise<{ fn1: PerformanceResult; fn2: PerformanceResult; ratio: number; faster: 'fn1' | 'fn2'; }>; //#endregion export { PerformanceMonitor, PerformanceResult, PerformanceTestOptions, comparePerformance, createPerformanceMonitor, measurePerformance };