echoes/frontend/services/performance-monitor.ts

54 lines
1.5 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// services/performance-monitor.ts
/**
* 性能指标接口
*/
interface PerformanceMetrics {
loadTime: number; // 加载时间(毫秒)
memoryUsage: number; // 内存使用量bytes
errors: string[]; // 错误信息列表
}
/**
* 性能监控服务
*/
export class PerformanceMonitor {
private static metrics: Map<string, PerformanceMetrics> = new Map();
/**
* 开始监控性能
* @param id 监控对象的唯一标识符
*/
static startMonitoring(id: string) {
const startTime = performance.now();
const startMemory = (performance as any).memory?.usedJSHeapSize || 0;
return {
// 结束监控并记录指标
end: () => {
const loadTime = performance.now() - startTime;
const memoryUsage = ((performance as any).memory?.usedJSHeapSize || 0) - startMemory;
this.metrics.set(id, {
loadTime,
memoryUsage,
errors: []
});
},
// 记录错误信息
error: (err: string) => {
const metrics = this.metrics.get(id) || { loadTime: 0, memoryUsage: 0, errors: [] };
metrics.errors.push(err);
this.metrics.set(id, metrics);
}
};
}
/**
* 获取性能指标
* @param id 监控对象的唯一标识符
*/
static getMetrics(id: string): PerformanceMetrics | undefined {
return this.metrics.get(id);
}
}