mobench 0.1.40

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
//! Configuration file support for mobench.
//!
//! This module provides support for `mobench.toml` configuration files that allow
//! users to persist project settings and avoid passing CLI flags repeatedly.
//!
//! ## Configuration File Location
//!
//! The configuration file is searched for in the following order:
//! 1. Current working directory (`./mobench.toml`)
//! 2. Parent directories (up to the repository root or filesystem root)
//!
//! ## Example Configuration
//!
//! ```toml
//! [project]
//! crate = "bench-mobile"
//! library_name = "bench_mobile"
//!
//! [android]
//! package = "com.example.bench"
//! min_sdk = 24
//! target_sdk = 34
//!
//! [ios]
//! bundle_id = "com.example.bench"
//! deployment_target = "15.0"
//! runner = "swiftui"
//!
//! [benchmarks]
//! default_function = "my_crate::my_benchmark"
//! default_iterations = 100
//! default_warmup = 10
//! ```

use anyhow::{Context, Result};
use serde::{Deserialize, Serialize};
use std::path::{Path, PathBuf};

/// The default configuration file name.
pub const CONFIG_FILE_NAME: &str = "mobench.toml";

/// Root configuration structure for `mobench.toml`.
///
/// This struct represents the complete configuration file format and is
/// automatically loaded when CLI commands run.
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
#[serde(default)]
pub struct MobenchConfig {
    /// Project-level configuration.
    pub project: ProjectConfig,

    /// Android-specific configuration.
    pub android: AndroidConfig,

    /// iOS-specific configuration.
    pub ios: IosConfig,

    /// Benchmark execution defaults.
    pub benchmarks: BenchmarksConfig,

    /// Optional BrowserStack-specific configuration.
    pub browserstack: BrowserStackConfig,
}

/// Project-level configuration.
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
#[serde(default)]
pub struct ProjectConfig {
    /// Name of the benchmark crate (e.g., "bench-mobile").
    ///
    /// If not specified, mobench will auto-detect the crate by looking for
    /// `bench-mobile/` or `crates/sample-fns/` directories.
    #[serde(rename = "crate")]
    pub crate_name: Option<String>,

    /// Library name for the Rust crate (e.g., "bench_mobile").
    ///
    /// This is typically the crate name with hyphens replaced by underscores.
    /// If not specified, it's derived from the crate name.
    pub library_name: Option<String>,

    /// Output directory for build artifacts.
    ///
    /// Defaults to `target/mobench/` if not specified.
    pub output_dir: Option<PathBuf>,
}

/// Android-specific configuration.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(default)]
pub struct AndroidConfig {
    /// Android package name (e.g., "com.example.bench").
    ///
    /// Defaults to "dev.world.bench" if not specified.
    pub package: String,

    /// Minimum Android SDK version.
    ///
    /// Defaults to 24 (Android 7.0).
    pub min_sdk: u32,

    /// Target Android SDK version.
    ///
    /// Defaults to 34 (Android 14).
    pub target_sdk: u32,

    /// Android ABIs to build for.
    ///
    /// Defaults to ["arm64-v8a"].
    pub abis: Option<Vec<String>>,
}

impl Default for AndroidConfig {
    fn default() -> Self {
        Self {
            package: "dev.world.bench".to_string(),
            min_sdk: 24,
            target_sdk: 34,
            abis: None,
        }
    }
}

/// iOS-specific configuration.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(default)]
pub struct IosConfig {
    /// iOS bundle identifier (e.g., "com.example.bench").
    ///
    /// Defaults to "dev.world.bench" if not specified.
    pub bundle_id: String,

    /// iOS deployment target version.
    ///
    /// Defaults to "15.0".
    pub deployment_target: String,

    /// Development team ID for code signing.
    ///
    /// If not specified, ad-hoc signing is used.
    pub team_id: Option<String>,
}

impl Default for IosConfig {
    fn default() -> Self {
        Self {
            bundle_id: "dev.world.bench".to_string(),
            deployment_target: "15.0".to_string(),
            team_id: None,
        }
    }
}

/// Benchmark execution defaults.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(default)]
pub struct BenchmarksConfig {
    /// Default benchmark function to run.
    ///
    /// Can be overridden via CLI `--function` flag.
    pub default_function: Option<String>,

    /// Default number of benchmark iterations.
    ///
    /// Defaults to 100. Can be overridden via CLI `--iterations` flag.
    pub default_iterations: u32,

    /// Default number of warmup iterations.
    ///
    /// Defaults to 10. Can be overridden via CLI `--warmup` flag.
    pub default_warmup: u32,
}

impl Default for BenchmarksConfig {
    fn default() -> Self {
        Self {
            default_function: None,
            default_iterations: 100,
            default_warmup: 10,
        }
    }
}

/// BrowserStack-specific configuration.
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
#[serde(default)]
pub struct BrowserStackConfig {
    /// Timeout in seconds for the generated iOS XCUITest harness to wait for benchmark completion.
    pub ios_completion_timeout_secs: Option<u64>,
}

impl MobenchConfig {
    /// Creates a new configuration with default values.
    pub fn new() -> Self {
        Self::default()
    }

    /// Loads configuration from the specified file path.
    ///
    /// # Arguments
    ///
    /// * `path` - Path to the configuration file
    ///
    /// # Returns
    ///
    /// * `Ok(MobenchConfig)` - Successfully loaded configuration
    /// * `Err` - If the file cannot be read or parsed
    pub fn load_from_file(path: &Path) -> Result<Self> {
        let contents = std::fs::read_to_string(path)
            .with_context(|| format!("Failed to read config file: {:?}", path))?;

        let config: MobenchConfig = toml::from_str(&contents)
            .with_context(|| format!("Failed to parse config file: {:?}", path))?;

        Ok(config)
    }

    /// Attempts to find and load configuration from the current directory
    /// or any parent directory.
    ///
    /// This searches for `mobench.toml` starting from the current directory
    /// and walking up the directory tree until a config file is found or
    /// the root is reached.
    ///
    /// # Returns
    ///
    /// * `Ok(Some((config, path)))` - Found and loaded configuration with its path
    /// * `Ok(None)` - No configuration file found
    /// * `Err` - If a config file was found but couldn't be parsed
    pub fn discover() -> Result<Option<(Self, PathBuf)>> {
        let cwd = std::env::current_dir().context("Failed to get current directory")?;
        Self::discover_from(&cwd)
    }

    /// Attempts to find and load configuration starting from the specified directory.
    ///
    /// # Arguments
    ///
    /// * `start_dir` - Directory to start searching from
    ///
    /// # Returns
    ///
    /// * `Ok(Some((config, path)))` - Found and loaded configuration with its path
    /// * `Ok(None)` - No configuration file found
    /// * `Err` - If a config file was found but couldn't be parsed
    pub fn discover_from(start_dir: &Path) -> Result<Option<(Self, PathBuf)>> {
        let mut current = start_dir.to_path_buf();

        loop {
            let config_path = current.join(CONFIG_FILE_NAME);

            if config_path.is_file() {
                let config = Self::load_from_file(&config_path)?;
                return Ok(Some((config, config_path)));
            }

            // Stop at repository root or filesystem root
            if current.join(".git").exists() || !current.pop() {
                break;
            }
        }

        Ok(None)
    }

    /// Saves the configuration to the specified file path.
    ///
    /// # Arguments
    ///
    /// * `path` - Path to write the configuration file
    ///
    /// # Returns
    ///
    /// * `Ok(())` - Successfully saved configuration
    /// * `Err` - If the file cannot be written
    pub fn save_to_file(&self, path: &Path) -> Result<()> {
        let contents = toml::to_string_pretty(self).context("Failed to serialize configuration")?;

        std::fs::write(path, contents)
            .with_context(|| format!("Failed to write config file: {:?}", path))?;

        Ok(())
    }

    /// Returns the library name, either from config or derived from crate name.
    pub fn library_name(&self) -> Option<String> {
        self.project.library_name.clone().or_else(|| {
            self.project
                .crate_name
                .as_ref()
                .map(|c| c.replace('-', "_"))
        })
    }

    /// Generates a starter configuration with sensible defaults.
    ///
    /// # Arguments
    ///
    /// * `crate_name` - Name of the benchmark crate
    ///
    /// # Returns
    ///
    /// A new `MobenchConfig` with the provided crate name and default values.
    pub fn starter(crate_name: &str) -> Self {
        let library_name = crate_name.replace('-', "_");
        let package = format!("dev.world.{}", library_name.replace('_', ""));

        Self {
            project: ProjectConfig {
                crate_name: Some(crate_name.to_string()),
                library_name: Some(library_name.clone()),
                output_dir: None, // Use default (target/mobench/)
            },
            android: AndroidConfig {
                package: package.clone(),
                min_sdk: 24,
                target_sdk: 34,
                abis: None,
            },
            ios: IosConfig {
                bundle_id: package,
                deployment_target: "15.0".to_string(),
                team_id: None,
            },
            benchmarks: BenchmarksConfig {
                default_function: Some(format!("{}::my_benchmark", library_name)),
                default_iterations: 100,
                default_warmup: 10,
            },
            browserstack: BrowserStackConfig::default(),
        }
    }

    /// Generates a starter configuration file as a formatted TOML string.
    ///
    /// This includes helpful comments explaining each configuration option.
    ///
    /// # Arguments
    ///
    /// * `crate_name` - Name of the benchmark crate
    ///
    /// # Returns
    ///
    /// A formatted TOML string suitable for writing to `mobench.toml`.
    pub fn generate_starter_toml(crate_name: &str) -> String {
        let library_name = crate_name.replace('-', "_");
        let package = format!("dev.world.{}", library_name.replace('_', ""));

        format!(
            r#"# mobench configuration file
# This file configures mobench for building and running mobile benchmarks.
# CLI flags override these settings when provided.

[project]
# Name of the benchmark crate
crate = "{crate_name}"

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

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

[android]
# Android package name
package = "{package}"

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

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

# Android ABIs to build for (optional, defaults to arm64-v8a)
# abis = ["arm64-v8a"]

[ios]
# iOS bundle identifier
bundle_id = "{package}"

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

# iOS app runner: swiftui (iOS 15+) or uikit-legacy (legacy targets).
# If omitted, mobench chooses uikit-legacy for deployment targets below 15.0.
# runner = "swiftui"

# Development team ID for code signing (optional, uses ad-hoc signing if not set)
# team_id = "YOUR_TEAM_ID"

[benchmarks]
# Default benchmark function to run
default_function = "{library_name}::my_benchmark"

# Default number of benchmark iterations (can be overridden with --iterations)
default_iterations = 100

# Default number of warmup iterations (can be overridden with --warmup)
default_warmup = 10

[browserstack]
# Timeout in seconds for the generated iOS XCUITest harness to wait for completion
# ios_completion_timeout_secs = 1200

# Timeout in seconds for the generated Android benchmark watchdog
# android_benchmark_timeout_secs = 1800

# Heartbeat interval in seconds for Android benchmark progress logging
# android_heartbeat_interval_secs = 10
"#,
            crate_name = crate_name,
            library_name = library_name,
            package = package,
        )
    }
}

/// Configuration resolver that merges config file values with CLI arguments.
///
/// CLI arguments always take precedence over config file values.
#[derive(Debug, Default)]
pub struct ConfigResolver {
    /// Loaded configuration, if any.
    pub config: Option<MobenchConfig>,

    /// Path to the loaded config file, if any.
    pub config_path: Option<PathBuf>,
}

impl ConfigResolver {
    /// Creates a new resolver by discovering and loading configuration.
    ///
    /// If no config file is found, the resolver will use default values
    /// which can be overridden by CLI arguments.
    pub fn new() -> Result<Self> {
        match MobenchConfig::discover()? {
            Some((config, path)) => Ok(Self {
                config: Some(config),
                config_path: Some(path),
            }),
            None => Ok(Self {
                config: None,
                config_path: None,
            }),
        }
    }

    /// Returns the crate name from config, or None if not configured.
    pub fn crate_name(&self) -> Option<&str> {
        self.config
            .as_ref()
            .and_then(|c| c.project.crate_name.as_deref())
    }

    /// Returns the library name from config, derived from crate name if needed.
    pub fn library_name(&self) -> Option<String> {
        self.config.as_ref().and_then(|c| c.library_name())
    }

    /// Returns the output directory from config.
    pub fn output_dir(&self) -> Option<&Path> {
        self.config
            .as_ref()
            .and_then(|c| c.project.output_dir.as_deref())
    }

    /// Returns the default function from config.
    pub fn default_function(&self) -> Option<&str> {
        self.config
            .as_ref()
            .and_then(|c| c.benchmarks.default_function.as_deref())
    }

    /// Returns the default iterations from config.
    pub fn default_iterations(&self) -> u32 {
        self.config
            .as_ref()
            .map(|c| c.benchmarks.default_iterations)
            .unwrap_or(100)
    }

    /// Returns the default warmup from config.
    pub fn default_warmup(&self) -> u32 {
        self.config
            .as_ref()
            .map(|c| c.benchmarks.default_warmup)
            .unwrap_or(10)
    }

    /// Returns the Android configuration.
    pub fn android(&self) -> AndroidConfig {
        self.config
            .as_ref()
            .map(|c| c.android.clone())
            .unwrap_or_default()
    }

    /// Returns the iOS configuration.
    pub fn ios(&self) -> IosConfig {
        self.config
            .as_ref()
            .map(|c| c.ios.clone())
            .unwrap_or_default()
    }

    /// Resolves a CLI value, using config as fallback.
    ///
    /// # Arguments
    ///
    /// * `cli_value` - Value from CLI argument (None if not provided)
    /// * `config_getter` - Function to get value from config
    /// * `default` - Default value if neither CLI nor config provides a value
    ///
    /// # Returns
    ///
    /// The resolved value, preferring CLI over config over default.
    pub fn resolve<T, F>(&self, cli_value: Option<T>, config_getter: F, default: T) -> T
    where
        F: FnOnce(&MobenchConfig) -> Option<T>,
    {
        cli_value
            .or_else(|| self.config.as_ref().and_then(config_getter))
            .unwrap_or(default)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::io::Write;
    use tempfile::TempDir;

    #[test]
    fn test_default_config() {
        let config = MobenchConfig::default();
        assert_eq!(config.android.min_sdk, 24);
        assert_eq!(config.android.target_sdk, 34);
        assert_eq!(config.ios.deployment_target, "15.0");
        assert_eq!(config.benchmarks.default_iterations, 100);
        assert_eq!(config.benchmarks.default_warmup, 10);
        assert_eq!(config.browserstack.ios_completion_timeout_secs, None);
    }

    #[test]
    fn test_starter_config() {
        let config = MobenchConfig::starter("my-bench");
        assert_eq!(config.project.crate_name, Some("my-bench".to_string()));
        assert_eq!(config.project.library_name, Some("my_bench".to_string()));
        assert_eq!(config.android.package, "dev.world.mybench");
        assert_eq!(config.ios.bundle_id, "dev.world.mybench");
        assert_eq!(config.browserstack.ios_completion_timeout_secs, None);
    }

    #[test]
    fn test_load_from_file() {
        let temp_dir = TempDir::new().unwrap();
        let config_path = temp_dir.path().join("mobench.toml");

        let toml_content = r#"
[project]
crate = "test-bench"
library_name = "test_bench"

[android]
package = "com.test.bench"
min_sdk = 21
target_sdk = 33

[ios]
bundle_id = "com.test.bench"
deployment_target = "14.0"

[benchmarks]
default_function = "test_bench::test_fn"
default_iterations = 50
default_warmup = 5

[browserstack]
ios_completion_timeout_secs = 1200
"#;

        let mut file = std::fs::File::create(&config_path).unwrap();
        file.write_all(toml_content.as_bytes()).unwrap();

        let config = MobenchConfig::load_from_file(&config_path).unwrap();

        assert_eq!(config.project.crate_name, Some("test-bench".to_string()));
        assert_eq!(config.project.library_name, Some("test_bench".to_string()));
        assert_eq!(config.android.package, "com.test.bench");
        assert_eq!(config.android.min_sdk, 21);
        assert_eq!(config.android.target_sdk, 33);
        assert_eq!(config.ios.bundle_id, "com.test.bench");
        assert_eq!(config.ios.deployment_target, "14.0");
        assert_eq!(
            config.benchmarks.default_function,
            Some("test_bench::test_fn".to_string())
        );
        assert_eq!(config.benchmarks.default_iterations, 50);
        assert_eq!(config.benchmarks.default_warmup, 5);
        assert_eq!(config.browserstack.ios_completion_timeout_secs, Some(1200));
    }

    #[test]
    fn test_discover_config() {
        let temp_dir = TempDir::new().unwrap();
        let config_path = temp_dir.path().join("mobench.toml");

        let toml_content = r#"
[project]
crate = "discovered-bench"
"#;

        std::fs::write(&config_path, toml_content).unwrap();

        let result = MobenchConfig::discover_from(temp_dir.path()).unwrap();
        assert!(result.is_some());

        let (config, path) = result.unwrap();
        assert_eq!(
            config.project.crate_name,
            Some("discovered-bench".to_string())
        );
        assert_eq!(path, config_path);
    }

    #[test]
    fn test_discover_no_config() {
        let temp_dir = TempDir::new().unwrap();
        // Create a .git directory to stop the search
        std::fs::create_dir(temp_dir.path().join(".git")).unwrap();

        let result = MobenchConfig::discover_from(temp_dir.path()).unwrap();
        assert!(result.is_none());
    }

    #[test]
    fn test_config_resolver() {
        let config = MobenchConfig::starter("test-crate");
        let resolver = ConfigResolver {
            config: Some(config),
            config_path: None,
        };

        // CLI value takes precedence
        let result = resolver.resolve(Some(200), |c| Some(c.benchmarks.default_iterations), 50);
        assert_eq!(result, 200);

        // Config value used when CLI is None
        let result: u32 = resolver.resolve(None, |c| Some(c.benchmarks.default_iterations), 50);
        assert_eq!(result, 100);
    }

    #[test]
    fn test_generate_starter_toml() {
        let toml = MobenchConfig::generate_starter_toml("my-bench");
        assert!(toml.contains("crate = \"my-bench\""));
        assert!(toml.contains("library_name = \"my_bench\""));
        assert!(toml.contains("min_sdk = 24"));
        assert!(toml.contains("target_sdk = 34"));
        assert!(toml.contains("deployment_target = \"15.0\""));
        assert!(toml.contains("runner = \"swiftui\""));
        assert!(toml.contains("default_iterations = 100"));
        assert!(toml.contains("default_warmup = 10"));
        assert!(toml.contains("[browserstack]"));
        assert!(toml.contains("ios_completion_timeout_secs"));
        assert!(toml.contains("android_benchmark_timeout_secs"));
        assert!(toml.contains("android_heartbeat_interval_secs"));
    }
}