echoes/frontend/services/performance-monitor.ts

54 lines
1.5 KiB
TypeScript
Raw Normal View History

// 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);
}
}