1 line
7.9 KiB
Plaintext
1 line
7.9 KiB
Plaintext
|
|
{"version":3,"file":"index.mjs","sources":["../../../src/stats/index.ts"],"sourcesContent":["import { cancelFrame, frame, frameData } from \"../frameloop\"\nimport { activeAnimations } from \"./animation-count\"\nimport { ActiveStatsBuffer, statsBuffer } from \"./buffer\"\nimport { StatsSummary, Summary } from \"./types\"\n\nfunction record() {\n const { value } = statsBuffer\n\n if (value === null) {\n cancelFrame(record)\n return\n }\n\n value.frameloop.rate.push(frameData.delta)\n value.animations.mainThread.push(activeAnimations.mainThread)\n value.animations.waapi.push(activeAnimations.waapi)\n value.animations.layout.push(activeAnimations.layout)\n}\n\nfunction mean(values: number[]) {\n return values.reduce((acc, value) => acc + value, 0) / values.length\n}\n\nfunction summarise(\n values: number[],\n calcAverage: (allValues: number[]) => number = mean\n): Summary {\n if (values.length === 0) {\n return {\n min: 0,\n max: 0,\n avg: 0,\n }\n }\n\n return {\n min: Math.min(...values),\n max: Math.max(...values),\n avg: calcAverage(values),\n }\n}\n\nconst msToFps = (ms: number) => Math.round(1000 / ms)\n\nfunction clearStatsBuffer() {\n statsBuffer.value = null\n statsBuffer.addProjectionMetrics = null\n}\n\nfunction reportStats(): StatsSummary {\n const { value } = statsBuffer\n\n if (!value) {\n throw new Error(\"Stats are not being measured\")\n }\n\n clearStatsBuffer()\n cancelFrame(record)\n\n const summary = {\n frameloop: {\n setup: summarise(value.frameloop.setup),\n rate: summarise(value.frameloop.rate),\n read: summarise(value.frameloop.read),\n resolveKeyframes: summarise(value.frameloop.resolveKeyframes),\n preUpdate: summarise(value.frameloop.preUpdate),\n update: summarise(value.frameloop.update),\n preRender: summarise(value.frameloop.preRender),\n render: summarise(value.frameloop.render),\n postRender: summarise(value.frameloop.postRender),\n },\n animations: {\n mainThread: summarise(value.animations.mainThread),\n waapi: summarise(value.animations.waapi),\n layout: summarise(value.animations.layout),\n },\n layoutProjection: {\n nodes: summarise(value.layoutProjection.nodes),\n calculatedTargetDeltas: summarise(\n value.layoutProjection.calculatedTargetDeltas\n ),\n calculatedProjections: summarise(\n value.layoutProjection.calculatedProjections\n ),\n },\n }\n\n /**\n * Convert the rate to FPS\n */\n const { rate } = summary.frameloop\n rate.min = msToFps(rate.min)\n rate.max = msToFps(rate.max)\n rate.avg = msToFps(rate.avg)\n // Swap these as the min and max are inverted when converted to FPS\n ;[rate.min, rate.max] = [rate.max, rate.min]\n\n return summary\n}\n\nexport function recordStats() {\n if (statsBuffer.value) {\n clearStatsBuffer()\n throw new Error(\"Stats are already being measured\")\n }\n\n const newStatsBuffer = statsBuffer as unknown as ActiveStatsBuffer\n\n newStatsBuffer.value = {\n frameloop: {\n setup: [],\n rate: [],\n read: [],\n resolveKeyframes: [],\n preUpdate: [],\n update: [],\n preRender: [],\n render: [],\n postRender: [],\n },\n animations: {\n mainThread: [],\n waapi: [],\n layout: [],\n },\n layoutProjection: {\n nodes: [],\n calculatedTargetDeltas: [],\n calculatedProjections: [],\n },\n }\n\n newStatsBuffer.addProjectionMetrics = (metrics) => {\n const { layoutProjection } = newStatsBuffer.value\n layoutProjection.nodes.push(metrics.nodes)\n layoutProjection.calcul
|