aiwg
Version:
Deployment tool and support utility for AI context. Copies agents, skills, commands, rules, and behaviors into the paths each AI platform reads (Claude Code, Codex, Copilot, Cursor, Warp, OpenClaw, and 6 more) so one source of truth works across 10 platfo
163 lines • 5.01 kB
JavaScript
/**
* @file performance-monitor.ts
* @description Real-time performance monitoring and alerting
*
* Implements F-011/UC-011: Performance Monitoring
* - Real-time performance tracking
* - Alerting on threshold violations
* - Performance profiling
* - Bottleneck detection
* - Resource monitoring (CPU, memory, disk)
*
* @implements NFR-PERF-001: <1% performance overhead
* @implements NFR-PERF-002: Alert latency <5s
* @implements NFR-PERF-003: 99.9% uptime
*/
import { EventEmitter } from 'events';
// ============================================================================
// Performance Monitor Class
// ============================================================================
export class PerformanceMonitor extends EventEmitter {
config;
samples;
monitoring;
monitoringTimer;
constructor(config = {}) {
super();
this.config = {
sampleInterval: config.sampleInterval || 1000,
alertCallback: config.alertCallback || (() => { }),
thresholds: config.thresholds || this.getDefaultThresholds()
};
this.samples = [];
this.monitoring = false;
}
/**
* Start monitoring
*/
start() {
if (this.monitoring)
return;
this.monitoring = true;
this.monitoringTimer = setInterval(() => {
this.collectSample();
}, this.config.sampleInterval);
}
/**
* Stop monitoring
*/
stop() {
this.monitoring = false;
if (this.monitoringTimer) {
clearInterval(this.monitoringTimer);
}
}
/**
* Collect performance sample
*/
collectSample() {
const sample = {
timestamp: new Date(),
cpu: this.getCPUUsage(),
memory: this.getMemoryUsage(),
responseTime: 0,
throughput: 0,
errorRate: 0
};
this.samples.push(sample);
this.checkThresholds(sample);
this.emit('sample', sample);
}
/**
* Get CPU usage (not implemented - returns 0)
*/
getCPUUsage() {
return 0;
}
/**
* Get memory usage
*/
getMemoryUsage() {
const used = process.memoryUsage().heapUsed;
return used / 1024 / 1024; // Convert to MB
}
/**
* Check thresholds and emit alerts
*/
checkThresholds(sample) {
for (const threshold of this.config.thresholds) {
const value = this.getSampleValue(sample, threshold.metric);
if (value >= threshold.critical) {
this.emitAlert(threshold.metric, value, threshold.critical, 'critical');
}
else if (value >= threshold.warning) {
this.emitAlert(threshold.metric, value, threshold.warning, 'warning');
}
}
}
/**
* Get sample value for metric
*/
getSampleValue(sample, metric) {
switch (metric) {
case 'cpu': return sample.cpu;
case 'memory': return sample.memory;
case 'responseTime': return sample.responseTime;
case 'throughput': return sample.throughput;
case 'errorRate': return sample.errorRate;
default: return 0;
}
}
/**
* Emit performance alert
*/
emitAlert(metric, value, threshold, severity) {
const alert = {
timestamp: new Date(),
metric,
value,
threshold,
severity,
message: `${metric} ${severity}: ${value} exceeds threshold ${threshold}`
};
this.emit('alert', alert);
this.config.alertCallback(alert);
}
/**
* Get default thresholds
*/
getDefaultThresholds() {
return [
{ metric: 'cpu', warning: 70, critical: 90, unit: '%' },
{ metric: 'memory', warning: 1024, critical: 2048, unit: 'MB' },
{ metric: 'responseTime', warning: 500, critical: 1000, unit: 'ms' },
{ metric: 'errorRate', warning: 1, critical: 5, unit: '%' }
];
}
/**
* Get recent samples
*/
getSamples(count = 100) {
return this.samples.slice(-count);
}
/**
* Get performance statistics
*/
getStatistics() {
if (this.samples.length === 0) {
return { avgCPU: 0, avgMemory: 0, avgResponseTime: 0, samples: 0 };
}
const totals = this.samples.reduce((acc, sample) => ({
cpu: acc.cpu + sample.cpu,
memory: acc.memory + sample.memory,
responseTime: acc.responseTime + sample.responseTime
}), { cpu: 0, memory: 0, responseTime: 0 });
return {
avgCPU: totals.cpu / this.samples.length,
avgMemory: totals.memory / this.samples.length,
avgResponseTime: totals.responseTime / this.samples.length,
samples: this.samples.length
};
}
}
//# sourceMappingURL=performance-monitor.js.map