UNPKG

traceperf

Version:

High-performance function execution tracking and monitoring for Node.js

193 lines (192 loc) 6.03 kB
/** * Browser-compatible version of TracePerf * * This file provides a browser-compatible version of TracePerf that can be used * in React, Next.js, or other frontend applications. */ import { ILoggerConfig, LogMode, ITrackOptions } from './types'; export declare const TrackingMode: { PERFORMANCE: string; BALANCED: string; DETAILED: string; DEBUG: string; }; /** * Browser-compatible performance monitoring */ declare class BrowserPerformanceMonitor { private _timers; private _defaultThreshold; constructor(options?: { defaultThreshold?: number; }); startTimer(label: string): string; endTimer(id: string): number; isBottleneck(duration: number, threshold?: number): boolean; /** * Get memory usage from browser performance API * @returns Memory usage object with heap metrics or undefined if not available */ getMemoryUsage(): { heapUsed: number; heapTotal: number; external: number; rss: number; } | undefined; /** * Calculate memory difference * @param start - Starting memory usage * @returns Memory difference in bytes */ getMemoryDiff(start: { heapUsed: number; }): number; now(): number; generateSuggestion(functionName: string, duration: number, memoryUsage?: number): string; } /** * Browser-compatible execution tracker for tracking function calls and generating flow charts */ declare class BrowserExecutionTracker { private _callStack; private _executions; private _currentExecution; private _defaultThreshold; private _globalScope; private _trackedFunctions; private _performanceMonitor; /** * Create a new BrowserExecutionTracker instance * * @param options - Tracker options */ constructor(options?: { defaultThreshold?: number; }); /** * Generate a performance optimization suggestion based on execution duration * * @param name - Function name * @param duration - Execution duration in ms * @returns Optimization suggestion */ private generateSuggestion; /** * Track the execution of a function * * @param fn - The function to track * @param options - Options for tracking * @returns The return value of the tracked function */ track<T>(fn: () => T, options?: any): T; /** * Create a trackable version of a function * * This is a helper method to create a tracked version of a function * that can be used for nested function tracking. * * @param fn - The function to make trackable * @param options - Options for tracking * @returns A tracked version of the function */ createTrackable<T extends (...args: any[]) => any>(fn: T, options?: Omit<ITrackOptions, 'label'> & { label?: string; }): (...args: Parameters<T>) => ReturnType<T>; /** * Get the current call stack * * @returns The current call stack */ getCallStack(): string[]; /** * Generate a visual representation of the execution flow * * @returns ASCII flow chart of the execution */ generateFlowChart(): string; /** * Prepare execution data for the flow chart * * @param executions - The execution records to prepare * @returns A list of execution data */ private prepareExecutionData; /** * Add children to the result list * * @param children - The children to add * @param result - The result list */ private addChildrenToResult; /** * Format memory size for display * @param bytes Memory usage in bytes * @returns Formatted string */ private formatMemorySize; /** * Clear all execution records */ clear(): void; } /** * Browser-compatible logger */ declare class BrowserLogger { private _mode; private _indentLevel; private _executionTracker; private _performanceMonitor; private _trackingMode; constructor(config?: Partial<ILoggerConfig>); info(message: string | object, ...args: any[]): void; warn(message: string | object, ...args: any[]): void; error(message: string | object, ...args: any[]): void; debug(message: string | object, ...args: any[]): void; group(label: string): void; groupEnd(): void; setMode(mode: LogMode): void; getMode(): LogMode; /** * Set the tracking mode * @param mode Tracking mode from TrackingMode enum * @returns This instance for chaining */ setTrackingMode(mode: string): BrowserLogger; /** * Get the current tracking mode * @returns Current tracking mode */ getTrackingMode(): string; track<T>(fn: () => T, options?: ITrackOptions): T; /** * Create a trackable version of a function * * This is a helper method to create a tracked version of a function * that can be used for nested function tracking. * * @param fn - The function to make trackable * @param options - Options for tracking * @returns A tracked version of the function */ createTrackable<T extends (...args: any[]) => any>(fn: T, options?: Omit<ITrackOptions, 'label'> & { label?: string; }): (...args: Parameters<T>) => ReturnType<T>; private log; } /** * Create a browser-compatible logger instance * @param config Configuration options * @returns Browser logger instance */ export declare function createBrowserLogger(config?: Partial<ILoggerConfig>): BrowserLogger; /** * Create a browser-compatible TracePerf instance * Alias of createBrowserLogger for API consistency with Node.js version * @param config Configuration options * @returns Browser logger instance */ export declare function createTracePerf(config?: Partial<ILoggerConfig>): BrowserLogger; declare const browserLogger: BrowserLogger; export default browserLogger; export { BrowserLogger, BrowserPerformanceMonitor, BrowserExecutionTracker };