1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193
| class Bootstrap { private initializer: ParallelInitializer; private lazyLoader: LazyLoader; private monitor: StartupMonitor; constructor() { this.initializer = new ParallelInitializer(); this.lazyLoader = new LazyLoader(); this.monitor = new StartupMonitor(); this.registerTasks(); } private registerTasks(): void { this.initializer.register({ id: 'env-check', name: '检查环境', phase: BootstrapPhase.PreBoot, priority: 100, run: async () => { await this.checkEnvironment(); }, timeout: 5000, onFailure: 'abort', }); this.initializer.register({ id: 'error-handler', name: '设置错误处理', phase: BootstrapPhase.PreBoot, priority: 90, run: async () => { this.setupErrorHandler(); }, timeout: 1000, onFailure: 'abort', }); this.initializer.register({ id: 'config', name: '加载配置', phase: BootstrapPhase.ParallelInit, priority: 80, run: async () => { await configCenter.initialize({ watch: true }); }, timeout: 10000, onFailure: 'abort', }); this.initializer.register({ id: 'auth', name: '验证认证', phase: BootstrapPhase.ParallelInit, priority: 80, run: async () => { await authManager.validate(); }, timeout: 10000, onFailure: 'abort', }); this.initializer.register({ id: 'plugins', name: '加载插件', phase: BootstrapPhase.ParallelInit, priority: 60, run: async () => { await pluginManager.loadAll(); }, timeout: 30000, onFailure: 'continue', }); this.initializer.register({ id: 'mcp', name: '连接 MCP 服务', phase: BootstrapPhase.ParallelInit, priority: 60, run: async () => { await mcpManager.connectAll(); }, timeout: 15000, onFailure: 'continue', }); this.initializer.register({ id: 'lazy-setup', name: '设置懒加载', phase: BootstrapPhase.LazyPrep, priority: 40, run: async () => { this.setupLazyLoading(); }, }); } private setupLazyLoading(): void { this.lazyLoader.register('memory-system', async () => { return await import('./memory/MemorySystem'); }); this.lazyLoader.register('analytics', async () => { return await import('./analytics/Analytics'); }); this.lazyLoader.register('ui-themes', async () => { return await import('./ui/Themes'); }); this.lazyLoader.preload('memory-system'); } async start(): Promise<void> { console.log('[Bootstrap] Starting...'); const startTime = Date.now(); try { await this.initializer.runPhase(BootstrapPhase.PreBoot); this.monitor.recordPhase(BootstrapPhase.PreBoot, Date.now() - startTime); const phase1Start = Date.now(); await this.initializer.runPhase(BootstrapPhase.ParallelInit); this.monitor.recordPhase(BootstrapPhase.ParallelInit, Date.now() - phase1Start); const phase2Start = Date.now(); await this.initializer.runPhase(BootstrapPhase.LazyPrep); this.monitor.recordPhase(BootstrapPhase.LazyPrep, Date.now() - phase2Start); this.state.phase = BootstrapPhase.Ready; this.state.readyAt = Date.now(); const totalDuration = this.state.readyAt - startTime; console.log(`[Bootstrap] Ready in ${totalDuration}ms`); const report = this.monitor.generateReport(); this.logPerformanceReport(report); this.runBackgroundTasks(); } catch (error) { console.error('[Bootstrap] Startup failed:', error); throw error; } } private runBackgroundTasks(): void { setTimeout(async () => { await this.checkForUpdates(); await this.syncData(); await this.sendTelemetry(); }, 0); } private logPerformanceReport(report: StartupReport): void { console.log('\n[Bootstrap] 启动性能报告:'); console.log('─────────────────────────────────────'); console.log(`总启动时间:${report.totalDuration}ms`); console.log(''); console.log('阶段耗时:'); for (const phase of report.phases) { console.log(`- ${phase.phase}: ${phase.duration}ms`); } console.log(''); if (report.slowTasks.length > 0) { console.log('慢任务 Top 5:'); for (const task of report.slowTasks) { console.log(`${task.taskId}: ${task.duration}ms`); } console.log(''); } if (report.suggestions.length > 0) { console.log('优化建议:'); for (const suggestion of report.suggestions) { console.log(`- ${suggestion}`); } } } }
|