fluxbench-cli 0.1.3

FluxBench CLI: Supervisor process, benchmark filtering, output generation, and worker orchestration
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
//! Configuration loading from flux.toml
//!
//! FluxBench configuration can be specified in a `flux.toml` file in the project root.
//! The configuration is automatically discovered by walking up from the current directory.

use serde::{Deserialize, Serialize};
use std::path::Path;

/// FluxBench configuration
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct FluxConfig {
    /// Runner configuration
    #[serde(default)]
    pub runner: RunnerConfig,
    /// Visualization configuration
    #[serde(default)]
    pub visuals: VisualsConfig,
    /// Allocator tracking configuration
    #[serde(default)]
    pub allocator: AllocatorConfig,
    /// Output configuration
    #[serde(default)]
    pub output: OutputConfig,
    /// CI/CD configuration
    #[serde(default)]
    pub ci: CiConfig,
}

/// Isolation mode for benchmark execution
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Default)]
#[serde(rename_all = "kebab-case")]
pub enum IsolationMode {
    /// Run each benchmark in a separate worker process (default)
    #[default]
    Process,
    /// Run benchmarks in-process (no isolation, useful for debugging)
    InProcess,
    /// Run benchmarks in threads (no isolation)
    Thread,
}

impl IsolationMode {
    /// Whether this mode provides process isolation
    pub fn is_isolated(self) -> bool {
        matches!(self, IsolationMode::Process)
    }
}

/// Runner configuration for benchmark execution
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RunnerConfig {
    /// Timeout for a single benchmark (e.g., "60s", "5m")
    #[serde(default = "default_timeout")]
    pub timeout: String,
    /// Isolation mode: "process", "in-process", or "thread"
    #[serde(default)]
    pub isolation: IsolationMode,
    /// Warmup duration before measurement (e.g., "3s")
    #[serde(default = "default_warmup")]
    pub warmup_time: String,
    /// Measurement duration (e.g., "5s")
    #[serde(default = "default_measurement")]
    pub measurement_time: String,
    /// Fixed sample count: skip warmup, run exactly N iterations (each = one sample)
    #[serde(default)]
    pub samples: Option<u64>,
    /// Minimum number of iterations
    #[serde(default)]
    pub min_iterations: Option<u64>,
    /// Maximum number of iterations
    #[serde(default)]
    pub max_iterations: Option<u64>,
    /// Number of bootstrap iterations for statistics
    #[serde(default = "default_bootstrap_iterations")]
    pub bootstrap_iterations: usize,
    /// Confidence level (e.g., 0.95 for 95%)
    #[serde(default = "default_confidence_level")]
    pub confidence_level: f64,
    /// Number of parallel isolated workers
    #[serde(default)]
    pub jobs: Option<usize>,
}

impl Default for RunnerConfig {
    fn default() -> Self {
        Self {
            timeout: default_timeout(),
            isolation: IsolationMode::default(),
            warmup_time: default_warmup(),
            measurement_time: default_measurement(),
            samples: None,
            min_iterations: None,
            max_iterations: None,
            bootstrap_iterations: default_bootstrap_iterations(),
            confidence_level: default_confidence_level(),
            jobs: None,
        }
    }
}

fn default_timeout() -> String {
    "60s".to_string()
}
fn default_warmup() -> String {
    "3s".to_string()
}
fn default_measurement() -> String {
    "5s".to_string()
}
fn default_bootstrap_iterations() -> usize {
    10_000
}
fn default_confidence_level() -> f64 {
    0.95
}

/// Visualization configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct VisualsConfig {
    /// Color theme: "light" or "dark"
    #[serde(default = "default_theme")]
    pub theme: String,
    /// Chart width in pixels
    #[serde(default = "default_width")]
    pub width: u32,
    /// Chart height in pixels
    #[serde(default = "default_height")]
    pub height: u32,
}

impl Default for VisualsConfig {
    fn default() -> Self {
        Self {
            theme: default_theme(),
            width: default_width(),
            height: default_height(),
        }
    }
}

fn default_theme() -> String {
    "light".to_string()
}
fn default_width() -> u32 {
    1280
}
fn default_height() -> u32 {
    720
}

/// Allocator tracking configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AllocatorConfig {
    /// Enable allocation tracking
    #[serde(default = "default_track")]
    pub track: bool,
    /// Fail if any allocation occurs during measurement
    #[serde(default)]
    pub fail_on_allocation: bool,
    /// Maximum bytes allowed per iteration (None = unlimited)
    #[serde(default)]
    pub max_bytes_per_iter: Option<u64>,
}

impl Default for AllocatorConfig {
    fn default() -> Self {
        Self {
            track: default_track(),
            fail_on_allocation: false,
            max_bytes_per_iter: None,
        }
    }
}

fn default_track() -> bool {
    true
}

/// Output configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct OutputConfig {
    /// Default output format: "human", "json", "github", "html", "csv"
    #[serde(default = "default_format")]
    pub format: String,
    /// Output directory for reports
    #[serde(default = "default_output_dir")]
    pub directory: String,
    /// Save JSON baseline after each run
    #[serde(default)]
    pub save_baseline: bool,
    /// Baseline file path
    #[serde(default)]
    pub baseline_path: Option<String>,
}

impl Default for OutputConfig {
    fn default() -> Self {
        Self {
            format: default_format(),
            directory: default_output_dir(),
            save_baseline: false,
            baseline_path: None,
        }
    }
}

fn default_format() -> String {
    "human".to_string()
}
fn default_output_dir() -> String {
    "target/fluxbench".to_string()
}

/// CI/CD configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CiConfig {
    /// Regression threshold percentage (fail if exceeded)
    #[serde(default = "default_threshold")]
    pub regression_threshold: f64,
    /// Enable GitHub Actions annotations
    #[serde(default)]
    pub github_annotations: bool,
    /// Fail on any critical verification failure
    #[serde(default = "default_fail_on_critical")]
    pub fail_on_critical: bool,
}

impl Default for CiConfig {
    fn default() -> Self {
        Self {
            regression_threshold: default_threshold(),
            github_annotations: false,
            fail_on_critical: default_fail_on_critical(),
        }
    }
}

fn default_threshold() -> f64 {
    5.0
}
fn default_fail_on_critical() -> bool {
    true
}

impl FluxConfig {
    /// Load configuration from a TOML file
    pub fn load(path: impl AsRef<Path>) -> anyhow::Result<Self> {
        let content = std::fs::read_to_string(path.as_ref())?;
        let config: Self = toml::from_str(&content)?;
        Ok(config)
    }

    /// Try to discover and load configuration by walking up from current directory
    pub fn discover() -> Option<Self> {
        let mut dir = std::env::current_dir().ok()?;
        loop {
            let config_path = dir.join("flux.toml");
            if config_path.exists() {
                return Self::load(&config_path).ok();
            }
            if !dir.pop() {
                break;
            }
        }
        None
    }

    /// Generate a default configuration as TOML string
    pub fn default_toml() -> String {
        r#"# FluxBench Configuration
# https://github.com/ml-rust/fluxbench

[runner]
# Warmup duration before measurement
warmup_time = "3s"
# Measurement duration
measurement_time = "5s"
# Timeout for a single benchmark
timeout = "60s"
# Isolation mode: "process" or "thread"
isolation = "process"  # "process", "in-process", or "thread"
# Fixed sample count: skip warmup, run exactly N iterations (uncomment to enable)
# samples = 5
# Minimum iterations (uncomment to enable)
# min_iterations = 100
# Maximum iterations (uncomment to enable)
# max_iterations = 1000000
# Number of parallel isolated workers (uncomment to enable)
# jobs = 4
# Bootstrap iterations for confidence intervals
bootstrap_iterations = 10000
# Confidence level (0.0 to 1.0)
confidence_level = 0.95

[allocator]
# Track memory allocations during benchmarks
track = true
# Fail if any allocation occurs during measurement
fail_on_allocation = false
# Maximum bytes per iteration (uncomment to enable)
# max_bytes_per_iter = 1024

[output]
# Default output format: human, json, github, html, csv
format = "human"
# Output directory for reports
directory = "target/fluxbench"
# Save JSON baseline after each run
save_baseline = false
# Baseline file for comparison (uncomment to enable)
# baseline_path = "baseline.json"

[visuals]
# Color theme: light or dark
theme = "light"
# Chart dimensions
width = 1280
height = 720

[ci]
# Regression threshold percentage (fail CI if exceeded)
regression_threshold = 5.0
# Enable GitHub Actions annotations
github_annotations = false
# Fail on critical verification failures
fail_on_critical = true
"#
        .to_string()
    }

    /// Parse duration string (e.g., "3s", "500ms", "2m") to nanoseconds
    pub fn parse_duration(s: &str) -> anyhow::Result<u64> {
        let s = s.trim();
        if s.is_empty() {
            return Err(anyhow::anyhow!("Empty duration string"));
        }

        // Find where the number ends and unit begins
        let (num_part, unit_part) = s
            .char_indices()
            .find(|(_, c)| c.is_alphabetic())
            .map(|(i, _)| s.split_at(i))
            .unwrap_or((s, "s"));

        let value: f64 = num_part
            .parse()
            .map_err(|_| anyhow::anyhow!("Invalid duration number: {}", num_part))?;

        let multiplier: u64 = match unit_part.to_lowercase().as_str() {
            "ns" => 1,
            "us" | "µs" => 1_000,
            "ms" => 1_000_000,
            "s" | "" => 1_000_000_000,
            "m" | "min" => 60_000_000_000,
            _ => return Err(anyhow::anyhow!("Unknown duration unit: {}", unit_part)),
        };

        Ok((value * multiplier as f64) as u64)
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_default_config() {
        let config = FluxConfig::default();
        assert_eq!(config.runner.warmup_time, "3s");
        assert_eq!(config.runner.measurement_time, "5s");
        assert!(config.allocator.track);
        assert!(!config.allocator.fail_on_allocation);
    }

    #[test]
    fn test_parse_duration() {
        assert_eq!(FluxConfig::parse_duration("3s").unwrap(), 3_000_000_000);
        assert_eq!(FluxConfig::parse_duration("500ms").unwrap(), 500_000_000);
        assert_eq!(FluxConfig::parse_duration("100us").unwrap(), 100_000);
        assert_eq!(FluxConfig::parse_duration("1000ns").unwrap(), 1000);
        assert_eq!(FluxConfig::parse_duration("2m").unwrap(), 120_000_000_000);
        assert_eq!(FluxConfig::parse_duration("1.5s").unwrap(), 1_500_000_000);
    }

    #[test]
    fn test_parse_toml() {
        let toml_str = r#"
            [runner]
            warmup_time = "1s"
            measurement_time = "2s"

            [allocator]
            track = false
        "#;

        let config: FluxConfig = toml::from_str(toml_str).unwrap();
        assert_eq!(config.runner.warmup_time, "1s");
        assert_eq!(config.runner.measurement_time, "2s");
        assert!(!config.allocator.track);
        // Defaults should still apply
        assert_eq!(config.output.format, "human");
    }

    #[test]
    fn test_default_toml_parses() {
        let default_toml = FluxConfig::default_toml();
        let config: FluxConfig = toml::from_str(&default_toml).unwrap();
        assert_eq!(config.runner.warmup_time, "3s");
    }
}