adpa-enterprise-framework-automation
Version:
Modular, standards-compliant Node.js/TypeScript automation framework for enterprise requirements, project, and data management. Provides CLI and API for BABOK v3, PMBOK 7th Edition, and DMBOK 2.0 (in progress). Production-ready Express.js API with TypeSpe
156 lines ⢠5.16 kB
JavaScript
/**
* Performance Monitoring Utilities
* Provides timing and metrics for CLI commands
*/
// 1. Node.js built-ins
import { performance } from 'perf_hooks';
class PerformanceMonitor {
metrics = new Map();
/**
* Start timing a command
*/
startCommand(commandName) {
const id = `${commandName}-${Date.now()}-${Math.random()}`;
const startTime = performance.now();
this.metrics.set(id, {
commandName,
startTime,
success: false
});
if (process.env.DEBUG) {
console.log(`š Starting command: ${commandName}`);
}
return id;
}
/**
* End timing a command
*/
endCommand(id, success = true, errorMessage) {
const metrics = this.metrics.get(id);
if (!metrics) {
return null;
}
const endTime = performance.now();
const duration = endTime - metrics.startTime;
const memoryUsage = process.memoryUsage();
const updatedMetrics = {
...metrics,
endTime,
duration,
memoryUsage,
success,
errorMessage
};
this.metrics.set(id, updatedMetrics);
if (process.env.DEBUG) {
const status = success ? 'ā
' : 'ā';
console.log(`${status} Command ${metrics.commandName} completed in ${this.formatDuration(duration)}`);
console.log(` Memory: ${this.formatMemory(memoryUsage.heapUsed)} heap, ${this.formatMemory(memoryUsage.rss)} RSS`);
}
return updatedMetrics;
}
/**
* Get metrics for a command
*/
getMetrics(id) {
return this.metrics.get(id) || null;
}
/**
* Get all metrics
*/
getAllMetrics() {
return Array.from(this.metrics.values());
}
/**
* Generate performance report
*/
generateReport() {
const allMetrics = this.getAllMetrics().filter(m => m.duration);
if (allMetrics.length === 0) {
return 'No performance data available.';
}
const successful = allMetrics.filter(m => m.success);
const failed = allMetrics.filter(m => !m.success);
const avgDuration = successful.reduce((sum, m) => sum + (m.duration || 0), 0) / successful.length;
const totalDuration = allMetrics.reduce((sum, m) => sum + (m.duration || 0), 0);
let report = `\nš Performance Report\n`;
report += `āāāāāāāāāāāāāāāāāāāā\n`;
report += `Total Commands: ${allMetrics.length}\n`;
report += `Successful: ${successful.length}\n`;
report += `Failed: ${failed.length}\n`;
report += `Average Duration: ${this.formatDuration(avgDuration)}\n`;
report += `Total Duration: ${this.formatDuration(totalDuration)}\n\n`;
if (allMetrics.length > 0) {
report += `Command Details:\n`;
allMetrics.forEach(m => {
const status = m.success ? 'ā
' : 'ā';
report += `${status} ${m.commandName}: ${this.formatDuration(m.duration || 0)}\n`;
if (!m.success && m.errorMessage) {
report += ` Error: ${m.errorMessage}\n`;
}
});
}
return report;
}
/**
* Format duration in human-readable format
*/
formatDuration(ms) {
if (ms < 1000) {
return `${Math.round(ms)}ms`;
}
return `${(ms / 1000).toFixed(2)}s`;
}
/**
* Format memory in human-readable format
*/
formatMemory(bytes) {
const mb = bytes / 1024 / 1024;
return `${mb.toFixed(1)}MB`;
}
/**
* Clear all metrics
*/
clear() {
this.metrics.clear();
}
}
// Global performance monitor instance
export const performanceMonitor = new PerformanceMonitor();
/**
* Decorator for measuring command performance
*/
export function measurePerformance(commandName) {
return function (target, propertyName, descriptor) {
const method = descriptor.value;
descriptor.value = (async function (...args) {
const id = performanceMonitor.startCommand(commandName);
try {
const result = await method.apply(this, args);
performanceMonitor.endCommand(id, true);
return result;
}
catch (error) {
performanceMonitor.endCommand(id, false, error instanceof Error ? error.message : 'Unknown error');
throw error;
}
});
return descriptor;
};
}
/**
* Simple function wrapper for performance measurement
*/
export async function withPerformanceTracking(commandName, fn) {
const id = performanceMonitor.startCommand(commandName);
try {
const result = await fn();
performanceMonitor.endCommand(id, true);
return result;
}
catch (error) {
performanceMonitor.endCommand(id, false, error instanceof Error ? error.message : 'Unknown error');
throw error;
}
}
//# sourceMappingURL=performance.js.map