traceperf
Version:
High-performance function execution tracking and monitoring for Node.js
112 lines • 3.77 kB
JavaScript
import { getHighResTime, getDuration } from '../utils/timing';
import { v4 as uuidv4 } from 'uuid';
/**
* Performance monitor for tracking execution times and memory usage
*/
export 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}-${uuidv4()}`;
this._timers.set(id, 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 = 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`;
}
}
//# sourceMappingURL=performance.js.map