UNPKG

traceperf

Version:

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

116 lines 3.94 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.PerformanceMonitor = void 0; const timing_1 = require("../utils/timing"); const uuid_1 = require("uuid"); /** * Performance monitor for tracking execution times and memory usage */ class PerformanceMonitor { /** * Create a new PerformanceMonitor instance * * @param options - Monitor options */ constructor(options = {}) { var _a; this._timers = new Map(); this._defaultThreshold = (_a = options.defaultThreshold) !== null && _a !== void 0 ? _a : 100; // ms } /** * Start timing an operation * * @param label - Label for the operation * @returns A unique identifier for the timing operation */ startTimer(label) { const id = `${label}-${(0, uuid_1.v4)()}`; this._timers.set(id, (0, timing_1.getHighResTime)()); return id; } /** * End timing an operation * * @param id - The identifier returned by startTimer * @returns The duration in milliseconds * @throws Error if the timer doesn't exist */ endTimer(id) { const startTime = this._timers.get(id); if (!startTime) { throw new Error(`Timer with id ${id} not found`); } const duration = (0, timing_1.getDuration)(startTime); this._timers.delete(id); return duration; } /** * Check if an operation exceeds the performance threshold * * @param duration - The duration in milliseconds * @param threshold - The threshold to check against * @returns Whether the operation is considered slow */ isBottleneck(duration, threshold) { const effectiveThreshold = threshold !== null && threshold !== void 0 ? threshold : this._defaultThreshold; return duration > effectiveThreshold; } /** * Get memory usage information * * @returns Memory usage object */ getMemoryUsage() { try { return process.memoryUsage(); } catch (e) { console.warn('Unable to track memory usage:', e); return undefined; } } /** * Calculate memory difference between current and start memory * * @param start - Starting memory usage snapshot * @returns Memory difference or undefined */ getMemoryDiff(start) { try { const current = this.getMemoryUsage(); if (!current) return 0; const diff = current.heapUsed - start.heapUsed; // Return zero instead of negative values // Negative values usually indicate garbage collection, not actual memory release return Math.max(0, diff); } catch (e) { console.warn('Error calculating memory usage:', e); return 0; } } /** * Generate a suggestion for a bottleneck * * @param functionName - The name of the function * @param duration - The duration in milliseconds * @param memoryUsage - Memory usage in bytes * @returns A suggestion for improving performance */ generateSuggestion(functionName, duration, memoryUsage) { // Simple heuristic-based suggestions if (memoryUsage && memoryUsage > 10 * 1024 * 1024) { return `🛠 Potential Fix: Check for memory leaks or large object allocations in ${functionName}`; } if (duration > 1000) { return `🛠 Potential Fix: Consider optimizing or adding caching to ${functionName}`; } if (duration > 500) { return `🛠 Potential Fix: Look for blocking operations in ${functionName}`; } return `🛠 Potential Fix: Review ${functionName} for performance optimizations`; } } exports.PerformanceMonitor = PerformanceMonitor; //# sourceMappingURL=performance.js.map