git-perf 0.20.0

Track, plot, and statistically validate simple measurements using git-notes for storage
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
//! Import measurements from external test runners and benchmarks
//!
//! This module provides functionality to import measurements from various
//! test and benchmark formats into git-perf's measurement storage.

use anyhow::{Context, Result};
use regex::Regex;
use std::collections::HashMap;
use std::io::{self, Read};
use std::time::{SystemTime, UNIX_EPOCH};

use git_perf_cli_types::ImportFormat;

use crate::config;
use crate::converters::{convert_to_measurements, ConversionOptions};
use crate::data::MeasurementData;
use crate::defaults;
use crate::parsers::{CriterionJsonParser, JunitXmlParser, Parser};
use crate::serialization::serialize_multiple;

/// Options for the import command
pub struct ImportOptions {
    pub commit: String,
    pub format: ImportFormat,
    pub file: Option<String>,
    pub prefix: Option<String>,
    pub metadata: Vec<(String, String)>,
    pub filter: Option<String>,
    pub dry_run: bool,
    pub verbose: bool,
}

/// Handle the import command
///
/// Reads input from stdin or file, parses it according to the specified format,
/// converts to MeasurementData, and stores in git notes.
pub fn handle_import(options: ImportOptions) -> Result<()> {
    let ImportOptions {
        commit,
        format,
        file,
        prefix,
        metadata,
        filter,
        dry_run,
        verbose,
    } = options;
    // Read input from stdin or file
    let input = read_input(file.as_deref())?;

    // Select parser based on format
    let parsed = match format {
        ImportFormat::Junit => {
            let parser = JunitXmlParser;
            parser.parse(&input).context("Failed to parse JUnit XML")?
        }
        ImportFormat::CriterionJson => {
            let parser = CriterionJsonParser;
            parser
                .parse(&input)
                .context("Failed to parse criterion JSON")?
        }
    };

    if verbose {
        println!("Parsed {} measurements", parsed.len());
    }

    // Apply regex filter if specified
    let filtered = if let Some(filter_pattern) = filter {
        let regex = Regex::new(&filter_pattern).context("Invalid regex pattern for filter")?;

        let original_count = parsed.len();
        let filtered_parsed: Vec<_> = parsed
            .into_iter()
            .filter(|p| {
                let name = match p {
                    crate::parsers::ParsedMeasurement::Test(t) => &t.name,
                    crate::parsers::ParsedMeasurement::Benchmark(b) => &b.id,
                };
                regex.is_match(name)
            })
            .collect();

        if verbose {
            println!(
                "Filtered to {} measurements (from {}) using pattern: {}",
                filtered_parsed.len(),
                original_count,
                filter_pattern
            );
        }

        filtered_parsed
    } else {
        parsed
    };

    // Build conversion options
    let timestamp = SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .context("Failed to get system time")?
        .as_secs_f64();

    let extra_metadata: HashMap<String, String> = metadata.into_iter().collect();

    let options = ConversionOptions {
        prefix,
        extra_metadata,
        epoch: 0, // Will be determined per-measurement in conversion
        timestamp,
    };

    // Convert to MeasurementData
    let measurements = convert_to_measurements(filtered, &options);

    if measurements.is_empty() {
        println!("No measurements to import (tests without durations are skipped)");
        return Ok(());
    }

    // Update epoch for each measurement based on config
    let measurements: Vec<MeasurementData> = measurements
        .into_iter()
        .map(|mut m| {
            m.epoch =
                config::determine_epoch_from_config(&m.name).unwrap_or(defaults::DEFAULT_EPOCH);
            m
        })
        .collect();

    if verbose || dry_run {
        println!("\nMeasurements to import:");
        for m in &measurements {
            println!("  {} = {} (epoch: {})", m.name, m.val, m.epoch);
            if verbose {
                for (k, v) in &m.key_values {
                    println!("    {}: {}", k, v);
                }
            }
        }
        println!("\nTotal: {} measurements", measurements.len());
    }

    // Store measurements (unless dry run)
    if dry_run {
        println!("\n[DRY RUN] Measurements not stored");
    } else {
        store_measurements(&commit, &measurements)?;
        println!("Successfully imported {} measurements", measurements.len());
    }

    Ok(())
}

/// Read input from stdin or file
fn read_input(file: Option<&str>) -> Result<String> {
    match file {
        None | Some("-") => {
            // Read from stdin
            let mut buffer = String::new();
            io::stdin()
                .read_to_string(&mut buffer)
                .context("Failed to read from stdin")?;
            Ok(buffer)
        }
        Some(path) => {
            // Read from file
            std::fs::read_to_string(path).with_context(|| format!("Failed to read file: {}", path))
        }
    }
}

/// Store multiple measurements to git notes
///
/// This is similar to `measurement_storage::add_multiple` but handles
/// measurements with different names and metadata.
fn store_measurements(commit: &str, measurements: &[MeasurementData]) -> Result<()> {
    // Validate commit exists
    let resolved_commit = crate::git::git_interop::resolve_committish(commit)
        .context(format!("Failed to resolve commit '{}'", commit))?;

    let serialized = serialize_multiple(measurements);
    crate::git::git_interop::add_note_line(&resolved_commit, &serialized)?;
    Ok(())
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::git::git_interop::walk_commits;
    use crate::test_helpers::{dir_with_repo, hermetic_git_env};
    use std::collections::HashMap;
    use std::env::set_current_dir;
    use std::io::Write;
    use tempfile::NamedTempFile;

    // Sample test data
    const SAMPLE_JUNIT_XML: &str = r#"<?xml version="1.0" encoding="UTF-8"?>
<testsuites tests="3" failures="1" errors="0" time="3.5">
  <testsuite name="test_binary" tests="3" failures="1" time="3.5">
    <testcase name="test_passed" classname="module::tests" time="1.5"/>
    <testcase name="test_failed" classname="module::tests" time="2.0">
      <failure message="assertion failed"/>
    </testcase>
    <testcase name="test_skipped" classname="module::tests">
      <skipped/>
    </testcase>
  </testsuite>
</testsuites>"#;

    const SAMPLE_CRITERION_JSON: &str = r#"{"reason":"benchmark-complete","id":"fibonacci/fib_10","unit":"ns","mean":{"estimate":15456.78,"lower_bound":15234.0,"upper_bound":15678.5},"median":{"estimate":15400.0,"lower_bound":15350.0,"upper_bound":15450.0},"slope":{"estimate":15420.5,"lower_bound":15380.0,"upper_bound":15460.0},"median_abs_dev":{"estimate":123.45}}
{"reason":"benchmark-complete","id":"fibonacci/fib_20","unit":"us","mean":{"estimate":1234.56,"lower_bound":1200.0,"upper_bound":1270.0},"median":{"estimate":1220.0}}"#;

    #[test]
    fn test_read_input_from_file() {
        let mut file = NamedTempFile::new().unwrap();
        writeln!(file, "test content").unwrap();

        let content = read_input(Some(file.path().to_str().unwrap())).unwrap();
        assert_eq!(content.trim(), "test content");
    }

    #[test]
    fn test_read_input_nonexistent_file() {
        let result = read_input(Some("/nonexistent/file/path.xml"));
        assert!(result.is_err());
        assert!(result
            .unwrap_err()
            .to_string()
            .contains("Failed to read file"));
    }

    #[test]
    fn test_handle_import_junit_dry_run() {
        let tempdir = dir_with_repo();
        set_current_dir(tempdir.path()).unwrap();
        hermetic_git_env();

        let mut file = NamedTempFile::new().unwrap();
        write!(file, "{}", SAMPLE_JUNIT_XML).unwrap();

        // Get initial notes count before import
        let commits_before = walk_commits(1).unwrap();
        let notes_before = commits_before[0].note_lines.len();

        let result = handle_import(ImportOptions {
            commit: "HEAD".to_string(),
            format: ImportFormat::Junit,
            file: Some(file.path().to_str().unwrap().to_string()),
            prefix: None,
            metadata: vec![],
            filter: None,
            dry_run: true,
            verbose: false,
        });

        assert!(result.is_ok(), "Import should succeed: {:?}", result);

        // Verify no new measurements were stored (dry run)
        let commits_after = walk_commits(1).unwrap();
        let notes_after = commits_after[0].note_lines.len();

        assert_eq!(
            notes_after, notes_before,
            "No new measurements should be stored in dry run (before: {}, after: {})",
            notes_before, notes_after
        );
    }

    #[test]
    fn test_handle_import_junit_stores_measurements() {
        let tempdir = dir_with_repo();
        set_current_dir(tempdir.path()).unwrap();
        hermetic_git_env();

        let mut file = NamedTempFile::new().unwrap();
        write!(file, "{}", SAMPLE_JUNIT_XML).unwrap();

        let result = handle_import(ImportOptions {
            commit: "HEAD".to_string(),
            format: ImportFormat::Junit,
            file: Some(file.path().to_str().unwrap().to_string()),
            prefix: None,
            metadata: vec![],
            filter: None,
            dry_run: false,
            verbose: false,
        });

        assert!(result.is_ok(), "Import should succeed: {:?}", result);

        // Verify measurements were stored
        let commits = walk_commits(1).unwrap();
        let notes = &commits[0].note_lines;

        // Should have 2 measurements (passed and failed tests with durations)
        // Skipped test has no time attribute so it's not imported
        assert!(
            notes.len() >= 2,
            "Should have at least 2 measurement lines, got: {}",
            notes.len()
        );

        // Verify measurement names
        let notes_text = notes.join("\n");
        assert!(
            notes_text.contains("test::test_passed"),
            "Should contain test_passed measurement"
        );
        assert!(
            notes_text.contains("test::test_failed"),
            "Should contain test_failed measurement"
        );

        // Skipped test should not be stored (no time attribute means no duration)
        assert!(
            !notes_text.contains("test::test_skipped"),
            "Should not contain skipped test (no time attribute = no duration)"
        );
    }

    #[test]
    fn test_handle_import_junit_with_prefix() {
        let tempdir = dir_with_repo();
        set_current_dir(tempdir.path()).unwrap();
        hermetic_git_env();

        let mut file = NamedTempFile::new().unwrap();
        write!(file, "{}", SAMPLE_JUNIT_XML).unwrap();

        let result = handle_import(ImportOptions {
            commit: "HEAD".to_string(),
            format: ImportFormat::Junit,
            file: Some(file.path().to_str().unwrap().to_string()),
            prefix: Some("ci".to_string()),
            metadata: vec![],
            filter: None,
            dry_run: false,
            verbose: false,
        });

        assert!(result.is_ok(), "Import with prefix should succeed");

        let commits = walk_commits(1).unwrap();
        let notes_text = commits[0].note_lines.join("\n");

        assert!(
            notes_text.contains("ci::test::test_passed"),
            "Should contain prefixed measurement name"
        );
    }

    #[test]
    fn test_handle_import_junit_with_metadata() {
        let tempdir = dir_with_repo();
        set_current_dir(tempdir.path()).unwrap();
        hermetic_git_env();

        let mut file = NamedTempFile::new().unwrap();
        write!(file, "{}", SAMPLE_JUNIT_XML).unwrap();

        let result = handle_import(ImportOptions {
            commit: "HEAD".to_string(),
            format: ImportFormat::Junit,
            file: Some(file.path().to_str().unwrap().to_string()),
            prefix: None,
            metadata: vec![
                ("ci".to_string(), "true".to_string()),
                ("branch".to_string(), "main".to_string()),
            ],
            filter: None,
            dry_run: false,
            verbose: false,
        });

        assert!(result.is_ok(), "Import with metadata should succeed");

        let commits = walk_commits(1).unwrap();
        let notes_text = commits[0].note_lines.join("\n");

        // Metadata should be included in the stored measurements
        assert!(
            notes_text.contains("ci") && notes_text.contains("true"),
            "Should contain ci metadata"
        );
        assert!(
            notes_text.contains("branch") && notes_text.contains("main"),
            "Should contain branch metadata"
        );
    }

    #[test]
    fn test_handle_import_junit_with_filter() {
        let tempdir = dir_with_repo();
        set_current_dir(tempdir.path()).unwrap();
        hermetic_git_env();

        let mut file = NamedTempFile::new().unwrap();
        write!(file, "{}", SAMPLE_JUNIT_XML).unwrap();

        // Filter to only import tests matching "passed"
        let result = handle_import(ImportOptions {
            commit: "HEAD".to_string(),
            format: ImportFormat::Junit,
            file: Some(file.path().to_str().unwrap().to_string()),
            prefix: None,
            metadata: vec![],
            filter: Some("passed".to_string()),
            dry_run: false,
            verbose: false,
        });

        assert!(result.is_ok(), "Import with filter should succeed");

        let commits = walk_commits(1).unwrap();
        let notes_text = commits[0].note_lines.join("\n");

        assert!(
            notes_text.contains("test::test_passed"),
            "Should contain filtered test_passed"
        );
        assert!(
            !notes_text.contains("test::test_failed"),
            "Should not contain filtered out test_failed"
        );
    }

    #[test]
    fn test_handle_import_criterion_json() {
        let tempdir = dir_with_repo();
        set_current_dir(tempdir.path()).unwrap();
        hermetic_git_env();

        let mut file = NamedTempFile::new().unwrap();
        write!(file, "{}", SAMPLE_CRITERION_JSON).unwrap();

        let result = handle_import(ImportOptions {
            commit: "HEAD".to_string(),
            format: ImportFormat::CriterionJson,
            file: Some(file.path().to_str().unwrap().to_string()),
            prefix: None,
            metadata: vec![],
            filter: None,
            dry_run: false,
            verbose: false,
        });

        assert!(
            result.is_ok(),
            "Criterion import should succeed: {:?}",
            result
        );

        let commits = walk_commits(1).unwrap();
        let notes_text = commits[0].note_lines.join("\n");

        // Should have multiple statistics per benchmark
        assert!(
            notes_text.contains("bench::fibonacci/fib_10::mean"),
            "Should contain mean statistic"
        );
        assert!(
            notes_text.contains("bench::fibonacci/fib_10::median"),
            "Should contain median statistic"
        );
        assert!(
            notes_text.contains("bench::fibonacci/fib_10::slope"),
            "Should contain slope statistic"
        );

        // Check unit conversion (us -> ns)
        assert!(
            notes_text.contains("bench::fibonacci/fib_20::mean"),
            "Should contain second benchmark"
        );
    }

    #[test]
    fn test_handle_import_invalid_format() {
        let tempdir = dir_with_repo();
        set_current_dir(tempdir.path()).unwrap();
        hermetic_git_env();

        let mut file = NamedTempFile::new().unwrap();
        write!(file, "invalid xml content").unwrap();

        let result = handle_import(ImportOptions {
            commit: "HEAD".to_string(),
            format: ImportFormat::Junit,
            file: Some(file.path().to_str().unwrap().to_string()),
            prefix: None,
            metadata: vec![],
            filter: None,
            dry_run: false,
            verbose: false,
        });

        assert!(result.is_err(), "Should fail with invalid XML");
        assert!(
            result.unwrap_err().to_string().contains("parse"),
            "Error should mention parsing failure"
        );
    }

    #[test]
    fn test_handle_import_empty_file() {
        let tempdir = dir_with_repo();
        set_current_dir(tempdir.path()).unwrap();
        hermetic_git_env();

        let mut file = NamedTempFile::new().unwrap();
        write!(
            file,
            r#"<?xml version="1.0"?><testsuites tests="0"></testsuites>"#
        )
        .unwrap();

        // Get initial notes count before import
        let commits_before = walk_commits(1).unwrap();
        let notes_before = commits_before[0].note_lines.len();

        let result = handle_import(ImportOptions {
            commit: "HEAD".to_string(),
            format: ImportFormat::Junit,
            file: Some(file.path().to_str().unwrap().to_string()),
            prefix: None,
            metadata: vec![],
            filter: None,
            dry_run: false,
            verbose: false,
        });

        // Should succeed but import no measurements
        assert!(result.is_ok(), "Should handle empty test results");

        // Verify no new measurements were added
        let commits_after = walk_commits(1).unwrap();
        let notes_after = commits_after[0].note_lines.len();

        assert_eq!(
            notes_after, notes_before,
            "Should not store any new measurements for empty results (before: {}, after: {})",
            notes_before, notes_after
        );
    }

    #[test]
    fn test_handle_import_invalid_regex_filter() {
        let tempdir = dir_with_repo();
        set_current_dir(tempdir.path()).unwrap();
        hermetic_git_env();

        let mut file = NamedTempFile::new().unwrap();
        write!(file, "{}", SAMPLE_JUNIT_XML).unwrap();

        let result = handle_import(ImportOptions {
            commit: "HEAD".to_string(),
            format: ImportFormat::Junit,
            file: Some(file.path().to_str().unwrap().to_string()),
            prefix: None,
            metadata: vec![],
            filter: Some("[invalid(regex".to_string()),
            dry_run: false,
            verbose: false,
        });

        assert!(result.is_err(), "Should fail with invalid regex");
        assert!(
            result.unwrap_err().to_string().contains("regex"),
            "Error should mention regex"
        );
    }

    #[test]
    fn test_store_measurements_integration() {
        let tempdir = dir_with_repo();
        set_current_dir(tempdir.path()).unwrap();
        hermetic_git_env();

        // Create test measurements
        let measurements = vec![
            MeasurementData {
                epoch: 0,
                name: "test::integration_test".to_string(),
                timestamp: 1234567890.0,
                val: 1500000000.0, // 1.5 seconds in nanoseconds
                key_values: {
                    let mut map = HashMap::new();
                    map.insert("type".to_string(), "test".to_string());
                    map.insert("status".to_string(), "passed".to_string());
                    map
                },
            },
            MeasurementData {
                epoch: 0,
                name: "bench::my_bench::mean".to_string(),
                timestamp: 1234567890.0,
                val: 15000.0, // 15000 nanoseconds
                key_values: {
                    let mut map = HashMap::new();
                    map.insert("type".to_string(), "bench".to_string());
                    map.insert("statistic".to_string(), "mean".to_string());
                    map
                },
            },
        ];

        let result = store_measurements("HEAD", &measurements);
        assert!(
            result.is_ok(),
            "Storing measurements should succeed: {:?}",
            result
        );

        // Verify measurements were stored
        let commits = walk_commits(1).unwrap();
        let notes = &commits[0].note_lines;

        assert!(
            notes.len() >= 2,
            "Should have stored 2 measurements, got: {}",
            notes.len()
        );

        let notes_text = notes.join("\n");
        assert!(
            notes_text.contains("test::integration_test"),
            "Should contain test measurement"
        );
        assert!(
            notes_text.contains("bench::my_bench::mean"),
            "Should contain benchmark measurement"
        );
    }
}