Skip to content

perf: shared benchmark harness and variant switch - #2149

Merged
edusperoni merged 3 commits into
mainfrom
perf/bench-tooling
Nov 22, 2026
Merged

edusperoni merged 3 commits into
mainfrom
perf/bench-tooling

Conversation

@edusperoni

Copy link
Copy Markdown
Collaborator

Description

This adds shared tooling for A/B performance experiments on the runtime, so an experiment no longer starts by copying and patching session scripts.

Run-time variant switch (feat(runtime): run-time variant switch for benchmark builds)

  • -PbenchVariants sets the CMake option NS_BENCH_VARIANTS (off by default). With it, the runtime reads debug.ns.benchvariant once, in JNI_OnLoad.
  • C++ reads it through tns::BenchVariant() (BenchVariant.h), and JS through a read-only __benchVariant global. An experiment puts its alternatives behind if (BenchVariant() == N) in a local, bench-only commit, and one APK then covers every label.
  • The runtime logs NS_BENCH_VARIANT=<n> at startup. The harness rejects a launch that ran a different variant, and aborts on an APK that reports none.
  • Without the option, BenchVariant() is constexpr 0: the branches fold away, nothing reads the property, and there is no global. Nothing that ships passes the flag; the root build's packaging tasks don't forward it.
  • The runtime's ccache switch also accepts -PuseCcache and NS_USE_CCACHE=1, next to the existing -PuseCCache. The CMake side (RULE_LAUNCH_COMPILE) and CI's use are unchanged.

ops in the JNI benchmark lines (test(bench): ...)

  • measureJs and measureCallback rows now report how many operations they ran in the launch, warm-up and reference calls included.
  • A counter difference between two scales, divided by the ops difference, gives counts per operation without hard-coding each row's iteration formula.

tools/perf (build(perf): ...): bash and python3, no new dependencies. Usage is in tools/perf/README.md.

  • build.sh builds the release, -Poptimized arm64 test app. Options:

    • --variants for the run-time variant switch.
    • --ccache for a dedicated, size-capped cache. CCACHE_BASEDIR is the checkout and CCACHE_NOHASHDIR=1, so worktrees share entries.
    • --profileable adds <profileable android:shell="true"/> for one build and restores the manifest afterwards.

    It records the commit, the flags, the .so hash, the wall time and the ccache hits.

  • run.sh runs rounds on one device:

    • It takes a mkdir lock per round and cleans up only while it holds it.
    • It refuses protected serials (emulator-5554 by default), and it refuses to mix devices in one output directory.
    • Each round installs once per APK, speed-compiles and discards the first launch, then relaunches per label and filter group in rotated order.
    • It logs each launch, its thermal status and the host load, plus the time and lock wait of each round.
  • table.py prints medians and within-round ratios to an anchor, as median [min-max]. With --aa and three or more rounds, it marks each range as inside (=) or outside (*) the A/A range, and it flags bimodal labels (B). --md prints Markdown for PR bodies.

  • stat.sh + statsum.py run two-scale simpleperf stat (instructions:u, cpu-cycles:u) on the JS thread, and report counts per op with A/A deltas.

The README covers:

  • the protocol and the reason for each rule;
  • the variant convention and its caveat: variants in one binary share a code layout, so a layout-sensitive result needs a separate-build confirmation;
  • how a branch adds rows or a whole bench;
  • the GC-timing pitfall;
  • a checklist for a new device: serial, first-launch check with --no-discard, simpleperf / API level (stat needs API 29+), thermal.

Measurements

ccache (arm64 -Poptimized --variants, whole build.sh wall time, host load about 8):

build wall time ccache
first build in a fresh worktree, empty cache 28 s 0/79 hits
first build in a second worktree (different path), same sources 18 s 79/79 hits, byte-identical libNativeScript.so
same worktree, native build state deleted, no ccache 22 s —
same worktree, native build state deleted, warm cache 12 s 79/79 hits

One configuration takes about 25 MB of cache. A ccache build is not byte-identical to a non-ccache build: the base-dir rewrite makes __FILE__ relative (../../../../src/main/cpp/... instead of the absolute path). So both sides of a comparison should use the same setting.

Round time (Galaxy S20 FE, Android 13; 4 labels × 3 filter groups = 12 measured launches per round):

APKs per round of which install + speed compile + discarded launch
before: one APK per label, reinstalled per label (an earlier experiment's session log, 12 rounds) 4 187-204 s 58-61 s
after: one --variants APK, run.sh (2-round smoke) 1 85-97 s 16-25 s

The smoke's rows were shorter, so compare the overhead column. For the earlier experiment's row mix, the saving is about 40 s of ~200 s per round, plus N-1 fewer builds. Those builds were already 10-37 s each, because the labels differed only in JS. Lock waits between rounds dominated wall time in both runs (10-19 minutes with other sessions on the same phone), which is what a second device would relieve.

Smoke A/B (2 rounds; base/base2 = variant 0, v1 = an empty asm volatile("") branch in PrepareV8Runtime, v2 = a 50-iteration spin at the top of CallJavaMethod as a positive control; neither variant is in this PR):

ratio to base within a round: median [min-max]              base2                 v1                 v2
static.noop()                                   1.003 [1.00-1.00]  1.006 [1.01-1.01]  1.972 [1.97-1.98]
static.add(int,int)                             0.969 [0.97-0.97]  0.991 [0.98-1.00]  1.565 [1.56-1.57]
instance.getInt()                               0.995 [0.99-1.00]  0.996 [1.00-1.00]  1.395 [1.38-1.41]
static field get int [small class]              1.025 [1.00-1.05]  1.134 [1.10-1.17]  0.990 [0.93-1.05]
field.get int                                   1.097 [1.00-1.20]  1.012 [1.00-1.03]  0.999 [1.00-1.00]
field.set int                                   0.999 [1.00-1.00]  1.002 [1.00-1.00]  1.001 [1.00-1.01]
callback.noArgs()                               0.982 [0.97-0.99]  0.652 [0.34-0.97]  0.663 [0.34-0.98]
  • v2 adds about 85-95 ns to every JS → Java method call and nothing to field access, which takes another path.
  • callback.noArgs() is bimodal per launch on this phone (about 415 or about 1230 ns), as seen before. Two rounds cannot separate that from v1/v2, and table.py flags it once there are four or more launches.

stat.sh check (same APK, made profileable, one pass, 2 min 11 s under one lock hold; per op on the JS thread, scales 4 and 20):

row / event base base2 v2 base2-base v2-base
field.get int instructions:u 1298.8 1298.0 1297.5 -0.8 -1.3
field.get int cpu-cycles:u 444.9 444.4 443.0 -0.5 -1.9
static.noop() instructions:u 809.4 809.5 1112.2 +0.0 +302.8
static.noop() cpu-cycles:u 255.5 254.9 494.5 -0.6 +239.1

The A/A deltas stay under one instruction per op. The positive control shows up only on the row whose path it changes.

Does your commit message include the wording below to reference a specific issue in this repo?

No.

Related Pull Requests

None.

Does your pull request have unit tests?

No new specs: the variant switch is off by default, and tools/perf is development tooling.

  • The full suite passes with the switch off on an API 36.1 arm64 emulator: 1788 specs, 0 failures.
  • With the switch on, the smoke above checked every launch's variant against the label.
  • ops was checked by the two-scale stat run.

-PbenchVariants (CMake NS_BENCH_VARIANTS, off by default) makes the runtime
read debug.ns.benchvariant once when the library loads. C++ reads it through
BenchVariant() and JS through the read-only __benchVariant global, so one
benchmark APK can carry every label of an A/B. The runtime logs
NS_BENCH_VARIANT=<n> at startup so a harness can reject a launch that ran the
wrong variant.

Without the option BenchVariant() is a constant 0, nothing reads the property
and there is no JS global.

The runtime's ccache switch also accepts -PuseCcache and NS_USE_CCACHE=1.
Every measureJs and measureCallback row now logs "ops": the operations it
ran in the launch, warm-up and reference calls included. A counter run at two
scales divided by the ops difference gives counts per operation without
hard-coding each row's iteration formula.
build.sh builds the release benchmark APK (optionally with the run-time
variant switch, ccache shared across checkouts, and a temporary
<profileable> manifest entry). run.sh runs rotated A/B rounds on one device
under a mkdir lock: one install, speed compile and discarded launch per APK
per round, then a relaunch per label and filter group, checking the variant
the runtime reports. table.py prints within-round ratios against an anchor,
with the A/A range and bimodality flags. stat.sh and statsum.py count
instructions and cycles per operation on the JS thread from two-scale
simpleperf stat runs.
@coderabbitai

coderabbitai Bot commented Nov 22, 2026

Copy link
Copy Markdown

Important

Draft PR not reviewed

Draft PRs are not automatically reviewed by default.

  • Trigger a manual review

To automatically review draft PRs, update your CodeRabbit configuration:

reviews:
  auto_review:
    drafts: true
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

@edusperoni
edusperoni marked this pull request as ready for review November 22, 2026 21:46
@edusperoni
edusperoni merged commit a59e6ba into main Nov 22, 2026
7 checks passed
@edusperoni
edusperoni deleted the perf/bench-tooling branch November 22, 2026 21:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant