@xiaolaa2/ableton-copilot-mcp
Version:
Ableton Live MCP depend on Ableton JS
100 lines • 3.29 kB
JavaScript
import { logger } from '../main.js';
/**
* Performance monitoring tool for tracking execution time and performance metrics of various operations
*/
class PerformanceMonitor {
metrics = new Map();
slowThreshold;
constructor(slowThresholdMs = 500) {
this.slowThreshold = slowThresholdMs;
}
/**
* Decorator factory for monitoring method performance
* @param category Operation category
* @returns Decorator function
*/
trackTime(category) {
return (target, propertyKey, descriptor) => {
const originalMethod = descriptor.value;
descriptor.value = async function (...args) {
const start = performance.now();
try {
return await originalMethod.apply(this, args);
}
finally {
const end = performance.now();
const executionTime = end - start;
// Collect performance metrics
PerformanceMonitor.instance.recordMetric(category, executionTime);
// Log slow operations
if (executionTime > PerformanceMonitor.instance.slowThreshold) {
logger.warn(`Slow operation detected: ${category} took ${executionTime.toFixed(2)}ms`);
}
}
};
return descriptor;
};
}
/**
* Record performance metric
* @param category Operation category
* @param time Execution time (milliseconds)
*/
recordMetric(category, time) {
const existing = this.metrics.get(category);
if (existing) {
existing.count++;
existing.totalTime += time;
existing.minTime = Math.min(existing.minTime, time);
existing.maxTime = Math.max(existing.maxTime, time);
}
else {
this.metrics.set(category, {
count: 1,
totalTime: time,
minTime: time,
maxTime: time
});
}
}
/**
* Get summary of all performance metrics
* @returns Performance metrics summary
*/
getMetricsSummary() {
const summary = {};
this.metrics.forEach((metric, category) => {
summary[category] = {
count: metric.count,
avgTime: metric.totalTime / metric.count,
minTime: metric.minTime,
maxTime: metric.maxTime
};
});
return summary;
}
/**
* Log current performance metrics
*/
logMetrics() {
const summary = this.getMetricsSummary();
logger.info('Performance metrics:', JSON.stringify(summary, null, 2));
}
/**
* Reset all performance metrics
*/
resetMetrics() {
this.metrics.clear();
}
// Singleton instance
static _instance;
static get instance() {
if (!this._instance) {
this._instance = new PerformanceMonitor();
}
return this._instance;
}
}
export default PerformanceMonitor;
export const trackTime = PerformanceMonitor.instance.trackTime.bind(PerformanceMonitor.instance);
//# sourceMappingURL=performance-monitor.js.map