ixa 2.0.0-beta2.4

A framework for building agent-based models
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
<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <title>Ixa Bench History</title>
    <style>
      :root {
        color-scheme: light dark;
      }

      *,
      *::before,
      *::after {
        box-sizing: border-box;
      }

      body {
        margin: 0;
        font: 14px/1.45 system-ui, -apple-system, Segoe UI, Roboto, Ubuntu, Cantarell,
          Noto Sans, Helvetica, Arial, "Apple Color Emoji", "Segoe UI Emoji";
        background: Canvas;
        color: CanvasText;
      }

      header {
        padding: 16px 20px;
        border-bottom: 1px solid rgba(127, 127, 127, 0.25);
      }

      header h1 {
        margin: 0;
        font-size: 16px;
        font-weight: 650;
      }

      header .meta {
        margin-top: 6px;
        opacity: 0.85;
        font-size: 12px;
      }

      main {
        padding: 16px 20px 24px;
        max-width: 1200px;
        margin: 0 auto;
      }

      .row {
        display: flex;
        gap: 12px;
        flex-wrap: wrap;
        align-items: end;
        margin-bottom: 16px;
      }

      label {
        display: grid;
        gap: 6px;
        font-size: 12px;
        opacity: 0.9;
      }

      select,
      input[type="url"],
      input[type="text"],
      input[type="file"],
      button {
        font: inherit;
        padding: 8px 10px;
        border-radius: 8px;
        border: 1px solid rgba(127, 127, 127, 0.35);
        background: Field;
        color: FieldText;
        min-width: 260px;
      }

      input[type="file"] {
        padding: 6px 10px;
      }

      input[type="checkbox"] {
        width: 16px;
        height: 16px;
        margin: 0;
        accent-color: Highlight;
      }

      button {
        min-width: auto;
        cursor: pointer;
        background: ButtonFace;
        color: ButtonText;
      }

      select:focus-visible,
      input[type="url"]:focus-visible,
      input[type="text"]:focus-visible,
      input[type="file"]:focus-visible,
      button:focus-visible {
        outline: 2px solid Highlight;
        outline-offset: 2px;
      }

      button:hover {
        border-color: rgba(127, 127, 127, 0.55);
      }

      .checkbox {
        display: inline-flex;
        align-items: center;
        gap: 8px;
        padding: 8px 10px;
        border: 1px solid rgba(127, 127, 127, 0.35);
        border-radius: 8px;
        user-select: none;
        opacity: 0.9;
        background: Field;
        color: FieldText;
      }

      .checkbox input {
        transform: translateY(1px);
      }

      .grid {
        display: grid;
        grid-template-columns: 1fr;
        gap: 18px;
      }

      .card {
        border: 1px solid rgba(127, 127, 127, 0.25);
        border-radius: 12px;
        padding: 12px;
      }

      .card h2 {
        margin: 0 0 10px;
        font-size: 13px;
        opacity: 0.9;
        font-weight: 650;
      }

      .canvasWrap {
        position: relative;
        height: 420px;
      }

      .error {
        color: #b00020;
        white-space: pre-wrap;
        margin-top: 12px;
      }

      .hint {
        margin-top: 10px;
        opacity: 0.8;
        font-size: 12px;
      }

      @media (min-width: 1000px) {
        .grid {
          grid-template-columns: 1.5fr 1fr;
        }
      }
    </style>
  </head>

  <body>
    <header>
      <h1>Ixa benchmark history</h1>
      <div class="meta" id="meta">Loading…</div>
    </header>

    <main>
      <div class="row">
        <label style="flex: 1 1 520px; min-width: 320px">
          History URL
          <input id="historyUrl" type="url" spellcheck="false" />
        </label>

        <button id="loadUrl" type="button">Load URL</button>

        <label style="flex: 1 1 420px; min-width: 320px">
          Load from disk
          <input id="historyFile" type="file" accept="application/json,.json" />
        </label>

        <label>
          Suite
          <select id="suite">
            <option value="criterion">criterion</option>
            <option value="hyperfine">hyperfine</option>
          </select>
        </label>

        <label class="checkbox" title="Render a separate time-series chart for every benchmark in the selected suite">
          <input id="showAll" type="checkbox" />
          Show all benchmarks
        </label>

        <label>
          Benchmark
          <select id="benchmark"></select>
        </label>

        <label>
          Metric
          <select id="metric"></select>
        </label>

        <label class="checkbox" title="Show an uncertainty band when available">
          <input id="showBand" type="checkbox" checked />
          Show band
        </label>

        <label class="checkbox" title="Start the time-series y-axis at 0.0">
          <input id="yAxisZero" type="checkbox" checked />
          y-axis starts at 0.0
        </label>

        <label class="checkbox" title="Clip extreme high outliers using a conservative MAD rule">
          <input id="madClip" type="checkbox" checked />
          MAD clipping
        </label>

        <button id="reload" type="button">Reload</button>
      </div>

      <div class="grid">
        <section class="card">
          <h2>Time series</h2>
          <div class="canvasWrap" id="tsSingleWrap"><canvas id="ts"></canvas></div>
          <div id="tsAllWrap" hidden></div>
          <div class="hint" id="tsHint"></div>
        </section>

        <section class="card">
          <h2>Latest vs previous (percent change)</h2>
          <div class="row" style="margin-top: 6px; margin-bottom: 10px">
            <label>
              Baseline run
              <div style="display: flex; gap: 8px; align-items: center; flex-wrap: wrap">
                <button id="basePrev" type="button" title="Older"></button>
                <select id="baseRun" style="min-width: 260px; max-width: 520px"></select>
                <button id="baseNext" type="button" title="Newer"></button>
              </div>
            </label>

            <label>
              Compare run
              <div style="display: flex; gap: 8px; align-items: center; flex-wrap: wrap">
                <button id="cmpPrev" type="button" title="Older"></button>
                <select id="cmpRun" style="min-width: 260px; max-width: 520px"></select>
                <button id="cmpNext" type="button" title="Newer"></button>
              </div>
            </label>
          </div>
          <div class="canvasWrap" id="deltaWrap"><canvas id="delta"></canvas></div>
          <div class="hint" id="deltaHint"></div>
        </section>
      </div>

      <div class="error" id="error" hidden></div>
    </main>

    <script src="https://cdn.jsdelivr.net/npm/chart.js@4.4.1/dist/chart.umd.min.js"></script>
    <script>
      const DEFAULT_HISTORY_URL =
        "https://raw.githubusercontent.com/cdcgov/ixa/refs/heads/benchmarks-history/bench-history.json";

      /** @type {{ kind: 'url', url: string } | { kind: 'file', file: File }} */
      let currentSource = { kind: "url", url: DEFAULT_HISTORY_URL };

      /** @type {any} */
      let currentHistory = null;

      /**
       * Expected JSON shape (schema v1):
       * { schema: 1, runs: [ { run_at, branch, base, head, criterion: [], hyperfine: [] } ] }
       */

      /** @type {Chart | null} */
      let tsChart = null;
      /** @type {Chart[]} */
      let tsCharts = [];
      /** @type {Chart | null} */
      let deltaChart = null;

      const MAD_CLIP_MODIFIED_Z_THRESHOLD = 5.0;
      const MAD_CLIP_MIN_POINTS = 8;
      const MAD_CLIP_PADDING = 1.05;
      const MAD_CLIP_MIN_SEPARATION_RATIO = 2.0;

      const $ = (id) => document.getElementById(id);

      function setError(message) {
        const el = $("error");
        if (!message) {
          el.hidden = true;
          el.textContent = "";
          return;
        }
        el.hidden = false;
        el.textContent = message;
      }

      function formatDate(iso) {
        const d = new Date(iso);
        if (Number.isNaN(d.getTime())) return String(iso);
        return d.toISOString().slice(0, 19).replace("T", " ") + "Z";
      }

      function bestUnit(seconds) {
        const s = Math.abs(seconds);
        if (s === 0) return { scale: 1, unit: "s" };
        if (s >= 1) return { scale: 1, unit: "s" };
        if (s >= 1e-3) return { scale: 1e3, unit: "ms" };
        if (s >= 1e-6) return { scale: 1e6, unit: "µs" };
        return { scale: 1e9, unit: "ns" };
      }

      function fmtSeconds(seconds, forcedUnit) {
        const { scale, unit } = forcedUnit ?? bestUnit(seconds);
        const v = seconds * scale;
        const abs = Math.abs(v);
        const digits = abs >= 100 ? 0 : abs >= 10 ? 1 : 2;
        return `${v.toFixed(digits)} ${unit}`;
      }

      function safeSha(sha) {
        if (!sha) return "";
        return String(sha).slice(0, 7);
      }

      function median(sortedNumbers) {
        const n = sortedNumbers.length;
        if (!n) return null;
        const mid = Math.floor(n / 2);
        return n % 2
          ? sortedNumbers[mid]
          : (sortedNumbers[mid - 1] + sortedNumbers[mid]) / 2;
      }

      function madClipMaxSeconds(valuesSeconds) {
        const finiteValues = valuesSeconds.filter((v) => Number.isFinite(v));
        const positive = finiteValues
          .filter((v) => Number.isFinite(v) && v > 0)
          .map((v) => Math.log(v))
          .sort((a, b) => a - b);
        if (positive.length < MAD_CLIP_MIN_POINTS) return null;

        const med = median(positive);
        if (med == null) return null;

        const deviations = positive.map((v) => Math.abs(v - med)).sort((a, b) => a - b);
        const mad = median(deviations);
        if (!mad || mad <= 0) return null;

        const nonOutliers = finiteValues.filter((v) => {
          if (v <= 0) return true;
          const modifiedZ = (0.6745 * (Math.log(v) - med)) / mad;
          return modifiedZ <= MAD_CLIP_MODIFIED_Z_THRESHOLD;
        });
        if (!nonOutliers.length || nonOutliers.length === finiteValues.length) return null;

        const maxNonOutlier = Math.max(...nonOutliers);
        if (!Number.isFinite(maxNonOutlier) || maxNonOutlier <= 0) return null;
        const maxValue = Math.max(...finiteValues);
        if (maxValue / maxNonOutlier < MAD_CLIP_MIN_SEPARATION_RATIO) return null;
        return maxNonOutlier * MAD_CLIP_PADDING;
      }

      function destroyCharts() {
        if (tsChart) {
          tsChart.destroy();
          tsChart = null;
        }
        for (const c of tsCharts) c.destroy();
        tsCharts = [];
        if (deltaChart) {
          deltaChart.destroy();
          deltaChart = null;
        }
      }

      function getSuiteRuns(history, suite) {
        const runs = Array.isArray(history?.runs) ? history.runs.slice() : [];
        runs.sort((a, b) => new Date(a.run_at) - new Date(b.run_at));
        return runs.map((r) => ({
          run_at: r.run_at,
          branch: r.branch,
          pr_number: r.pr_number,
          base: r.base,
          head: r.head,
          entries: Array.isArray(r?.[suite]) ? r[suite] : [],
        }));
      }

      function indexByName(entries) {
        /** @type {Map<string, any>} */
        const map = new Map();
        for (const e of entries) {
          if (e && typeof e.name === "string") map.set(e.name, e);
        }
        return map;
      }

      function listBenchmarks(suiteRuns) {
        const set = new Set();
        for (const r of suiteRuns) {
          for (const e of r.entries) {
            if (e && typeof e.name === "string") set.add(e.name);
          }
        }
        return [...set].sort((a, b) => a.localeCompare(b));
      }

      function listMetricsForSuite(suite) {
        if (suite === "criterion") {
          return [
            { value: "estimate", label: "estimate" },
            { value: "lower", label: "lower" },
            { value: "upper", label: "upper" },
          ];
        }
        return [
          { value: "mean_sec", label: "mean" },
          { value: "min_sec", label: "min" },
          { value: "max_sec", label: "max" },
          { value: "stddev_sec", label: "stddev" },
        ];
      }

      function readValueSeconds(suite, entry, metric) {
        if (!entry) return null;

        if (suite === "criterion") {
          const arr = entry.time_sec;
          if (!Array.isArray(arr) || arr.length < 3) return null;
          const lower = Number(arr[0]);
          const est = Number(arr[1]);
          const upper = Number(arr[2]);
          if ([lower, est, upper].some((x) => !Number.isFinite(x))) return null;

          if (metric === "lower") return lower;
          if (metric === "upper") return upper;
          return est;
        }

        const v = Number(entry[metric]);
        if (!Number.isFinite(v)) return null;
        return v;
      }

      function readBandSeconds(suite, entry) {
        if (!entry) return null;
        if (suite === "criterion") {
          const arr = entry.time_sec;
          if (!Array.isArray(arr) || arr.length < 3) return null;
          const lower = Number(arr[0]);
          const upper = Number(arr[2]);
          if (!Number.isFinite(lower) || !Number.isFinite(upper)) return null;
          return { lower, upper };
        }

        const mean = Number(entry.mean_sec);
        const sd = Number(entry.stddev_sec);
        if (!Number.isFinite(mean) || !Number.isFinite(sd)) return null;
        const lower = Math.max(0, mean - sd);
        const upper = mean + sd;
        return { lower, upper };
      }

      function makeLabels(suiteRuns) {
        return suiteRuns.map((r) => {
          const sha = safeSha(r?.head?.sha);
          const pr = r.pr_number ? `PR #${r.pr_number}` : r.branch ? r.branch : "";
          const left = formatDate(r.run_at);
          const right = [sha, pr].filter(Boolean).join(" · ");
          return right ? `${left}  ${right}` : left;
        });
      }

      function pickDefaultBenchmark(benchmarks) {
        if (!benchmarks.length) return "";
        // Prefer a common-looking benchmark when available
        const preferredPrefix = ["large_sir::ixa", "large_sir::baseline", "counts/"];
        for (const p of preferredPrefix) {
          const found = benchmarks.find((b) => b.startsWith(p));
          if (found) return found;
        }
        return benchmarks[0];
      }

      function renderSingleTimeSeriesChart({
        canvas,
        labels,
        suiteRuns,
        suite,
        benchmarkName,
        metric,
        showBand,
        yAxisZero,
        madClip,
      }) {
        const allPoints = [];
        const lowerBand = [];
        const upperBand = [];

        for (const r of suiteRuns) {
          const byName = indexByName(r.entries);
          const entry = byName.get(benchmarkName);
          allPoints.push(readValueSeconds(suite, entry, metric));
          const band = showBand ? readBandSeconds(suite, entry) : null;
          lowerBand.push(band ? band.lower : null);
          upperBand.push(band ? band.upper : null);
        }

        const vals = allPoints.filter((x) => Number.isFinite(x));
        const unit = vals.length
          ? bestUnit(vals.slice().sort((a, b) => a - b)[Math.floor(vals.length / 2)])
          : { scale: 1, unit: "s" };

        const datasets = [];
        if (showBand) {
          datasets.push({
            label: "lower",
            data: lowerBand.map((s) => (s == null ? null : s * unit.scale)),
            borderColor: "rgba(0,0,0,0)",
            pointRadius: 0,
            borderWidth: 0,
            tension: 0.15,
            spanGaps: true,
          });
          datasets.push({
            label: "upper",
            data: upperBand.map((s) => (s == null ? null : s * unit.scale)),
            borderColor: "rgba(0,0,0,0)",
            pointRadius: 0,
            borderWidth: 0,
            backgroundColor: "rgba(80, 120, 255, 0.15)",
            fill: "-1",
            tension: 0.15,
            spanGaps: true,
          });
        }

        datasets.push({
          label: `${benchmarkName} (${metric})`,
          data: allPoints.map((s) => (s == null ? null : s * unit.scale)),
          borderColor: "rgba(80, 120, 255, 0.95)",
          backgroundColor: "rgba(80, 120, 255, 0.25)",
          pointRadius: 2,
          borderWidth: 2,
          tension: 0.15,
          spanGaps: true,
        });

        const clipMaxSeconds = madClip
          ? madClipMaxSeconds([...allPoints, ...lowerBand, ...upperBand])
          : null;
        const clipMax = clipMaxSeconds == null ? undefined : clipMaxSeconds * unit.scale;

        return new Chart(canvas, {
          type: "line",
          data: { labels, datasets },
          options: {
            responsive: true,
            maintainAspectRatio: false,
            interaction: { mode: "index", intersect: false },
            plugins: {
              legend: { display: false },
              tooltip: {
                callbacks: {
                  label: (item) => {
                    const scaled = item.parsed.y;
                    if (scaled == null) return "(missing)";
                    return `${item.dataset.label}: ${fmtSeconds(
                      scaled / unit.scale,
                      unit
                    )}`;
                  },
                },
              },
            },
            scales: {
              y: {
                min: yAxisZero ? 0 : undefined,
                max: clipMax,
                title: { display: true, text: `Time (${unit.unit})` },
                ticks: { callback: (v) => `${v} ${unit.unit}` },
              },
              x: {
                ticks: {
                  maxRotation: 0,
                  autoSkip: true,
                  maxTicksLimit: 6,
                },
              },
            },
          },
        });
      }

      function renderTimeSeries({
        suiteRuns,
        suite,
        benchmarkName,
        metric,
        showBand,
        yAxisZero,
        madClip,
      }) {
        if (typeof Chart === "undefined") {
          throw new Error(
            "Chart.js failed to load (Chart is undefined). Check network access or CDN availability."
          );
        }

        // Reset previous time-series chart(s)
        if (tsChart) {
          tsChart.destroy();
          tsChart = null;
        }
        for (const c of tsCharts) c.destroy();
        tsCharts = [];

        const showAll = $("showAll").checked;
        const labels = makeLabels(suiteRuns);

        $("tsSingleWrap").hidden = showAll;
        $("tsAllWrap").hidden = !showAll;

        if (!showAll) {
          tsChart = renderSingleTimeSeriesChart({
            canvas: $("ts"),
            labels,
            suiteRuns,
            suite,
            benchmarkName,
            metric,
            showBand,
            yAxisZero,
            madClip,
          });
        } else {
          const allWrap = $("tsAllWrap");

          const benchmarks = listBenchmarks(suiteRuns);
          const grid = document.createElement("div");
          grid.style.display = "grid";
          grid.style.gridTemplateColumns = "1fr";
          grid.style.gap = "12px";
          allWrap.replaceChildren(grid);

          for (const name of benchmarks) {
            const block = document.createElement("div");
            block.style.border = "1px solid rgba(127, 127, 127, 0.25)";
            block.style.borderRadius = "12px";
            block.style.padding = "10px";

            const title = document.createElement("div");
            title.textContent = name;
            title.style.fontSize = "12px";
            title.style.opacity = "0.9";
            title.style.fontWeight = "650";
            title.style.marginBottom = "8px";
            block.appendChild(title);

            const wrap = document.createElement("div");
            wrap.style.position = "relative";
            wrap.style.height = "220px";
            block.appendChild(wrap);

            const canvas = document.createElement("canvas");
            wrap.appendChild(canvas);
            grid.appendChild(block);

            tsCharts.push(
              renderSingleTimeSeriesChart({
                canvas,
                labels,
                suiteRuns,
                suite,
                benchmarkName: name,
                metric,
                showBand,
                yAxisZero,
                madClip,
              })
            );
          }
        }

        const lastIdx = suiteRuns.length - 1;
        const last = lastIdx >= 0 ? suiteRuns[lastIdx] : null;
        const hintParts = [];
        if (suiteRuns.length) hintParts.push(`${suiteRuns.length} run(s)`);
        if (last?.head?.sha) hintParts.push(`latest head: ${safeSha(last.head.sha)}`);
        if (showAll) hintParts.push("showing all benchmarks");
        $("tsHint").textContent = hintParts.join(" · ");
      }

      function renderLatestDelta({ suiteRuns, suite, metric, baseIdx, cmpIdx }) {
        if (typeof Chart === "undefined") {
          throw new Error(
            "Chart.js failed to load (Chart is undefined). Check network access or CDN availability."
          );
        }

        if (suiteRuns.length < 2) {
          if (deltaChart) {
            deltaChart.destroy();
            deltaChart = null;
          }
          $("deltaHint").textContent = "Need at least 2 runs.";
          return;
        }

        const hi = suiteRuns.length - 1;
        const bi = clampInt(baseIdx, 0, hi);
        const ci = clampInt(cmpIdx, 0, hi);

        if (bi === ci) {
          if (deltaChart) {
            deltaChart.destroy();
            deltaChart = null;
          }
          $("deltaHint").textContent = "Pick two different runs to compare.";
          return;
        }

        const prev = suiteRuns[bi];
        const next = suiteRuns[ci];
        const prevMap = indexByName(prev.entries);
        const nextMap = indexByName(next.entries);

        /** @type {{name: string, pct: number}[]} */
        const changes = [];
        for (const [name, nextEntry] of nextMap.entries()) {
          const prevEntry = prevMap.get(name);
          const a = readValueSeconds(suite, prevEntry, metric);
          const b = readValueSeconds(suite, nextEntry, metric);
          if (a == null || b == null) continue;
          if (a === 0) continue;
          const pct = ((b - a) / a) * 100;
          if (!Number.isFinite(pct)) continue;
          changes.push({ name, pct });
        }

        changes.sort((x, y) => y.pct - x.pct);
        const MAX = 25;
        const trimmed = changes.slice(0, MAX);

        // Make the chart height scale with bar count (helps readability)
        const deltaWrap = $("deltaWrap");
        const desired = 90 + trimmed.length * 18;
        deltaWrap.style.height = `${Math.max(320, Math.min(900, desired))}px`;

        const labels = trimmed.map((c) => c.name);
        const data = trimmed.map((c) => c.pct);

        const ctx = $("delta");
        if (deltaChart) deltaChart.destroy();
        deltaChart = new Chart(ctx, {
          type: "bar",
          data: {
            labels,
            datasets: [
              {
                label: "% change (y vs x)",
                data,
                backgroundColor: data.map((v) =>
                  v >= 0 ? "rgba(220, 70, 70, 0.65)" : "rgba(70, 180, 120, 0.65)"
                ),
                borderColor: data.map((v) =>
                  v >= 0 ? "rgba(220, 70, 70, 0.95)" : "rgba(70, 180, 120, 0.95)"
                ),
                borderWidth: 1,
              },
            ],
          },
          options: {
            responsive: true,
            maintainAspectRatio: false,
            indexAxis: "y",
            plugins: {
              legend: { display: false },
              tooltip: {
                callbacks: {
                  title: (items) => {
                    const i = items?.[0];
                    return i ? String(i.label) : "";
                  },
                  label: (item) => {
                    const v = item.parsed.x;
                    return `${Number(v).toFixed(2)}%`;
                  },
                },
              },
            },
            scales: {
              x: {
                title: { display: true, text: "% change (time; lower is better)" },
                ticks: { callback: (v) => `${v}%` },
              },
              y: {
                ticks: {
                  autoSkip: false,
                  callback: (_v, idx) => {
                    const s = labels[idx] ?? "";
                    const max = 42;
                    return s.length > max ? s.slice(0, max - 1) + "" : s;
                  },
                },
              },
            },
          },
        });

        const prevSha = safeSha(prev?.head?.sha);
        const nextSha = safeSha(next?.head?.sha);
        $("deltaHint").textContent =
          `Comparing ${formatDate(prev.run_at)} (${prevSha})  ${formatDate(next.run_at)} (${nextSha}). ` +
          `Showing up to ${Math.min(MAX, changes.length)} benchmarks (full names in tooltip).`;
      }

	      function populateSelect(selectEl, options, value) {
	        selectEl.replaceChildren();
	        for (const opt of options) {
	          const o = document.createElement("option");
	          o.value = opt.value ?? opt;
	          o.textContent = opt.label ?? opt;
	          selectEl.appendChild(o);
	        }
	        if (value) selectEl.value = value;
	      }

	      function clampInt(n, lo, hi) {
	        const x = Number(n);
	        if (!Number.isFinite(x)) return lo;
	        return Math.min(hi, Math.max(lo, Math.trunc(x)));
	      }

	      function parseHistoryUrl(rawUrl) {
	        const parsed = new URL(String(rawUrl), window.location.href);
	        if (!["http:", "https:"].includes(parsed.protocol)) {
	          throw new Error(`Only http(s) URLs are allowed: ${parsed.protocol}`);
	        }
	        if (parsed.username || parsed.password) {
	          throw new Error("Credentials in URLs are not allowed");
	        }
	        return parsed;
	      }

	      function safeHistoryHref(rawUrl) {
	        try {
	          return parseHistoryUrl(rawUrl).href;
	        } catch {
	          return null;
	        }
	      }

	      async function fetchHistoryFromUrl(url) {
	        const safeUrl = parseHistoryUrl(url).href;
	        const res = await fetch(safeUrl, {
	          cache: "no-store",
	          headers: { Accept: "application/json" },
	        });
	        if (!res.ok) {
	          throw new Error(`Fetch failed: ${res.status} ${res.statusText}`);
	        }
	        return await res.json();
	      }

      async function fetchHistoryFromFile(file) {
        const text = await file.text();
        try {
          return JSON.parse(text);
        } catch (e) {
          throw new Error(
            `Failed to parse JSON from file: ${file?.name ?? "(unknown)"}\n\n${String(e?.message ?? e)}`
          );
        }
      }

      async function fetchHistory(source) {
        if (source?.kind === "file") return await fetchHistoryFromFile(source.file);
        const url = source?.url ?? DEFAULT_HISTORY_URL;
        return await fetchHistoryFromUrl(url);
      }

      function updateMeta(history, source) {
        const runs = Array.isArray(history?.runs) ? history.runs : [];
        const last =
          runs.length
            ? runs
                .slice()
                .sort((a, b) => new Date(a.run_at) - new Date(b.run_at))[runs.length - 1]
            : null;
        const lastTxt = last?.run_at ? formatDate(last.run_at) : "(unknown)";
        if (source?.kind === "file") {
          $("meta").textContent =
            `Source: ${source.file?.name ?? "(local file)"} · Runs: ${runs.length} · Latest: ${lastTxt}`;
          return;
        }

	        const url = source?.url ?? DEFAULT_HISTORY_URL;
	        const metaEl = $("meta");
	        metaEl.replaceChildren();
	        metaEl.append("Source: ");

	        const linkEl = document.createElement("a");
	        linkEl.textContent = url;
	        linkEl.href = safeHistoryHref(url) ?? "#";
	        linkEl.rel = "noreferrer noopener";
	        linkEl.target = "_blank";
	        metaEl.append(linkEl);

        metaEl.append(` · Runs: ${runs.length} · Latest: ${lastTxt}`);
      }

      function initUiOnce() {
        if (initUiOnce._done) return;
        initUiOnce._done = true;

        const suiteEl = $("suite");
        const showAllEl = $("showAll");
        const benchEl = $("benchmark");
        const metricEl = $("metric");
        const showBandEl = $("showBand");
        const yAxisZeroEl = $("yAxisZero");
        const madClipEl = $("madClip");

        const baseRunEl = $("baseRun");
        const cmpRunEl = $("cmpRun");
        const basePrevEl = $("basePrev");
        const baseNextEl = $("baseNext");
        const cmpPrevEl = $("cmpPrev");
        const cmpNextEl = $("cmpNext");

        function rerender() {
          if (!currentHistory) return;
          const suite = suiteEl.value;
          const suiteRuns = getSuiteRuns(currentHistory, suite);
          const bench = benchEl.value;
          const metric = metricEl.value;
          const showBand = showBandEl.checked;
          const yAxisZero = yAxisZeroEl.checked;
          const madClip = madClipEl.checked;

          benchEl.disabled = showAllEl.checked;

          renderTimeSeries({
            suiteRuns,
            suite,
            benchmarkName: bench,
            metric,
            showBand,
            yAxisZero,
            madClip,
          });
          renderLatestDelta({
            suiteRuns,
            suite,
            metric,
            baseIdx: baseRunEl.value,
            cmpIdx: cmpRunEl.value,
          });
        }

        function onSuiteChange() {
          if (!currentHistory) return;
          const suite = suiteEl.value;
          const suiteRuns = getSuiteRuns(currentHistory, suite);
          const benchmarks = listBenchmarks(suiteRuns);
          const defaultBench = pickDefaultBenchmark(benchmarks);

          const prevBench = benchEl.value;
          const nextBench = benchmarks.includes(prevBench) ? prevBench : defaultBench;
          populateSelect(
            benchEl,
            benchmarks.map((b) => ({ value: b, label: b })),
            nextBench
          );

          const metrics = listMetricsForSuite(suite);
          const defaultMetric = suite === "criterion" ? "estimate" : "mean_sec";
          const prevMetric = metricEl.value;
          const nextMetric = metrics.includes(prevMetric) ? prevMetric : defaultMetric;
          populateSelect(metricEl, metrics, nextMetric);

          // Delta chart run pickers
          const runLabels = makeLabels(suiteRuns);
          const runOptions = runLabels.map((label, idx) => ({
            value: String(idx),
            label,
          }));

          const n = suiteRuns.length;
          const defaultBase = String(Math.max(0, n - 2));
          const defaultCmp = String(Math.max(0, n - 1));

          const prevBase = baseRunEl.value;
          const prevCmp = cmpRunEl.value;
          const baseVal = prevBase !== "" && Number(prevBase) < n ? prevBase : defaultBase;
          const cmpVal = prevCmp !== "" && Number(prevCmp) < n ? prevCmp : defaultCmp;

          populateSelect(baseRunEl, runOptions, baseVal);
          populateSelect(cmpRunEl, runOptions, cmpVal);

          rerender();
        }

        function stepRun(selectEl, delta) {
          if (!currentHistory) return;
          const suite = suiteEl.value;
          const suiteRuns = getSuiteRuns(currentHistory, suite);
          const hi = suiteRuns.length - 1;
          if (hi < 0) return;

          const otherEl = selectEl === baseRunEl ? cmpRunEl : baseRunEl;
          let idx = clampInt(selectEl.value, 0, hi) + delta;
          idx = clampInt(idx, 0, hi);

          const other = clampInt(otherEl.value, 0, hi);
          if (idx === other) {
            idx = clampInt(idx + (delta >= 0 ? 1 : -1), 0, hi);
            if (idx === other) return;
          }

          selectEl.value = String(idx);
          rerender();
        }

        suiteEl.addEventListener("change", onSuiteChange);
        showAllEl.addEventListener("change", rerender);
        benchEl.addEventListener("change", rerender);
        metricEl.addEventListener("change", rerender);
        showBandEl.addEventListener("change", rerender);
        yAxisZeroEl.addEventListener("change", rerender);
        madClipEl.addEventListener("change", rerender);

        baseRunEl.addEventListener("change", rerender);
        cmpRunEl.addEventListener("change", rerender);
        basePrevEl.addEventListener("click", () => stepRun(baseRunEl, -1));
        baseNextEl.addEventListener("click", () => stepRun(baseRunEl, +1));
        cmpPrevEl.addEventListener("click", () => stepRun(cmpRunEl, -1));
        cmpNextEl.addEventListener("click", () => stepRun(cmpRunEl, +1));

        // Expose for loaders
        initUiOnce._onSuiteChange = onSuiteChange;
      }

      async function loadAndRender(source) {
        setError("");
        destroyCharts();
        $("meta").textContent = "Loading…";

        const history = await fetchHistory(source);
        currentHistory = history;
        currentSource = source?.kind ? source : { kind: "url", url: DEFAULT_HISTORY_URL };
        updateMeta(history, currentSource);

        initUiOnce();
        initUiOnce._onSuiteChange?.();
      }

      // UI wiring for sources
      $("historyUrl").value = DEFAULT_HISTORY_URL;

	      $("loadUrl").addEventListener("click", async () => {
	        try {
	          const urlRaw = String($("historyUrl").value ?? "").trim() || DEFAULT_HISTORY_URL;
	          const url = parseHistoryUrl(urlRaw).href;
	          await loadAndRender({ kind: "url", url });
	        } catch (e) {
	          setError(String(e?.stack ?? e));
	        }
	      });

      $("historyUrl").addEventListener("keydown", async (ev) => {
        if (ev.key !== "Enter") return;
        ev.preventDefault();
        $("loadUrl").click();
      });

      $("historyFile").addEventListener("change", async (ev) => {
        try {
          const file = ev?.target?.files?.[0];
          if (!file) return;
          await loadAndRender({ kind: "file", file });
        } catch (e) {
          setError(String(e?.stack ?? e));
        }
      });

      $("reload").addEventListener("click", async () => {
        try {
          await loadAndRender(currentSource);
        } catch (e) {
          setError(String(e?.stack ?? e));
        }
      });

      loadAndRender(currentSource).catch((e) => {
        setError(
          `Failed to load or render bench history.\n\n${String(e?.stack ?? e)}\n\n` +
            `If this is opened from a file:// URL, your browser may block some requests depending on settings. ` +
            `Try a different browser, or run a local server like: python3 -m http.server -d scripts 8000`
        );
      });
    </script>
  </body>
</html>