()
| 128 | |
| 129 | // 获取核心Web指标 |
| 130 | function getCoreWebVitals() { |
| 131 | return new Promise(resolve => { |
| 132 | let webVitals = {}; |
| 133 | |
| 134 | // LCP (Largest Contentful Paint) |
| 135 | new PerformanceObserver((entryList) => { |
| 136 | const entries = entryList.getEntries(); |
| 137 | const lastEntry = entries[entries.length - 1]; |
| 138 | webVitals.lcp = lastEntry.renderTime || lastEntry.loadTime; |
| 139 | }).observe({entryTypes: ['largest-contentful-paint']}); |
| 140 | |
| 141 | // FID (First Input Delay) |
| 142 | new PerformanceObserver((entryList) => { |
| 143 | const firstInput = entryList.getEntries()[0]; |
| 144 | if (firstInput) { |
| 145 | webVitals.fid = firstInput.processingTime; |
| 146 | webVitals.firstInputTime = firstInput.startTime; |
| 147 | } |
| 148 | }).observe({entryTypes: ['first-input']}); |
| 149 | |
| 150 | // CLS (Cumulative Layout Shift) |
| 151 | let clsValue = 0; |
| 152 | new PerformanceObserver((entryList) => { |
| 153 | for (const entry of entryList.getEntries()) { |
| 154 | if (!entry.hadRecentInput) { |
| 155 | clsValue += entry.value; |
| 156 | } |
| 157 | } |
| 158 | webVitals.cls = clsValue; |
| 159 | }).observe({entryTypes: ['layout-shift']}); |
| 160 | |
| 161 | setTimeout(() => resolve(webVitals), 3000); |
| 162 | }); |
| 163 | } |
| 164 | |
| 165 | // 获取性能指标 |
| 166 | function getPerformanceMetrics() { |
no test coverage detected