perfgate-cli 0.17.0

CLI for perfgate performance budgets and baseline diffs
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
//! Integration tests for `perfgate paired` command
//!
//! **Validates: Paired benchmarking mode with interleaved execution**

mod common;
use common::perfgate_cmd;
use predicates::prelude::*;
use std::fs;
use tempfile::tempdir;

/// Returns a cross-platform command that exits successfully.
#[cfg(unix)]
fn success_command() -> Vec<&'static str> {
    vec!["true"]
}

#[cfg(windows)]
fn success_command() -> Vec<&'static str> {
    vec!["cmd", "/c", "exit", "0"]
}

/// Returns a cross-platform command that exits with failure.
#[cfg(unix)]
fn fail_command() -> Vec<&'static str> {
    vec!["false"]
}

#[cfg(windows)]
fn fail_command() -> Vec<&'static str> {
    vec!["cmd", "/c", "exit", "1"]
}

/// Test basic paired run produces valid JSON receipt
#[test]
fn test_paired_basic_produces_valid_json() {
    let temp_dir = tempdir().expect("failed to create temp dir");
    let output_path = temp_dir.path().join("paired.json");

    let mut cmd = perfgate_cmd();
    cmd.arg("paired")
        .arg("--name")
        .arg("test-paired")
        .arg("--repeat")
        .arg("2")
        .arg("--baseline-cmd");

    for arg in success_command() {
        cmd.arg(arg);
    }

    cmd.arg("--current-cmd");
    for arg in success_command() {
        cmd.arg(arg);
    }

    cmd.arg("--out").arg(&output_path);

    cmd.assert().success();

    // Verify output file exists
    assert!(output_path.exists(), "output file should exist");

    // Read and parse the output file
    let content = fs::read_to_string(&output_path).expect("failed to read output file");
    let receipt: serde_json::Value =
        serde_json::from_str(&content).expect("output should be valid JSON");

    // Verify schema field is "perfgate.paired.v1"
    assert_eq!(
        receipt["schema"].as_str(),
        Some("perfgate.paired.v1"),
        "schema should be 'perfgate.paired.v1'"
    );

    // Verify bench name matches
    assert_eq!(
        receipt["bench"]["name"].as_str(),
        Some("test-paired"),
        "bench name should be 'test-paired'"
    );

    // Verify samples array exists and has expected count
    let samples = receipt["samples"]
        .as_array()
        .expect("samples should be an array");
    assert_eq!(samples.len(), 2, "should have 2 paired samples (repeat=2)");
}

/// Test paired stats have correct structure (mean, median, std_dev, ci_lower, ci_upper)
#[test]
fn test_paired_stats_have_correct_structure() {
    let temp_dir = tempdir().expect("failed to create temp dir");
    let output_path = temp_dir.path().join("paired.json");

    let mut cmd = perfgate_cmd();
    cmd.arg("paired")
        .arg("--name")
        .arg("stats-test")
        .arg("--repeat")
        .arg("3")
        .arg("--baseline-cmd");

    for arg in success_command() {
        cmd.arg(arg);
    }

    cmd.arg("--current-cmd");
    for arg in success_command() {
        cmd.arg(arg);
    }

    cmd.arg("--out").arg(&output_path);

    cmd.assert().success();

    let content = fs::read_to_string(&output_path).expect("failed to read output file");
    let receipt: serde_json::Value =
        serde_json::from_str(&content).expect("output should be valid JSON");

    // Verify stats structure
    let stats = &receipt["stats"];

    // Verify baseline_wall_ms summary (U64Summary: median, min, max)
    assert!(
        stats["baseline_wall_ms"]["median"].is_u64(),
        "baseline_wall_ms should have median"
    );
    assert!(
        stats["baseline_wall_ms"]["min"].is_u64(),
        "baseline_wall_ms should have min"
    );
    assert!(
        stats["baseline_wall_ms"]["max"].is_u64(),
        "baseline_wall_ms should have max"
    );

    // Verify current_wall_ms summary
    assert!(
        stats["current_wall_ms"]["median"].is_u64(),
        "current_wall_ms should have median"
    );
    assert!(
        stats["current_wall_ms"]["min"].is_u64(),
        "current_wall_ms should have min"
    );
    assert!(
        stats["current_wall_ms"]["max"].is_u64(),
        "current_wall_ms should have max"
    );

    // Verify wall_diff_ms (PairedDiffSummary: mean, median, std_dev, min, max, count)
    let wall_diff = &stats["wall_diff_ms"];
    assert!(
        wall_diff["mean"].is_f64() || wall_diff["mean"].is_i64(),
        "wall_diff_ms should have mean"
    );
    assert!(
        wall_diff["median"].is_f64() || wall_diff["median"].is_i64(),
        "wall_diff_ms should have median"
    );
    assert!(
        wall_diff["std_dev"].is_f64() || wall_diff["std_dev"].is_i64(),
        "wall_diff_ms should have std_dev"
    );
    assert!(
        wall_diff["min"].is_f64() || wall_diff["min"].is_i64(),
        "wall_diff_ms should have min"
    );
    assert!(
        wall_diff["max"].is_f64() || wall_diff["max"].is_i64(),
        "wall_diff_ms should have max"
    );
    assert!(
        wall_diff["count"].is_u64(),
        "wall_diff_ms should have count"
    );
    assert_eq!(
        wall_diff["count"].as_u64(),
        Some(3),
        "wall_diff_ms count should match repeat"
    );
}

/// Test paired with work_units for throughput calculations
#[test]
fn test_paired_with_work_units_throughput() {
    let temp_dir = tempdir().expect("failed to create temp dir");
    let output_path = temp_dir.path().join("paired.json");

    let mut cmd = perfgate_cmd();
    cmd.arg("paired")
        .arg("--name")
        .arg("throughput-test")
        .arg("--repeat")
        .arg("2")
        .arg("--work")
        .arg("1000")
        .arg("--baseline-cmd");

    for arg in success_command() {
        cmd.arg(arg);
    }

    cmd.arg("--current-cmd");
    for arg in success_command() {
        cmd.arg(arg);
    }

    cmd.arg("--out").arg(&output_path);

    cmd.assert().success();

    let content = fs::read_to_string(&output_path).expect("failed to read output file");
    let receipt: serde_json::Value =
        serde_json::from_str(&content).expect("output should be valid JSON");

    // Verify bench has work_units
    assert_eq!(
        receipt["bench"]["work_units"].as_u64(),
        Some(1000),
        "bench should have work_units"
    );

    // Verify stats include throughput fields when work_units provided
    let stats = &receipt["stats"];
    assert!(
        stats["baseline_throughput_per_s"].is_object(),
        "baseline_throughput_per_s should exist when work_units provided"
    );
    assert!(
        stats["current_throughput_per_s"].is_object(),
        "current_throughput_per_s should exist when work_units provided"
    );
    assert!(
        stats["throughput_diff_per_s"].is_object(),
        "throughput_diff_per_s should exist when work_units provided"
    );
}

/// Test that paired samples are structured correctly with interleaved execution
#[test]
fn test_paired_samples_structure() {
    let temp_dir = tempdir().expect("failed to create temp dir");
    let output_path = temp_dir.path().join("paired.json");

    let mut cmd = perfgate_cmd();
    cmd.arg("paired")
        .arg("--name")
        .arg("samples-test")
        .arg("--repeat")
        .arg("3")
        .arg("--warmup")
        .arg("1")
        .arg("--baseline-cmd");

    for arg in success_command() {
        cmd.arg(arg);
    }

    cmd.arg("--current-cmd");
    for arg in success_command() {
        cmd.arg(arg);
    }

    cmd.arg("--out").arg(&output_path);

    cmd.assert().success();

    let content = fs::read_to_string(&output_path).expect("failed to read output file");
    let receipt: serde_json::Value =
        serde_json::from_str(&content).expect("output should be valid JSON");

    let samples = receipt["samples"]
        .as_array()
        .expect("samples should be an array");

    // Should have warmup + repeat samples
    assert_eq!(
        samples.len(),
        4,
        "should have 4 samples (1 warmup + 3 repeat)"
    );

    // Verify each sample has required paired structure
    for (i, sample) in samples.iter().enumerate() {
        // Check pair_index
        assert_eq!(
            sample["pair_index"].as_u64(),
            Some(i as u64),
            "pair_index should match iteration"
        );

        // Check warmup flag
        let is_warmup = i < 1; // first sample is warmup
        assert_eq!(
            sample["warmup"].as_bool(),
            Some(is_warmup),
            "warmup flag should be correct for sample {i}"
        );

        // Check baseline half
        assert!(
            sample["baseline"]["wall_ms"].is_u64(),
            "baseline should have wall_ms"
        );
        assert!(
            sample["baseline"]["exit_code"].is_i64(),
            "baseline should have exit_code"
        );
        assert!(
            sample["baseline"]["timed_out"].is_boolean(),
            "baseline should have timed_out"
        );

        // Check current half
        assert!(
            sample["current"]["wall_ms"].is_u64(),
            "current should have wall_ms"
        );
        assert!(
            sample["current"]["exit_code"].is_i64(),
            "current should have exit_code"
        );
        assert!(
            sample["current"]["timed_out"].is_boolean(),
            "current should have timed_out"
        );

        // Check wall_diff_ms computed correctly
        assert!(
            sample["wall_diff_ms"].is_i64(),
            "sample should have wall_diff_ms"
        );
    }
}

/// Test error handling when baseline command fails
#[test]
fn test_paired_error_when_baseline_fails() {
    let temp_dir = tempdir().expect("failed to create temp dir");
    let output_path = temp_dir.path().join("paired.json");

    let mut cmd = perfgate_cmd();
    cmd.arg("paired")
        .arg("--name")
        .arg("fail-test")
        .arg("--repeat")
        .arg("1")
        .arg("--baseline-cmd");

    for arg in fail_command() {
        cmd.arg(arg);
    }

    cmd.arg("--current-cmd");
    for arg in success_command() {
        cmd.arg(arg);
    }

    cmd.arg("--out").arg(&output_path);

    // Should fail because baseline exits non-zero
    cmd.assert()
        .failure()
        .stderr(predicate::str::contains("baseline"));
}

/// Test error handling when current command fails
#[test]
fn test_paired_error_when_current_fails() {
    let temp_dir = tempdir().expect("failed to create temp dir");
    let output_path = temp_dir.path().join("paired.json");

    let mut cmd = perfgate_cmd();
    cmd.arg("paired")
        .arg("--name")
        .arg("fail-test")
        .arg("--repeat")
        .arg("1")
        .arg("--baseline-cmd");

    for arg in success_command() {
        cmd.arg(arg);
    }

    cmd.arg("--current-cmd");
    for arg in fail_command() {
        cmd.arg(arg);
    }

    cmd.arg("--out").arg(&output_path);

    // Should fail because current exits non-zero
    cmd.assert()
        .failure()
        .stderr(predicate::str::contains("current"));
}

/// Test --allow-nonzero flag allows non-zero exits
#[test]
fn test_paired_allow_nonzero_succeeds() {
    let temp_dir = tempdir().expect("failed to create temp dir");
    let output_path = temp_dir.path().join("paired.json");

    let mut cmd = perfgate_cmd();
    cmd.arg("paired")
        .arg("--name")
        .arg("allow-nonzero-test")
        .arg("--repeat")
        .arg("1")
        .arg("--allow-nonzero")
        .arg("--baseline-cmd");

    for arg in fail_command() {
        cmd.arg(arg);
    }

    cmd.arg("--current-cmd");
    for arg in success_command() {
        cmd.arg(arg);
    }

    cmd.arg("--out").arg(&output_path);

    // Should succeed with --allow-nonzero
    cmd.assert().success();

    // Verify file was written
    assert!(output_path.exists(), "output file should exist");
}

/// Test missing required arguments fails
#[test]
fn test_paired_missing_name_fails() {
    let mut cmd = perfgate_cmd();
    cmd.arg("paired")
        .arg("--baseline-cmd")
        .arg("true")
        .arg("--current-cmd")
        .arg("true");

    cmd.assert()
        .failure()
        .stderr(predicate::str::contains("--name"));
}

/// Test missing baseline-cmd fails
#[test]
fn test_paired_missing_baseline_cmd_fails() {
    let mut cmd = perfgate_cmd();
    cmd.arg("paired")
        .arg("--name")
        .arg("test")
        .arg("--current-cmd")
        .arg("true");

    cmd.assert()
        .failure()
        .stderr(predicate::str::contains("baseline"));
}

/// Test missing current-cmd fails
#[test]
fn test_paired_missing_current_cmd_fails() {
    let mut cmd = perfgate_cmd();
    cmd.arg("paired")
        .arg("--name")
        .arg("test")
        .arg("--baseline-cmd")
        .arg("true");

    cmd.assert()
        .failure()
        .stderr(predicate::str::contains("current"));
}

/// Test receipt contains required tool info
#[test]
fn test_paired_receipt_contains_tool_info() {
    let temp_dir = tempdir().expect("failed to create temp dir");
    let output_path = temp_dir.path().join("paired.json");

    let mut cmd = perfgate_cmd();
    cmd.arg("paired")
        .arg("--name")
        .arg("tool-info-test")
        .arg("--repeat")
        .arg("1")
        .arg("--baseline-cmd");

    for arg in success_command() {
        cmd.arg(arg);
    }

    cmd.arg("--current-cmd");
    for arg in success_command() {
        cmd.arg(arg);
    }

    cmd.arg("--out").arg(&output_path);

    cmd.assert().success();

    let content = fs::read_to_string(&output_path).expect("failed to read output file");
    let receipt: serde_json::Value =
        serde_json::from_str(&content).expect("output should be valid JSON");

    // Verify tool info
    assert_eq!(
        receipt["tool"]["name"].as_str(),
        Some("perfgate"),
        "tool name should be 'perfgate'"
    );
    assert!(
        receipt["tool"]["version"].is_string(),
        "tool version should be present"
    );

    // Verify run metadata
    assert!(receipt["run"]["id"].is_string(), "run id should be present");
    assert!(
        receipt["run"]["started_at"].is_string(),
        "started_at should be present"
    );
    assert!(
        receipt["run"]["ended_at"].is_string(),
        "ended_at should be present"
    );
    assert!(receipt["run"]["host"].is_object(), "host should be present");
}

/// Test --pretty flag produces formatted JSON
#[test]
fn test_paired_pretty_flag_formats_json() {
    let temp_dir = tempdir().expect("failed to create temp dir");
    let output_path = temp_dir.path().join("paired.json");

    let mut cmd = perfgate_cmd();
    cmd.arg("paired")
        .arg("--name")
        .arg("pretty-test")
        .arg("--repeat")
        .arg("1")
        .arg("--pretty")
        .arg("--baseline-cmd");

    for arg in success_command() {
        cmd.arg(arg);
    }

    cmd.arg("--current-cmd");
    for arg in success_command() {
        cmd.arg(arg);
    }

    cmd.arg("--out").arg(&output_path);

    cmd.assert().success();

    let content = fs::read_to_string(&output_path).expect("failed to read output file");

    // Pretty-printed JSON should contain newlines and indentation
    assert!(
        content.contains('\n'),
        "pretty-printed JSON should contain newlines"
    );
    assert!(
        content.contains("  "),
        "pretty-printed JSON should contain indentation"
    );
}

/// Test default output file name
#[test]
fn test_paired_default_output_file() {
    let temp_dir = tempdir().expect("failed to create temp dir");

    let mut cmd = perfgate_cmd();
    cmd.current_dir(temp_dir.path())
        .arg("paired")
        .arg("--name")
        .arg("default-out-test")
        .arg("--repeat")
        .arg("1")
        .arg("--baseline-cmd");

    for arg in success_command() {
        cmd.arg(arg);
    }

    cmd.arg("--current-cmd");
    for arg in success_command() {
        cmd.arg(arg);
    }

    cmd.assert().success();

    // Default output file should be "perfgate-paired.json"
    let default_output = temp_dir.path().join("perfgate-paired.json");
    assert!(
        default_output.exists(),
        "default output file 'perfgate-paired.json' should exist"
    );
}

/// Test bench metadata contains correct commands
#[test]
fn test_paired_bench_metadata() {
    let temp_dir = tempdir().expect("failed to create temp dir");
    let output_path = temp_dir.path().join("paired.json");

    let baseline_cmd = success_command();
    let current_cmd = success_command();

    let mut cmd = perfgate_cmd();
    cmd.arg("paired")
        .arg("--name")
        .arg("meta-test")
        .arg("--repeat")
        .arg("5")
        .arg("--warmup")
        .arg("2")
        .arg("--baseline-cmd");

    for arg in &baseline_cmd {
        cmd.arg(arg);
    }

    cmd.arg("--current-cmd");
    for arg in &current_cmd {
        cmd.arg(arg);
    }

    cmd.arg("--out").arg(&output_path);

    cmd.assert().success();

    let content = fs::read_to_string(&output_path).expect("failed to read output file");
    let receipt: serde_json::Value =
        serde_json::from_str(&content).expect("output should be valid JSON");

    let bench = &receipt["bench"];

    // Verify bench metadata
    assert_eq!(
        bench["name"].as_str(),
        Some("meta-test"),
        "bench name should match"
    );
    assert_eq!(bench["repeat"].as_u64(), Some(5), "repeat should match");
    assert_eq!(bench["warmup"].as_u64(), Some(2), "warmup should match");

    // Verify commands are arrays
    assert!(
        bench["current_command"].is_array(),
        "current_command should be an array"
    );
}

/// Test paired command with shell string arguments
#[test]
fn test_paired_with_shell_strings() {
    let temp_dir = tempdir().expect("failed to create temp dir");
    let output_path = temp_dir.path().join("paired.json");

    let mut cmd = perfgate_cmd();
    cmd.arg("paired")
        .arg("--name")
        .arg("shell-test")
        .arg("--repeat")
        .arg("1");

    #[cfg(unix)]
    {
        cmd.arg("--baseline-cmd").arg("true");
        cmd.arg("--current-cmd").arg("true");
    }
    #[cfg(windows)]
    {
        cmd.arg("--baseline-cmd").arg("cmd /c exit 0");
        cmd.arg("--current-cmd").arg("cmd /c exit 0");
    }

    cmd.arg("--out").arg(&output_path);

    cmd.assert().success();

    let content = fs::read_to_string(&output_path).expect("failed to read output file");
    let receipt: serde_json::Value =
        serde_json::from_str(&content).expect("output should be valid JSON");

    assert_eq!(
        receipt["bench"]["name"].as_str(),
        Some("shell-test"),
        "bench name should match"
    );

    let baseline_cmd = receipt["bench"]["baseline_command"]
        .as_array()
        .expect("baseline_command should be array");
    let current_cmd = receipt["bench"]["current_command"]
        .as_array()
        .expect("current_command should be array");

    #[cfg(unix)]
    {
        assert_eq!(baseline_cmd[0], "true");
        assert_eq!(current_cmd[0], "true");
    }
    #[cfg(windows)]
    {
        assert_eq!(baseline_cmd[0], "cmd");
        assert_eq!(baseline_cmd[1], "/c");
        assert_eq!(baseline_cmd[2], "exit");
        assert_eq!(baseline_cmd[3], "0");
        assert_eq!(current_cmd[0], "cmd");
    }
}