diff --git a/android/app/src/main/java/com/bitpay/wallet/MainApplication.kt b/android/app/src/main/java/com/bitpay/wallet/MainApplication.kt index f5e879f78c..cba34243c7 100644 --- a/android/app/src/main/java/com/bitpay/wallet/MainApplication.kt +++ b/android/app/src/main/java/com/bitpay/wallet/MainApplication.kt @@ -41,7 +41,7 @@ class MainApplication : Application(), ReactApplication { super.onCreate() val context: Context = this loadReactNative(context) - PerfMarks.startJsProfiler() + PerfMarks.startJsProfiler(context) // Set custom OkHttpClient OkHttpClientProvider.setOkHttpClientFactory(UserAgentClientFactory(context)) diff --git a/android/app/src/noperf/java/com/bitpay/wallet/PerfMarks.kt b/android/app/src/noperf/java/com/bitpay/wallet/PerfMarks.kt index 341c917b78..516a9327f4 100644 --- a/android/app/src/noperf/java/com/bitpay/wallet/PerfMarks.kt +++ b/android/app/src/noperf/java/com/bitpay/wallet/PerfMarks.kt @@ -1,5 +1,7 @@ package com.bitpay.wallet +import android.content.Context + /** * Every build except perf builds: the harness's native marks do nothing, and * react-native-performance is not linked at all (react-native.config.js). @@ -9,7 +11,7 @@ package com.bitpay.wallet object PerfMarks { fun mark(@Suppress("UNUSED_PARAMETER") name: String) {} - fun startJsProfiler() {} + fun startJsProfiler(@Suppress("UNUSED_PARAMETER") context: Context) {} fun processStart() {} } diff --git a/android/app/src/perf/java/com/bitpay/wallet/PerfMarks.kt b/android/app/src/perf/java/com/bitpay/wallet/PerfMarks.kt index c264410fca..84c40297a6 100644 --- a/android/app/src/perf/java/com/bitpay/wallet/PerfMarks.kt +++ b/android/app/src/perf/java/com/bitpay/wallet/PerfMarks.kt @@ -1,5 +1,9 @@ package com.bitpay.wallet +import android.content.BroadcastReceiver +import android.content.Context +import android.content.Intent +import android.content.IntentFilter import android.os.Process import com.oblador.performance.RNPerformance @@ -23,16 +27,49 @@ object PerfMarks { * runtime exists, so bundle evaluation is sampled too. Dumped by * AppPerfModule.dumpJsProfile once the app settles. */ - fun startJsProfiler() { + fun startJsProfiler(context: Context) { if (BuildConfig.PERF_PROFILE) { try { com.facebook.hermes.instrumentation.HermesSamplingProfiler.enable() } catch (t: Throwable) { android.util.Log.w("BitPayPerf", "sampling profiler unavailable: $t") } + registerProfileReceiver(context) } } + /** + * Profiling builds only: lets the harness bracket an interaction (a + * Maestro flow) with a fresh sampled trace, after the startup trace has + * been dumped and sampling stopped: + * + * adb shell am broadcast -a com.bitpay.wallet.PERF_JS_PROFILE --es cmd start + * adb shell am broadcast -a com.bitpay.wallet.PERF_JS_PROFILE --es cmd dump + * + * "dump" writes /bp-flow-profile.json and stops sampling. + */ + private fun registerProfileReceiver(context: Context) { + val receiver = object : BroadcastReceiver() { + override fun onReceive(c: Context, intent: Intent) { + val profiler = com.facebook.hermes.instrumentation.HermesSamplingProfiler + try { + when (intent.getStringExtra("cmd")) { + "start" -> profiler.enable() + "dump" -> { + val file = java.io.File(c.getExternalFilesDir(null), "bp-flow-profile.json") + profiler.dumpSampledTraceToFile(file.absolutePath) + profiler.disable() + } + } + android.util.Log.i("BitPayPerf", "flow profile: ${intent.getStringExtra("cmd")} ok") + } catch (t: Throwable) { + android.util.Log.w("BitPayPerf", "flow profile: $t") + } + } + } + context.registerReceiver(receiver, IntentFilter("com.bitpay.wallet.PERF_JS_PROFILE")) + } + /** The real process start, which react-native-performance only estimates. */ fun processStart() { if (BuildConfig.PERF_BUILD) { diff --git a/perf/cli.js b/perf/cli.js index d1114813b1..573152739c 100755 --- a/perf/cli.js +++ b/perf/cli.js @@ -194,6 +194,17 @@ async function runIteration(ctx, scenario) { // Startup launches clear the log themselves; interaction runs must clear // it here so the collector only sees this iteration's payloads. android.clearLogcat(serial); + // Activity scenarios diff the app's 2 s activity snapshots; let one + // land while the app is idle so the flow has a clean starting point. + if ((scenario.measures || []).includes('activity')) { + await sleep(2600); + } + // The process's memory mode moves PSS, jank and peak CPU of a whole + // run (one process serves every iteration): record it so a mismatch + // between two runs is visible. Context only — compare.js never judges it. + if (wantsMemory) { + metrics.unknown_mb_start = android.readMemoryMb(serial).unknown_mb; + } android.resetGfxInfo(serial); startSampling(); visibleAnchor = Date.now(); @@ -288,6 +299,14 @@ async function runIteration(ctx, scenario) { } let profile = null; + if ((scenario.measures || []).includes('activity')) { + // The final snapshot can trail the flow by up to one interval. + await sleep(2000); + payloads = collector.collect(serial); + const activity = collector.extractActivity(payloads); + Object.assign(metrics, activity.metrics); + profile = activity.profile; + } if ((scenario.measures || []).includes('load')) { const load = collector.extractLoadProfile(payloads, ctx.moduleMap); Object.assign(metrics, load.metrics); diff --git a/perf/collector.js b/perf/collector.js index 4daaab768c..c1cbc5d238 100644 --- a/perf/collector.js +++ b/perf/collector.js @@ -347,7 +347,71 @@ function bucketModulesByPhase(startupPayload, mods, moduleMap) { ); } +/** + * What one interaction flow loaded, from the app's post-settle 'activity' + * snapshots (PerfMonitor.flushActivity): the first snapshot of the iteration + * (taken while idle, before the flow starts) against the last. + */ +function extractActivity(payloads) { + const snaps = (payloads || []) + .filter(p => p.kind === 'activity') + .sort((a, b) => a.now - b.now); + if (snaps.length < 2) { + return {metrics: {}, profile: null}; + } + const first = snaps[0]; + const last = snaps[snaps.length - 1]; + const reqs = snaps + .slice(1) + .flatMap(p => p.requests || []) + .filter(r => r.startMs >= first.now); + const round = v => Number(v.toFixed(1)); + const metrics = { + requests_flow: last.requestsStarted - first.requestsStarted, + request_kb_flow: round(reqs.reduce((s, r) => s + (r.bytes || 0), 0) / 1024), + actions_flow: last.actionTotal - first.actionTotal, + action_ms_flow: round(last.actionMs - first.actionMs), + }; + if (typeof last.modules === 'number' && typeof first.modules === 'number') { + metrics.modules_flow = last.modules - first.modules; + } + const heaps = snaps.map(p => p.heapMb).filter(Number.isFinite); + if (heaps.length) { + metrics.js_heap_mb_peak_flow = round(Math.max(...heaps)); + } + + const endpoints = {}; + reqs.forEach(r => { + const key = `${r.method} ${r.url}`; + const e = + endpoints[key] || + (endpoints[key] = {count: 0, totalMs: 0, bytes: 0, failed: 0}); + e.count++; + e.totalMs += r.durationMs || 0; + e.bytes += r.bytes || 0; + if (!r.status || r.status >= 400) { + e.failed++; + } + }); + // Per-type action deltas: the latest full map against the first one. + const firstActions = (snaps.find(p => p.actions) || {}).actions || {}; + const lastActions = + ([...snaps].reverse().find(p => p.actions) || {}).actions || {}; + const actions = {}; + Object.entries(lastActions).forEach(([type, a]) => { + const before = firstActions[type] || {count: 0, ms: 0}; + if (a.count > before.count) { + actions[type] = { + count: a.count - before.count, + ms: round(a.ms - before.ms), + }; + } + }); + return {metrics, profile: {requests: endpoints, actions}}; +} + module.exports = { + extractActivity, collect, extractStartupTimeline, extractTransitions, diff --git a/perf/config.js b/perf/config.js index 4e560755e1..bd0e14ee85 100644 --- a/perf/config.js +++ b/perf/config.js @@ -78,6 +78,13 @@ const THRESHOLDS = { action_ms_settled: {direction: 'lower', tolerance: 0.15}, js_heap_mb_settled: {direction: 'lower', tolerance: 0.1}, store_kb_settled: {direction: 'lower', tolerance: 0.05}, + // Interaction activity (trade-* scenarios): what one flow loaded. + requests_flow: {direction: 'lower', tolerance: 0.1}, + request_kb_flow: {direction: 'lower', tolerance: 0.1}, + actions_flow: {direction: 'lower', tolerance: 0.1}, + action_ms_flow: {direction: 'lower', tolerance: 0.15}, + modules_flow: {direction: 'lower', tolerance: 0.05}, + js_heap_mb_peak_flow: {direction: 'lower', tolerance: 0.1}, /** Sizes are deterministic — zero noise, so a tight tolerance is safe. */ apk_size_bytes: {direction: 'lower', tolerance: 0.05}, bundle_size_bytes: {direction: 'lower', tolerance: 0.05}, @@ -133,6 +140,30 @@ const SCENARIOS = { measures: ['transitions', 'frames', 'cpu', 'memory'], description: 'Hops across the bottom tab bar, timing each transition.', }, + // Buy / Sell / Swap, opened from Home's buttons. Each flow picks a + // currency, enters an amount, waits for the quotes, then backs out — it + // never continues to a checkout, signs or submits anything (real wallet). + 'trade-buy': { + flow: 'trade_buy.yaml', + launchMode: 'none', + measures: ['transitions', 'frames', 'cpu', 'memory', 'activity'], + description: + 'Home → Buy: default currency, amount pill, wait for provider offers, back.', + }, + 'trade-sell': { + flow: 'trade_sell.yaml', + launchMode: 'none', + measures: ['transitions', 'frames', 'cpu', 'memory', 'activity'], + description: + 'Home → Sell: pick the SOL wallet, type 0.3 SOL, wait for provider offers, back.', + }, + 'trade-swap': { + flow: 'trade_swap.yaml', + launchMode: 'none', + measures: ['transitions', 'frames', 'cpu', 'memory', 'activity'], + description: + 'Home → Swap: currencies load, pick To = BTC wallet, open the amount sheet, back (no quotes: see trade_swap.yaml).', + }, 'scroll-home': { flow: 'scroll_home.yaml', launchMode: 'none', diff --git a/perf/drivers/android.js b/perf/drivers/android.js index 503819a18a..cae5f7b163 100644 --- a/perf/drivers/android.js +++ b/perf/drivers/android.js @@ -519,6 +519,9 @@ function readMemoryMb(serial) { (out.match(/^\s*TOTAL\s+(\d+)/m) || [])[1]; const javaHeap = (out.match(/Java Heap:\s*(\d+)/) || [])[1]; const nativeHeap = (out.match(/Native Heap:\s*(\d+)/) || [])[1]; + // App summary's "Unknown" row: bimodal per process on the test phone + // (~136 or ~290 MB at launch, whatever the build), see perf/loop/FINDINGS-trade.md. + const unknown = (out.match(/^\s*Unknown:\s*(\d+)/m) || [])[1]; const toMb = kb => (kb ? Number((Number(kb) / 1024).toFixed(1)) : null); @@ -526,6 +529,7 @@ function readMemoryMb(serial) { pss_mb: toMb(totalPss), java_heap_mb: toMb(javaHeap), native_heap_mb: toMb(nativeHeap), + unknown_mb: toMb(unknown), }; } diff --git a/perf/loop/FINDINGS-trade.md b/perf/loop/FINDINGS-trade.md new file mode 100644 index 0000000000..c799caf3b1 --- /dev/null +++ b/perf/loop/FINDINGS-trade.md @@ -0,0 +1,121 @@ +# Buy / Sell / Swap loop — findings + +Second instance of the self-healing loop (`LOOP.md`), on the screens opened +from Home's Buy, Sell and Swap buttons. Branch `perf/buy-sell-swap`, based on +`perf/self-healing` at `54d9d06bb` (R8 on, shipped baseline profile applied +after install). Same device (Motorola one 5G ace, Android 11), same funded +test wallet ($269, 30 wallets), shared through `device-lock.sh`. + +Every number is a median of 10 runs of one scenario (first run discarded); +a change counts only beyond 2× the larger noise (see `compare.js`). + +## Scenarios (`perf/scenarios/trade_*.yaml`) + +| Scenario | Flow | Headline metrics | +|---|---|---| +| `trade-buy` | Home → Buy (opens on the SOL wallet with a preset $200, quotes at once) → wait for the best offer → `$100` pill → wait for the new best offer → back | `BuyCurrencies` span (spinner until the coin list is built), `BuyQuotes` span (quote search → best offer shown) | +| `trade-sell` | Home → Sell → Choose Crypto → Solana Account → SOL → type `.3` (0.3 SOL) → wait for the best offer → back | `SellCurrencies`, `SellQuotes` | +| `trade-swap` | Home → Swap (from SOL) → To: Bitcoin → first BTC wallet → open the amount sheet → back | `SwapCurrencies` (init → supported coins set), `SwapCryptoRoot` transition | + +All three also report frames (janky %, p95/p99), JS/UI/render-thread CPU, +PSS, and what the flow loaded (`requests_flow`, `request_kb_flow`, +`actions_flow`, `modules_flow`, `js_heap_mb_peak_flow`, per endpoint). + +**Funds safety.** Flows never tap "Continue with …" (provider checkout) or +"Slide to swap". Buy/Sell quotes are read-only (`*GetQuote`, +`*GetSellQuote`). **Swap quotes are not measured**: as soon as Changelly is +the best offer, `SwapCryptoRoot` creates a fixed-rate exchange order at +Changelly (`changellyCreateFixTransaction`, sending the wallet's payout and +refund addresses) — viewing a quote submits data to a provider, so +`trade-swap` stops before an amount is committed. + +## Harness added + +- `PerfMonitor`: post-settle `activity` snapshots every 2 s (requests, + actions, modules, heap), diffed per flow by the harness; `spanStart` / + `spanEnd` for in-screen work, reported as pseudo-screens. +- `flowprofile.sh`: Hermes CPU profile of a Maestro flow (profiling builds + register a `PERF_JS_PROFILE` broadcast receiver to restart/dump the + sampler); spans become `jsprofile.js` phases. +- `iterate.sh`: one accepted run per scenario set + (`state/accepted-.json`); `compare.js` judges per-screen + transitions and spans (`transition__ms`). + +## Baselines (`e45bab517`, 10 runs each) + +| Scenario | Headline | Frames | Load per flow | +|---|---|---|---| +| trade-buy | BuyCurrencies **3,492 ± 23 ms**; BuyQuotes **6,736 ± 130 ms**; transition 290 ms | janky 2.2%, p95 13, p99 21.5 ms; JS 16.7% | 22 requests, 586 KB (Transak fiat currencies 407 KB ×2, Sardine limits 156 KB ×2) | +| trade-sell | SellCurrencies 460 ms; SellQuotes **5,930 ± 24 ms** | janky 2.6%, p99 23 ms | 5 requests, 269 KB | +| trade-swap | SwapCurrencies **1,305 ± 36 ms** (no network: prefetch cache fresh); transition 455 ms | janky 2.6%, p99 27 ms; JS 10.9% | 2 requests | + +## Fixes + +| # | Area | Change | Measured | Commit | +|---|---|---|---|---| +| T1 | Swap | Currency logos looked up from a coin+chain index (built once per token map) instead of `Object.values(tokenDataByAddress).find` per currency | SwapCurrencies **1305 → 220 ms (−83%)**, JS thread 10.9 → 8.5% (p<.001) | `15101aac7` | +| T2 | Swap | Supported-token list (~21k symbols) memoised on the token list instead of rebuilt every render | vs a base run in the same process memory mode, 3/3 candidate runs KEEP: SwapCryptoRoot transition **481 → 125 ms (−74%)**, SwapCurrencies 232 → ~115 ms, janky 3.8 → 2.8–3.2%; JS heap growth per Swap visit ~8 → ~2.6 MB | `f3b689630` | +| T3 | Buy, Sell | Best offer picked as soon as the last participating provider finishes (the 3.5 s settle wait stays while any is still quoting, and for re-runs with unchanged flags, e.g. a payment-method change) | **BuyQuotes 6736 → 3230 ms (−52%)**, **SellQuotes 5930 → 2464 ms (−58%)** (p<.001). compare.js REJECT overridden: CPU % rose because the flows got 4–8 s shorter (Buy CPU-seconds fell: process 36.1 → 33.1, JS 8.78 → 8.57); Sell PSS/JS-CPU-s were the high memory mode (unknown 460 vs 196 MB; same mode costs +1.4 JS CPU-s on an unchanged build); Buy request KB was BWS's ramp/assets size | `ea44b8dbf` | +| T4 | Buy, Sell | Buy coin list: the merged token map is built once per init (was: a ~21k-key spread per ERC-20 coin) and both token lookups use the coin+chain index | **BuyCurrencies 3418 → 42 ms (−99%)**, JS heap peak 128 → 92 MB (p<.001); SellCurrencies 484 → 419 ms (p=.005) | `c72a57a89` | + +Regression check on the main loop's interaction scenario: `nav-tabs`, base +`54d9d06bb` APK vs HEAD `c72a57a89` APK (all four fixes + the trade +instrumentation, incl. the 2 s post-settle activity snapshots), 10 runs +each: **NEUTRAL** — no metric moved beyond noise. + +## Candidates + +| ID | Where | Evidence | Status | +|---|---|---|---| +| TC1 | Swap: `tokenOptions` (~21k symbols) rebuilt on every `SwapCryptoRoot` render | flow profile: 895 ms JS per flow in the render body after init (picker, wallet pick, amount sheet) | **fixed (T2)** — first judged REJECT on PSS +100 MB / janky +0.5 / peak CPU; all three were the process memory mode (below) | +| TC2 | Buy/Sell: best offer chosen 3,500 ms after the last provider-state change, even when every provider has answered | `externalServicesOfferSelector` effects; Swap's selector already picks as soon as all exchanges finish | **fixed (T3)** | +| TC3 | Buy/Sell: 2,000 ms debounce before quoting, also for a single pill tap | same file, `immediateQuote ? 0 : 2000`; after T3 it is ~60% of BuyQuotes (3.2 s) | open — expected −2 s on a pill tap; keep the debounce for keypad typing | +| TC4 | Buy init: per ERC-20 coin, `Object.values({...BitpaySupportedTokens, ...tokenDataByAddress}).find`; `getLogoUri` scans every token | flow profile: 1,911 ms synchronous in the coin-list map (1,052 spread+find, 412 getLogoUri) | **fixed (T4)** | +| TC5 | Buy: Transak fiat currencies (407 KB), Sardine limits (156 KB), Ramp assets (3–287 KB) re-downloaded on every quote round | activity profile: same endpoints ×2 per flow (one per quote round) | open — cache per session with a TTL; mostly bandwidth, the requests run in parallel with the quotes | +| TC6 | `BuyAndSellRoot`: `tokenOptions` rebuilt on every render (every keypad digit) | same pattern as TC1 (T2 cut Swap's transition by 74%) | open — next | +| TC7 | Swap opens on the "To" `GlobalSelect`: ~150 ms JS per open (`GlobalSelect.tsx:1131-1142`, account-list signatures) | flow profile, "end SwapCryptoRoot → start Home" phase | open | +| TC8 | JS heap grows ~8 MB per Swap visit on the base build (84 → 156 MB over 10 visits); ~2.6 MB after T2 | per-iteration `js_heap_mb_peak_flow` | open — check for retained closures after T2 | + +## Measurement notes + +- **Process memory mode.** The app's "Unknown" memory (dumpsys meminfo) is + ~136 MB in some cold launches and ~290 MB (once ~470 MB) in others, for + both base and candidate builds, before any trade screen is opened (4 + launches of each). Interaction scenarios serve all 10 iterations from one + process, so each run is a single draw: the high mode alone moved base + trade-swap PSS 695 → 817 MB, janky 2.5 → 3.75%, peak CPU 107 → 182%. + Runs now record `unknown_mb_start` (context only, never judged); compare + PSS/jank only between runs in the same mode. +- **CPU percentages are rates.** A fix that shortens the flow raises + `*_cpu_pct` for the same work; judge CPU by percent × `visible_ms` + (CPU-seconds) when the flow length changes. +- **Server-side sizes drift.** BWS `ramp/assets` answered 3 KB during one + baseline and 287 KB later (same request count), moving `request_kb_flow` + by ±284 KB. + +## Parked — needs a decision + +- **Swap quotes create Changelly orders.** Showing a Swap quote whose best + offer is Changelly immediately calls `changellyCreateFixTransaction` (the + `selectedOffer` effect in `SwapCryptoRoot`), registering a fixed-rate + order with the wallet's payout and refund addresses before the user has + committed. Not measured here for that reason; whether orders should be + created only on "Slide to swap" (with the fixed rate fetched separately) + is a product/provider decision. +- **Indistinguishable BTC wallets in "Select Wallet to Deposit to".** For + UTXO chains `buildAccountList` names the account after the wallet + (`walletName`, here "Bitcoin" for all three of the key's BTC wallets) and + `AccountListRow` shows only "BTC" beneath it, so the three rows read + "Bitcoin BTC 0.00" ×3 (screenshot: `x-swap-1.png` in the session scratch + dir). Selection works (verified with adb taps). Same on both platforms — + a labelling gap, not an Android layout bug; what to show (account index, + address type) is a product call. + +## Observations + +- Home's LINK row shows empty placeholder pills instead of a day change + (the wallet perf/loop/FINDINGS.md lists under B5, invalid history) — main-loop + scope. +- An earlier Maestro tap at 61% height in the BTC wallet sheet hit the + backdrop and closed it — looked like a selection bug, was the flow; the + flow taps the first row at 73%. diff --git a/perf/loop/compare.js b/perf/loop/compare.js index 69b9d86633..a05153dd46 100755 --- a/perf/loop/compare.js +++ b/perf/loop/compare.js @@ -45,6 +45,9 @@ const POSITIONAL = /(_start_ms|_end_ms|_done_ms|_checked_ms|_route_ms|_success_ms|_render_ms|_commit_ms|^process_start_ms|^content_appeared_ms|^js_entry_ms|^store_ready_ms|^rehydrate_ms|^app_init_ms|^nav_ready_ms)$/; // Too noisy or harness-dominated to judge on. const IGNORE = new Set(['wall_clock_ms', 'am_wait_time_ms', 'visible_ms']); +// Process state, not cost: printed so a memory-mode mismatch between the two +// runs is visible, never counted as a regression or a win. +const CONTEXT = new Set(['unknown_mb_start']); // Small absolute moves in these never matter, whatever the noise says. const MIN_ABS = {_ms: 5, _mb: 1, _kb: 2, _pct: 0.5}; // CPU totals are ~10 ms-granular (clock ticks); ignore single-tick moves. @@ -151,8 +154,20 @@ function compareRuns(accepted, candidate, targets = []) { if (!base) { return; } - Object.entries(cand.metrics || {}).forEach(([key, c]) => { - const b = (base.metrics || {})[key]; + // Per-screen transitions (and the app's spans, which report the same + // way) compare as transition__ms. + const withScreens = run => ({ + ...(run.metrics || {}), + ...Object.fromEntries( + Object.entries(run.perScreen || {}).map(([screen, v]) => [ + `transition_${screen}_ms`, + v.transition_ms, + ]), + ), + }); + const baseMetrics = withScreens(base); + Object.entries(withScreens(cand)).forEach(([key, c]) => { + const b = baseMetrics[key]; if (IGNORE.has(key) || !b || c.median == null || b.median == null) { return; } @@ -180,7 +195,9 @@ function compareRuns(accepted, candidate, targets = []) { real, better, // phase_* are durations, whatever their names end in. - positional: !key.startsWith('phase_') && POSITIONAL.test(key), + positional: + CONTEXT.has(key) || + (!key.startsWith('phase_') && POSITIONAL.test(key)), target: targets.includes(key), }); }); diff --git a/perf/loop/flowprofile.sh b/perf/loop/flowprofile.sh new file mode 100644 index 0000000000..8984e81c90 --- /dev/null +++ b/perf/loop/flowprofile.sh @@ -0,0 +1,109 @@ +#!/bin/bash +# +# Symbolicated JS CPU profiles of an interaction flow (a Maestro scenario), +# not of startup: cold start, let startup settle (its own trace is dumped and +# sampling stops), then restart the Hermes sampler, run the flow, dump. +# +# perf/loop/flowprofile.sh --flow trade_swap.yaml [--runs N] [--no-build] +# +# Needs a PROFILING perf build (BP_PERF_PROFILE=1), whose PerfMarks registers +# the PERF_JS_PROFILE broadcast receiver used here. Each run's spans and +# transitions become the phase marks for jsprofile.js, so samples are +# grouped as e.g. "nav_start:SwapCryptoRoot → span_end:SwapCurrencies". + +set -uo pipefail +# Build first (no device needed), then hold the shared phone for the rest +# (see device-lock.sh): re-exec under the lock with --no-build. +if [ -z "${BP_DEVICE_LOCKED:-}" ]; then + if [[ " $* " != *" --no-build "* ]]; then + ROOT0="$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd)" + echo "▸ profiling build" + if ! (cd "$ROOT0" && BP_PERF_PROFILE=1 yarn -s build:android:perf \ + > "$ROOT0/perf/loop/state/flowprofile-build.log" 2>&1); then + echo "BUILD FAILED — see perf/loop/state/flowprofile-build.log" >&2 + exit 1 + fi + set -- "$@" --no-build + fi + export BP_DEVICE_LOCKED=1 + exec "$(dirname "${BASH_SOURCE[0]}")/device-lock.sh" "$0" "$@" +fi + +ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd)" +STATE="$ROOT/perf/loop/state" +APK="$ROOT/android/app/build/outputs/apk/release/app-release.apk" +APP_ID=com.bitpay.wallet +REMOTE="/sdcard/Android/data/$APP_ID/files/bp-flow-profile.json" +ACTION=com.bitpay.wallet.PERF_JS_PROFILE + +FLOW="" +RUNS=3 +while [ $# -gt 0 ]; do + case "$1" in + --flow) FLOW="$2"; shift ;; + --runs) RUNS="$2"; shift ;; + --no-build) ;; + *) echo "unknown option: $1" >&2; exit 64 ;; + esac + shift +done +[ -n "$FLOW" ] || { echo "--flow is required" >&2; exit 64; } + +cd "$ROOT" +STAMP="$(date +%Y%m%d-%H%M%S)" +OUT="$STATE/flowprofile-$STAMP-${FLOW%.yaml}" +mkdir -p "$OUT" + +cp android/app/build/generated/sourcemaps/react/release/index.android.bundle.map "$OUT/bundle.map" +adb install -r "$APK" > /dev/null +if [ "${BP_PERF_COMPILE:-}" != none ]; then + adb shell am broadcast -a androidx.profileinstaller.action.INSTALL_PROFILE \ + -n "$APP_ID/androidx.profileinstaller.ProfileInstallReceiver" > /dev/null + adb shell cmd package compile -m speed-profile -f "$APP_ID" > /dev/null +fi + +adb shell svc power stayon usb +adb shell input keyevent KEYCODE_WAKEUP +adb shell settings put global window_animation_scale 0 +adb shell settings put global transition_animation_scale 0 +adb shell settings put global animator_duration_scale 0 + +for i in $(seq 1 "$RUNS"); do + echo "▸ run $i/$RUNS" + adb shell am force-stop $APP_ID + adb shell rm -f "$REMOTE" + sleep 2 + adb shell am start -n "$APP_ID/.LaunchActivity" > /dev/null + # Startup settles ~10 s after the splash; its trace is dumped then. + sleep 25 + adb logcat -c + adb shell am broadcast -a $ACTION --es cmd start > /dev/null + maestro --device "$(adb get-serialno)" test "perf/scenarios/$FLOW" > "$OUT/maestro-$i.log" 2>&1 \ + || echo " flow failed — see $OUT/maestro-$i.log" >&2 + sleep 3 # let the 2 s navigation flush emit the last spans + adb shell am broadcast -a $ACTION --es cmd dump > /dev/null + sleep 2 + if ! adb pull "$REMOTE" "$OUT/trace-$i.json" > /dev/null 2>&1; then + echo " no trace: $(adb logcat -d -s BitPayPerf:* | grep 'flow profile' | tail -1)" >&2 + continue + fi + # Navigation/span measures → phase marks for jsprofile.js. + node -e " + const c = require('$ROOT/perf/collector'); + const entries = []; + c.collect().filter(p => p.kind === 'navigation').forEach(p => + (p.entries || []).forEach(e => { + const name = e.name.replace(/^bp_nav:/, ''); + entries.push({name: 'bp_start ' + name, entryType: 'mark', startTime: e.startTime}); + entries.push({name: 'bp_end ' + name, entryType: 'mark', startTime: e.startTime + e.duration}); + })); + require('fs').writeFileSync('$OUT/marks-$i.json', JSON.stringify({entries})); + require('fs').writeFileSync('$OUT/spans-$i.json', JSON.stringify(entries)); + " +done + +adb shell settings put global window_animation_scale 1 +adb shell settings put global transition_animation_scale 1 +adb shell settings put global animator_duration_scale 1 + +node "$ROOT/perf/loop/jsprofile.js" "$OUT" --top 15 diff --git a/perf/loop/iterate.sh b/perf/loop/iterate.sh index 6a9ee274b0..25a7ec3f4a 100755 --- a/perf/loop/iterate.sh +++ b/perf/loop/iterate.sh @@ -42,6 +42,14 @@ while [ $# -gt 0 ]; do shift done +# One accepted baseline per scenario set: startup-load keeps accepted.json, +# anything else gets accepted-.json. +if [ "$SCENARIOS" = startup-load ]; then + ACCEPTED="$STATE/accepted.json" +else + ACCEPTED="$STATE/accepted-${SCENARIOS//,/+}.json" +fi + cd "$ROOT" STAMP="$(date +%Y%m%d-%H%M%S)" SHA="$(git rev-parse --short HEAD)$(git diff --quiet HEAD -- . ':!perf/loop' || echo '-dirty')" @@ -67,8 +75,8 @@ if [ $RUN_EXIT -ne 0 ]; then exit 1 fi -if [ -f "$STATE/accepted.json" ]; then - node "$ROOT/perf/loop/compare.js" "$STATE/accepted.json" "$STATE/run-$STAMP-$SHA.json" \ +if [ -f "$ACCEPTED" ]; then + node "$ROOT/perf/loop/compare.js" "$ACCEPTED" "$STATE/run-$STAMP-$SHA.json" \ ${TARGET:+--target "$TARGET"} VERDICT=$? else @@ -78,8 +86,8 @@ else fi if [ "$ACCEPT" = 1 ]; then - cp "$STATE/run-$STAMP-$SHA.json" "$STATE/accepted.json" - echo "$STAMP $SHA" >> "$STATE/accepted.log" + cp "$STATE/run-$STAMP-$SHA.json" "$ACCEPTED" + echo "$STAMP $SHA $(basename "$ACCEPTED")" >> "$STATE/accepted.log" echo "▸ accepted run-$STAMP-$SHA.json" fi diff --git a/perf/scenarios/trade_buy.yaml b/perf/scenarios/trade_buy.yaml new file mode 100644 index 0000000000..a766209558 --- /dev/null +++ b/perf/scenarios/trade_buy.yaml @@ -0,0 +1,51 @@ +appId: com.bitpay.wallet +--- +# Home → Buy → wait for the best offer → change the amount with a pill → +# wait for the new best offer → back to Home. +# +# REAL WALLET: this flow must never tap "Continue with …" (that opens the +# provider checkout). It only lets quotes load. Quote requests are read-only +# (`*GetQuote`); nothing is created at a provider until checkout. +# +# Buy opens on the default wallet with a preset amount and quotes at once; +# the pill then exercises the "user changed the amount" path (debounced). +# Timings come from the app's spans (BuyCurrencies, BuyQuotes) and the +# BuyAndSellRoot transition. + +# A failed earlier iteration can leave the app on a trade screen: back out +# to Home first (each Back only while Home is not showing). +- repeat: + times: 4 + commands: + - runFlow: + when: + notVisible: + id: 'portfolio-balance-toggle' + commands: + - back + - waitForAnimationToEnd +# Home can be left scrolled far down (it keeps its scroll position). +- scrollUntilVisible: + element: + id: 'portfolio-balance-toggle' + direction: UP + speed: 90 + timeout: 30000 +- tapOn: + id: 'buy-button' +- extendedWaitUntil: + visible: 'Continue with .*' + timeout: 45000 +- tapOn: '\$100' +- extendedWaitUntil: + visible: 'Continue' + timeout: 5000 +- extendedWaitUntil: + visible: 'Continue with .*' + timeout: 45000 +- back +- extendedWaitUntil: + visible: + id: 'portfolio-balance-toggle' + timeout: 15000 +- waitForAnimationToEnd diff --git a/perf/scenarios/trade_sell.yaml b/perf/scenarios/trade_sell.yaml new file mode 100644 index 0000000000..9e1f0e102c --- /dev/null +++ b/perf/scenarios/trade_sell.yaml @@ -0,0 +1,58 @@ +appId: com.bitpay.wallet +--- +# Home → Sell → Choose Crypto → Solana account → SOL → 0.3 SOL → wait for +# the best offer → back to Home. +# +# REAL WALLET: never tap "Continue with …" (provider checkout). Sell quotes +# are read-only (`*GetSellQuote`). + +# A failed earlier iteration can leave the app on a trade screen: back out +# to Home first (each Back only while Home is not showing). +- repeat: + times: 4 + commands: + - runFlow: + when: + notVisible: + id: 'portfolio-balance-toggle' + commands: + - back + - waitForAnimationToEnd +# Home can be left scrolled far down (it keeps its scroll position). +- scrollUntilVisible: + element: + id: 'portfolio-balance-toggle' + direction: UP + speed: 90 + timeout: 30000 +- tapOn: + id: 'sell-button' +- extendedWaitUntil: + visible: 'Choose Crypto' + timeout: 30000 +- tapOn: 'Choose Crypto' +- extendedWaitUntil: + visible: 'Solana Account.*' + timeout: 15000 +- tapOn: 'Solana Account.*' +# The SOL row is the one carrying the "Gas Token" badge. +- extendedWaitUntil: + visible: 'Gas Token' + timeout: 10000 +- tapOn: 'Gas Token' +# Sell takes the amount in SOL here (fiat pills hidden): type 0.3 SOL +# (~$31; the balance is ~0.56 SOL). +- extendedWaitUntil: + visible: 'MAX' + timeout: 30000 +- tapOn: '\.' +- tapOn: '3' +- extendedWaitUntil: + visible: 'Continue with .*' + timeout: 45000 +- back +- extendedWaitUntil: + visible: + id: 'portfolio-balance-toggle' + timeout: 15000 +- waitForAnimationToEnd diff --git a/perf/scenarios/trade_swap.yaml b/perf/scenarios/trade_swap.yaml new file mode 100644 index 0000000000..db3ae94ef8 --- /dev/null +++ b/perf/scenarios/trade_swap.yaml @@ -0,0 +1,80 @@ +appId: com.bitpay.wallet +--- +# Home → Swap (from: default SOL wallet) → To: Bitcoin → first BTC wallet → +# open the amount sheet → back to Home. +# +# REAL WALLET: this flow stops BEFORE an amount is committed. Once a quote's +# best offer is Changelly, SwapCryptoRoot immediately creates a fixed-rate +# exchange order at Changelly (changellyCreateFixTransaction, with the +# wallet's payout/refund addresses), so loading swap quotes submits data to a +# provider — out of bounds for repeated measurement. Never tap a pill or +# the keypad here, and never touch "Slide to swap". +# +# Timings: SwapCryptoRoot transition, SwapCurrencies span (supported-coin +# list built from Changelly/Thorswap currencies). + +# A failed earlier iteration can leave the app on a trade screen: back out +# to Home first (each Back only while Home is not showing). +- repeat: + times: 4 + commands: + - runFlow: + when: + notVisible: + id: 'portfolio-balance-toggle' + commands: + - back + - waitForAnimationToEnd +# Home can be left scrolled far down (it keeps its scroll position). +- scrollUntilVisible: + element: + id: 'portfolio-balance-toggle' + direction: UP + speed: 90 + timeout: 30000 +- tapOn: + id: 'swap-button' +- extendedWaitUntil: + visible: + id: 'swap-crypto-to-wallet-selector' + timeout: 45000 +- extendedWaitUntil: + visible: + id: 'swap-crypto-enter-amount-button' + timeout: 45000 +- tapOn: + id: 'swap-crypto-to-wallet-selector' +- extendedWaitUntil: + visible: 'Bitcoin, BTC' + timeout: 15000 +- tapOn: 'Bitcoin, BTC' +- extendedWaitUntil: + visible: 'Select Wallet to Deposit to' + timeout: 10000 +- waitForAnimationToEnd +# First wallet row of the sheet (the sheet's title sits at ~67%). +- tapOn: + point: 50%,73% +- extendedWaitUntil: + visible: '.*BTC' + timeout: 15000 +- tapOn: + id: 'swap-crypto-enter-amount-button' +- extendedWaitUntil: + visible: 'MAX' + timeout: 10000 +- back +- waitForAnimationToEnd +# Back closes the amount sheet; a second Back leaves Swap — but only if we +# are still on it (a Back on Home would background the app). +- runFlow: + when: + visible: + id: 'swap-crypto-to-wallet-selector' + commands: + - back +- extendedWaitUntil: + visible: + id: 'portfolio-balance-toggle' + timeout: 15000 +- waitForAnimationToEnd diff --git a/src/navigation/services/components/externalServicesOfferSelector.tsx b/src/navigation/services/components/externalServicesOfferSelector.tsx index 62e33cb8c7..fc7f1d27a6 100644 --- a/src/navigation/services/components/externalServicesOfferSelector.tsx +++ b/src/navigation/services/components/externalServicesOfferSelector.tsx @@ -1,4 +1,5 @@ import React, {memo, useEffect, useRef, useState} from 'react'; +import {spanEnd, spanStart} from '../../../perf/PerfMonitor'; import {offerSettleDelay} from '../utils/offer-settle'; import uuid from 'react-native-uuid'; import {useTheme} from '../../../contexts'; @@ -2981,6 +2982,7 @@ const ExternalServicesOfferSelector: React.FC< } setOfferSelectorText(t('Searching for our Best Offer')); + spanStart(context === 'sellCrypto' ? 'SellQuotes' : 'BuyQuotes'); setOfferWarnMsg(undefined); setSelectedOffer(undefined); onSelectOffer?.(undefined); @@ -3088,6 +3090,7 @@ const ExternalServicesOfferSelector: React.FC< ); if (filteredOffers.length === 0) { if (allExchangesFinished) { + spanEnd('BuyQuotes'); setOfferWarnMsg( t( 'There are currently no offers that satisfy your request. Please try again later.', @@ -3118,6 +3121,7 @@ const ExternalServicesOfferSelector: React.FC< ? moonpayOffer : _selectedOffer; setSelectedOffer(offerToSelect); + spanEnd('BuyQuotes'); onSelectOffer?.(offerToSelect); setOfferWarnMsg(undefined); setOfferSelectorText( @@ -3200,6 +3204,7 @@ const ExternalServicesOfferSelector: React.FC< ); if (filteredOffers.length === 0) { if (allSellExchangesFinished) { + spanEnd('SellQuotes'); setOfferWarnMsg( t( 'There are currently no offers that satisfy your request. Please try again later.', @@ -3221,6 +3226,7 @@ const ExternalServicesOfferSelector: React.FC< const amountReceiving = Number(_selectedOffer.amountReceiving); if (amountReceiving !== 0 && !isNaN(amountReceiving)) { setSelectedOffer(_selectedOffer); + spanEnd('SellQuotes'); onSelectOffer?.(_selectedOffer); setOfferWarnMsg(undefined); setOfferSelectorText( diff --git a/src/navigation/services/screens/BuyAndSellRoot.tsx b/src/navigation/services/screens/BuyAndSellRoot.tsx index c141be4b72..b509942c7d 100644 --- a/src/navigation/services/screens/BuyAndSellRoot.tsx +++ b/src/navigation/services/screens/BuyAndSellRoot.tsx @@ -1,4 +1,5 @@ import React, {useCallback, useEffect, useMemo, useRef, useState} from 'react'; +import {spanEnd, spanStart} from '../../../perf/PerfMonitor'; import {useTranslation} from 'react-i18next'; import {findTokenByCoinAndChain} from '../utils/token-lookup'; import SwapCurrenciesSvg from '../../../../assets/img/swap-currencies.svg'; @@ -1188,6 +1189,7 @@ const BuyAndSellRoot = ({ }; const initBuyCrypto = async () => { + spanStart('BuyCurrencies'); try { let config: ExternalServicesConfig; if (isExternalServicesConfigCacheFresh()) { @@ -1332,6 +1334,7 @@ const BuyAndSellRoot = ({ setBuyCryptoSupportedCoinsFullObj(initialBuyCryptoSupportedCoinsFullObj); setIsInitializing(false); + spanEnd('BuyCurrencies'); } catch (err) { // eslint-disable-next-line @typescript-eslint/no-unused-vars -- kept so the JSON.stringify(err) evaluation (which can throw) is unchanged const errMsg = err instanceof Error ? err.message : JSON.stringify(err); @@ -1683,6 +1686,7 @@ const BuyAndSellRoot = ({ }; const initSellCrypto = async () => { + spanStart('SellCurrencies'); if (fromDeeplink) { await sleep(300); } @@ -1932,6 +1936,7 @@ const BuyAndSellRoot = ({ ({symbol}) => symbol, ); setSellCryptoSupportedCoins(_sellCryptoSupportedCoins); + spanEnd('SellCurrencies'); if (!fromWallet) { setIsInitializing(false); } diff --git a/src/navigation/services/swap-crypto/components/SwapCryptoOfferSelector.tsx b/src/navigation/services/swap-crypto/components/SwapCryptoOfferSelector.tsx index d129065a83..4025f337d6 100644 --- a/src/navigation/services/swap-crypto/components/SwapCryptoOfferSelector.tsx +++ b/src/navigation/services/swap-crypto/components/SwapCryptoOfferSelector.tsx @@ -1,4 +1,5 @@ import React, {memo, useEffect, useMemo, useRef, useState} from 'react'; +import {spanEnd, spanStart} from '../../../../perf/PerfMonitor'; import {useTheme} from '../../../../contexts'; import {Caution, Slate, SlateDark, White} from '../../../../styles/colors'; import {useTranslation} from 'react-i18next'; @@ -932,6 +933,7 @@ const SwapCryptoOfferSelector: React.FC = ({ onSelectOffer?.(undefined); setSelectedOfferLoading(true); setOffersLoading?.(true); + spanStart('SwapQuotes'); // Clean up the previous timeout if the value changes if (timeoutRef.current) { @@ -1008,6 +1010,7 @@ const SwapCryptoOfferSelector: React.FC = ({ onSelectOffer?.(undefined); setSelectedOfferLoading(false); setOffersLoading?.(false); + spanEnd('SwapQuotes'); return; } const _selectedOffer = [...filteredOffers].reduce((prev, curr) => @@ -1041,6 +1044,7 @@ const SwapCryptoOfferSelector: React.FC = ({ } setSelectedOfferLoading(false); setOffersLoading?.(false); + spanEnd('SwapQuotes'); // eslint-disable-next-line react-hooks/exhaustive-deps -- picks the best offer only when a partner finishes quoting; other values are read at that time }, [finishedChangelly, finishedThorswap, updateView, dispatch]); diff --git a/src/navigation/services/swap-crypto/screens/SwapCryptoRoot.tsx b/src/navigation/services/swap-crypto/screens/SwapCryptoRoot.tsx index b1e7f77352..e631c767cd 100644 --- a/src/navigation/services/swap-crypto/screens/SwapCryptoRoot.tsx +++ b/src/navigation/services/swap-crypto/screens/SwapCryptoRoot.tsx @@ -1,4 +1,5 @@ import React, {useCallback, useEffect, useMemo, useRef, useState} from 'react'; +import {spanEnd, spanStart} from '../../../../perf/PerfMonitor'; import { ActivityIndicator, ScrollView, @@ -1531,6 +1532,7 @@ const SwapCryptoRoot: React.FC = () => { }; const init = async () => { + spanStart('SwapCurrencies'); const isCacheFresh = !!prefetchedOpts?.fetchedAt && Date.now() - prefetchedOpts.fetchedAt < SWAP_CRYPTO_CACHE_TTL; @@ -1741,6 +1743,7 @@ const SwapCryptoRoot: React.FC = () => { if (allSupportedCoinsOrdered?.length > 0) { setSwapCryptoSupportedCoinsFrom(allSupportedCoinsOrdered); + spanEnd('SwapCurrencies'); // Write back to cache so subsequent opens skip network calls if (!isCacheFresh) { diff --git a/src/perf/PerfMonitor.ts b/src/perf/PerfMonitor.ts index 80e180350d..9e46027f63 100644 --- a/src/perf/PerfMonitor.ts +++ b/src/perf/PerfMonitor.ts @@ -219,6 +219,32 @@ export function onNavigationStateChange(currentRouteName?: string): void { }); } +const openSpans = new Set(); + +/** + * Time something a screen does after it opens (e.g. amount entered → best + * offer shown). Reported by the harness as the pseudo-screen `name` in the + * per-screen transitions. A span restarted before it ends restarts the clock. + */ +export function spanStart(name: string): void { + if (!enabled) { + return; + } + openSpans.add(name); + mark(`bp_span_start:${name}`); +} + +/** End a span started with spanStart; no-op if none is open. */ +export function spanEnd(name: string): void { + if (!enabled || !openSpans.has(name)) { + return; + } + openSpans.delete(name); + const endMark = `bp_span_end:${name}`; + mark(endMark); + measure(`bp_nav:${name}`, `bp_span_start:${name}`, endMark); +} + /** * Ship the navigation measures collected so far. The harness calls for these * at the end of a scenario; batching keeps the sink off the critical path @@ -531,6 +557,57 @@ function flushLoad(label: string): void { }); } +// ------------------------------------------------------- interaction activity + +/** + * After startup settles, a small cumulative snapshot every 2 s: requests + * started (and the records of those that completed since the last snapshot), + * actions by type, modules initialised, JS heap. Interaction scenarios + * (perf/scenarios/trade_*.yaml) diff the first snapshot of an iteration + * against the last, which says what one flow loaded. Started only once the + * app has settled, so startup measurements are unaffected. + */ +const ACTIVITY_INTERVAL_MS = 2000; +const postedRequests = new WeakSet(); +let lastActionTotal = -1; + +function flushActivity(): void { + const done = requests.filter( + r => r.durationMs !== null && !postedRequests.has(r), + ); + done.forEach(r => postedRequests.add(r)); + let actionTotal = 0; + let actionMs = 0; + Object.values(actionCounts).forEach(a => { + actionTotal += a.count; + actionMs += a.ms; + }); + const heap = heapStats(); + post('activity', { + now: perfNow(), + requestsStarted: requests.length, + requests: done, + actionTotal, + actionMs, + // The per-type map only when it changed: it is the largest part. + actions: actionTotal !== lastActionTotal ? actionCounts : undefined, + modules: (global as any).__BP_MODULE_STATS__?.count ?? null, + heapMb: + heap && typeof heap.js_heapSize === 'number' + ? heap.js_heapSize / 1048576 + : null, + }); + lastActionTotal = actionTotal; +} + +let activityTimer: ReturnType | null = null; +function startActivityReporting(): void { + if (!enabled || activityTimer) { + return; + } + activityTimer = setInterval(flushActivity, ACTIVITY_INTERVAL_MS); +} + let loadScheduled = false; /** Call when the splash hides. */ @@ -545,6 +622,7 @@ export function scheduleLoadProfile(): void { afterIdle(() => { flushLoad('settled'); dumpJsProfile(); + startActivityReporting(); }), LOAD_SETTLE_MS, );