Skip to content

Commit ef06776

Browse files
authored
Upgrade Tinybench to 6.2.3
Summary: ## Context React Native benchmarks use Tinybench to run synchronous tasks. In Tinybench 4.1.0, implicit task-mode detection can execute callbacks during registration and cannot be explicitly disabled with `async: false`. This makes execution depend on detection and transform behavior rather than the benchmark harness's declared contract. We want predictable task execution and trustworthy result reporting before changing timing or sampling policies. Tinybench 6.2.3 provides explicit synchronous registration, result states, and sample counts independent of sample retention. The adapters and type definitions need to be migrated together to use those capabilities safely. ## Changes - Upgrade Tinybench to 6.2.3 and regenerate its dependency records and vendored runtime. - Explicitly register synchronous tasks and reject non-completed results before verification or reporting. - Read sample counts from `samplesCount` rather than requiring retained sample arrays. - Update Flow definitions and regression tests for invocation counts, result states, and optional sample retention. - Leave clocks, warmup, execution policy, mounting behavior, and output schema unchanged to isolate the dependency upgrade. Changelog: [Internal] Differential Revision: D132295558 Pull Request resolved: #60051
1 parent b6a37ec commit ef06776

8 files changed

Lines changed: 322 additions & 145 deletions

File tree

‎flow-typed/npm/tinybench_v4.1.x.js‎

Lines changed: 0 additions & 130 deletions
This file was deleted.

‎flow-typed/npm/tinybench_v6.2.x.js‎

Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
/**
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*
7+
* @flow strict
8+
* @format
9+
*/
10+
11+
declare module 'tinybench' {
12+
declare export class Task extends EventTarget {
13+
readonly name: string;
14+
readonly result: Readonly<TaskResult>;
15+
readonly runs: number;
16+
readonly detectedResolution: void | number;
17+
18+
reset(emit?: boolean): void;
19+
run(): Promise<Task>;
20+
runSync(): Task;
21+
warmup(): Promise<void>;
22+
warmupSync(): void;
23+
}
24+
25+
export type HookMode = 'run' | 'warmup';
26+
27+
export type Hook = (task: Task, mode: HookMode) => Promise<void> | void;
28+
29+
export type FnHook = (this: Task, mode?: HookMode) => Promise<void> | void;
30+
31+
export type Concurrency = null | 'task' | 'bench';
32+
33+
export type BenchOptions = {
34+
concurrency?: Concurrency,
35+
iterations?: number,
36+
name?: string,
37+
now?: () => number,
38+
retainSamples?: boolean,
39+
setup?: Hook,
40+
signal?: AbortSignal,
41+
subtractTimerOverhead?: boolean,
42+
teardown?: Hook,
43+
threshold?: number,
44+
throws?: boolean,
45+
time?: number,
46+
warmup?: boolean,
47+
warmupIterations?: number,
48+
warmupTime?: number,
49+
};
50+
51+
export interface Statistics {
52+
aad: number;
53+
critical: number;
54+
df: number;
55+
mad: number;
56+
max: number;
57+
mean: number;
58+
min: number;
59+
moe: number;
60+
p50: number;
61+
p75: number;
62+
p99: number;
63+
p995: number;
64+
p999: number;
65+
rme: number;
66+
samples: void | number[];
67+
samplesCount: number;
68+
sd: number;
69+
sem: number;
70+
variance: number;
71+
}
72+
73+
export type TaskResultRuntimeInfo = {
74+
runtime: string,
75+
runtimeVersion: string,
76+
timestampProviderName: string,
77+
};
78+
79+
export type TaskResultWithStatistics = {
80+
...TaskResultRuntimeInfo,
81+
latency: Statistics,
82+
period: number,
83+
throughput: Statistics,
84+
totalTime: number,
85+
};
86+
87+
export type TaskResultCompleted = {
88+
...TaskResultWithStatistics,
89+
state: 'completed',
90+
};
91+
92+
export type TaskResultAbortedWithStatistics = {
93+
...TaskResultWithStatistics,
94+
state: 'aborted-with-statistics',
95+
};
96+
97+
export type TaskResultErrored = {
98+
...TaskResultRuntimeInfo,
99+
state: 'errored',
100+
error: Error,
101+
};
102+
103+
export type TaskResult =
104+
| TaskResultCompleted
105+
| TaskResultAbortedWithStatistics
106+
| TaskResultErrored
107+
| {
108+
...TaskResultRuntimeInfo,
109+
state: 'not-started' | 'started' | 'aborted',
110+
};
111+
112+
export type FnOptions = {
113+
afterAll?: FnHook,
114+
afterEach?: FnHook,
115+
async?: boolean,
116+
beforeAll?: FnHook,
117+
beforeEach?: FnHook,
118+
retainSamples?: boolean,
119+
signal?: AbortSignal,
120+
};
121+
122+
// A closed object catches misspelled measurement overrides.
123+
export type FnReturnedObject = {
124+
overriddenDuration?: number,
125+
overriddenIterationCost?: number,
126+
};
127+
128+
// Fantom callbacks intentionally expose a narrower return type than upstream.
129+
export type Fn = () =>
130+
Promise<void | FnReturnedObject> | void | FnReturnedObject;
131+
132+
declare export class Bench extends EventTarget {
133+
concurrency: Concurrency;
134+
iterations: number;
135+
name?: string;
136+
now: () => number;
137+
retainSamples: boolean;
138+
runtime: string;
139+
runtimeVersion: string;
140+
setup: Hook;
141+
signal?: AbortSignal;
142+
subtractTimerOverhead: boolean;
143+
teardown: Hook;
144+
threshold: number;
145+
throws: boolean;
146+
time: number;
147+
timerOverhead?: number;
148+
warmup: boolean;
149+
warmupIterations: number;
150+
warmupTime: number;
151+
readonly results: Array<Readonly<TaskResult>>;
152+
readonly tasks: Array<Task>;
153+
154+
constructor(options?: BenchOptions): this;
155+
add(name: string, fn: Fn, fnOpts?: FnOptions): this;
156+
getTask(name: string): void | Task;
157+
remove(name: string): this;
158+
reset(): void;
159+
run(): Promise<Array<Task>>;
160+
runSync(): Array<Task>;
161+
table(
162+
convert?: (task: Task) => null | Record<string, void | string | number>,
163+
): Array<null | Record<string, void | string | number>>;
164+
}
165+
}

‎package.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@
130130
"signedsource": "^2.0.0",
131131
"supports-color": "^7.1.0",
132132
"temp-dir": "^2.0.0",
133-
"tinybench": "^4.1.0",
133+
"tinybench": "^6.2.3",
134134
"tinyglobby": "^0.2.15",
135135
"typescript": "^6.0.3",
136136
"ws": "^7.5.10"

‎private/react-native-fantom/src/Benchmark.js‎

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,18 @@
88
* @format
99
*/
1010

11+
import type {SuiteResults} from './getCompletedBenchmarkResults';
12+
1113
import NativeCPUTime from '../../../packages/react-native/src/private/testing/fantom/specs/NativeCPUTime';
1214
import {reportBenchmarkResult} from '../runtime/setup';
15+
import getCompletedBenchmarkResults from './getCompletedBenchmarkResults';
1316
import {getConstants} from './index';
1417
import nullthrows from 'nullthrows';
1518
import {
1619
Bench,
1720
type BenchOptions,
1821
type FnOptions,
1922
type FnReturnedObject,
20-
type TaskResult,
2123
} from 'tinybench';
2224

2325
type SyncFn = () => FnReturnedObject | void;
@@ -56,8 +58,6 @@ type InternalTestOptions = Readonly<{
5658
only?: boolean,
5759
}>;
5860

59-
type SuiteResults = Array<Readonly<TaskResult>>;
60-
6161
type TestWithArgName<TestArgType> = string | ((testArg: TestArgType) => string);
6262

6363
type TestWithArgOptions<TestArgType> =
@@ -176,7 +176,7 @@ export function suite(
176176
}
177177

178178
const {only, ...options} = task.options ?? {};
179-
bench.add(task.name, task.fn, options);
179+
bench.add(task.name, task.fn, {...options, async: false});
180180
}
181181

182182
if (isTestOnly) {
@@ -190,13 +190,14 @@ export function suite(
190190
const runStartTime = performance.now();
191191

192192
bench.runSync();
193+
const results = getCompletedBenchmarkResults(bench.results);
193194

194195
if (!isTestOnly) {
195196
printBenchmarkResults(bench, runStartTime);
196197
}
197198

198199
for (const verify of verifyFns) {
199-
verify(bench.results);
200+
verify(results);
200201
}
201202

202203
if (!isTestOnly && !NativeCPUTime.hasAccurateCPUTimeNanosForBenchmarks()) {
@@ -215,7 +216,7 @@ export function suite(
215216
);
216217
}
217218
if (!isTestOnly) {
218-
reportBenchmarkResult(createBenchmarkResultsObject(bench, tasks));
219+
reportBenchmarkResult(createBenchmarkResultsObject(results, tasks));
219220
}
220221
});
221222

@@ -300,13 +301,13 @@ function printBenchmarkResults(bench: Bench, runStartTime: number) {
300301
}
301302

302303
function createBenchmarkResultsObject(
303-
bench: Bench,
304+
results: SuiteResults,
304305
tasks: Array<TestTask>,
305306
): BenchmarkResult {
306307
return {
307308
type: 'benchmark-result',
308309
timings: tasks.map((task, i) => {
309-
const result = bench.results[i];
310+
const result = results[i];
310311
const {min, max, mean, p50, p75, p99} = result.latency;
311312
return {
312313
name: task.name,

0 commit comments

Comments
 (0)