/** * Memory profiler for detecting unbounded growth under sustained load. * * Samples process memory (RSS, heap) at configurable intervals and applies * linear regression to determine whether RSS is growing over time. A stable * system shows a near-zero slope after the initial warmup period. * * @module diagnostics/memory-profiler */ /** A single point-in-time memory measurement. */ export interface MemorySample { timestamp: number; rss: number; heapUsed: number; heapTotal: number; external: number; arrayBuffers: number; } /** Summary of a profiling session. */ export interface MemoryProfile { samples: MemorySample[]; durationMilliseconds: number; peakRss: number; averageRss: number; } /** Result of a stability analysis. */ export interface StabilityResult { /** Whether RSS growth rate is within the acceptable threshold. */ stable: boolean; /** Estimated RSS growth in bytes per second (from linear regression). */ rssGrowthRatePerSecond: number; /** The threshold used for comparison (bytes/sec). */ thresholdPerSecond: number; /** Number of samples analyzed (after warmup skip). */ samplesAnalyzed: number; } /** * Options for stability analysis. * * @example * ```ts * import { analyzeStability, type StabilityOptions } from '@lostgradient/weft'; * * const options: StabilityOptions = { * maxGrowthRatePerSecond: 5 * 1024, // 5 KB/s threshold * warmupSamples: 3, * }; * const result = analyzeStability([], options); * console.log(result.stable); // true (no samples) * ``` */ export interface StabilityOptions { /** * Maximum acceptable RSS growth rate in bytes per second. * Default: 10 KB/sec — accounts for minor GC jitter and allocator noise. */ maxGrowthRatePerSecond?: number; /** * Number of initial samples to skip (warmup period where the runtime * JIT-compiles, allocates caches, etc.). Default: 5. */ warmupSamples?: number; } /** * Simple least-squares linear regression over (x, y) points. * Returns the slope and intercept of the best-fit line y = slope * x + intercept. * * @example * ```ts * import { linearRegression } from '@lostgradient/weft'; * * const points: [number, number][] = [ * [0, 100], [1, 105], [2, 110], [3, 115], * ]; * const { slope, intercept } = linearRegression(points); * console.log(slope); // ~5 * console.log(intercept); // ~100 * ``` */ export declare function linearRegression(points: [number, number][]): { slope: number; intercept: number; }; /** * Analyze a series of memory samples to determine if RSS is stable. * * Applies linear regression to RSS values over time (in seconds) after * skipping a configurable warmup period. The system is considered stable * if the growth rate is below the configured threshold. * * @example * ```ts * import { MemoryProfiler, analyzeStability } from '@lostgradient/weft'; * * const profiler = new MemoryProfiler(); * profiler.start(100); * await new Promise((r) => setTimeout(r, 600)); * profiler.stop(); * const { samples } = profiler.profile(); * const stability = analyzeStability(samples, { warmupSamples: 2 }); * console.log(stability.stable); * ``` */ export declare function analyzeStability(samples: MemorySample[], options?: StabilityOptions): StabilityResult; /** * Interval-based memory profiler. Call {@link start} to begin sampling and * {@link stop} when the workload is done. Use {@link profile} to retrieve * the collected samples and summary statistics. * * @example * ```ts * import { MemoryProfiler } from '@lostgradient/weft'; * * const profiler = new MemoryProfiler(); * profiler.start(200); // sample every 200ms * await new Promise((r) => setTimeout(r, 1000)); * profiler.stop(); * const { peakRss, averageRss, samples } = profiler.profile(); * console.log('Peak RSS:', peakRss); * console.log('Samples:', samples.length); * ``` */ export declare class MemoryProfiler { #private; constructor(); /** Take a single snapshot of current process memory. */ snapshot(): MemorySample; /** Begin sampling memory at the given interval (milliseconds). */ start(intervalMilliseconds: number): void; /** Stop interval sampling. Idempotent. */ stop(): void; /** Clear all collected samples. */ reset(): void; /** Return the collected profile with summary statistics. */ profile(): MemoryProfile; }