|
| 1 | +import { |
| 2 | + runDeterministicWorkloadIteration, |
| 3 | + summarizeDurationSamples, |
| 4 | + toPrettyJson |
| 5 | +} from "../shared/debugInspectorData.js"; |
| 6 | +import { readToolHostSharedContextFromLocation } from "../shared/toolHostSharedContext.js"; |
| 7 | +import { registerToolBootContract } from "../shared/toolBootContract.js"; |
| 8 | + |
| 9 | +const refs = { |
| 10 | + runWorkloadButton: document.getElementById("runWorkloadButton"), |
| 11 | + runFrameSampleButton: document.getElementById("runFrameSampleButton"), |
| 12 | + stopButton: document.getElementById("stopProfilerButton"), |
| 13 | + statusText: document.getElementById("profilerStatusText"), |
| 14 | + workloadIterationsInput: document.getElementById("workloadIterationsInput"), |
| 15 | + workSizeInput: document.getElementById("workSizeInput"), |
| 16 | + frameSamplesInput: document.getElementById("frameSamplesInput"), |
| 17 | + output: document.getElementById("profileOutput") |
| 18 | +}; |
| 19 | + |
| 20 | +const state = { |
| 21 | + frameHandle: 0, |
| 22 | + frameSampling: false |
| 23 | +}; |
| 24 | + |
| 25 | +function setStatus(message) { |
| 26 | + if (refs.statusText instanceof HTMLElement) { |
| 27 | + refs.statusText.textContent = message; |
| 28 | + } |
| 29 | +} |
| 30 | + |
| 31 | +function readPositiveInt(input, fallback, min, max) { |
| 32 | + if (!(input instanceof HTMLInputElement)) { |
| 33 | + return fallback; |
| 34 | + } |
| 35 | + const numeric = Math.trunc(Number(input.value)); |
| 36 | + if (!Number.isFinite(numeric)) { |
| 37 | + return fallback; |
| 38 | + } |
| 39 | + return Math.max(min, Math.min(max, numeric)); |
| 40 | +} |
| 41 | + |
| 42 | +function writeOutput(payload) { |
| 43 | + if (refs.output instanceof HTMLElement) { |
| 44 | + refs.output.textContent = toPrettyJson(payload); |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +function runWorkloadProfile() { |
| 49 | + const iterations = readPositiveInt(refs.workloadIterationsInput, 300, 10, 5000); |
| 50 | + const workSize = readPositiveInt(refs.workSizeInput, 3000, 100, 100000); |
| 51 | + const durations = []; |
| 52 | + let checksum = 0; |
| 53 | + |
| 54 | + for (let index = 0; index < iterations; index += 1) { |
| 55 | + const started = performance.now(); |
| 56 | + checksum += runDeterministicWorkloadIteration(workSize); |
| 57 | + durations.push(performance.now() - started); |
| 58 | + } |
| 59 | + |
| 60 | + const summary = summarizeDurationSamples(durations); |
| 61 | + writeOutput({ |
| 62 | + schema: "tools.performance-profiler.workload-profile/1", |
| 63 | + iterations, |
| 64 | + workSize, |
| 65 | + checksum, |
| 66 | + summary |
| 67 | + }); |
| 68 | + setStatus(`Workload profile complete. iterations=${iterations}, avg=${summary.avgMs.toFixed(3)}ms, p95=${summary.p95Ms.toFixed(3)}ms.`); |
| 69 | +} |
| 70 | + |
| 71 | +function stopFrameSampling() { |
| 72 | + if (state.frameHandle) { |
| 73 | + cancelAnimationFrame(state.frameHandle); |
| 74 | + state.frameHandle = 0; |
| 75 | + } |
| 76 | + state.frameSampling = false; |
| 77 | +} |
| 78 | + |
| 79 | +function runFrameSample() { |
| 80 | + stopFrameSampling(); |
| 81 | + const frameSamples = readPositiveInt(refs.frameSamplesInput, 120, 10, 600); |
| 82 | + const frameDurations = []; |
| 83 | + let lastTimestamp = 0; |
| 84 | + let sampleCount = 0; |
| 85 | + state.frameSampling = true; |
| 86 | + |
| 87 | + function tick(timestamp) { |
| 88 | + if (!state.frameSampling) { |
| 89 | + return; |
| 90 | + } |
| 91 | + |
| 92 | + if (lastTimestamp > 0) { |
| 93 | + frameDurations.push(Math.max(0, timestamp - lastTimestamp)); |
| 94 | + sampleCount += 1; |
| 95 | + } |
| 96 | + lastTimestamp = timestamp; |
| 97 | + |
| 98 | + if (sampleCount >= frameSamples) { |
| 99 | + stopFrameSampling(); |
| 100 | + const summary = summarizeDurationSamples(frameDurations); |
| 101 | + const avgFps = summary.avgMs > 0 ? 1000 / summary.avgMs : 0; |
| 102 | + writeOutput({ |
| 103 | + schema: "tools.performance-profiler.frame-sample/1", |
| 104 | + frameSamples, |
| 105 | + summary, |
| 106 | + estimatedFps: avgFps |
| 107 | + }); |
| 108 | + setStatus(`Frame sample complete. samples=${frameSamples}, avg=${summary.avgMs.toFixed(3)}ms, estFps=${avgFps.toFixed(2)}.`); |
| 109 | + return; |
| 110 | + } |
| 111 | + |
| 112 | + state.frameHandle = requestAnimationFrame(tick); |
| 113 | + } |
| 114 | + |
| 115 | + state.frameHandle = requestAnimationFrame(tick); |
| 116 | + setStatus(`Frame sampling started (${frameSamples} samples).`); |
| 117 | +} |
| 118 | + |
| 119 | +function bindEvents() { |
| 120 | + if (refs.runWorkloadButton instanceof HTMLButtonElement) { |
| 121 | + refs.runWorkloadButton.addEventListener("click", runWorkloadProfile); |
| 122 | + } |
| 123 | + if (refs.runFrameSampleButton instanceof HTMLButtonElement) { |
| 124 | + refs.runFrameSampleButton.addEventListener("click", runFrameSample); |
| 125 | + } |
| 126 | + if (refs.stopButton instanceof HTMLButtonElement) { |
| 127 | + refs.stopButton.addEventListener("click", () => { |
| 128 | + stopFrameSampling(); |
| 129 | + setStatus("Profiler stopped."); |
| 130 | + }); |
| 131 | + } |
| 132 | +} |
| 133 | + |
| 134 | +let initialized = false; |
| 135 | + |
| 136 | +const performanceProfilerApi = { |
| 137 | + captureProjectState() { |
| 138 | + return { |
| 139 | + workloadIterations: readPositiveInt(refs.workloadIterationsInput, 300, 10, 5000), |
| 140 | + workSize: readPositiveInt(refs.workSizeInput, 3000, 100, 100000), |
| 141 | + frameSamples: readPositiveInt(refs.frameSamplesInput, 120, 10, 600) |
| 142 | + }; |
| 143 | + }, |
| 144 | + applyProjectState(snapshot) { |
| 145 | + if (refs.workloadIterationsInput instanceof HTMLInputElement && Number.isFinite(snapshot?.workloadIterations)) { |
| 146 | + refs.workloadIterationsInput.value = String(snapshot.workloadIterations); |
| 147 | + } |
| 148 | + if (refs.workSizeInput instanceof HTMLInputElement && Number.isFinite(snapshot?.workSize)) { |
| 149 | + refs.workSizeInput.value = String(snapshot.workSize); |
| 150 | + } |
| 151 | + if (refs.frameSamplesInput instanceof HTMLInputElement && Number.isFinite(snapshot?.frameSamples)) { |
| 152 | + refs.frameSamplesInput.value = String(snapshot.frameSamples); |
| 153 | + } |
| 154 | + return true; |
| 155 | + } |
| 156 | +}; |
| 157 | + |
| 158 | +function bootPerformanceProfiler() { |
| 159 | + if (!initialized) { |
| 160 | + bindEvents(); |
| 161 | + const hostContext = readToolHostSharedContextFromLocation(window.location); |
| 162 | + if (hostContext?.state && typeof hostContext.state === "object") { |
| 163 | + performanceProfilerApi.applyProjectState(hostContext.state); |
| 164 | + setStatus("Profiler initialized from host context state."); |
| 165 | + } |
| 166 | + initialized = true; |
| 167 | + } |
| 168 | + window.performanceProfilerApp = performanceProfilerApi; |
| 169 | + return performanceProfilerApi; |
| 170 | +} |
| 171 | + |
| 172 | +registerToolBootContract("performance-profiler", { |
| 173 | + init: bootPerformanceProfiler, |
| 174 | + destroy() { |
| 175 | + stopFrameSampling(); |
| 176 | + return true; |
| 177 | + }, |
| 178 | + getApi() { |
| 179 | + return window.performanceProfilerApp || null; |
| 180 | + } |
| 181 | +}); |
| 182 | + |
| 183 | +bootPerformanceProfiler(); |
0 commit comments