@agentkai/core
Version:
AgentKai核心包,提供AI助手系统的基础功能
66 lines (65 loc) • 1.59 kB
JavaScript
import { Logger } from './logger';
/**
* 性能监控工具,用于记录操作执行时间
*/
export class PerformanceMonitor {
constructor(moduleName) {
Object.defineProperty(this, "timers", {
enumerable: true,
configurable: true,
writable: true,
value: {}
});
Object.defineProperty(this, "logger", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
this.logger = new Logger(`${moduleName}:Performance`);
}
/**
* 开始计时
*/
start(label) {
this.timers[label] = Date.now();
}
/**
* 结束计时并返回持续时间(毫秒)
*/
end(label) {
const startTime = this.timers[label];
if (startTime === undefined) {
this.logger.warn(`计时器 ${label} 未启动`);
return 0;
}
const duration = Date.now() - startTime;
this.logger.info(`${label} 完成`, { durationMs: duration });
delete this.timers[label];
return duration;
}
/**
* 测量函数执行时间的包装器
*/
async measure(label, fn) {
this.start(label);
try {
return await fn();
}
finally {
this.end(label);
}
}
/**
* 获取活跃的计时器标签
*/
getActiveTimers() {
return Object.keys(this.timers);
}
/**
* 清除所有计时器
*/
clearTimers() {
this.timers = {};
}
}