zenbench 0.1.9

Interleaved microbenchmarking with paired statistics, CI regression testing, and hardware-adaptive measurement
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
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
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
//! HTML report generation with inline SVG charts and expandable details.
//!
//! Produces a self-contained HTML file — no external dependencies,
//! no JavaScript required. SVG bar charts render natively in all browsers.
//! Each benchmark has a nested `<details>` section with full statistics.

use crate::format::format_ns;
use crate::results::{ComparisonResult, SuiteResult};

/// Generate a baseline comparison HTML section.
#[allow(dead_code)]
pub fn baseline_comparison_html(comparison: &crate::baseline::BaselineComparison) -> String {
    let mut html = String::new();
    html.push_str("<details open><summary><h2>Baseline Comparison</h2></summary>\n");

    if !comparison.warnings.is_empty() {
        for w in &comparison.warnings {
            html.push_str(&format!(
                "<p><span class=\"pill pill-yellow\">⚠</span> {w}</p>\n"
            ));
        }
    }

    html.push_str("<table><tr><th>benchmark</th><th>baseline</th><th>current</th><th>change</th><th>status</th></tr>\n");

    for delta in &comparison.benchmarks {
        let color = if delta.regressed {
            "var(--red)"
        } else if delta.improved {
            "var(--green)"
        } else {
            "inherit"
        };
        let status = if delta.regressed {
            "<span class=\"pill pill-red\">▲ REGRESSION</span>"
        } else if delta.improved {
            "<span class=\"pill pill-green\">▼ improved</span>"
        } else {
            "<span class=\"pill pill-dim\">unchanged</span>"
        };
        html.push_str(&format!(
            "<tr style=\"color:{color}\"><td>{}::{}</td><td>{}</td><td>{}</td><td>{:+.2}%</td><td>{status}</td></tr>\n",
            delta.group, delta.name,
            format_ns(delta.baseline_mean),
            format_ns(delta.new_mean),
            delta.pct_change,
        ));
    }
    html.push_str("</table>\n");

    // Summary
    let total = comparison.regressions + comparison.improvements + comparison.unchanged;
    html.push_str(&format!(
        "<p><strong>{}</strong> benchmarks: \
         <span class=\"pill pill-red\">{} regressions</span> \
         <span class=\"pill pill-green\">{} improvements</span> \
         <span class=\"pill pill-dim\">{} unchanged</span></p>\n",
        total, comparison.regressions, comparison.improvements, comparison.unchanged,
    ));

    if !comparison.new_benchmarks.is_empty() {
        html.push_str(&format!(
            "<p>New (not in baseline): {}</p>\n",
            comparison.new_benchmarks.join(", ")
        ));
    }
    if !comparison.missing_benchmarks.is_empty() {
        html.push_str(&format!(
            "<p>Missing (in baseline): {}</p>\n",
            comparison.missing_benchmarks.join(", ")
        ));
    }

    html.push_str("</details>\n");
    html
}

/// Generate a complete HTML report as a string.
pub fn to_html(result: &SuiteResult) -> String {
    let mut html = String::with_capacity(32_000);

    html.push_str(HTML_HEAD);
    html.push_str(&format!(
        "<h1>zenbench <small>{}</small></h1>\n",
        result.run_id
    ));

    // Header metadata
    html.push_str("<div class=\"header-meta\">");
    if let Some(hash) = &result.git_hash {
        html.push_str(&format!("<span>git: <code>{hash}</code></span>"));
    }
    if let Some(ci) = &result.ci_environment {
        html.push_str(&format!("<span>ci: {ci}</span>"));
    }
    html.push_str(&format!(
        "<span>total: {:.1}s</span>",
        result.total_time.as_secs_f64()
    ));
    if result.gate_waits > 0 {
        html.push_str(&format!("<span>{} noisy rounds</span>", result.gate_waits));
    }
    html.push_str(&format!(
        "<span>timer: {}ns</span>",
        result.timer_resolution_ns
    ));
    html.push_str(&format!(
        "<span>overhead: {:.2}ns/iter</span>",
        result.loop_overhead_ns
    ));
    if let Some(tb) = &result.testbed {
        html.push_str(&format!(
            "<span>{} ({}/{})</span>",
            tb.cpu_model, tb.arch, tb.os
        ));
    }
    if let Some(cal) = &result.calibration {
        html.push_str(&format!(
            "<span>cal: int={:.2}ns mem_bw={:.1}GiB/s lat={:.1}ns</span>",
            cal.integer_ns, cal.memory_bw_gibps, cal.memory_lat_ns
        ));
    }
    html.push_str("</div>\n");

    for comp in &result.comparisons {
        html.push_str(&render_group(comp));
    }

    html.push_str("<footer style=\"margin-top:2rem;padding:1rem 0;border-top:1px solid var(--border);color:var(--dim);font-size:0.85rem;text-align:center\">\
        Generated by <a href=\"https://github.com/imazen/zenbench\" style=\"color:var(--accent)\">zenbench</a> · \
        <a href=\"https://imazen.github.io/zenbench\" style=\"color:var(--accent)\">Documentation</a>\
        </footer>\n");
    html.push_str("</div></body></html>\n");
    html
}

fn render_group(comp: &ComparisonResult) -> String {
    let mut html = String::new();
    let tp_unit = comp.throughput_unit.as_deref();
    let has_throughput = comp.throughput.is_some();

    // Group header
    let mut meta_parts = vec![format!("{} rounds", comp.completed_rounds)];
    if comp.iterations_per_sample > 0 {
        meta_parts.push(format!("{} calls/sample", comp.iterations_per_sample));
    }
    if comp.cache_firewall {
        meta_parts.push("cache firewall".to_string());
    }
    if comp.cold_start {
        meta_parts.push("cold start".to_string());
    }
    html.push_str(&format!(
        "<details open><summary><h2>{}</h2>\
         <span class=\"meta\">{}</span></summary>\n",
        comp.group_name,
        meta_parts.join(" · "),
    ));

    // Methodology explanation
    html.push_str("<p class=\"methodology\">");
    html.push_str(&format!(
        "Each <strong>round</strong>, all {} benchmarks run in shuffled order. \
         Each benchmark runs <strong>{} calls</strong> (iterations) per round to produce one sample. \
         After <strong>{} rounds</strong>, paired statistics compare the round-by-round differences.",
        comp.benchmarks.len(),
        comp.iterations_per_sample,
        comp.completed_rounds,
    ));
    if comp.cold_start {
        html.push_str(" <strong>Cold-start mode</strong>: 1 call per sample with cache eviction between benchmarks.");
    }
    html.push_str("</p>\n");

    // Baseline lookup
    let baseline_name = comp
        .analyses
        .first()
        .map(|(base, _, _)| base.as_str())
        .unwrap_or_else(|| {
            comp.benchmarks
                .first()
                .map(|b| b.name.as_str())
                .unwrap_or("")
        });
    let baseline_analyses: std::collections::HashMap<&str, &crate::stats::PairedAnalysis> = comp
        .analyses
        .iter()
        .filter(|(base, _, _)| base == baseline_name)
        .map(|(_, cand, a)| (cand.as_str(), a))
        .collect();
    let fastest_mean = comp
        .benchmarks
        .iter()
        .map(|b| b.summary.mean)
        .fold(f64::INFINITY, f64::min);

    // Main table — real columns for alignment
    html.push_str("<table><tr class=\"header-row\"><th>benchmark</th><th>min</th><th>mean</th><th>±mad</th><th>95% CI</th>");
    if has_throughput {
        html.push_str("<th>throughput</th>");
    }
    html.push_str("</tr>\n");

    let mut current_subgroup: Option<&str> = None;

    for bench in &comp.benchmarks {
        // Subgroup header row
        if bench.subgroup.as_deref() != current_subgroup {
            current_subgroup = bench.subgroup.as_deref();
            if let Some(sg) = current_subgroup {
                let ncols = if has_throughput { 6 } else { 5 };
                html.push_str(&format!(
                    "<tr class=\"subgroup\"><td colspan=\"{ncols}\">{sg}</td></tr>\n"
                ));
            }
        }

        let is_fastest =
            (bench.summary.mean - fastest_mean).abs() < f64::EPSILON && comp.benchmarks.len() > 1;
        let cls = if is_fastest { " class=\"fastest\"" } else { "" };

        // CI column
        let ci_str = if bench.name == baseline_name {
            if let Some(ci) = &bench.mean_ci {
                format!("[{}{}]", format_ns(ci.lower), format_ns(ci.upper))
            } else {
                "".to_string()
            }
        } else if let Some(a) = baseline_analyses.get(bench.name.as_str()) {
            let bm = a.baseline.mean;
            if bm.abs() > f64::EPSILON {
                let lo = a.ci_lower / bm * 100.0;
                let hi = a.ci_upper / bm * 100.0;
                let color = if hi < 0.0 {
                    "color:#9ece6a"
                } else if lo > 0.0 {
                    "color:#f7768e"
                } else {
                    ""
                };
                format!("<span style=\"{color}\">[{lo:+.1}%–{hi:+.1}%]</span>")
            } else {
                "".to_string()
            }
        } else {
            "".to_string()
        };

        // Throughput cell
        let tp = if has_throughput {
            comp.throughput
                .as_ref()
                .map(|tp| {
                    let (val, unit) = tp.compute(bench.summary.mean, tp_unit);
                    format!("<td>{val:.2} {unit}</td>")
                })
                .unwrap_or_else(|| "<td></td>".to_string())
        } else {
            String::new()
        };

        let analysis = baseline_analyses.get(bench.name.as_str()).copied();
        let ncols = if has_throughput { 6 } else { 5 };
        let bench_id = format!(
            "b-{}-{}",
            comp.group_name.replace(' ', "_"),
            bench.name.replace(' ', "_")
        );

        // Data row — real table cells for column alignment
        html.push_str(&format!(
            "<tr{cls}>\
             <td><input type=\"checkbox\" id=\"{bench_id}\" class=\"detail-toggle\" hidden>\
             <label class=\"bench-toggle\" for=\"{bench_id}\">{}</label></td>\
             <td>{}</td><td>{}</td><td>±{}</td><td>{ci_str}</td>{tp}</tr>\n",
            bench.name,
            format_ns(bench.summary.min),
            format_ns(bench.summary.mean),
            format_ns(bench.summary.mad),
        ));
        // Detail row — hidden by default, shown when checkbox checked
        html.push_str(&format!(
            "<tr class=\"detail-row\"><td colspan=\"{ncols}\">\n"
        ));
        html.push_str(&render_bench_detail_content(bench, analysis));
        html.push_str("</td></tr>\n");
    }
    html.push_str("</table>\n");

    // SVG bar chart — matrix layout when benchmarks have variant/param names
    html.push_str(&render_chart(comp));

    html.push_str("</details>\n");
    html
}

fn render_bench_detail_content(
    bench: &crate::results::BenchmarkResult,
    analysis: Option<&crate::stats::PairedAnalysis>,
) -> String {
    let mut html = String::new();
    html.push_str("<div class=\"detail-grid\">\n");

    // Summary stats
    let s = &bench.summary;
    html.push_str("<div class=\"detail-section\"><h4>Summary</h4><table class=\"detail-table\">");
    html.push_str(&format!("<tr><td>n</td><td>{}</td></tr>", s.n));
    html.push_str(&format!(
        "<tr><td>min</td><td>{}</td></tr>",
        format_ns(s.min)
    ));
    html.push_str(&format!(
        "<tr><td>mean</td><td>{}</td></tr>",
        format_ns(s.mean)
    ));
    html.push_str(&format!(
        "<tr><td>median</td><td>{}</td></tr>",
        format_ns(s.median)
    ));
    html.push_str(&format!(
        "<tr><td>max</td><td>{}</td></tr>",
        format_ns(s.max)
    ));
    html.push_str(&format!(
        "<tr><td>MAD</td><td>{}</td></tr>",
        format_ns(s.mad)
    ));
    html.push_str(&format!(
        "<tr><td>std dev</td><td>{}</td></tr>",
        format_ns(s.std_dev())
    ));
    let cv = s.cv() * 100.0;
    let cv_label = if cv < 5.0 {
        "excellent"
    } else if cv < 15.0 {
        "good"
    } else if cv < 30.0 {
        "noisy"
    } else {
        "very noisy — try a quieter system"
    };
    let cv_pill = if cv < 5.0 {
        "pill-green"
    } else if cv < 15.0 {
        "pill-dim"
    } else if cv < 30.0 {
        "pill-yellow"
    } else {
        "pill-red"
    };
    html.push_str(&format!(
        "<tr><td>CV</td><td>{cv:.1}% <span class=\"pill {cv_pill}\">{cv_label}</span></td></tr>",
    ));
    if bench.cold_start_ns > 0.0 {
        html.push_str(&format!(
            "<tr><td>cold start</td><td>{}</td></tr>",
            format_ns(bench.cold_start_ns)
        ));
    }
    if let Some(ci) = &bench.mean_ci {
        html.push_str(&format!(
            "<tr><td>mean CI</td><td>[{}{}]</td></tr>",
            format_ns(ci.lower),
            format_ns(ci.upper)
        ));
    }
    if bench.timer_ticks_per_sample < 50.0 {
        html.push_str(&format!(
            "<tr><td>timer ticks/sample</td><td>{:.0} <span class=\"pill pill-yellow\">resolution-limited</span></td></tr>",
            bench.timer_ticks_per_sample,
        ));
    }
    if let Some(slope) = bench.slope_ns {
        html.push_str(&format!(
            "<tr><td>slope (OLS)</td><td>{}</td></tr>",
            format_ns(slope)
        ));
    }
    html.push_str("</table></div>\n");

    // Paired analysis (if vs baseline)
    if let Some(a) = analysis {
        html.push_str(
            "<div class=\"detail-section\"><h4>vs Baseline</h4><table class=\"detail-table\">",
        );
        html.push_str(&format!(
            "<tr><td>change</td><td>{:+.2}%</td></tr>",
            a.pct_change
        ));
        let sig_pill = if a.resolution_limited {
            "<span class=\"pill pill-yellow\">below timer resolution</span>"
        } else if a.significant {
            "<span class=\"pill pill-green\">yes — real difference</span>"
        } else {
            "<span class=\"pill pill-dim\">no — within noise</span>"
        };
        html.push_str(&format!("<tr><td>significant</td><td>{sig_pill}</td></tr>",));
        let d = a.cohens_d.abs();
        let d_label = if d < 0.2 {
            "negligible"
        } else if d < 0.5 {
            "small"
        } else if d < 0.8 {
            "medium"
        } else {
            "large"
        };
        html.push_str(&format!(
            "<tr><td>effect size</td><td>d={:.2} <span class=\"pill pill-dim\">{d_label}</span></td></tr>",
            a.cohens_d,
        ));
        html.push_str(&format!(
            "<tr><td>Wilcoxon p</td><td>{:.4}</td></tr>",
            a.wilcoxon_p
        ));
        html.push_str(&format!(
            "<tr><td>CI</td><td>[{}{}]</td></tr>",
            format_ns(a.ci_lower),
            format_ns(a.ci_upper)
        ));
        let drift = a.drift_correlation;
        let drift_label = if drift.abs() < 0.3 {
            "stable — no systematic trend"
        } else if drift > 0.0 {
            "later rounds slower — possible thermal throttling"
        } else {
            "later rounds faster — cache/branch predictor warming up"
        };
        let drift_pill = if drift.abs() < 0.3 {
            "pill-green"
        } else if drift.abs() < 0.6 {
            "pill-yellow"
        } else {
            "pill-red"
        };
        html.push_str(&format!(
            "<tr><td>drift</td><td>r={drift:.2} <span class=\"{drift_pill} pill\">{drift_label}</span></td></tr>",
        ));
        html.push_str(&format!(
            "<tr><td>samples</td><td>{}</td></tr>",
            a.n_samples
        ));
        html.push_str(&format!(
            "<tr><td>outliers removed</td><td>{}</td></tr>",
            a.n_outliers
        ));
        html.push_str("</table></div>\n");
    }

    // Alloc stats
    #[cfg(feature = "alloc-profiling")]
    if let Some(alloc) = &bench.alloc_stats {
        html.push_str(
            "<div class=\"detail-section\"><h4>Allocations</h4><table class=\"detail-table\">",
        );
        html.push_str(&format!(
            "<tr><td>allocs/iter</td><td>{:.1}</td></tr>",
            alloc.allocs_per_iter
        ));
        html.push_str(&format!(
            "<tr><td>deallocs/iter</td><td>{:.1}</td></tr>",
            alloc.deallocs_per_iter
        ));
        html.push_str(&format!(
            "<tr><td>reallocs/iter</td><td>{:.1}</td></tr>",
            alloc.reallocs_per_iter
        ));
        html.push_str(&format!(
            "<tr><td>bytes/iter</td><td>{:.0}</td></tr>",
            alloc.bytes_per_iter
        ));
        html.push_str("</table></div>\n");
    }

    // Tags
    if !bench.tags.is_empty() {
        html.push_str("<div class=\"detail-section\"><h4>Tags</h4><table class=\"detail-table\">");
        for (k, v) in &bench.tags {
            html.push_str(&format!("<tr><td>{k}</td><td>{v}</td></tr>"));
        }
        html.push_str("</table></div>\n");
    }

    html.push_str("</div>\n");
    html
}

// ---- Matrix chart support ----

/// Colors for grouped bar charts. Visually distinct on dark (#1a1b26) background.
const PALETTE: &[&str] = &[
    "#7aa2f7", // blue (Tokyo Night accent)
    "#9ece6a", // green
    "#e0af68", // amber
    "#f7768e", // pink
    "#bb9af7", // purple
    "#73daca", // teal
    "#ff9e64", // orange
    "#2ac3de", // cyan
];

/// Parsed matrix structure: benchmarks with `variant/param` names.
pub(crate) struct MatrixChart {
    /// Parameter values in order of first appearance (e.g., "256x256", "512x512").
    pub(crate) params: Vec<String>,
    /// Variant names sorted alphabetically for deterministic color assignment.
    pub(crate) variants: Vec<String>,
    /// (variant_idx, param_idx) → benchmark index in the ComparisonResult.
    pub(crate) cells: std::collections::HashMap<(usize, usize), usize>,
}

/// Try to detect matrix structure from benchmark names containing `/`.
pub(crate) fn detect_matrix(comp: &ComparisonResult) -> Option<MatrixChart> {
    let mut param_order: Vec<String> = Vec::new();
    let mut variant_set: std::collections::HashSet<String> = std::collections::HashSet::new();
    let mut entries: Vec<(String, String, usize)> = Vec::new(); // (variant, param, bench_idx)

    for (i, bench) in comp.benchmarks.iter().enumerate() {
        if let Some(slash) = bench.name.rfind('/') {
            let variant = bench.name[..slash].to_string();
            let param = bench.name[slash + 1..].to_string();
            if !param.is_empty() && !variant.is_empty() {
                if !param_order.contains(&param) {
                    param_order.push(param.clone());
                }
                variant_set.insert(variant.clone());
                entries.push((variant, param, i));
            }
        }
    }

    // Need at least 2 variants and 2 params to form a matrix
    if variant_set.len() < 2 || param_order.len() < 2 {
        return None;
    }
    // All benchmarks should have the `/` pattern (no mixed flat+matrix)
    if entries.len() != comp.benchmarks.len() {
        return None;
    }

    let mut variants: Vec<String> = variant_set.into_iter().collect();
    variants.sort();

    let variant_idx = |v: &str| variants.iter().position(|x| x == v).unwrap();
    let param_idx = |p: &str| param_order.iter().position(|x| x == p).unwrap();

    let mut cells = std::collections::HashMap::new();
    for (variant, param, bench_idx) in &entries {
        cells.insert((variant_idx(variant), param_idx(param)), *bench_idx);
    }

    Some(MatrixChart {
        params: param_order,
        variants,
        cells,
    })
}

/// Render a grouped bar chart SVG for matrix-structured benchmarks.
fn render_matrix_svg_bar_chart(comp: &ComparisonResult, matrix: &MatrixChart) -> String {
    let tp_unit = comp.throughput_unit.as_deref();
    let has_throughput = comp.throughput.is_some();

    let bar_h: usize = 22;
    let bar_gap: usize = 3;
    let section_gap: usize = 18;
    let header_h: usize = 20;
    let legend_h: usize = 30;
    let label_w: usize = 180;
    let chart_w: usize = 400;
    let value_w: usize = 130;
    let total_w = label_w + chart_w + value_w + 20;

    // Calculate total height
    let section_h = matrix.variants.len() * (bar_h + bar_gap) + header_h + section_gap;
    let total_h = legend_h + matrix.params.len() * section_h + 10;

    let mut svg = format!(
        "<svg width=\"100%\" viewBox=\"0 0 {total_w} {total_h}\" xmlns=\"http://www.w3.org/2000/svg\">\n\
         <style>\n\
           text {{ font-family: -apple-system, sans-serif; font-size: 12px; fill: #c0caf5; }}\n\
           .param {{ font-size: 13px; font-weight: 600; fill: #a9b1d6; }}\n\
           .value {{ font-size: 11px; fill: #737aa2; }}\n\
           .legend {{ font-size: 11px; fill: #a9b1d6; }}\n\
         </style>\n\
         <rect width=\"100%\" height=\"100%\" fill=\"#1a1b26\" rx=\"6\"/>\n"
    );

    // Legend
    let mut lx = 12;
    for (vi, variant) in matrix.variants.iter().enumerate() {
        let color = PALETTE[vi % PALETTE.len()];
        svg.push_str(&format!(
            "<rect x=\"{lx}\" y=\"8\" width=\"12\" height=\"12\" rx=\"2\" fill=\"{color}\"/>\n\
             <text x=\"{}\" y=\"18\" class=\"legend\">{variant}</text>\n",
            lx + 16,
        ));
        lx += 16 + variant.len() * 7 + 14;
    }

    let mut y = legend_h;

    for (pi, param) in matrix.params.iter().enumerate() {
        // Section header
        svg.push_str(&format!(
            "<text x=\"{label_w}\" y=\"{}\" class=\"param\">{param}</text>\n",
            y + 14,
        ));
        y += header_h;

        // Per-section max for scaling
        let section_max = matrix
            .variants
            .iter()
            .enumerate()
            .filter_map(|(vi, _)| {
                matrix
                    .cells
                    .get(&(vi, pi))
                    .map(|&bi| comp.benchmarks[bi].summary.mean)
            })
            .fold(0.0_f64, f64::max);

        let section_min = matrix
            .variants
            .iter()
            .enumerate()
            .filter_map(|(vi, _)| {
                matrix
                    .cells
                    .get(&(vi, pi))
                    .map(|&bi| comp.benchmarks[bi].summary.mean)
            })
            .fold(f64::INFINITY, f64::min);

        for (vi, variant) in matrix.variants.iter().enumerate() {
            let color = PALETTE[vi % PALETTE.len()];

            if let Some(&bi) = matrix.cells.get(&(vi, pi)) {
                let bench = &comp.benchmarks[bi];
                let mean = bench.summary.mean;

                // Scale: slowest fills chart_w, fastest is proportionally shorter
                let frac = if has_throughput && section_min > 0.0 {
                    section_min / mean // throughput: faster = longer bar
                } else {
                    mean / section_max // time: slower = longer bar
                };
                let bar_w = (frac * chart_w as f64).max(3.0) as usize;

                let is_fastest =
                    (mean - section_min).abs() < f64::EPSILON && matrix.variants.len() > 1;
                let opacity = if is_fastest { "1.0" } else { "0.8" };

                let value = if has_throughput {
                    comp.throughput
                        .as_ref()
                        .map(|tp| tp.format(mean, tp_unit))
                        .unwrap_or_default()
                } else {
                    format_ns(mean)
                };

                // Truncate variant name for label
                let label = if variant.len() > 22 {
                    format!("{}...", &variant[..20])
                } else {
                    variant.clone()
                };

                let ty = y + bar_h / 2;
                let lx = label_w - 8;
                let vx = label_w + bar_w + 6;
                let bh = bar_h - 2;
                svg.push_str(&format!(
                    "<text x=\"{lx}\" y=\"{ty}\" text-anchor=\"end\" dominant-baseline=\"middle\">{label}</text>\n\
                     <rect x=\"{label_w}\" y=\"{y}\" width=\"{bar_w}\" height=\"{bh}\" rx=\"3\" fill=\"{color}\" opacity=\"{opacity}\"/>\n\
                     <text x=\"{vx}\" y=\"{ty}\" class=\"value\" dominant-baseline=\"middle\">{value}</text>\n",
                ));
            }

            y += bar_h + bar_gap;
        }

        y += section_gap;
    }

    svg.push_str("</svg>\n");
    svg
}

/// Dispatch to matrix or flat chart based on benchmark name structure.
///
/// Used internally by the HTML report. For standalone SVG export, use
/// [`render_chart_standalone`].
fn render_chart(comp: &ComparisonResult) -> String {
    if let Some(matrix) = detect_matrix(comp) {
        render_matrix_svg_bar_chart(comp, &matrix)
    } else {
        render_svg_bar_chart(comp)
    }
}

/// Render a chart as a standalone SVG for README/docs embedding.
///
/// Uses `@media (prefers-color-scheme)` so one SVG works in both light and
/// dark contexts (e.g., GitHub). Includes ±MAD error indicators and
/// proper labeling with group name as title.
pub(crate) fn render_chart_standalone(comp: &ComparisonResult) -> String {
    if let Some(matrix) = detect_matrix(comp) {
        render_readme_matrix_svg(comp, &matrix)
    } else {
        render_chart(comp)
    }
}

/// Standalone dual-theme matrix chart for README embedding.
fn render_readme_matrix_svg(comp: &ComparisonResult, matrix: &MatrixChart) -> String {
    let tp_unit = comp.throughput_unit.as_deref();
    let has_throughput = comp.throughput.is_some();

    let bar_h: usize = 24;
    let bar_gap: usize = 4;
    let section_gap: usize = 22;
    let header_h: usize = 24;
    let title_h: usize = 32;
    let legend_h: usize = 32;
    let label_w: usize = 160;
    let chart_w: usize = 420;
    let value_w: usize = 160; // wider for "123.4ms ±1.2"
    let total_w = label_w + chart_w + value_w + 20;

    let section_h = matrix.variants.len() * (bar_h + bar_gap) + header_h + section_gap;
    let total_h = title_h + legend_h + matrix.params.len() * section_h + 10;

    let mut svg = format!(
        "<svg width=\"100%\" viewBox=\"0 0 {total_w} {total_h}\" \
         xmlns=\"http://www.w3.org/2000/svg\">\n\
         <style>\n\
           text {{ font-family: -apple-system, 'Segoe UI', system-ui, sans-serif; }}\n\
           .title {{ font-size: 14px; font-weight: 600; }}\n\
           .param {{ font-size: 12px; font-weight: 600; }}\n\
           .label {{ font-size: 11px; }}\n\
           .value {{ font-size: 10.5px; }}\n\
           .mad {{ font-size: 9.5px; }}\n\
           .legend {{ font-size: 11px; }}\n\
           @media (prefers-color-scheme: dark) {{\n\
             .bg {{ fill: #1a1b26; }}\n\
             .title {{ fill: #c0caf5; }}\n\
             .param {{ fill: #a9b1d6; }}\n\
             .label {{ fill: #c0caf5; }}\n\
             .value {{ fill: #a9b1d6; }}\n\
             .mad {{ fill: #565f89; }}\n\
             .legend {{ fill: #a9b1d6; }}\n\
             .whisker {{ stroke: #565f89; }}\n\
           }}\n\
           @media (prefers-color-scheme: light) {{\n\
             .bg {{ fill: #ffffff; }}\n\
             .title {{ fill: #24292f; }}\n\
             .param {{ fill: #57606a; }}\n\
             .label {{ fill: #24292f; }}\n\
             .value {{ fill: #57606a; }}\n\
             .mad {{ fill: #8b949e; }}\n\
             .legend {{ fill: #57606a; }}\n\
             .whisker {{ stroke: #8b949e; }}\n\
           }}\n\
         </style>\n\
         <rect class=\"bg\" width=\"100%\" height=\"100%\" rx=\"6\"/>\n"
    );

    // Title
    svg.push_str(&format!(
        "<text x=\"{x}\" y=\"22\" class=\"title\">{}</text>\n",
        comp.group_name,
        x = label_w,
    ));

    // Legend
    let mut lx: usize = 12;
    let ly = title_h + 6;
    for (vi, variant) in matrix.variants.iter().enumerate() {
        let color = PALETTE[vi % PALETTE.len()];
        svg.push_str(&format!(
            "<rect x=\"{lx}\" y=\"{ly}\" width=\"12\" height=\"12\" rx=\"2\" fill=\"{color}\"/>\n\
             <text x=\"{}\" y=\"{}\" class=\"legend\">{variant}</text>\n",
            lx + 16,
            ly + 10,
        ));
        lx += 16 + variant.len() * 7 + 14;
    }

    let mut y = title_h + legend_h;

    for (pi, param) in matrix.params.iter().enumerate() {
        // Section header
        svg.push_str(&format!(
            "<text x=\"{label_w}\" y=\"{}\" class=\"param\">{param}</text>\n",
            y + 16,
        ));
        y += header_h;

        // Per-section max for scaling
        let section_max = matrix
            .variants
            .iter()
            .enumerate()
            .filter_map(|(vi, _)| {
                matrix.cells.get(&(vi, pi)).map(|&bi| {
                    let b = &comp.benchmarks[bi];
                    b.summary.mean + b.summary.mad // include MAD in scale
                })
            })
            .fold(0.0_f64, f64::max);

        let section_min = matrix
            .variants
            .iter()
            .enumerate()
            .filter_map(|(vi, _)| {
                matrix
                    .cells
                    .get(&(vi, pi))
                    .map(|&bi| comp.benchmarks[bi].summary.mean)
            })
            .fold(f64::INFINITY, f64::min);

        for (vi, variant) in matrix.variants.iter().enumerate() {
            let color = PALETTE[vi % PALETTE.len()];

            if let Some(&bi) = matrix.cells.get(&(vi, pi)) {
                let bench = &comp.benchmarks[bi];
                let mean = bench.summary.mean;
                let mad = bench.summary.mad;

                let frac = if has_throughput && section_min > 0.0 {
                    section_min / mean
                } else {
                    mean / section_max
                };
                let bar_w = (frac * chart_w as f64).max(3.0) as usize;

                let is_fastest =
                    (mean - section_min).abs() < f64::EPSILON && matrix.variants.len() > 1;
                let opacity = if is_fastest { "0.95" } else { "0.75" };

                let mean_str = if has_throughput {
                    comp.throughput
                        .as_ref()
                        .map(|tp| tp.format(mean, tp_unit))
                        .unwrap_or_default()
                } else {
                    format_ns(mean)
                };

                let mad_str = format_ns(mad);

                let label = if variant.len() > 20 {
                    format!("{}...", &variant[..18])
                } else {
                    variant.clone()
                };

                let ty = y + bar_h / 2;
                let lx = label_w - 8;
                let vx = label_w + bar_w + 8;
                let bh = bar_h - 2;

                // MAD whisker line
                let mad_frac = mad / section_max;
                let whisker_w = (mad_frac * chart_w as f64).clamp(1.0, 40.0) as usize;
                let whisker_x = label_w + bar_w;
                let whisker_top = y + 4;
                let whisker_bot = y + bh - 4;

                svg.push_str(&format!(
                    "<text x=\"{lx}\" y=\"{ty}\" text-anchor=\"end\" \
                     dominant-baseline=\"middle\" class=\"label\">{label}</text>\n\
                     <rect x=\"{label_w}\" y=\"{y}\" width=\"{bar_w}\" \
                     height=\"{bh}\" rx=\"3\" fill=\"{color}\" opacity=\"{opacity}\"/>\n\
                     <line x1=\"{whisker_x}\" y1=\"{ty}\" \
                     x2=\"{}\" y2=\"{ty}\" \
                     class=\"whisker\" stroke-width=\"1.5\"/>\n\
                     <line x1=\"{}\" y1=\"{whisker_top}\" \
                     x2=\"{}\" y2=\"{whisker_bot}\" \
                     class=\"whisker\" stroke-width=\"1\"/>\n\
                     <text x=\"{vx}\" y=\"{}\" \
                     dominant-baseline=\"middle\" class=\"value\">{mean_str}</text>\n\
                     <text x=\"{vx}\" y=\"{}\" \
                     dominant-baseline=\"middle\" class=\"mad\">\u{00b1}{mad_str}</text>\n",
                    whisker_x + whisker_w, // whisker horizontal end
                    whisker_x + whisker_w, // whisker cap x
                    whisker_x + whisker_w, // whisker cap x
                    ty - 5,                // value text (above center)
                    ty + 6,                // MAD text (below center)
                ));
            }

            y += bar_h + bar_gap;
        }

        y += section_gap;
    }

    svg.push_str("</svg>\n");
    svg
}

fn render_svg_bar_chart(comp: &ComparisonResult) -> String {
    if comp.benchmarks.len() < 2 {
        return String::new();
    }

    let tp_unit = comp.throughput_unit.as_deref();
    let has_throughput = comp.throughput.is_some();

    let mut sorted: Vec<&crate::results::BenchmarkResult> = comp.benchmarks.iter().collect();
    sorted.sort_by(|a, b| a.summary.mean.total_cmp(&b.summary.mean));

    let max_mean = sorted.last().map(|b| b.summary.mean).unwrap_or(1.0);
    let min_mean = sorted.first().map(|b| b.summary.mean).unwrap_or(1.0);
    let fastest_mean = sorted
        .first()
        .map(|b| b.summary.mean)
        .unwrap_or(f64::INFINITY);

    let bar_h = 24;
    let gap = 4;
    let label_w = 200;
    let chart_w = 400;
    let value_w = 140;
    let total_w = label_w + chart_w + value_w + 20;
    let total_h = sorted.len() * (bar_h + gap) + 10;

    let mut svg = format!(
        "<svg width=\"100%\" viewBox=\"0 0 {total_w} {total_h}\" xmlns=\"http://www.w3.org/2000/svg\">\n\
         <style>\n\
           text {{ font-family: -apple-system, sans-serif; font-size: 13px; fill: #c0caf5; }}\n\
           .bar {{ fill: #7aa2f7; }}\n\
           .bar-fastest {{ fill: #9ece6a; }}\n\
           .value {{ font-size: 12px; fill: #565f89; }}\n\
         </style>\n\
         <rect width=\"100%\" height=\"100%\" fill=\"#1a1b26\" rx=\"6\"/>\n"
    );

    for (i, bench) in sorted.iter().enumerate() {
        let y = i * (bar_h + gap) + 5;
        let is_fastest = (bench.summary.mean - fastest_mean).abs() < f64::EPSILON;

        let frac = if has_throughput && min_mean > 0.0 {
            min_mean / bench.summary.mean
        } else {
            bench.summary.mean / max_mean
        };
        let bar_w = (frac * chart_w as f64).max(2.0) as usize;
        let cls = if is_fastest { "bar-fastest" } else { "bar" };

        let label = if bench.name.len() > 25 {
            format!("{}", &bench.name[..24])
        } else {
            bench.name.clone()
        };

        let value = if has_throughput {
            comp.throughput
                .as_ref()
                .map(|tp| tp.format(bench.summary.mean, tp_unit))
                .unwrap_or_default()
        } else {
            format_ns(bench.summary.mean)
        };

        svg.push_str(&format!(
            "  <text x=\"5\" y=\"{ty}\" dominant-baseline=\"middle\">{label}</text>\n\
             <rect class=\"{cls}\" x=\"{label_w}\" y=\"{y}\" width=\"{bar_w}\" height=\"{bar_h}\" rx=\"3\"/>\n\
             <text class=\"value\" x=\"{vx}\" y=\"{ty}\" dominant-baseline=\"middle\">{value}</text>\n",
            ty = y + bar_h / 2,
            vx = label_w + bar_w + 8,
        ));
    }

    svg.push_str("</svg>\n");
    svg
}

const HTML_HEAD: &str = r#"<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>zenbench results</title>
<style>
  :root { --bg: #1a1b26; --fg: #c0caf5; --accent: #7aa2f7; --dim: #737aa2; --green: #9ece6a; --red: #f7768e; --yellow: #e0af68; --surface: #24283b; --border: #2f3549; }
  body { font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif; background: var(--bg); color: var(--fg); margin: 0; padding: 0; font-size: 15px; line-height: 1.5; }
  .container { max-width: 1060px; margin: 0 auto; padding: 2rem; }
  h1 { color: #fff; margin-bottom: 0.5rem; font-size: 1.8rem; } h1 small { color: var(--dim); font-weight: normal; font-size: 0.4em; }
  h2 { color: var(--accent); font-size: 1.2rem; margin: 0; display: inline; }
  h4 { color: var(--accent); font-size: 0.95rem; margin: 0.6rem 0 0.3rem; }
  .meta { color: var(--dim); font-size: 0.9rem; margin-left: 1rem; }
  .header-meta { display: flex; gap: 1.5rem; flex-wrap: wrap; color: var(--dim); font-size: 0.9rem; margin-bottom: 1.2rem; padding: 0.6rem 0.8rem; background: var(--surface); border-radius: 6px; }
  summary { cursor: pointer; padding: 0.5rem 0; }
  summary:hover { opacity: 0.8; }
  details { margin: 0.8rem 0; border: 1px solid var(--border); border-radius: 8px; padding: 0.8rem 1.2rem; }
  table { border-collapse: collapse; width: 100%; margin: 0.5rem 0; }
  th, td { padding: 0.5rem 0.8rem; text-align: right; font-size: 1.05rem; border: none; }
  th { color: var(--dim); font-weight: 600; font-size: 0.85rem; border-bottom: 2px solid var(--border); }
  th:first-child, td:first-child { text-align: left; }
  td { font-variant-numeric: tabular-nums; }
  .header-row th { padding-bottom: 0.4rem; }
  tr:hover > td { background: rgba(255,255,255,0.03); }
  .fastest > td { color: var(--green); }
  .subgroup td { color: var(--dim); font-style: italic; font-size: 0.9rem; padding-top: 0.8rem; border-bottom: 1px solid var(--border); text-align: left; }
  .bench-toggle { cursor: pointer; white-space: nowrap; }
  .bench-toggle::before { content: '▸ '; color: var(--dim); }
  .detail-toggle:checked + .bench-toggle::before { content: '▾ '; }
  .bench-toggle:hover { opacity: 0.8; }
  .detail-row { display: none; }
  .detail-row td { padding: 0.2rem 0.8rem 0.6rem; border: none; }
  tr:has(.detail-toggle:checked) + .detail-row { display: table-row; }
  .detail-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(240px, 1fr)); gap: 0.6rem; padding: 0.6rem 0; }
  .detail-section { background: var(--surface); border-radius: 6px; padding: 0.6rem 0.9rem; }
  .detail-table { margin: 0; }
  .detail-table td { font-size: 0.9rem; padding: 0.2rem 0.5rem; border-bottom: 1px solid rgba(255,255,255,0.05); }
  .detail-table td:first-child { color: var(--dim); width: 45%; }
  .explain { font-size: 0.8rem; color: var(--dim); font-style: italic; margin-top: 0.15rem; }
  .methodology { font-size: 0.85rem; color: var(--dim); margin: 0.3rem 0 0.6rem; line-height: 1.6; }
  .methodology strong { color: var(--fg); font-weight: 500; }
  .pill { display: inline-block; font-size: 0.75rem; padding: 0.1em 0.5em; border-radius: 10px; font-weight: 600; }
  .pill-green { background: rgba(158,206,106,0.15); color: var(--green); }
  .pill-red { background: rgba(247,118,142,0.15); color: var(--red); }
  .pill-yellow { background: rgba(224,175,104,0.15); color: var(--yellow); }
  .pill-dim { background: rgba(115,122,162,0.15); color: var(--dim); }
  svg { margin: 0.8rem 0; display: block; max-width: 100%; }
  code { background: var(--surface); padding: 0.15em 0.4em; border-radius: 3px; font-size: 0.9em; }
</style>
</head>
<body><div class="container">
"#;

#[cfg(test)]
mod tests {
    use super::*;
    use crate::results::{BenchmarkResult, ComparisonResult};
    use crate::stats::Summary;

    fn make_bench(name: &str, mean: f64) -> BenchmarkResult {
        BenchmarkResult {
            name: name.to_string(),
            summary: Summary::from_slice(&[mean * 0.95, mean, mean * 1.05]),
            ..Default::default()
        }
    }

    fn make_comp(name: &str, benches: Vec<BenchmarkResult>) -> ComparisonResult {
        ComparisonResult {
            group_name: name.to_string(),
            benchmarks: benches,
            analyses: Vec::new(),
            completed_rounds: 30,
            throughput: None,
            cache_firewall: false,
            cache_firewall_bytes: 0,
            baseline_only: false,
            throughput_unit: None,
            sort_by_speed: false,
            expect_sub_ns: false,
            cold_start: false,
            iterations_per_sample: 100,
        }
    }

    #[test]
    fn detect_matrix_valid() {
        let comp = make_comp(
            "decode",
            vec![
                make_bench("mozjpeg/256x256", 275.0),
                make_bench("zune/256x256", 255.0),
                make_bench("zenjpeg/256x256", 246.0),
                make_bench("mozjpeg/512x512", 1060.0),
                make_bench("zune/512x512", 1080.0),
                make_bench("zenjpeg/512x512", 1030.0),
            ],
        );
        let m = detect_matrix(&comp).expect("should detect matrix");
        assert_eq!(m.variants, ["mozjpeg", "zenjpeg", "zune"]);
        assert_eq!(m.params, ["256x256", "512x512"]);
        assert_eq!(m.cells.len(), 6);
    }

    #[test]
    fn detect_matrix_flat_names() {
        let comp = make_comp(
            "sort",
            vec![make_bench("std_sort", 100.0), make_bench("unstable", 80.0)],
        );
        assert!(detect_matrix(&comp).is_none());
    }

    #[test]
    fn detect_matrix_single_param() {
        let comp = make_comp(
            "decode",
            vec![
                make_bench("mozjpeg/256x256", 275.0),
                make_bench("zune/256x256", 255.0),
            ],
        );
        // Only one parameter — not a matrix
        assert!(detect_matrix(&comp).is_none());
    }

    #[test]
    fn detect_matrix_single_variant() {
        let comp = make_comp(
            "decode",
            vec![
                make_bench("zenjpeg/256x256", 246.0),
                make_bench("zenjpeg/512x512", 1030.0),
            ],
        );
        // Only one variant — not a matrix
        assert!(detect_matrix(&comp).is_none());
    }

    #[test]
    fn render_matrix_produces_svg() {
        let comp = make_comp(
            "decode",
            vec![
                make_bench("mozjpeg/256", 275.0),
                make_bench("zenjpeg/256", 200.0),
                make_bench("mozjpeg/512", 1060.0),
                make_bench("zenjpeg/512", 900.0),
            ],
        );
        let svg = render_chart(&comp);
        assert!(svg.contains("<svg"), "should produce SVG");
        assert!(svg.contains("256"), "should contain param labels");
        assert!(svg.contains("512"), "should contain param labels");
        assert!(svg.contains("mozjpeg"), "should contain variant in legend");
        assert!(svg.contains("zenjpeg"), "should contain variant in legend");
        assert!(svg.contains(PALETTE[0]), "should use palette colors");
    }

    #[test]
    fn flat_chart_for_non_matrix() {
        let comp = make_comp(
            "sort",
            vec![
                make_bench("std_sort", 100.0),
                make_bench("unstable", 80.0),
                make_bench("parallel", 50.0),
            ],
        );
        let svg = render_chart(&comp);
        assert!(svg.contains("<svg"), "should produce SVG");
        assert!(svg.contains("std_sort"), "should contain bench names");
        // Should NOT have matrix structure (no param sections)
        assert!(!svg.contains("class=\"param\""), "should be flat chart");
    }
}