Skip to content

Commit b5de7b8

Browse files
Examples: Implemented a statistics summary when fps is set (#109)
When `fps` is set on a run of an Example Test, skipping the first 10 fps samples, every 30 fps samples print out a statistics summary of those samples. Example: ``` --------------------------------- Average FPS: 27.25 Median FPS: 26 P01 FPS: 19 P05 FPS: 26 P25 FPS: 26 Std Dev FPS: 2.7798381247835278 Num samples: 100 --------------------------------- ```
2 parents 7f5913c + 2060172 commit b5de7b8

File tree

2 files changed

+51
-2
lines changed

2 files changed

+51
-2
lines changed

examples/README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,9 @@ pnpm watch
4747
- `resolution` (number, default: 720)
4848
- Resolution (height) of to render the test at (in logical pixels)
4949
- `fps` (boolean, default: "false")
50-
- Whether or not to log the latest FPS to the console every 1 second.
50+
- Whether or not to log the latest FPS sample to the console every 1 second.
51+
- After skipping the first 10 samples, every 100 samples after that will result
52+
in a statistics summary printed to the console.
5153
- `ppr` (number, default: 1)
5254
- Device physical pixel ratio.
5355
- `multiplier` (number, default: 1)

examples/index.ts

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,8 +211,55 @@ async function initRenderer(
211211
driver,
212212
);
213213

214+
/**
215+
* FPS sample captured
216+
*/
217+
const fpsSamples: number[] = [];
218+
/**
219+
* Number of samples to capture before calculating FPS stats
220+
*/
221+
const fpsSampleCount = 100;
222+
/**
223+
* Number of samples to skip before starting to capture FPS samples.
224+
*/
225+
const fpsSampleSkipCount = 10;
226+
/**
227+
* FPS sample index
228+
*/
229+
let fpsSampleIndex = 0;
230+
let fpsSamplesLeft = fpsSampleCount;
214231
renderer.on('fpsUpdate', (target: RendererMain, fps: number) => {
215-
console.log(`FPS: ${fps}`);
232+
const captureSample = fpsSampleIndex >= fpsSampleSkipCount;
233+
if (captureSample) {
234+
fpsSamples.push(fps);
235+
fpsSamplesLeft--;
236+
if (fpsSamplesLeft === 0) {
237+
const sortedSamples = fpsSamples.sort((a, b) => a - b);
238+
const averageFps =
239+
fpsSamples.reduce((a, b) => a + b, 0) / fpsSamples.length;
240+
const p01Fps = sortedSamples[Math.floor(fpsSamples.length * 0.01)]!;
241+
const p05Fps = sortedSamples[Math.floor(fpsSamples.length * 0.05)]!;
242+
const p25Fps = sortedSamples[Math.floor(fpsSamples.length * 0.25)]!;
243+
const medianFps = sortedSamples[Math.floor(fpsSamples.length * 0.5)]!;
244+
const stdDevFps = Math.sqrt(
245+
fpsSamples.reduce((a, b) => a + (b - averageFps) ** 2, 0) /
246+
fpsSamples.length,
247+
);
248+
console.log(`---------------------------------`);
249+
console.log(`Average FPS: ${averageFps}`);
250+
console.log(`Median FPS: ${medianFps}`);
251+
console.log(`P01 FPS: ${p01Fps}`);
252+
console.log(`P05 FPS: ${p05Fps}`);
253+
console.log(`P25 FPS: ${p25Fps}`);
254+
console.log(`Std Dev FPS: ${stdDevFps}`);
255+
console.log(`Num samples: ${fpsSamples.length}`);
256+
console.log(`---------------------------------`);
257+
fpsSamples.length = 0;
258+
fpsSamplesLeft = fpsSampleCount;
259+
}
260+
}
261+
console.log(`FPS: ${fps} (samples left: ${fpsSamplesLeft})`);
262+
fpsSampleIndex++;
216263
});
217264

218265
await renderer.init();

0 commit comments

Comments
 (0)