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
//! Integration tests for `perfgate export` command
//!
//! **Validates: Requirements for export functionality**

mod common;
use common::perfgate_cmd;
use predicates::prelude::*;

use std::fs;

use tempfile::tempdir;

use common::{fixtures_dir, generate_compare_receipt};

// ============================================================================
// Run receipt export tests
// ============================================================================

/// Test exporting run receipt to CSV format
#[test]
fn test_export_run_to_csv() {
    let temp_dir = tempdir().expect("failed to create temp dir");
    let export_path = temp_dir.path().join("export.csv");

    let baseline = fixtures_dir().join("baseline.json");

    let mut cmd = perfgate_cmd();
    cmd.arg("export")
        .arg("--run")
        .arg(&baseline)
        .arg("--format")
        .arg("csv")
        .arg("--out")
        .arg(&export_path);

    cmd.assert().success();

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

    // Read and verify content
    let content = fs::read_to_string(&export_path).expect("failed to read export file");

    // Verify CSV header
    assert!(
        content.starts_with("bench_name,wall_ms_median,wall_ms_min,wall_ms_max,binary_bytes_median,cpu_ms_median,ctx_switches_median,max_rss_kb_median,page_faults_median,io_read_bytes_median,io_write_bytes_median,network_packets_median,energy_uj_median,throughput_median,sample_count,timestamp\n"),
        "CSV should have correct header. Got: {}",
        content.lines().next().unwrap_or("")
    );

    // Verify benchmark name is present
    assert!(
        content.contains("test-benchmark"),
        "CSV should contain benchmark name"
    );

    // Verify wall_ms stats
    assert!(
        content.contains("100,98,102"),
        "CSV should contain wall_ms stats (median=100, min=98, max=102)"
    );

    // Verify max_rss_kb median
    assert!(
        content.contains("1024"),
        "CSV should contain max_rss_kb median"
    );
}

/// Test exporting run receipt to JSONL format
#[test]
fn test_export_run_to_jsonl() {
    let temp_dir = tempdir().expect("failed to create temp dir");
    let export_path = temp_dir.path().join("export.jsonl");

    let baseline = fixtures_dir().join("baseline.json");

    let mut cmd = perfgate_cmd();
    cmd.arg("export")
        .arg("--run")
        .arg(&baseline)
        .arg("--format")
        .arg("jsonl")
        .arg("--out")
        .arg(&export_path);

    cmd.assert().success();

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

    // Read and verify content
    let content = fs::read_to_string(&export_path).expect("failed to read export file");

    // Should be a single line of valid JSON
    let lines: Vec<&str> = content.trim().split('\n').collect();
    assert_eq!(
        lines.len(),
        1,
        "JSONL should have exactly 1 line for run receipt"
    );

    let parsed: serde_json::Value = serde_json::from_str(lines[0]).expect("should be valid JSON");
    assert_eq!(parsed["bench_name"], "test-benchmark");
    assert_eq!(parsed["wall_ms_median"], 100);
    assert_eq!(parsed["wall_ms_min"], 98);
    assert_eq!(parsed["wall_ms_max"], 102);
    assert_eq!(parsed["sample_count"], 5);
}

// ============================================================================
// Compare receipt export tests
// ============================================================================

/// Test exporting compare receipt to CSV format
#[test]
fn test_export_compare_to_csv() {
    let temp_dir = tempdir().expect("failed to create temp dir");
    let compare_receipt_path = temp_dir.path().join("compare.json");
    let export_path = temp_dir.path().join("export.csv");

    let baseline = fixtures_dir().join("baseline.json");
    let current = fixtures_dir().join("current_pass.json");

    // First, generate a compare receipt
    generate_compare_receipt(&baseline, &current, &compare_receipt_path)
        .expect("failed to generate compare receipt");

    assert!(
        compare_receipt_path.exists(),
        "compare receipt should exist"
    );

    let mut cmd = perfgate_cmd();
    cmd.arg("export")
        .arg("--compare")
        .arg(&compare_receipt_path)
        .arg("--format")
        .arg("csv")
        .arg("--out")
        .arg(&export_path);

    cmd.assert().success();

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

    // Read and verify content
    let content = fs::read_to_string(&export_path).expect("failed to read export file");

    // Verify CSV header
    assert!(
        content.starts_with(
            "bench_name,metric,baseline_value,current_value,regression_pct,status,threshold\n"
        ),
        "CSV should have correct header"
    );

    // Verify benchmark name and metrics
    assert!(
        content.contains("test-benchmark"),
        "CSV should contain benchmark name"
    );
    assert!(
        content.contains("wall_ms"),
        "CSV should contain wall_ms metric"
    );
}

/// Test exporting compare receipt to JSONL format
#[test]
fn test_export_compare_to_jsonl() {
    let temp_dir = tempdir().expect("failed to create temp dir");
    let compare_receipt_path = temp_dir.path().join("compare.json");
    let export_path = temp_dir.path().join("export.jsonl");

    let baseline = fixtures_dir().join("baseline.json");
    let current = fixtures_dir().join("current_pass.json");

    // First, generate a compare receipt
    generate_compare_receipt(&baseline, &current, &compare_receipt_path)
        .expect("failed to generate compare receipt");

    let mut cmd = perfgate_cmd();
    cmd.arg("export")
        .arg("--compare")
        .arg(&compare_receipt_path)
        .arg("--format")
        .arg("jsonl")
        .arg("--out")
        .arg(&export_path);

    cmd.assert().success();

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

    // Read and verify content
    let content = fs::read_to_string(&export_path).expect("failed to read export file");

    // Should have one line per metric
    let lines: Vec<&str> = content
        .trim()
        .split('\n')
        .filter(|s| !s.is_empty())
        .collect();
    assert!(
        !lines.is_empty(),
        "JSONL should have at least 1 line per metric"
    );

    // All lines should be valid JSON
    for line in &lines {
        let parsed: serde_json::Value = serde_json::from_str(line).expect("should be valid JSON");
        assert!(
            parsed.get("bench_name").is_some(),
            "each line should have bench_name"
        );
        assert!(
            parsed.get("metric").is_some(),
            "each line should have metric"
        );
    }
}

/// Test exporting run receipt to HTML format.
#[test]
fn test_export_run_to_html() {
    let temp_dir = tempdir().expect("failed to create temp dir");
    let export_path = temp_dir.path().join("export.html");
    let baseline = fixtures_dir().join("baseline.json");

    let mut cmd = perfgate_cmd();
    cmd.arg("export")
        .arg("--run")
        .arg(&baseline)
        .arg("--format")
        .arg("html")
        .arg("--out")
        .arg(&export_path);

    cmd.assert().success();

    let content = fs::read_to_string(&export_path).expect("failed to read export file");
    assert!(
        content.contains("<table"),
        "HTML export should contain a table"
    );
    assert!(
        content.contains("test-benchmark"),
        "HTML export should include bench name"
    );
}

/// Test exporting compare receipt to Prometheus format.
#[test]
fn test_export_compare_to_prometheus() {
    let temp_dir = tempdir().expect("failed to create temp dir");
    let compare_receipt_path = temp_dir.path().join("compare.json");
    let export_path = temp_dir.path().join("export.prom");

    let baseline = fixtures_dir().join("baseline.json");
    let current = fixtures_dir().join("current_pass.json");
    generate_compare_receipt(&baseline, &current, &compare_receipt_path)
        .expect("failed to generate compare receipt");

    let mut cmd = perfgate_cmd();
    cmd.arg("export")
        .arg("--compare")
        .arg(&compare_receipt_path)
        .arg("--format")
        .arg("prometheus")
        .arg("--out")
        .arg(&export_path);

    cmd.assert().success();

    let content = fs::read_to_string(&export_path).expect("failed to read export file");
    assert!(
        content.contains("perfgate_compare_regression_pct"),
        "Prometheus export should include regression metric"
    );
    assert!(
        content.contains("metric=\"wall_ms\""),
        "Prometheus export should include wall_ms label"
    );
}

// ============================================================================
// Format and argument tests
// ============================================================================

/// Test default format is CSV
#[test]
fn test_export_default_format_is_csv() {
    let temp_dir = tempdir().expect("failed to create temp dir");
    let export_path = temp_dir.path().join("export.csv");

    let baseline = fixtures_dir().join("baseline.json");

    let mut cmd = perfgate_cmd();
    cmd.arg("export")
        .arg("--run")
        .arg(&baseline)
        .arg("--out")
        .arg(&export_path);

    cmd.assert().success();

    let content = fs::read_to_string(&export_path).expect("failed to read export file");
    assert!(
        content.starts_with("bench_name,"),
        "default format should be CSV with header"
    );
}

/// Test invalid format returns error
#[test]
fn test_export_invalid_format() {
    let temp_dir = tempdir().expect("failed to create temp dir");
    let export_path = temp_dir.path().join("export.csv");

    let baseline = fixtures_dir().join("baseline.json");

    let mut cmd = perfgate_cmd();
    cmd.arg("export")
        .arg("--run")
        .arg(&baseline)
        .arg("--format")
        .arg("invalid")
        .arg("--out")
        .arg(&export_path);

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

/// Test mutually exclusive --run and --compare
#[test]
fn test_export_run_and_compare_mutually_exclusive() {
    let temp_dir = tempdir().expect("failed to create temp dir");
    let export_path = temp_dir.path().join("export.csv");

    let baseline = fixtures_dir().join("baseline.json");

    let mut cmd = perfgate_cmd();
    cmd.arg("export")
        .arg("--run")
        .arg(&baseline)
        .arg("--compare")
        .arg(&baseline)
        .arg("--out")
        .arg(&export_path);

    cmd.assert()
        .failure()
        .stderr(predicate::str::contains("cannot be used with"));
}

/// Test missing required argument
#[test]
fn test_export_missing_input() {
    let temp_dir = tempdir().expect("failed to create temp dir");
    let export_path = temp_dir.path().join("export.csv");

    let mut cmd = perfgate_cmd();
    cmd.arg("export").arg("--out").arg(&export_path);

    cmd.assert()
        .failure()
        .stderr(predicate::str::contains("--run").or(predicate::str::contains("--compare")));
}

/// Test missing input file
#[test]
fn test_export_missing_input_file() {
    let temp_dir = tempdir().expect("failed to create temp dir");
    let export_path = temp_dir.path().join("export.csv");
    let nonexistent = temp_dir.path().join("nonexistent.json");

    let mut cmd = perfgate_cmd();
    cmd.arg("export")
        .arg("--run")
        .arg(&nonexistent)
        .arg("--out")
        .arg(&export_path);

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

/// Test export with non-existent compare input file fails gracefully
#[test]
fn test_export_missing_compare_input_file() {
    let temp_dir = tempdir().expect("failed to create temp dir");
    let export_path = temp_dir.path().join("export.csv");
    let nonexistent = temp_dir.path().join("does_not_exist.json");

    let mut cmd = perfgate_cmd();
    cmd.arg("export")
        .arg("--compare")
        .arg(&nonexistent)
        .arg("--out")
        .arg(&export_path);

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

    // Output file should not exist
    assert!(
        !export_path.exists(),
        "export file should not be created on failure"
    );
}

// ============================================================================
// Stable ordering tests
// ============================================================================

/// Test that CSV output is deterministic across multiple runs
#[test]
fn test_export_csv_deterministic() {
    let temp_dir = tempdir().expect("failed to create temp dir");
    let compare_receipt_path = temp_dir.path().join("compare.json");
    let export_path1 = temp_dir.path().join("export1.csv");
    let export_path2 = temp_dir.path().join("export2.csv");

    let baseline = fixtures_dir().join("baseline.json");
    let current = fixtures_dir().join("current_pass.json");

    // Generate a compare receipt
    generate_compare_receipt(&baseline, &current, &compare_receipt_path)
        .expect("failed to generate compare receipt");

    // Export twice
    for export_path in [&export_path1, &export_path2] {
        let mut cmd = perfgate_cmd();
        cmd.arg("export")
            .arg("--compare")
            .arg(&compare_receipt_path)
            .arg("--format")
            .arg("csv")
            .arg("--out")
            .arg(export_path);

        cmd.assert().success();
    }

    let content1 = fs::read_to_string(&export_path1).expect("failed to read export1");
    let content2 = fs::read_to_string(&export_path2).expect("failed to read export2");

    assert_eq!(content1, content2, "CSV export should be deterministic");
}

/// Test metrics are sorted alphabetically in compare export
#[test]
fn test_export_compare_metrics_sorted() {
    let temp_dir = tempdir().expect("failed to create temp dir");
    let compare_receipt_path = temp_dir.path().join("compare.json");
    let export_path = temp_dir.path().join("export.csv");

    let baseline = fixtures_dir().join("baseline.json");
    let current = fixtures_dir().join("current_pass.json");

    // Generate a compare receipt
    generate_compare_receipt(&baseline, &current, &compare_receipt_path)
        .expect("failed to generate compare receipt");

    let mut cmd = perfgate_cmd();
    cmd.arg("export")
        .arg("--compare")
        .arg(&compare_receipt_path)
        .arg("--format")
        .arg("csv")
        .arg("--out")
        .arg(&export_path);

    cmd.assert().success();

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

    // Check that max_rss_kb comes before wall_ms (alphabetical order)
    let max_rss_pos = content.find("max_rss_kb");
    let wall_ms_pos = content.find("wall_ms");

    if let (Some(max_rss), Some(wall_ms)) = (max_rss_pos, wall_ms_pos) {
        assert!(
            max_rss < wall_ms,
            "max_rss_kb should come before wall_ms (alphabetical order)"
        );
    }
}