Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
b856aaa
feat(example): add a map benchmark spike
jkasprzyk17 Dec 6, 2026
eb7d309
fix(example): draw every bench marker
jkasprzyk17 Dec 6, 2026
f3cf847
feat(example): add a frame and memory recorder
jkasprzyk17 Dec 6, 2026
f2ba5ad
feat(example): run map benchmark suites in-app
jkasprzyk17 Dec 6, 2026
dda6de7
feat(benchmarks): add the iOS simulator runner
jkasprzyk17 Dec 6, 2026
ac1c0e7
docs: add benchmarks against react-native-maps
jkasprzyk17 Dec 6, 2026
7fdf04f
docs(example): correct two bench comments
jkasprzyk17 Dec 6, 2026
b14dfd3
fix(benchmarks): add the missing frame math
jkasprzyk17 Dec 6, 2026
fc455be
fix(example): require the bench only on its path
jkasprzyk17 Dec 6, 2026
3ddb293
docs: add a benchmark re-run on main after #232
jkasprzyk17 Dec 6, 2026
c165aa2
fix(benchmarks): correct the summary definitions
jkasprzyk17 Dec 6, 2026
c1e8d02
fix(benchmarks): target the runner's simulator
jkasprzyk17 Dec 6, 2026
638ca1c
fix(example): harden the benchmark harness
jkasprzyk17 Dec 6, 2026
81fd036
docs: re-derive the benchmark tables
jkasprzyk17 Dec 6, 2026
d11d534
chore(example): keep index keys in the bench
jkasprzyk17 Dec 6, 2026
8c4a45b
fix(example): check marker states by coordinates
jkasprzyk17 Dec 6, 2026
d8c8c7e
fix(benchmarks): call old draw times bounds
jkasprzyk17 Dec 6, 2026
33095bd
fix(benchmarks): reject sessions the app refuses
jkasprzyk17 Dec 6, 2026
00f544e
fix(example): ignore malformed bench links
jkasprzyk17 Dec 6, 2026
aef213a
fix(benchmarks): report memory runs left out
jkasprzyk17 Dec 6, 2026
6f7a354
fix(benchmarks): reject unknown suite names
jkasprzyk17 Dec 6, 2026
b9e9c94
fix(benchmarks): describe draw times exactly
jkasprzyk17 Dec 6, 2026
dccdd91
docs: bound how far draw dating moves Settled
jkasprzyk17 Dec 6, 2026
bf752dc
fix(benchmarks): check every validation census
jkasprzyk17 Dec 6, 2026
424e311
docs: drop the draw-time error bound
jkasprzyk17 Dec 6, 2026
683ab3c
fix(benchmarks): no draw time when none is drawn
jkasprzyk17 Dec 6, 2026
c7b73ba
docs: define All drawn by from the last poll
jkasprzyk17 Dec 6, 2026
46e36ba
fix(benchmarks): mark reconstructed draw times
jkasprzyk17 Dec 6, 2026
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions benchmarks/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
results/
44 changes: 44 additions & 0 deletions benchmarks/dataset.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { describe, expect, test } from 'bun:test';
import {
benchCoordinates,
movedCoordinates,
sameCoordinates,
type BenchCoordinate,
} from '../example/bench/dataset';

const flatten = (coordinates: readonly BenchCoordinate[]) =>
coordinates.flatMap((coordinate) => [
coordinate.latitude,
coordinate.longitude,
]);

/** The marker state after update tick `last`, as the scenario builds it. */
function stateAfter(count: number, last: number) {
let state = benchCoordinates(count);
for (let tick = 0; tick <= last; tick += 1) {
state = movedCoordinates(state, tick);
}
return state;
}

describe('sameCoordinates', () => {
test('accepts the same state in any marker order', () => {
const state = stateAfter(100, 10);
expect(sameCoordinates(flatten([...state].reverse()), state)).toBe(true);
});

test('rejects state 8 where state 10 is expected', () => {
// Groups 0-8 moved north against groups 1-9: the same coordinate sum.
for (const count of [100, 1000, 2000]) {
expect(
sameCoordinates(flatten(stateAfter(count, 8)), stateAfter(count, 10)),
).toBe(false);
}
});

test('rejects a missing marker or no map', () => {
const state = stateAfter(100, 3);
expect(sameCoordinates(flatten(state.slice(1)), state)).toBe(false);
expect(sameCoordinates(null, state)).toBe(false);
});
});
168 changes: 168 additions & 0 deletions benchmarks/frames.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
import { describe, expect, test } from 'bun:test';
import {
jsFrames,
jsFramesFrom,
mainFrames,
mainFramesFrom,
percentile,
settleAfter,
summarize,
type Frame,
} from './frames';

/** Display-link stamps every `step` ms from `start`, with gaps inserted. */
function stamps(
start: number,
count: number,
step: number,
gaps: Record<number, number> = {},
) {
const result: number[] = [];
let at = start;
for (let index = 0; index < count; index += 1) {
at += gaps[index] ?? 0;
result.push(at);
at += step;
}
return result;
}

describe('summarize', () => {
test('counts long frames, dropped refreshes and blocked time', () => {
// 60 Hz, one 100 ms stall before the 11th callback.
const timestamps = stamps(0, 21, 1000 / 60, { 10: 100 - 1000 / 60 });
const frames = mainFrames(
timestamps,
timestamps.map(() => 1000 / 60),
);
const summary = summarize(frames);
expect(summary.frames).toBe(20);
expect(summary.longFrames).toBe(1);
expect(summary.max).toBeCloseTo(100);
expect(summary.droppedFrames).toBe(5);
expect(summary.blockedMs).toBeCloseTo(100 - 1000 / 60);
expect(summary.p50).toBeCloseTo(1000 / 60);
});

test('limits the summary to frames that ended in the window', () => {
const timestamps = stamps(0, 61, 1000 / 60);
const frames = mainFrames(
timestamps,
timestamps.map(() => 1000 / 60),
);
// Callbacks every 16.7 ms: those ending at 516.7 … 1000 ms.
expect(summarize(frames, 508, 1008).frames).toBe(30);
});
});

describe('frames from a window start', () => {
const frameMs = 1000 / 60;

test('counts the time before a late first callback as a frame', () => {
// The window starts at 0; the thread is busy until the first stamp at 150.
const frames = jsFramesFrom(0, stamps(150, 10, frameMs), frameMs);
expect(frames).toHaveLength(10);
expect(frames[0].interval).toBeCloseTo(150);
expect(summarize(frames).longFrames).toBe(1);
});

test('adds nothing when the first callback came within one interval', () => {
// A display-link timestamp is the vsync time, which can precede the start;
// a callback up to one interval late is only the refresh phase.
for (const first of [-3, 10]) {
const timestamps = stamps(first, 10, frameMs);
const expected = timestamps.map(() => frameMs);
expect(mainFramesFrom(0, timestamps, expected)).toEqual(
mainFrames(timestamps, expected),
);
}
expect(jsFramesFrom(0, stamps(12, 10, frameMs), frameMs)).toHaveLength(9);
});

test('judges the leading main-thread frame against the first interval', () => {
const timestamps = stamps(80, 5, 1000 / 120);
const frames = mainFramesFrom(
0,
timestamps,
timestamps.map(() => 1000 / 120),
);
expect(frames[0]).toEqual({ end: 80, interval: 80, expected: 1000 / 120 });
});
});

describe('percentile', () => {
test('uses the nearest rank', () => {
expect(percentile([1, 2, 3, 4], 50)).toBe(2);
expect(percentile([1, 2, 3, 4], 95)).toBe(4);
expect(percentile([], 50)).toBe(0);
});
});

describe('settleAfter', () => {
const frameMs = 1000 / 60;
const main = (gaps: Record<number, number>) => {
const timestamps = stamps(0, 400, frameMs, gaps);
return mainFrames(
timestamps,
timestamps.map(() => frameMs),
);
};

test('is zero when nothing is long', () => {
expect(settleAfter([main({})], 0, 400 * frameMs)).toBe(0);
});

test('starts the quiet span at the end of the last long frame', () => {
// A 300 ms stall ending at callback 30, a 50 ms stall at callback 60.
const frames = main({ 30: 300, 60: 50 });
const settled = settleAfter([frames], 0, frames[frames.length - 1].end);
const lastLong = frames.findLast((frame) => frame.interval > 60)!;
expect(lastLong.interval).toBeCloseTo(50 + frameMs);
expect(settled).toBeCloseTo(lastLong.end);
});

test('ignores a long frame after a quiet span', () => {
// Stall at 10, quiet for > 1 s, stall at 150.
const frames = main({ 10: 200, 150: 200 });
const settled = settleAfter([frames], 0, frames[frames.length - 1].end);
const firstLong = frames.find((frame) => frame.interval > 100)!;
expect(settled).toBeCloseTo(firstLong.end);
});

test('takes long frames from every thread', () => {
const quietMain = main({});
const js = jsFrames(stamps(0, 200, frameMs, { 100: 500 }), frameMs);
const settled = settleAfter([quietMain, js], 1000, 6000)!;
const jsLong = js.find((frame) => frame.interval > 100)!;
expect(settled).toBeCloseTo(jsLong.end - 1000);
});

test('orders overlapping long frames of two threads by their start', () => {
// Main is long over 1500-1600, JS over 500-2000: the JS frame starts
// first, so nothing is quiet before 2000.
const long = (start: number, end: number): Frame => ({
end,
interval: end - start,
expected: frameMs,
});
expect(settleAfter([[long(1500, 1600)], [long(500, 2000)]], 0, 4000)).toBe(
2000,
);
});

test('is null when no quiet span fits in the recording', () => {
// A stall every 50 callbacks (0.93 s apart) up to the last 0.83 s.
const frames = main({
50: 100,
100: 100,
150: 100,
200: 100,
250: 100,
300: 100,
350: 100,
});
expect(
settleAfter([frames], 0, frames[frames.length - 1].end, 1000),
).toBeNull();
});
});
170 changes: 170 additions & 0 deletions benchmarks/frames.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
/**
* Frame math for the map benchmark. The app records raw stamps only; every
* definition (jank, settle, percentiles) lives here, so changing one needs no
* new run.
*/

/** One frame: the interval that ended at `end`, and the one the display asked for. */
export interface Frame {
end: number;
interval: number;
expected: number;
}

/** A frame counts as long above this multiple of the display interval. */
export const LONG_FRAME_FACTOR = 1.5;

/** Main-thread frames from display-link timestamps and per-frame expected intervals. */
export function mainFrames(timestamps: number[], expected: number[]): Frame[] {
const frames: Frame[] = [];
for (let index = 1; index < timestamps.length; index += 1) {
frames.push({
end: timestamps[index],
interval: timestamps[index] - timestamps[index - 1],
expected: expected[index - 1] ?? expected[0] ?? 1000 / 60,
});
}
return frames;
}

/** JS-thread frames from `requestAnimationFrame` stamps, judged against `expected`. */
export function jsFrames(stamps: number[], expected: number): Frame[] {
return mainFrames(
stamps,
stamps.map(() => expected),
);
}

/**
* Main-thread frames of a window that started at `start`. When the first
* callback came more than one display interval after `start`, the time before
* it is a frame of its own: work that held the thread from the start of the
* window (a mount's first render, for one) would otherwise fall before the
* first interval. A shorter wait is only where the window fell in the refresh
* cycle, so it adds no frame.
*/
export function mainFramesFrom(
start: number,
timestamps: number[],
expected: number[],
): Frame[] {
const interval = expected[0] ?? 1000 / 60;
if (timestamps.length === 0 || timestamps[0] - start <= interval) {
return mainFrames(timestamps, expected);
}
return mainFrames([start, ...timestamps], [interval, ...expected]);
}

/** `jsFrames` of a window that started at `start`, as `mainFramesFrom`. */
export function jsFramesFrom(
start: number,
stamps: number[],
expected: number,
): Frame[] {
return jsFrames(
stamps.length > 0 && stamps[0] - start > expected
? [start, ...stamps]
: stamps,
expected,
);
}

export const isLong = (frame: Frame) =>
frame.interval > frame.expected * LONG_FRAME_FACTOR;

/** Nearest-rank percentile of an ascending sample. */
export function percentile(sorted: number[], p: number): number {
if (sorted.length === 0) {
return 0;
}
const rank = Math.ceil((p / 100) * sorted.length);
return sorted[Math.min(sorted.length - 1, Math.max(0, rank - 1))];
}

export function median(values: number[]): number {
return percentile(
[...values].sort((a, b) => a - b),
50,
);
}

export interface FrameSummary {
frames: number;
/** Display interval the frames were judged against (median). */
expectedMs: number;
p50: number;
p95: number;
p99: number;
max: number;
/** Frames longer than 1.5× the display interval. */
longFrames: number;
longFramePct: number;
/** Display refreshes that passed without a frame. */
droppedFrames: number;
/** Time beyond the display interval, summed: how long the thread was stuck. */
blockedMs: number;
}

/** Summarizes the frames that ended within [from, to). */
export function summarize(
frames: Frame[],
from = -Infinity,
to = Infinity,
): FrameSummary {
const window = frames.filter((frame) => frame.end >= from && frame.end < to);
const intervals = window.map((frame) => frame.interval).sort((a, b) => a - b);
let longFrames = 0;
let droppedFrames = 0;
let blockedMs = 0;
for (const frame of window) {
if (isLong(frame)) {
longFrames += 1;
}
droppedFrames += Math.max(
0,
Math.round(frame.interval / frame.expected) - 1,
);
blockedMs += Math.max(0, frame.interval - frame.expected);
}
return {
frames: window.length,
expectedMs: median(window.map((frame) => frame.expected)),
p50: percentile(intervals, 50),
p95: percentile(intervals, 95),
p99: percentile(intervals, 99),
max: intervals[intervals.length - 1] ?? 0,
longFrames,
longFramePct: window.length > 0 ? (100 * longFrames) / window.length : 0,
droppedFrames,
blockedMs,
};
}

/**
* When the threads settle after `from`: the start of the first span of
* `quietMs` with no long frame on any of the given threads. A long frame that
* overlaps `from` pushes the start to its end. Returns the time from `from`,
* or null when no such span fits before the recording ends at `recordedUntil`.
*/
export function settleAfter(
threads: Frame[][],
from: number,
recordedUntil: number,
quietMs = 1000,
): number | null {
// By start: long frames of different threads overlap, and one that starts
// earlier but ends later still breaks the span before the other.
const long = threads
.flat()
.filter((frame) => isLong(frame) && frame.end > from)
.sort((a, b) => a.end - a.interval - (b.end - b.interval));
let start = from;
for (const frame of long) {
const frameStart = frame.end - frame.interval;
if (frameStart - start >= quietMs) {
return start - from;
}
start = Math.max(start, frame.end);
}
return recordedUntil - start >= quietMs ? start - from : null;
}
Loading
Loading