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
2 changes: 2 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ SKIP_ONBOARDING=false
USE_WDYR=false
USE_REDUX_DEVTOOLS=false
CAPTURE_METRICS=false
# Comma-separated Sentry span names to emit as structured release benchmark logs.
EXPO_PUBLIC_BENCHMARK_SENTRY_SPANS=
ONYX_METRICS=false
USE_THIRD_PARTY_SCRIPTS=false
IS_EXPENSIFY_EMPLOYEE=false
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ storybook-static
# E2E test reports
tests/e2e/results/
.reassure
.benchmarks/

# Temporary files created by Metro to check the health of the file watcher
.metro-health-check*
Expand Down
77 changes: 77 additions & 0 deletions contributingGuides/BENCHMARKING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<!-- cspell:ignore devicectl -->

# Native app benchmarks

The native release bundle can emit structured completion events for selected manual Sentry spans. Benchmark logging is disabled unless a developer explicitly configures span names in the git-ignored root `.env` file.

## Configure benchmark spans

Add a comma-separated list of exact Sentry span names to `.env` before building the release app:

```dotenv
EXPO_PUBLIC_BENCHMARK_SENTRY_SPANS=ManualAppStartup,ManualAppStartupNetworkRequest
```

Rebuild the release app after changing the list. Enabled spans produce one flat, machine-readable line when they end successfully:

```text
[EXPENSIFY_BENCHMARK] {"event":"span_end","span":"ManualAppStartup","durationMs":1234,"timestamp":1710000000000}
```

The app uses `console.warn` for this opt-in output because production bundles remove `console.log`, `console.info`, and `console.debug`. The Sentry console integration only forwards errors, so benchmark lines do not create Sentry log events or additional network traffic. A dedicated native logging bridge would avoid warning semantics, but would add native code solely for local tooling on both platforms.

## Run the benchmark

The benchmark runs one unmeasured warm-up followed by 20 measured cold-process launches by default. It prints each sample and summary statistics, and stores raw samples under the git-ignored `.benchmarks` directory.

Android:

```shell
nr benchmark-app-startup -- android 20 30 --span ManualAppStartup --device emulator-5554
```

iOS physical device:

```shell
nr benchmark-app-startup -- ios 20 30 --span ManualAppStartup --device "Developer's iPhone"
```

The positional arguments are platform, measured run count, and timeout in seconds. Use `--span` to measure any other span included in `EXPO_PUBLIC_BENCHMARK_SENTRY_SPANS`, `--app-id` for a nonstandard Android application ID or iOS bundle identifier, and `--output` to select another CSV path.

The script runs with Bun and also exports `benchmarkAppStartups` and `benchmarkStartups`. Other local tooling, such as the PGO workflow, can install an artifact and invoke the same benchmark implementation. The lower-level Android and iOS process tooling lives in `scripts/lib/nativeAppBenchmark.ts` so callers do not need to duplicate `adb` or `xcrun devicectl` behavior.

For example, a PGO script can install each artifact and call `benchmarkAppStartups` with its platform, device, bundle identifier, output path, and span name. This keeps artifact building and installation in the PGO workflow while sharing all startup measurement and platform-device behavior.

```ts
import {benchmarkAppStartups} from '../benchmarkAppStartup';

await benchmarkAppStartups({
platform: 'android',
rootDirectory,
deviceIdentifier,
appID: 'org.me.mobiexpensifyg',
mode: 'process',
spanName: 'ManualAppStartup',
runs,
timeoutSeconds,
outputPath,
});
```

### True-cold mode

By default, each run terminates and relaunches the existing app process without clearing persisted state. Pass `--cold` to clear state before every launch.

On Android, true-cold mode clears package data and resets compiled package state with `cmd package compile --reset`:

```shell
nr benchmark-app-startup -- android --span ManualAppStartup --cold --device emulator-5554
```

On iOS, clearing app data requires uninstalling and reinstalling the signed app, so `--app-path` is required:

```shell
nr benchmark-app-startup -- ios --span ManualAppStartup --cold --device "Developer's iPhone" --app-path /path/to/Expensify.app
```

True-cold runs start with no authenticated account or persisted application state. Cold-process runs are usually more suitable for benchmarking authenticated startup flows.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
"build": "ENV=production bun ./node_modules/.bin/rsbuild build --config config/rsbuild/rsbuild.config.ts && bun ./scripts/combine-web-sourcemaps.ts",
"build-staging": "ENV=staging bun ./node_modules/.bin/rsbuild build --config config/rsbuild/rsbuild.config.ts && bun ./scripts/combine-web-sourcemaps.ts",
"build-adhoc": "ENV=adhoc bun ./node_modules/.bin/rsbuild build --config config/rsbuild/rsbuild.config.ts && bun ./scripts/combine-web-sourcemaps.ts",
"benchmark-app-startup": "./scripts/benchmarkAppStartup.ts",
"createDocsRoutes": "bun .github/scripts/createDocsRoutes.ts",
"generateAllowedUrls": "bun .github/scripts/generateAllowedUrls.ts",
"detectRedirectCycle": "bun .github/scripts/detectRedirectCycle.ts",
Expand Down
259 changes: 259 additions & 0 deletions scripts/benchmarkAppStartup.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,259 @@
#!/usr/bin/env bun

import CLI from 'expensify-common/CLI';
import {mkdirSync, writeFileSync} from 'node:fs';
import {dirname, join, resolve} from 'node:path';
import process from 'node:process';

import type {NativeAppBenchmarkAdapter, PlatformName, StartupMode} from './lib/nativeAppBenchmark';

import {
PLATFORM_NAMES,
createNativeAppBenchmarkAdapter,
findBenchmarkDuration,
iosBenchmarkMarkerPath,
parseIosInstalledAppURL,
parseBenchmarkLogEvents,
parseIosLaunchProcessIdentifier,
parseIosRunningAppProcessIdentifier,
} from './lib/nativeAppBenchmark';

const DEFAULT_SPAN_NAME = 'ManualAppStartup';
const DEFAULT_RUNS = 20;
const DEFAULT_TIMEOUT_SECONDS = 30;

type BenchmarkStats = {
runs: number;
average: number;
p50: number;
p75: number;
p90: number;
p95: number;
p99: number;
min: number;
max: number;
};
type BenchmarkStartupsOptions = {
mode: StartupMode;
spanName: string;
runs: number;
timeoutSeconds: number;
outputPath: string;
appPath?: string;
};
type BenchmarkAppStartupsOptions = BenchmarkStartupsOptions & {
platform: PlatformName;
rootDirectory: string;
appID: string;
deviceIdentifier?: string;
};
type BenchmarkResult = {
samples: number[];
stats: BenchmarkStats;
outputPath: string;
};

const scriptPath = process.argv.at(1);
const isDirectRun = scriptPath?.endsWith('benchmarkAppStartup.ts') ?? false;
const rootDirectory = scriptPath && isDirectRun ? resolve(dirname(resolve(scriptPath)), '..') : process.cwd();

function fail(message: string): never {
throw new Error(message);
}

function parseChoice<T extends string>(value: string, choices: readonly T[], label: string): T {
const choice = choices.find((candidate) => candidate === value);
if (!choice) {
fail(`${label} must be one of: ${choices.join(', ')}. Received: ${value}`);
}
return choice;
}

function parsePositiveInteger(value: string, label: string): number {
const parsed = Number(value);
if (!Number.isSafeInteger(parsed) || parsed <= 0) {
fail(`${label} must be a positive integer. Received: ${value}`);
}
return parsed;
}

function environmentString(name: string): string | undefined {
const value: unknown = process.env[name];
return typeof value === 'string' && value.length > 0 ? value : undefined;
}

function percentile(sortedValues: number[], fraction: number): number {
const position = (sortedValues.length - 1) * fraction;
const lowerIndex = Math.floor(position);
const upperIndex = Math.ceil(position);
const remainder = position - lowerIndex;
const lowerValue = sortedValues.at(lowerIndex);
const upperValue = sortedValues.at(upperIndex);
if (lowerValue === undefined || upperValue === undefined) {
fail('Cannot calculate a percentile without benchmark samples.');
}
return lowerValue + remainder * (upperValue - lowerValue);
}

function benchmarkStats(samples: number[]): BenchmarkStats {
const sortedValues = samples.toSorted((left, right) => left - right);
const min = sortedValues.at(0);
const max = sortedValues.at(-1);
if (min === undefined || max === undefined) {
fail('No benchmark samples were recorded.');
}

return {
runs: samples.length,
average: samples.reduce((sum, value) => sum + value, 0) / samples.length,
p50: percentile(sortedValues, 0.5),
p75: percentile(sortedValues, 0.75),
p90: percentile(sortedValues, 0.9),
p95: percentile(sortedValues, 0.95),
p99: percentile(sortedValues, 0.99),
min,
max,
};
}

async function measureStartup(adapter: NativeAppBenchmarkAdapter, options: Omit<BenchmarkStartupsOptions, 'runs' | 'outputPath'>): Promise<number> {
await adapter.prepareStartup(options.mode, options.appPath);
return adapter.launchAndWait(options.spanName, options.timeoutSeconds);
}

async function benchmarkStartups(adapter: NativeAppBenchmarkAdapter, options: BenchmarkStartupsOptions): Promise<BenchmarkResult> {
console.log('=== Native app startup benchmark ===');
console.table([
{
platform: adapter.name,
device: adapter.deviceIdentifier,
appID: adapter.appID,
span: options.spanName,
mode: options.mode,
measuredRuns: options.runs,
warmUpRuns: 1,
timeoutSeconds: options.timeoutSeconds,
appPath: options.appPath ?? 'installed app',
outputPath: options.outputPath,
},
]);
console.log(`Running one unmeasured ${options.mode === 'cold' ? 'true-cold' : 'cold-process'} warm-up startup for ${options.spanName}.`);
await measureStartup(adapter, options);

const samples: number[] = [];
for (let runNumber = 1; runNumber <= options.runs; runNumber += 1) {
const duration = await measureStartup(adapter, options);
samples.push(duration);
console.log(`Run ${runNumber}/${options.runs}: ${duration}ms`);
}

const stats = benchmarkStats(samples);
mkdirSync(dirname(options.outputPath), {recursive: true});
writeFileSync(options.outputPath, ['run,duration_ms', ...samples.map((duration, index) => `${index + 1},${duration}`), ''].join('\n'));
console.table([Object.fromEntries(Object.entries(stats).map(([key, value]) => [key, value.toFixed(2)]))]);
console.log(`Recorded ${options.runs} samples in ${options.outputPath}`);
return {samples, stats, outputPath: options.outputPath};
}

async function benchmarkAppStartups(options: BenchmarkAppStartupsOptions): Promise<BenchmarkResult> {
const adapter = createNativeAppBenchmarkAdapter(options);
return benchmarkStartups(adapter, options);
}

async function main(): Promise<void> {
/* eslint-disable @typescript-eslint/naming-convention */
const cli = new CLI({
positionalArgs: [
{
name: 'platform',
description: `Native platform to benchmark (${PLATFORM_NAMES.join(', ')})`,
parse: (value): PlatformName => parseChoice(value, PLATFORM_NAMES, 'Platform'),
},
{
name: 'runs',
description: 'Number of measured startup runs',
default: DEFAULT_RUNS,
parse: (value) => parsePositiveInteger(value, 'Run count'),
},
{
name: 'timeout',
description: 'Seconds to wait for the benchmark span',
default: DEFAULT_TIMEOUT_SECONDS,
parse: (value) => parsePositiveInteger(value, 'Timeout'),
},
],
namedArgs: {
span: {
description: 'Whitelisted Sentry span name to measure',
default: DEFAULT_SPAN_NAME,
},
device: {
description: 'adb serial on Android, or CoreDevice identifier, UDID, serial number, or name on iOS',
required: false,
},
'app-id': {
description: 'Application ID or bundle identifier',
required: false,
},
'app-path': {
description: 'Path to the signed iOS .app; required with --cold on iOS',
required: false,
},
output: {
description: 'CSV output path',
required: false,
},
},
flags: {
cold: {
description: 'Run true-cold starts by clearing app data; default only terminates and relaunches the process',
},
},
});
/* eslint-enable @typescript-eslint/naming-convention */

const platform = parseChoice(String(cli.positionalArgs.platform), PLATFORM_NAMES, 'Platform');
const spanName = cli.namedArgs.span ?? DEFAULT_SPAN_NAME;
const runs = Number(cli.positionalArgs.runs);
const timeoutSeconds = Number(cli.positionalArgs.timeout);
const mode: StartupMode = cli.flags.cold ? 'cold' : 'process';
const defaultAppID = platform === 'android' ? 'org.me.mobiexpensifyg' : 'com.expensify.expensifylite';
const appID = cli.namedArgs['app-id'] ?? defaultAppID;
const appPath = cli.namedArgs['app-path'] ?? environmentString('IOS_APP_PATH');
const safeSpanName = spanName.replaceAll(/[^a-zA-Z0-9_-]/g, '-');
const outputPath = resolve(cli.namedArgs.output ?? join(rootDirectory, '.benchmarks', `${platform}-${safeSpanName}-${mode}.csv`));

await benchmarkAppStartups({
platform,
rootDirectory,
deviceIdentifier: cli.namedArgs.device,
appID,
appPath,
mode,
spanName,
runs,
timeoutSeconds,
outputPath,
});
}

if (isDirectRun) {
main().catch((error: unknown) => {
console.error(error instanceof Error ? error.message : error);
process.exitCode = 1;
});
}

export {
benchmarkAppStartups,
benchmarkStartups,
benchmarkStats,
findBenchmarkDuration,
iosBenchmarkMarkerPath,
parseIosInstalledAppURL,
parseBenchmarkLogEvents,
parseIosLaunchProcessIdentifier,
parseIosRunningAppProcessIdentifier,
percentile,
};
export type {BenchmarkAppStartupsOptions, BenchmarkResult, BenchmarkStartupsOptions, BenchmarkStats};
Loading
Loading