Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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
11 changes: 8 additions & 3 deletions flow-typed/npm/tinybench_v4.1.x.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,15 @@ declare module 'tinybench' {
beforeEach?: (this: Task) => void | Promise<void>,
};

export interface FnReturnedObject {
overriddenDuration?: number;
}
// This is defined as an interface in tinybench but we define it as an object
// to catch problems like `overriddenDuration` being misspelled.
export type FnReturnedObject = {
overriddenDuration?: number,
};

// This type is defined as returning `unknown` instead of `void` in tinybench,
// but we type it this way to avoid mistakes (we can make breaking changes
// in our definition that they can't).
export type Fn = () =>
| Promise<void | FnReturnedObject>
| void
Expand Down
40 changes: 28 additions & 12 deletions private/react-native-fantom/src/Benchmark.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,13 @@ import NativeCPUTime from 'react-native/src/private/testing/fantom/specs/NativeC
import {
Bench,
type BenchOptions,
type Fn,
type FnOptions,
type FnReturnedObject,
type TaskResult,
} from 'tinybench';

type SyncFn = () => FnReturnedObject | void;

export type SuiteOptions = $ReadOnly<{
minIterations?: number,
minDuration?: number,
Expand Down Expand Up @@ -66,20 +68,20 @@ interface ParameterizedTestFunction {
<TestArgType>(
testArgs: $ReadOnlyArray<TestArgType>,
name: TestWithArgName<TestArgType>,
fn: (testArg: TestArgType) => ReturnType<Fn>,
fn: (testArg: TestArgType) => ReturnType<SyncFn>,
options?: TestWithArgOptions<TestArgType>,
): SuiteAPI;
only: <TestArgType>(
testArgs: $ReadOnlyArray<TestArgType>,
name: TestWithArgName<TestArgType>,
fn: (testArg: TestArgType) => ReturnType<Fn>,
fn: (testArg: TestArgType) => ReturnType<SyncFn>,
options?: TestWithArgOptions<TestArgType>,
) => SuiteAPI;
}

interface TestFunction {
(name: string, fn: Fn, options?: FnOptions): SuiteAPI;
only: (name: string, fn: Fn, options?: FnOptions) => SuiteAPI;
(name: string, fn: SyncFn, options?: FnOptions): SuiteAPI;
only: (name: string, fn: SyncFn, options?: FnOptions) => SuiteAPI;
// `each` allows to run the same test multiple times with different arguments, provided as an array of values:
each: ParameterizedTestFunction;
}
Expand All @@ -91,7 +93,7 @@ interface SuiteAPI {

interface TestTask {
name: string;
fn: Fn;
fn: SyncFn;
options: InternalTestOptions | void;
}

Expand Down Expand Up @@ -174,10 +176,16 @@ export function suite(
bench.add(task.name, task.fn, options);
}

if (!isTestOnly) {
console.log(`Running benchmark: ${suiteName}. Please wait.`);
}

const runStartTime = performance.now();

bench.runSync();

if (!isTestOnly) {
printBenchmarkResults(bench);
printBenchmarkResults(bench, runStartTime);
}

for (const verify of verifyFns) {
Expand All @@ -202,20 +210,20 @@ export function suite(
reportBenchmarkResult(createBenchmarkResultsObject(bench, tasks));
});

const test = (name: string, fn: Fn, options?: FnOptions): SuiteAPI => {
const test = (name: string, fn: SyncFn, options?: FnOptions): SuiteAPI => {
tasks.push({name, fn, options});
return suiteAPI;
};

test.only = (name: string, fn: Fn, options?: FnOptions): SuiteAPI => {
test.only = (name: string, fn: SyncFn, options?: FnOptions): SuiteAPI => {
tasks.push({name, fn, options: {...options, only: true}});
return suiteAPI;
};

const testWithArg = <TestArgType>(
testArg: TestArgType,
name: TestWithArgName<TestArgType>,
fn: (testArg: TestArgType) => ReturnType<Fn>,
fn: (testArg: TestArgType) => ReturnType<SyncFn>,
options?: TestWithArgOptions<TestArgType>,
only?: boolean = false,
): TestTask => {
Expand All @@ -233,7 +241,7 @@ export function suite(
const testEach: ParameterizedTestFunction = <TestArgType>(
testArgs: $ReadOnlyArray<TestArgType>,
name: TestWithArgName<TestArgType>,
fn: (testArg: TestArgType) => ReturnType<Fn>,
fn: (testArg: TestArgType) => ReturnType<SyncFn>,
options?: TestWithArgOptions<TestArgType>,
): SuiteAPI => {
for (const testArg of testArgs) {
Expand Down Expand Up @@ -262,16 +270,24 @@ export function suite(
return suiteAPI;
}

function printBenchmarkResults(bench: Bench) {
function printBenchmarkResults(bench: Bench, runStartTime: number) {
const {fantomConfigSummary} = getConstants();
const benchmarkName =
(bench.name ?? 'Benchmark') +
(fantomConfigSummary ? ` (${fantomConfigSummary})` : '');

const runDuration = performance.now() - runStartTime;
const durationStr =
runDuration < 1000
? `${runDuration.toFixed(0)}ms`
: `${(runDuration / 1000).toFixed(0)}s`;

console.log('');
console.log(`### ${benchmarkName} ###`);
console.table(nullthrows(bench.table()));
console.log('');
console.log(`Total benchmark duration: ${durationStr}`);
console.log('');
}

function createBenchmarkResultsObject(
Expand Down
Loading