Files
esengine/packages/editor-app/src/services/ProfilerService.ts

371 lines
10 KiB
TypeScript
Raw Normal View History

import { invoke } from '@tauri-apps/api/core';
export interface SystemPerformanceData {
name: string;
executionTime: number;
entityCount: number;
averageTime: number;
percentage: number;
}
export interface RemoteEntity {
id: number;
name: string;
enabled: boolean;
2025-10-16 11:55:41 +08:00
active: boolean;
activeInHierarchy: boolean;
componentCount: number;
componentTypes: string[];
parentId: number | null;
childIds: number[];
depth: number;
tag: number;
updateOrder: number;
}
export interface RemoteComponentDetail {
typeName: string;
properties: Record<string, any>;
}
export interface RemoteEntityDetails {
id: number;
name: string;
enabled: boolean;
active: boolean;
activeInHierarchy: boolean;
scene: string;
sceneName: string;
sceneType: string;
componentCount: number;
componentTypes: string[];
components: RemoteComponentDetail[];
parentName: string | null;
}
export interface ProfilerData {
totalFrameTime: number;
systems: SystemPerformanceData[];
entityCount: number;
componentCount: number;
fps: number;
entities?: RemoteEntity[];
}
type ProfilerDataListener = (data: ProfilerData) => void;
export class ProfilerService {
private ws: WebSocket | null = null;
private isServerRunning = false;
private wsPort = '8080';
private listeners: Set<ProfilerDataListener> = new Set();
private currentData: ProfilerData | null = null;
private checkServerInterval: NodeJS.Timeout | null = null;
private reconnectTimeout: NodeJS.Timeout | null = null;
constructor() {
this.startServerCheck();
}
public subscribe(listener: ProfilerDataListener): () => void {
this.listeners.add(listener);
// 如果已有数据,立即发送给新订阅者
if (this.currentData) {
listener(this.currentData);
}
return () => {
this.listeners.delete(listener);
};
}
public isConnected(): boolean {
return this.ws !== null && this.ws.readyState === WebSocket.OPEN;
}
public isServerActive(): boolean {
return this.isServerRunning;
}
private startServerCheck(): void {
this.checkServerStatus();
this.checkServerInterval = setInterval(() => {
this.checkServerStatus();
}, 2000);
}
private async checkServerStatus(): Promise<void> {
try {
const status = await invoke<boolean>('get_profiler_status');
const wasRunning = this.isServerRunning;
this.isServerRunning = status;
// 如果服务器还没运行,自动启动它
if (!status) {
await this.startServer();
return;
}
// 服务器启动了,尝试连接
if (status && !this.ws) {
this.connectToServer();
}
// 服务器从运行变为停止
if (wasRunning && !status) {
console.log('[ProfilerService] Server stopped');
this.disconnect();
}
} catch (error) {
this.isServerRunning = false;
}
}
private async startServer(): Promise<void> {
try {
const port = parseInt(this.wsPort);
const result = await invoke<string>('start_profiler_server', { port });
console.log('[ProfilerService]', result);
this.isServerRunning = true;
} catch (error) {
console.error('[ProfilerService] Failed to start server:', error);
}
}
private connectToServer(): void {
if (this.ws) return;
try {
console.log(`[ProfilerService] Connecting to ws://localhost:${this.wsPort}`);
const ws = new WebSocket(`ws://localhost:${this.wsPort}`);
ws.onopen = () => {
console.log('[ProfilerService] Connected to profiler server');
this.notifyListeners(this.createEmptyData());
};
ws.onclose = () => {
console.log('[ProfilerService] Disconnected from profiler server');
this.ws = null;
// 如果服务器还在运行,尝试重连
if (this.isServerRunning && !this.reconnectTimeout) {
this.reconnectTimeout = setTimeout(() => {
this.reconnectTimeout = null;
this.connectToServer();
}, 3000);
}
};
ws.onerror = (error) => {
console.error('[ProfilerService] WebSocket error:', error);
this.ws = null;
};
ws.onmessage = (event) => {
try {
const message = JSON.parse(event.data);
if (message.type === 'debug_data' && message.data) {
this.handleDebugData(message.data);
2025-10-16 11:55:41 +08:00
} else if (message.type === 'get_raw_entity_list_response' && message.data) {
this.handleRawEntityListResponse(message.data);
} else if (message.type === 'get_entity_details_response' && message.data) {
this.handleEntityDetailsResponse(message.data);
}
} catch (error) {
console.error('[ProfilerService] Failed to parse message:', error);
}
};
this.ws = ws;
} catch (error) {
console.error('[ProfilerService] Failed to create WebSocket:', error);
}
}
2025-10-16 11:55:41 +08:00
public requestEntityDetails(entityId: number): void {
if (!this.ws || this.ws.readyState !== WebSocket.OPEN) {
console.warn('[ProfilerService] Cannot request entity details: WebSocket not connected');
return;
}
try {
const request = {
type: 'get_entity_details',
requestId: `entity_details_${entityId}_${Date.now()}`,
entityId
};
console.log('[ProfilerService] Requesting entity details:', request);
this.ws.send(JSON.stringify(request));
} catch (error) {
console.error('[ProfilerService] Failed to request entity details:', error);
}
}
private handleDebugData(debugData: any): void {
const performance = debugData.performance;
if (!performance) return;
const totalFrameTime = performance.frameTime || 0;
const fps = totalFrameTime > 0 ? Math.round(1000 / totalFrameTime) : 0;
let systems: SystemPerformanceData[] = [];
if (performance.systemPerformance && Array.isArray(performance.systemPerformance)) {
systems = performance.systemPerformance
.map((sys: any) => ({
name: sys.systemName,
executionTime: sys.lastExecutionTime || sys.averageTime || 0,
entityCount: sys.entityCount || 0,
averageTime: sys.averageTime || 0,
percentage: 0
}))
.sort((a: SystemPerformanceData, b: SystemPerformanceData) =>
b.executionTime - a.executionTime
);
const totalTime = performance.frameTime || 1;
systems.forEach((sys: SystemPerformanceData) => {
sys.percentage = (sys.executionTime / totalTime) * 100;
});
}
2025-10-16 11:55:41 +08:00
const entityCount = debugData.entities?.totalEntities || debugData.entities?.totalCount || 0;
const componentTypes = debugData.components?.types || [];
const componentCount = componentTypes.length;
this.currentData = {
totalFrameTime,
systems,
entityCount,
componentCount,
fps,
2025-10-16 11:55:41 +08:00
entities: []
};
this.notifyListeners(this.currentData);
2025-10-16 11:55:41 +08:00
// 请求完整的实体列表
this.requestRawEntityList();
}
private requestRawEntityList(): void {
if (!this.ws || this.ws.readyState !== WebSocket.OPEN) {
console.warn('[ProfilerService] Cannot request entity list: WebSocket not connected');
return;
}
try {
const request = {
type: 'get_raw_entity_list',
requestId: `entity_list_${Date.now()}`
};
console.log('[ProfilerService] Requesting entity list:', request);
this.ws.send(JSON.stringify(request));
} catch (error) {
console.error('[ProfilerService] Failed to request entity list:', error);
}
}
private handleRawEntityListResponse(data: any): void {
if (!data || !Array.isArray(data)) {
console.warn('[ProfilerService] Invalid raw entity list response:', data);
return;
}
console.log('[ProfilerService] Received raw entity list, count:', data.length);
const entities: RemoteEntity[] = data.map((e: any) => ({
id: e.id,
name: e.name || `Entity ${e.id}`,
enabled: e.enabled !== false,
active: e.active !== false,
activeInHierarchy: e.activeInHierarchy !== false,
componentCount: e.componentCount || 0,
componentTypes: e.componentTypes || [],
parentId: e.parentId || null,
childIds: e.childIds || [],
depth: e.depth || 0,
tag: e.tag || 0,
updateOrder: e.updateOrder || 0
}));
if (this.currentData) {
this.currentData.entities = entities;
this.notifyListeners(this.currentData);
}
}
private handleEntityDetailsResponse(data: any): void {
if (!data) {
console.warn('[ProfilerService] Invalid entity details response:', data);
return;
}
console.log('[ProfilerService] Received entity details:', data);
const entityDetails: RemoteEntityDetails = {
id: data.id,
name: data.name || `Entity ${data.id}`,
enabled: data.enabled !== false,
active: data.active !== false,
activeInHierarchy: data.activeInHierarchy !== false,
scene: data.scene || '',
sceneName: data.sceneName || '',
sceneType: data.sceneType || '',
componentCount: data.componentCount || 0,
componentTypes: data.componentTypes || [],
components: data.components || [],
parentName: data.parentName || null
};
window.dispatchEvent(new CustomEvent('profiler:entity-details', {
detail: entityDetails
}));
}
private createEmptyData(): ProfilerData {
return {
totalFrameTime: 0,
systems: [],
entityCount: 0,
componentCount: 0,
fps: 0
};
}
private notifyListeners(data: ProfilerData): void {
this.listeners.forEach(listener => {
try {
listener(data);
} catch (error) {
console.error('[ProfilerService] Error in listener:', error);
}
});
}
private disconnect(): void {
if (this.ws) {
this.ws.close();
this.ws = null;
}
if (this.reconnectTimeout) {
clearTimeout(this.reconnectTimeout);
this.reconnectTimeout = null;
}
}
public destroy(): void {
this.disconnect();
if (this.checkServerInterval) {
clearInterval(this.checkServerInterval);
this.checkServerInterval = null;
}
this.listeners.clear();
this.currentData = null;
}
}