mobench 0.1.14

Rust mobile benchmark CLI with CI contract outputs and BrowserStack automation
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
# mobench

Mobile benchmarking CLI for Rust - Run benchmarks on real Android and iOS devices.

The `mobench` CLI is the easiest way to benchmark your Rust code on mobile devices. It handles everything from project setup to building mobile apps to running tests on real devices via BrowserStack.

## Installation

```bash
cargo binstall mobench
```

Or build from source:

```bash
cargo install mobench
```

Use as a Cargo subcommand:

```bash
cargo install mobench
cargo mobench --help
```

## Quick Start

### 1. Initialize Your Project

```bash
# Create mobile benchmarking setup for Android
cargo mobench init --target android

# Or for iOS
cargo mobench init --target ios

# Or for both platforms
cargo mobench init --target both
```

This creates:
- `bench-mobile/` - FFI wrapper crate with UniFFI bindings
- `android/` or `ios/` - Platform-specific app projects (generated to output directory)
- `bench-config.toml` - Run configuration file
- `mobench.toml` - Project configuration file (when using `init`)
- `benches/example.rs` - Example benchmarks (with `--examples`)

### 2. Write Benchmarks

```rust
// benches/my_benchmarks.rs
use mobench_sdk::benchmark;

#[benchmark]
fn fibonacci_30() {
    let result = fibonacci(30);
    std::hint::black_box(result);
}

fn fibonacci(n: u32) -> u64 {
    match n {
        0 => 0,
        1 => 1,
        _ => fibonacci(n - 1) + fibonacci(n - 2),
    }
}
```

### 3. Build for Mobile

```bash
# Build Android APK
cargo mobench build --target android

# Build iOS app
cargo mobench build --target ios
```

### 4. Run Benchmarks

Local device workflow (builds artifacts and writes the run spec; launch the app manually):
```bash
cargo mobench run --target android --function fibonacci_30 --iterations 50
```

On real devices via BrowserStack:
```bash
export BROWSERSTACK_USERNAME=your_username
export BROWSERSTACK_ACCESS_KEY=your_access_key

cargo mobench run \
  --target android \
  --function fibonacci_30 \
  --devices "Google Pixel 7-13.0" \
  --iterations 100 \
  --warmup 10 \
  --release
```

**Note**: Always use the `--release` flag for BrowserStack runs. Debug builds are significantly larger (~544MB vs ~133MB for release) and may cause upload timeouts.

## Commands

### `init` - Initialize Project

Create mobile benchmarking infrastructure:

```bash
cargo mobench init [OPTIONS]
```

**Options:**
- `--target <android|ios|both>` - Target platform (default: android)
- `--output <FILE>` - Config file path (default: bench-config.toml)

**Example:**
```bash
cargo mobench init --target both --output my-bench.toml
```

### `build` - Build Mobile Apps

Cross-compile and package for mobile platforms:

```bash
cargo mobench build --target <android|ios> [OPTIONS]
```

**Options:**
- `--target <android|ios>` - Platform to build for (required)
- `--release` - Build in release mode (default: debug)
- `--output-dir <DIR>` - Output directory for mobile artifacts (default: `target/mobench/`)
- `--crate-path <PATH>` - Path to the benchmark crate (default: auto-detect)
- `--dry-run` - Print what would be done without making changes
- `--verbose` / `-v` - Print verbose output including all commands

**Examples:**
```bash
# Build Android APK in release mode
cargo mobench build --target android --release

# Build iOS xcframework
cargo mobench build --target ios

# Preview build without making changes
cargo mobench build --target android --dry-run

# Build with verbose output
cargo mobench build --target ios --verbose

# Build to custom output directory
cargo mobench build --target android --output-dir ./my-output
```

**Outputs:**
- Android: `target/mobench/android/app/build/outputs/apk/debug/app-debug.apk`
- iOS: `target/mobench/ios/sample_fns.xcframework`

### `run` - Run Benchmarks

Execute benchmarks on devices:

```bash
cargo mobench run --target <android|ios> --function <NAME> [OPTIONS]
```

**Options:**
- `--target <android|ios>` - Platform (required)
- `--function <NAME>` - Benchmark function name (required)
- `--iterations <N>` - Number of iterations (default: 100)
- `--warmup <N>` - Warmup iterations (default: 10)
- `--devices <LIST>` - Comma-separated device list for BrowserStack
- `--device-matrix <FILE>` - Load devices from a matrix YAML file (overrides `device_matrix` from `--config` when both are provided)
- `--device-tags <TAG>` - Filter device matrix by tag (repeatable / comma-separated)
- `--local-only` - Skip mobile builds (no device run)
- `--config <FILE>` - Load run spec from config file
- `--ios-app <FILE>` - iOS .ipa or zipped .app for BrowserStack
- `--ios-test-suite <FILE>` - iOS XCUITest runner (.zip or .ipa)
- `--output <FILE>` - Save results to JSON file (default: target/mobench/results.json)
- `--summary-csv` - Write CSV summary alongside JSON/Markdown
- `--fetch` - Fetch BrowserStack results after completion
- `--ci` - CI mode (step summary + regression exit codes)
- `--baseline <path|url|artifact:<path>>` - Compare against baseline summary (non-zero on regressions); if baseline resolves to the output file path, mobench snapshots the previous file first
- `--regression-threshold-pct <N>` - Regression threshold percentage (default: 5.0)
- `--junit <FILE>` - Write JUnit XML report

**Outputs:**
- JSON summary (default: `target/mobench/results.json`)
- Markdown summary (same base name, `.md`)
- CSV summary (same base name, `.csv`, when `--summary-csv` is set)

**Examples:**
```bash
# Run locally (no BrowserStack devices specified)
cargo mobench run --target android --function fibonacci_30

# Run on BrowserStack devices (use --release for smaller APK)
cargo mobench run \
  --target android \
  --function sha256_hash \
  --devices "Google Pixel 7-13.0,Samsung Galaxy S23-13.0" \
  --iterations 50 \
  --release \
  --output results.json

# Run on iOS with auto-fetch (use --release for smaller artifacts)
cargo mobench run \
  --target ios \
  --function json_parse \
  --devices "iPhone 14-16,iPhone 15-17" \
  --release \
  --fetch
```

### `ci run` - One-command CI Orchestration

Run build/package/run/fetch/report end-to-end with stable CI output files:

```bash
cargo mobench ci run --target <android|ios|both> --function <NAME> [OPTIONS]
```

**Contract outputs (default directory: `target/mobench/ci/`):**
- `summary.json`
- `summary.md`
- `results.csv`

`summary.json` includes a `ci` section with metadata fields:
- `requested_by`
- `pr_number`
- `request_command`
- `mobench_ref`
- `mobench_version`

Contract references:
- `docs/CONTRACT_CI_V1.md`
- `docs/schemas/summary-v1.schema.json`
- `docs/schemas/ci-contract-v1.schema.json`

**Example:**
```bash
cargo mobench ci run \
  --target android \
  --function sample_fns::fibonacci \
  --devices "Google Pixel 7-13.0" \
  --release \
  --fetch

# Combined android + ios contract output
cargo mobench ci run \
  --target both \
  --function sample_fns::fibonacci \
  --local-only
```

When `--baseline` is omitted for `ci run`, mobench automatically uses the previous successful summary snapshot in the target output directory when present.

### `config validate` - Validate Run Config Contract

Validate `bench-config.toml` and referenced matrix/settings with contract-aligned issue categories:

```bash
cargo mobench config validate --config bench-config.toml
cargo mobench config validate --config bench-config.toml --format json
```

### `devices resolve` - Deterministic Matrix Resolution

Resolve matrix devices for a platform/profile without custom scripts:

```bash
cargo mobench devices resolve \
  --platform android \
  --profile default \
  --device-matrix device-matrix.yaml

cargo mobench devices resolve \
  --platform ios \
  --config bench-config.toml \
  --format json
```

### `fixture` - Fixture Lifecycle Commands

Manage reproducible fixture setup for CI:

```bash
# Create starter fixture files
cargo mobench fixture init

# Build fixture artifacts
cargo mobench fixture build --target both --release

# Verify fixture config + matrix resolution
cargo mobench fixture verify --config bench-config.toml

# Generate deterministic cache key
cargo mobench fixture cache-key --config bench-config.toml --format json
```

### `package-ipa` - Package iOS IPA

Create a signed IPA for BrowserStack:

```bash
cargo mobench package-ipa [OPTIONS]
```

**Options:**
- `--scheme <NAME>` - Xcode scheme (default: BenchRunner)
- `--method <adhoc|development>` - Signing method (default: adhoc)

**Example:**
```bash
cargo mobench package-ipa --method adhoc
```

**Output:** `target/mobench/ios/BenchRunner.ipa`

### `package-xcuitest` - Package XCUITest Runner

Create the XCUITest runner package required for BrowserStack iOS testing:

```bash
cargo mobench package-xcuitest [OPTIONS]
```

**Options:**
- `--scheme <NAME>` - Xcode scheme for UI tests (default: BenchRunnerUITests)

**Example:**
```bash
cargo mobench package-xcuitest
```

**Output:** `target/mobench/ios/BenchRunnerUITests.zip`

This command builds the XCUITest target and packages it into the zip format that BrowserStack expects for iOS test automation.

### `plan` - Generate Device Matrix

Create a template device matrix file:

```bash
cargo mobench plan [--output <FILE>]
```

**Example:**
```bash
cargo mobench plan --output devices.yaml
```

**Output:** `device-matrix.yaml`

```yaml
devices:
  - name: Google Pixel 7-13.0
    os: android
    os_version: "13.0"
    tags: [default, pixel]
  - name: iPhone 14-16
    os: ios
    os_version: "16"
    tags: [default, iphone]
```

### `list` - List Benchmarks

Show benchmarks discovered via `#[benchmark]`:

```bash
cargo mobench list
```

### `fetch` - Fetch Results

Download BrowserStack build artifacts:

```bash
cargo mobench fetch --target <android|ios> --build-id <ID> [OPTIONS]
```

**Options:**
- `--target <android|ios>` - Platform (required)
- `--build-id <ID>` - BrowserStack build ID (required)
- `--output-dir <DIR>` - Download directory (default: target/browserstack)

**Example:**
```bash
cargo mobench fetch \
  --target android \
  --build-id abc123def456 \
  --output-dir ./results
```

### `compare` - Compare Summaries

Compare two JSON run summaries and emit a Markdown report:

```bash
cargo mobench compare \
  --baseline results-v1.json \
  --candidate results-v2.json \
  --output comparison.md
```

### `report summarize` - Render CI Summary Markdown

Generate natural-language markdown from standardized output JSON:

```bash
cargo mobench report summarize --summary target/mobench/ci/summary.json
cargo mobench report summarize --summary target/mobench/ci/summary.json --output report.md
```

### `report github` - Sticky PR Comment Payload/Publish

Create or update sticky PR comments from standardized outputs:

```bash
# Print comment body
cargo mobench report github --pr 123 --summary target/mobench/ci/summary.json

# Publish/update comment (requires GITHUB_TOKEN + GITHUB_REPOSITORY)
cargo mobench report github \
  --pr 123 \
  --summary target/mobench/ci/summary.json \
  --publish \
  --marker "<!-- mobench-report -->"
```

## Configuration

### Project Configuration (`mobench.toml`)

mobench automatically loads `mobench.toml` from the current directory or any parent directory:

```toml
[project]
# Name of the benchmark crate
crate = "bench-mobile"

# Rust library name (typically crate name with hyphens replaced by underscores)
library_name = "bench_mobile"

# Output directory for build artifacts (default: target/mobench/)
# output_dir = "target/mobench"

[android]
# Android package name
package = "com.example.bench"

# Minimum Android SDK version (default: 24)
min_sdk = 24

# Target Android SDK version (default: 34)
target_sdk = 34

[ios]
# iOS bundle identifier
bundle_id = "com.example.bench"

# iOS deployment target version (default: 15.0)
deployment_target = "15.0"

# Development team ID for code signing (optional)
# team_id = "YOUR_TEAM_ID"

[benchmarks]
# Default benchmark function to run
default_function = "my_crate::my_benchmark"

# Default number of benchmark iterations
default_iterations = 100

# Default number of warmup iterations
default_warmup = 10
```

CLI flags always override config file values when provided.

### Run Config File Format (`bench-config.toml`)

For BrowserStack runs, you can also use a separate run configuration:

```toml
target = "android"
function = "sample_fns::fibonacci"
iterations = 100
warmup = 10
device_matrix = "device-matrix.yaml"
device_tags = ["default"] # optional; filter devices by tag

[browserstack]
app_automate_username = "${BROWSERSTACK_USERNAME}"
app_automate_access_key = "${BROWSERSTACK_ACCESS_KEY}"
project = "my-project-benchmarks"

[ios_xcuitest]
app = "target/mobench/ios/BenchRunner.ipa"
test_suite = "target/mobench/ios/BenchRunnerUITests.zip"
```

### Device Matrix Format (`device-matrix.yaml`)

```yaml
devices:
  - name: "Google Pixel 7-13.0"
    os: "android"
    os_version: "13.0"
    tags: ["default", "pixel"]
  - name: "iPhone 14-16"
    os: "ios"
    os_version: "16"
    tags: ["default", "iphone"]
```

### Environment Variables

BrowserStack credentials can be provided via:

1. **Environment variables** (recommended):
   ```bash
   export BROWSERSTACK_USERNAME=your_username
   export BROWSERSTACK_ACCESS_KEY=your_access_key
   ```

2. **`.env.local` file**:
   ```
   BROWSERSTACK_USERNAME=your_username
   BROWSERSTACK_ACCESS_KEY=your_access_key
   ```

3. **Config file** with variable expansion:
   ```toml
   [browserstack]
   username = "${BROWSERSTACK_USERNAME}"
   access_key = "${BROWSERSTACK_ACCESS_KEY}"
   ```

## Requirements

### For Android

- **Android NDK** - Set `ANDROID_NDK_HOME` environment variable
- **cargo-ndk** - Install with `cargo install cargo-ndk`
- **Android SDK** - API level 24+ required
- **Gradle** - For building APKs (bundled with Android project)

### For iOS

- **macOS** with Xcode installed
- **Xcode Command Line Tools** - `xcode-select --install`
- **Rust iOS targets**:
  ```bash
  rustup target add aarch64-apple-ios
  rustup target add aarch64-apple-ios-sim
  rustup target add x86_64-apple-ios
  ```
- **XcodeGen** - Install with `brew install xcodegen`

### For BrowserStack

- **BrowserStack App Automate account** - [Sign up]https://www.browserstack.com/app-automate
- **Credentials** - Username and access key from account settings

## Examples

### Benchmark Crypto Operations

```bash
# Initialize
cargo mobench init --target android

# Add benchmark
cat > benches/crypto.rs <<'EOF'
use mobench_sdk::benchmark;
use sha2::{Sha256, Digest};

#[benchmark]
fn sha256_1kb() {
    let data = vec![0u8; 1024];
    let hash = Sha256::digest(&data);
    std::hint::black_box(hash);
}
EOF

# Build
cargo mobench build --target android --release

# Run on multiple devices (use --release for BrowserStack)
cargo mobench run \
  --target android \
  --function sha256_1kb \
  --devices "Google Pixel 7-13.0,Samsung Galaxy S23-13.0,OnePlus 11-13.0" \
  --iterations 200 \
  --release \
  --output crypto-results.json
```

### Compare iOS Performance

```bash
# Run same benchmark on different iOS versions (use --release for BrowserStack)
cargo mobench run \
  --target ios \
  --function json_parse \
  --devices "iPhone 13-15,iPhone 14-16,iPhone 15-17" \
  --iterations 100 \
  --release \
  --fetch \
  --output ios-comparison.json
```

### CI Integration

Generate a ready-to-edit workflow + action wrapper:

```bash
cargo mobench ci init
```

This writes `.github/workflows/mobile-bench.yml` plus a local action in
`.github/actions/mobench/` that handles caching, Android setup, and artifact upload.

Example workflow excerpt:

```yaml
- uses: ./.github/actions/mobench
  with:
    command: cargo mobench ci run
    run-args: |
      --target android
      --function my_benchmark
      --devices "Google Pixel 7-13.0"
      --iterations 50
      --release
      --fetch
    ci: false
  env:
    BROWSERSTACK_USERNAME: ${{ secrets.BROWSERSTACK_USERNAME }}
    BROWSERSTACK_ACCESS_KEY: ${{ secrets.BROWSERSTACK_ACCESS_KEY }}
```

The local action currently supports `command` values `cargo mobench ci run` and `cargo mobench run`.

For CI dashboards, add `--junit path/to/results.junit.xml`.

### Typed Rust API

`mobench` also exposes a typed request/result surface for integrations:

```rust
use mobench::{DeviceSelection, MobileTarget, RunRequest, run_request};
use std::path::PathBuf;

let result = run_request(&RunRequest {
    target: MobileTarget::Android,
    function: "sample_fns::fibonacci".to_string(),
    iterations: 20,
    warmup: 5,
    device_selection: DeviceSelection {
        devices: vec!["Google Pixel 7-13.0".to_string()],
        device_matrix: None,
        device_tags: vec![],
    },
    config: None,
    baseline: None,
    regression_threshold_pct: 5.0,
    junit: None,
    local_only: true,
    release: false,
    ios_app: None,
    ios_test_suite: None,
    fetch: false,
    fetch_output_dir: PathBuf::from("target/browserstack"),
    fetch_poll_interval_secs: 5,
    fetch_timeout_secs: 300,
    progress: false,
    output_dir: PathBuf::from("target/mobench/ci"),
})?;
println!("summary: {}", result.report.summary_json.display());
```

## Workflow

```
┌─────────────────────┐
│ 1. cargo mobench    │
│    init             │
└──────────┬──────────┘
┌─────────────────────┐
│ 2. Write benchmarks │
│    with #[benchmark]│
└──────────┬──────────┘
┌─────────────────────┐
│ 3. cargo mobench    │
│    build            │
└──────────┬──────────┘
┌─────────────────────┐
│ 4. cargo mobench    │
│    run              │
└──────────┬──────────┘
      ┌────┴────┐
      ↓         ↓
┌──────────┐ ┌──────────────┐
│  Local   │ │ BrowserStack │
│ Emulator │ │ Real Devices │
└──────────┘ └──────────────┘
```

## Troubleshooting

### Android NDK not found

```bash
export ANDROID_NDK_HOME=/path/to/ndk
```

Or install via Android Studio: Tools → SDK Manager → SDK Tools → NDK

### iOS code signing issues

For BrowserStack testing, use ad-hoc signing:

```bash
cargo mobench package-ipa --method adhoc
```

### BrowserStack authentication failed

Verify credentials:

```bash
echo $BROWSERSTACK_USERNAME
echo $BROWSERSTACK_ACCESS_KEY
```

Or check `.env.local` file exists and contains valid credentials.

### Benchmark function not found

Ensure:
1. Function has `#[benchmark]` attribute
2. Function is compiled into the mobile binary
3. Function name matches exactly (case-sensitive)

## Part of mobench

This CLI is part of the mobench ecosystem:

- **[mobench]https://crates.io/crates/mobench** - This crate (CLI tool)
- **[mobench-sdk]https://crates.io/crates/mobench-sdk** - Core SDK with timing harness, build automation, and codegen
- **[mobench-macros]https://crates.io/crates/mobench-macros** - `#[benchmark]` proc macro

## See Also

- [mobench-sdk Documentation]https://crates.io/crates/mobench-sdk for programmatic API
- [BrowserStack App Automate]https://www.browserstack.com/app-automate for device cloud
- [UniFFI Documentation]https://mozilla.github.io/uniffi-rs/ for FFI details

## License

Licensed under the MIT License. See [LICENSE.md](../../LICENSE.md) for details.

Copyright (c) 2026 World Foundation