goose 0.19.0

A load testing framework inspired by Locust.
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
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
5446
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481
5482
5483
5484
5485
5486
5487
5488
5489
5490
5491
5492
5493
5494
5495
5496
5497
5498
5499
5500
5501
5502
5503
5504
5505
5506
5507
5508
5509
5510
5511
5512
5513
5514
5515
5516
5517
5518
5519
5520
5521
5522
5523
5524
5525
5526
5527
5528
5529
5530
5531
5532
5533
5534
5535
5536
5537
5538
5539
5540
5541
5542
5543
5544
5545
5546
5547
5548
5549
5550
5551
5552
5553
5554
5555
5556
5557
5558
5559
5560
5561
5562
5563
5564
5565
5566
5567
5568
5569
5570
5571
5572
5573
5574
5575
5576
5577
5578
5579
5580
5581
5582
5583
5584
5585
5586
5587
5588
5589
5590
5591
5592
5593
5594
5595
5596
5597
5598
5599
5600
5601
5602
5603
5604
5605
5606
5607
5608
5609
5610
5611
5612
5613
5614
5615
5616
5617
5618
5619
5620
5621
5622
5623
5624
5625
5626
5627
5628
5629
5630
5631
5632
5633
5634
5635
5636
5637
5638
5639
5640
5641
5642
5643
5644
5645
5646
5647
5648
5649
5650
5651
5652
5653
5654
5655
5656
5657
5658
5659
5660
5661
5662
5663
5664
5665
5666
5667
5668
5669
5670
5671
5672
5673
5674
5675
5676
5677
5678
5679
5680
5681
5682
5683
5684
5685
5686
5687
5688
5689
5690
5691
5692
5693
5694
5695
5696
5697
5698
5699
5700
5701
5702
5703
5704
5705
5706
5707
5708
5709
5710
5711
5712
5713
5714
5715
5716
5717
5718
5719
5720
5721
5722
5723
5724
5725
5726
5727
5728
5729
5730
5731
5732
5733
5734
5735
5736
5737
5738
5739
5740
5741
5742
5743
5744
5745
5746
5747
5748
5749
5750
5751
5752
5753
5754
5755
5756
5757
5758
5759
5760
5761
5762
5763
5764
5765
5766
5767
5768
5769
5770
5771
5772
5773
5774
5775
5776
5777
5778
5779
5780
5781
5782
5783
5784
5785
5786
5787
5788
5789
5790
5791
5792
5793
5794
5795
5796
5797
5798
5799
5800
5801
5802
5803
5804
5805
5806
5807
5808
5809
5810
5811
5812
5813
5814
5815
5816
5817
5818
5819
5820
5821
5822
5823
5824
5825
5826
5827
5828
5829
5830
5831
5832
5833
5834
5835
5836
5837
5838
5839
5840
5841
5842
5843
5844
5845
5846
5847
5848
5849
5850
5851
5852
5853
5854
5855
5856
5857
5858
5859
5860
5861
5862
5863
5864
5865
5866
5867
5868
5869
5870
5871
5872
5873
5874
5875
5876
5877
5878
5879
5880
5881
5882
5883
5884
5885
5886
5887
5888
5889
5890
5891
5892
5893
5894
5895
5896
5897
5898
5899
5900
5901
5902
5903
5904
5905
5906
5907
5908
5909
5910
5911
5912
5913
5914
5915
5916
5917
5918
5919
5920
5921
5922
5923
5924
5925
5926
5927
5928
5929
5930
5931
5932
5933
5934
5935
5936
5937
5938
5939
5940
5941
5942
5943
5944
5945
5946
5947
5948
5949
5950
5951
5952
5953
5954
5955
5956
5957
5958
5959
5960
5961
5962
5963
5964
5965
5966
5967
5968
5969
5970
5971
5972
5973
5974
5975
5976
5977
5978
5979
5980
5981
5982
5983
5984
5985
5986
5987
5988
5989
5990
5991
5992
5993
5994
5995
5996
5997
5998
5999
6000
6001
6002
6003
6004
6005
6006
6007
6008
6009
6010
6011
6012
6013
6014
6015
6016
6017
6018
6019
6020
6021
6022
6023
6024
6025
6026
6027
6028
6029
6030
6031
6032
6033
6034
6035
6036
6037
6038
6039
6040
6041
6042
6043
6044
6045
6046
6047
6048
6049
6050
6051
6052
6053
6054
6055
6056
6057
6058
6059
6060
6061
6062
6063
6064
6065
6066
6067
6068
6069
6070
6071
6072
6073
6074
6075
6076
6077
6078
6079
6080
6081
6082
6083
6084
6085
6086
6087
6088
6089
6090
6091
6092
6093
6094
6095
6096
6097
6098
6099
6100
6101
6102
6103
6104
6105
6106
6107
6108
6109
6110
6111
6112
6113
6114
6115
6116
6117
6118
6119
6120
6121
6122
6123
6124
6125
6126
6127
6128
6129
6130
6131
6132
6133
6134
6135
6136
6137
6138
6139
6140
6141
6142
6143
6144
6145
6146
6147
6148
6149
6150
6151
6152
6153
6154
6155
6156
6157
6158
6159
6160
6161
6162
6163
6164
6165
6166
6167
6168
6169
6170
6171
6172
6173
6174
6175
6176
6177
6178
6179
6180
6181
6182
6183
6184
6185
6186
6187
6188
6189
6190
6191
6192
6193
6194
6195
6196
6197
6198
6199
6200
6201
6202
6203
6204
6205
6206
6207
6208
6209
6210
6211
6212
6213
6214
6215
6216
6217
6218
6219
6220
6221
6222
6223
6224
6225
6226
6227
6228
6229
6230
6231
6232
6233
6234
6235
6236
6237
6238
6239
6240
6241
6242
6243
6244
6245
6246
6247
6248
6249
6250
6251
6252
6253
6254
6255
6256
6257
6258
6259
6260
6261
6262
6263
6264
6265
6266
6267
6268
6269
6270
6271
6272
6273
6274
6275
6276
6277
6278
6279
6280
6281
6282
6283
6284
6285
6286
6287
6288
6289
6290
6291
6292
6293
6294
6295
6296
6297
6298
6299
6300
6301
6302
6303
6304
6305
6306
6307
6308
6309
6310
6311
6312
6313
6314
6315
6316
6317
6318
6319
6320
6321
6322
6323
6324
6325
6326
6327
6328
6329
6330
6331
6332
6333
6334
6335
6336
6337
6338
6339
6340
6341
6342
6343
6344
6345
6346
6347
6348
6349
6350
6351
6352
6353
6354
6355
6356
6357
6358
6359
6360
6361
6362
6363
6364
6365
6366
6367
6368
6369
6370
6371
6372
6373
6374
6375
6376
6377
6378
6379
6380
6381
6382
6383
6384
6385
6386
6387
6388
6389
6390
6391
6392
6393
6394
6395
6396
6397
6398
6399
6400
6401
6402
6403
6404
6405
6406
6407
6408
6409
6410
6411
6412
6413
6414
6415
6416
6417
6418
6419
6420
6421
6422
6423
6424
6425
6426
6427
6428
6429
6430
6431
6432
6433
6434
6435
6436
6437
6438
6439
6440
6441
6442
6443
6444
6445
6446
6447
6448
6449
6450
6451
6452
6453
6454
6455
6456
6457
6458
6459
6460
6461
6462
6463
6464
6465
6466
6467
6468
6469
6470
6471
6472
6473
6474
6475
6476
6477
6478
6479
6480
6481
6482
6483
6484
6485
6486
6487
6488
6489
6490
6491
6492
6493
6494
6495
6496
6497
6498
6499
6500
6501
6502
6503
6504
6505
6506
6507
6508
6509
6510
6511
6512
6513
6514
6515
6516
6517
6518
6519
6520
6521
6522
6523
6524
6525
6526
6527
6528
6529
6530
6531
6532
6533
6534
6535
6536
6537
6538
6539
6540
6541
6542
6543
6544
6545
6546
6547
6548
6549
6550
6551
6552
6553
6554
6555
6556
6557
6558
6559
6560
6561
6562
6563
6564
6565
6566
6567
6568
6569
6570
6571
6572
6573
6574
6575
6576
6577
6578
6579
6580
6581
6582
6583
6584
6585
6586
6587
6588
6589
6590
6591
6592
6593
6594
6595
6596
6597
6598
6599
6600
6601
6602
6603
6604
6605
6606
6607
6608
6609
6610
6611
6612
6613
6614
6615
6616
6617
6618
6619
6620
6621
6622
6623
6624
6625
6626
6627
6628
6629
6630
6631
6632
6633
6634
6635
6636
6637
6638
6639
6640
6641
6642
6643
6644
6645
6646
6647
6648
6649
6650
6651
6652
6653
6654
6655
6656
6657
6658
6659
6660
6661
6662
6663
6664
6665
6666
6667
6668
6669
6670
6671
6672
6673
6674
6675
6676
6677
6678
6679
6680
6681
6682
6683
6684
6685
6686
6687
6688
6689
6690
6691
6692
6693
6694
6695
6696
6697
6698
6699
6700
6701
6702
6703
6704
6705
6706
6707
6708
6709
6710
6711
6712
6713
6714
6715
6716
6717
6718
6719
6720
6721
6722
6723
6724
6725
6726
6727
6728
6729
6730
6731
6732
6733
6734
6735
6736
6737
6738
6739
6740
6741
6742
6743
6744
6745
6746
6747
6748
6749
6750
6751
6752
6753
6754
6755
6756
6757
6758
6759
6760
6761
6762
6763
6764
6765
6766
6767
6768
6769
6770
6771
6772
6773
6774
6775
6776
6777
6778
6779
6780
6781
6782
6783
6784
6785
6786
6787
6788
6789
6790
6791
6792
6793
6794
6795
6796
6797
6798
6799
6800
6801
6802
6803
6804
6805
6806
6807
6808
6809
6810
6811
6812
6813
6814
6815
6816
6817
6818
6819
6820
6821
6822
6823
6824
6825
6826
6827
6828
6829
6830
6831
6832
6833
6834
6835
6836
6837
6838
6839
6840
6841
6842
6843
6844
6845
6846
6847
6848
6849
6850
6851
6852
6853
6854
6855
6856
6857
6858
6859
6860
6861
6862
6863
6864
6865
6866
6867
6868
6869
6870
6871
6872
6873
6874
6875
6876
6877
6878
6879
6880
6881
6882
6883
6884
6885
6886
6887
6888
6889
6890
6891
6892
6893
6894
6895
6896
6897
6898
6899
6900
6901
6902
6903
6904
6905
6906
6907
6908
6909
6910
6911
6912
6913
6914
6915
6916
6917
6918
6919
6920
6921
6922
6923
6924
6925
6926
6927
6928
6929
6930
6931
6932
6933
6934
6935
6936
6937
6938
6939
6940
6941
6942
6943
6944
6945
6946
6947
6948
6949
6950
6951
6952
6953
6954
6955
6956
6957
6958
6959
6960
6961
6962
6963
6964
6965
6966
6967
6968
6969
6970
6971
6972
6973
6974
6975
6976
6977
6978
6979
6980
6981
6982
6983
6984
6985
6986
6987
6988
6989
6990
6991
6992
6993
6994
6995
6996
6997
6998
6999
7000
7001
7002
7003
7004
7005
7006
7007
7008
7009
7010
7011
7012
7013
7014
7015
7016
7017
7018
7019
7020
7021
7022
7023
7024
7025
7026
7027
7028
7029
7030
7031
7032
7033
7034
7035
7036
7037
7038
7039
7040
7041
7042
7043
7044
7045
7046
7047
7048
7049
7050
7051
7052
7053
7054
7055
7056
7057
7058
7059
7060
7061
7062
7063
7064
7065
7066
7067
7068
7069
7070
7071
7072
7073
7074
7075
7076
7077
7078
7079
7080
7081
7082
7083
7084
7085
7086
7087
7088
7089
7090
7091
7092
7093
7094
7095
7096
7097
7098
7099
7100
7101
7102
7103
7104
7105
7106
7107
7108
7109
7110
7111
7112
7113
7114
7115
7116
7117
7118
7119
7120
7121
7122
7123
7124
7125
7126
7127
7128
7129
7130
7131
7132
7133
7134
7135
7136
7137
7138
7139
7140
7141
7142
7143
7144
7145
7146
7147
7148
7149
7150
7151
7152
7153
7154
7155
7156
7157
7158
7159
7160
7161
7162
7163
7164
7165
7166
7167
7168
7169
7170
7171
7172
7173
7174
7175
7176
7177
7178
7179
7180
7181
7182
7183
7184
7185
7186
7187
7188
7189
7190
7191
7192
7193
7194
7195
7196
7197
7198
7199
7200
7201
7202
7203
7204
7205
7206
7207
7208
7209
7210
7211
7212
7213
7214
7215
7216
7217
7218
7219
7220
7221
7222
7223
7224
7225
7226
7227
7228
7229
7230
7231
7232
7233
7234
7235
7236
7237
//! Optional graph data collected during the load tests.
//!
//! If the HTML report is enabled the graph data will be collected and stored in
//! the [`GraphData`] structure during the load test. When the report is written
//! this data is converted into [`Graph`] structures and HTML markup is generated
//! based on them.

use crate::metrics::dashboard_snapshot::{
    Heatmap, HeatmapColumn, HeatmapCursor, RequestSeries, SeriesWindow, SERIES_WINDOW_SECS,
};
use crate::metrics::{buckets, GooseRequestMetricTimingData};
use crate::test_plan::{TestPlanHistory, TestPlanStepAction};
use chrono::prelude::*;
use itertools::Itertools;
use serde::{Deserialize, Serialize};
use serde_json::json;
use std::cmp::Ordering;
use std::collections::{BTreeMap, HashMap, HashSet, VecDeque};
use std::convert::TryFrom;
use std::fmt::Write;
use std::marker::PhantomData;
use std::sync::atomic::{AtomicU64, Ordering as AtomicOrdering};
use std::sync::OnceLock;

/// One key's counts by second, and for requests, which latency ring holds the
/// request's own response times. Laid out in order, so a request's pending
/// response times follow the fields a batch merge reads first.
#[derive(Clone)]
#[repr(C)]
struct KeySeries {
    counts: TimeSeries<u32, u32>,
    /// The request's response times from the newest batches, kept here,
    /// next to the count each batch already updates, until they move to its
    /// ring (see [`PendingLatency`]). Allocated the first time the heatmap
    /// records a response time for the request, so without the heatmap a
    /// name costs one pointer more than its counts, and errors never
    /// allocate one.
    pending: Option<Box<PendingLatency>>,
}

/// A request's batched response times for two consecutive seconds, counted
/// by fixed bucket index, waiting to move to its latency ring. It lives in
/// the request's [`KeySeries`], so merging a batch writes next to the count
/// the batch already updates instead of reaching into the ring's slot for
/// the second; the counts move to the ring once both seconds are past, or
/// when a heatmap is built. Laid out in order: what every merge writes
/// first, and both seconds' counts of a bucket side by side.
#[derive(Clone)]
#[repr(C)]
struct PendingLatency {
    /// The index of the request's ring in [`LatencyHistograms`], or `None`
    /// when it has no ring of its own, assigned when this is allocated.
    /// Keeping it here lets a batch merge find the ring with the same lookup
    /// that counts the request. Reset with the latency histograms, by
    /// [`GraphData::reset_rate_series`].
    ring: Option<usize>,
    /// The seconds held: `second` and `second + 1`, once `held`.
    second: usize,
    held: bool,
    /// Fastest, slowest and total in microseconds, and count, for each
    /// second held.
    totals: [[u64; 4]; 2],
    /// Rows past the fixed ones (from 61 seconds): which second held (0 or
    /// 1), the row and its count.
    overflow: Vec<(usize, usize, usize)>,
    /// Counts by fixed bucket index, for each second held.
    counts: [[u32; 2]; FIXED_BUCKETS],
}

impl PendingLatency {
    fn new(ring: Option<usize>) -> Self {
        PendingLatency {
            ring,
            second: 0,
            held: false,
            totals: [[0; 4]; 2],
            overflow: Vec::new(),
            counts: [[0; 2]; FIXED_BUCKETS],
        }
    }

    /// Add a batch's timing data for held second `at` (0 or 1).
    fn add(&mut self, at: usize, data: &GooseRequestMetricTimingData) {
        self.add_totals(at, data);
        for (&bucket, &count) in &data.times_us {
            self.add_bucket(at, bucket, count);
        }
    }

    /// Add a batch's fastest, slowest, total and count for held second `at`.
    fn add_totals(&mut self, at: usize, data: &GooseRequestMetricTimingData) {
        let totals = &mut self.totals[at];
        if data.counter > 0 && (totals[3] == 0 || data.minimum_time_us < totals[0]) {
            totals[0] = data.minimum_time_us;
        }
        totals[1] = totals[1].max(data.maximum_time_us);
        totals[2] += data.total_time_us;
        totals[3] += data.counter as u64;
    }

    /// Add `count` responses in the timing data bucket that starts at
    /// `bucket` microseconds to held second `at`.
    fn add_bucket(&mut self, at: usize, bucket: u64, count: usize) {
        let row = bucket_row(bucket);
        match self.counts.get_mut(row) {
            Some(counts) => {
                let held = &mut counts[at];
                *held = held.saturating_add(u32::try_from(count).unwrap_or(u32::MAX));
            }
            None => self.overflow.push((at, row, count)),
        }
    }

    /// Move everything held to `ring`, each second marked with `rev` of its
    /// own, and empty the counts.
    fn move_to(
        &mut self,
        ring: &mut LatencyRing,
        sink: &mut ColumnSink<'_>,
        mut next_rev: impl FnMut() -> u64,
    ) {
        for at in 0..2 {
            let [minimum, maximum, total, counter] = self.totals[at];
            if counter == 0 {
                continue;
            }
            let (counts, overflow) = (&self.counts, &self.overflow);
            ring.write(self.second + at, next_rev(), sink, |histogram| {
                for (index, counts) in counts.iter().enumerate() {
                    if counts[at] > 0 {
                        histogram.add_dense(index, counts[at] as usize);
                    }
                }
                for &(held, row, count) in overflow {
                    if held == at {
                        histogram.add_bucket(row, count);
                    }
                }
                histogram.add_totals(minimum, maximum, total, counter as usize);
            });
            for counts in self.counts.iter_mut() {
                counts[at] = 0;
            }
            self.totals[at] = [0; 4];
        }
        self.overflow.clear();
    }
}

impl std::ops::Deref for KeySeries {
    type Target = TimeSeries<u32, u32>;
    fn deref(&self) -> &Self::Target {
        &self.counts
    }
}

impl std::ops::DerefMut for KeySeries {
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.counts
    }
}

#[derive(Clone)]
struct ItemsPerSecond {
    series: HashMap<String, KeySeries>,
    /// Absolute second used as `origin` for newly inserted keys.
    ///
    /// After dashboard pruning / rate-series reset this tracks the retained window
    /// so a late-seen request name does not allocate from absolute second 0.
    origin: usize,
}

impl ItemsPerSecond {
    fn new() -> ItemsPerSecond {
        ItemsPerSecond {
            series: Default::default(),
            origin: 0,
        }
    }

    fn with_origin(origin: usize) -> ItemsPerSecond {
        ItemsPerSecond {
            series: Default::default(),
            origin,
        }
    }

    #[inline(always)]
    fn initialize_or_increment(&mut self, key: &str, second: usize, value: u32) -> u32 {
        let data = self.increment(key, second, value);
        data.get(second)
    }

    /// Add `value` to `key`'s count for `second`, returning the key's entry.
    #[inline(always)]
    fn increment(&mut self, key: &str, second: usize, value: u32) -> &mut KeySeries {
        let origin = self.origin;
        let data = self
            .series
            .entry(key.to_string())
            .or_insert_with(|| KeySeries {
                counts: TimeSeries::with_origin(origin),
                pending: None,
            });
        data.increase_value(second, value);
        data
    }

    /// A report graph of every key's counts, in the map's iteration order.
    fn graph<'a>(
        &'a self,
        html_id: &'a str,
        y_axis_label: &'a str,
        granular_data: bool,
    ) -> Graph<'a, u32, u32> {
        Graph {
            html_id,
            y_axis_label,
            granular_data,
            left_out: None,
            data: self
                .series
                .iter()
                .map(|(label, series)| (label.as_str(), &series.counts))
                .collect(),
        }
    }

    #[cfg(test)]
    #[inline(always)]
    fn insert(&mut self, key: &str, time_series: TimeSeries<u32, u32>) {
        self.series.insert(
            key.to_string(),
            KeySeries {
                counts: time_series,
                pending: None,
            },
        );
    }

    #[cfg(test)]
    #[inline(always)]
    fn len(&self) -> usize {
        self.series.len()
    }

    #[cfg(test)]
    #[inline(always)]
    fn get(&self, key: &str) -> Option<TimeSeries<u32, u32>> {
        self.series.get(key).map(|data| data.counts.clone())
    }
}

/// Most request names that get their own latency series. Later names still
/// count toward the all requests series.
pub(crate) const MAX_REQUEST_SERIES: usize = 100;

/// The heatmap's fixed rows, from 0 µs up: where each band starts, the
/// width of its rows and how many it has, all in microseconds. 100 µs rows
/// under a millisecond, 1 ms rows to 64 ms, 2 ms to 100 ms, 10 ms to
/// 500 ms, 50 ms to 1 s, 100 ms to 5 s and 1 s to 61 s. No row is narrower
/// than the timing data buckets it takes, so every row can fill.
const ROW_BANDS: [(u64, u64, usize); 7] = [
    (0, 100, 10),
    (1_000, 1_000, 63),
    (64_000, 2_000, 18),
    (100_000, 10_000, 40),
    (500_000, 50_000, 10),
    (1_000_000, 100_000, 40),
    (5_000_000, 1_000_000, 56),
];

/// The fixed rows, every time up to 61 seconds, within the 256 a
/// [`SecondHistogram`] entry's 8 bit index holds.
const FIXED_BUCKETS: usize = 237;

/// Where the fixed rows end. Past it, possible with a longer `--timeout`,
/// each timing data bucket is a row of its own.
const FIXED_END_US: u64 = 61_000_000;

/// The heatmap row of a time in microseconds: its fixed row, or past 61
/// seconds the row of its timing data bucket.
fn display_index(us: u64) -> usize {
    let mut first = 0;
    for &(start, width, rows) in &ROW_BANDS {
        if us < start + width * rows as u64 {
            return first + ((us - start) / width) as usize;
        }
        first += rows;
    }
    FIXED_BUCKETS + buckets::bucket_index(us) - buckets::bucket_index(FIXED_END_US)
}

/// The first and the last microsecond of heatmap row `index`.
fn display_range(index: usize) -> (u64, u64) {
    let mut first = 0;
    for &(start, width, rows) in &ROW_BANDS {
        if index < first + rows {
            let lowest = start + width * (index - first) as u64;
            return (lowest, lowest + width - 1);
        }
        first += rows;
    }
    // The first starts at 61 seconds, though its bucket may start before.
    let bucket = buckets::bucket_at(buckets::bucket_index(FIXED_END_US) + index - FIXED_BUCKETS);
    (bucket.max(FIXED_END_US), buckets::bucket_end(bucket) - 1)
}

/// The time, in microseconds, heatmap row `index` stands for: its middle.
fn display_value(index: usize) -> u64 {
    let (lowest, highest) = display_range(index);
    lowest + (highest + 1 - lowest) / 2
}

/// The heatmap row of a timing data bucket that starts at `bucket`
/// microseconds: the row of its last microsecond. A bucket is never wider
/// than a row, so it reaches at most into the next one, and a whole
/// millisecond time, which starts a row, stays in that row.
fn bucket_row(bucket: u64) -> usize {
    // Looked up for every bucket a batch merges, so the rows of the buckets
    // up to 61 seconds are worked out once.
    static ROWS: OnceLock<Vec<u8>> = OnceLock::new();
    let rows = ROWS.get_or_init(|| {
        (0..=buckets::bucket_index(FIXED_END_US))
            .map(|index| {
                let row = display_index(buckets::bucket_end(buckets::bucket_at(index)) - 1);
                u8::try_from(row).expect("a row the packed index holds")
            })
            .collect()
    });
    match rows.get(buckets::bucket_index(bucket)) {
        Some(&row) => row as usize,
        None => display_index(buckets::bucket_end(bucket) - 1),
    }
}

/// Bits of a [`SecondHistogram`] entry that hold the count; the bucket index
/// is in the top 8 bits.
const COUNT_BITS: u32 = 24;
/// Most one entry counts. A bucket that fills an entry continues in the next.
const COUNT_MAX: u32 = (1 << COUNT_BITS) - 1;

// Every fixed row's index fits in an entry's top bits.
const _: () = assert!(FIXED_BUCKETS <= 1 << (32 - COUNT_BITS));

/// One second's response times, counted by heatmap row (see
/// [`ROW_BANDS`]), held compactly: a sorted list with 4 bytes for each row
/// the second has seen (its index and its count), so a quiet name costs
/// little and a busy one at most about 1 KB a second. Rows past 61 seconds,
/// possible with a longer `--timeout`, go to a short sorted overflow list.
/// The metrics processor merges each batch's response times for a request
/// name (the batch's
/// [`GooseRequestMetricTimingData`](crate::metrics::GooseRequestMetricTimingData),
/// all from one second) into the rings, each of its buckets into the row
/// it ends in. A ring counts its newest seconds in a flat array
/// instead (see [`LatencyRing`]), so merging a batch is an addition per
/// bucket rather than a search, and packs each second once it stops
/// filling.
#[derive(Clone, Debug, Default)]
pub(crate) struct SecondHistogram {
    entries: Vec<u32>,
    overflow: Vec<(usize, u32)>,
    /// While a ring's second is still filling: its count for each fixed
    /// row, with `entries` empty.
    open: Option<Box<[usize; FIXED_BUCKETS]>>,
    /// The fastest response in microseconds, not rounded.
    pub(crate) minimum_time_us: u64,
    /// The slowest response in microseconds, not rounded.
    pub(crate) maximum_time_us: u64,
    /// Sum of all response times, in microseconds.
    pub(crate) total_time_us: u64,
    /// Number of responses.
    pub(crate) counter: usize,
}

/// Two histograms are equal when they hold the same response times, however
/// each holds them.
impl PartialEq for SecondHistogram {
    fn eq(&self, other: &Self) -> bool {
        self.minimum_time_us == other.minimum_time_us
            && self.maximum_time_us == other.maximum_time_us
            && self.total_time_us == other.total_time_us
            && self.counter == other.counter
            && self
                .buckets()
                .coalesce(merge_bucket)
                .eq(other.buckets().coalesce(merge_bucket))
    }
}

/// A row as `(index, count)`.
type Bucket = (usize, usize);

/// Join consecutive pieces of one row (a row past [`COUNT_MAX`]).
fn merge_bucket(a: Bucket, b: Bucket) -> Result<Bucket, (Bucket, Bucket)> {
    if a.0 == b.0 {
        Ok((a.0, a.1 + b.1))
    } else {
        Err((a, b))
    }
}

impl SecondHistogram {
    /// An empty histogram for a ring's newest second, counted by fixed index
    /// until [`pack`](Self::pack).
    fn open() -> Self {
        Self::open_with(Box::new([0; FIXED_BUCKETS]))
    }

    /// [`Self::open`], counting in `counts`, which must be all zero.
    fn open_with(counts: Box<[usize; FIXED_BUCKETS]>) -> Self {
        debug_assert!(counts.iter().all(|&count| count == 0));
        SecondHistogram {
            open: Some(counts),
            ..Default::default()
        }
    }

    /// Add `count` to the row with fixed index `index`, searching from
    /// entry `from` on. Returns where the search for a higher index can
    /// start.
    fn add_indexed(&mut self, from: usize, index: usize, mut count: usize) -> usize {
        debug_assert!(index < FIXED_BUCKETS && FIXED_BUCKETS <= 1 << (32 - COUNT_BITS));
        let bucket = index as u32;
        let key = bucket << COUNT_BITS;
        let mut at = from + self.entries[from..].partition_point(|&entry| entry < key);
        while count > 0 {
            if at < self.entries.len() && self.entries[at] >> COUNT_BITS == bucket {
                let room = (COUNT_MAX - (self.entries[at] & COUNT_MAX)) as usize;
                let add = room.min(count);
                self.entries[at] += add as u32;
                count -= add;
            } else {
                let add = count.min(COUNT_MAX as usize);
                self.entries.insert(at, key | add as u32);
                count -= add;
            }
            at += 1;
        }
        at
    }

    /// Add `count` responses to heatmap row `row`.
    fn add_bucket(&mut self, row: usize, count: usize) {
        if row < FIXED_BUCKETS {
            self.add_dense(row, count);
        } else {
            self.add_overflow(row, count);
        }
    }

    /// Add `count` to the row with fixed index `index`.
    fn add_dense(&mut self, index: usize, count: usize) {
        match self.open.as_mut() {
            Some(open) => open[index] += count,
            None => {
                self.add_indexed(0, index, count);
            }
        }
    }

    /// Add `count` to a row past the fixed ones.
    fn add_overflow(&mut self, row: usize, count: usize) {
        let count = u32::try_from(count).unwrap_or(u32::MAX);
        match self.overflow.binary_search_by_key(&row, |&(row, _)| row) {
            Ok(at) => self.overflow[at].1 = self.overflow[at].1.saturating_add(count),
            Err(at) => self.overflow.insert(at, (row, count)),
        }
    }

    fn add_totals(&mut self, minimum: u64, maximum: u64, total: u64, counter: usize) {
        // The counters, not a minimum of 0, say whether there are times yet:
        // a time can be 0 µs.
        if counter > 0 && (self.counter == 0 || minimum < self.minimum_time_us) {
            self.minimum_time_us = minimum;
        }
        if maximum > self.maximum_time_us {
            self.maximum_time_us = maximum;
        }
        self.total_time_us += total;
        self.counter += counter;
    }

    /// Record one response time in whole milliseconds (unit tests).
    #[cfg(test)]
    pub(crate) fn record(&mut self, time: u64) {
        self.record_us(time.saturating_mul(1000));
    }

    /// Record one response time in microseconds, in the row of its timing
    /// data bucket, as a batch merge counts it.
    pub(crate) fn record_us(&mut self, us: u64) {
        self.add_totals(us, us, us, 1);
        self.add_bucket(bucket_row(buckets::bucket_floor(us)), 1);
    }

    /// Add another second's response times.
    pub(crate) fn merge(&mut self, other: &SecondHistogram) {
        if let Some(open) = self.open.as_mut() {
            for (index, count) in other.indexed() {
                open[index] += count;
            }
        } else {
            // Both are sorted, so each search starts where the last one ended.
            let mut from = 0;
            for (index, count) in other.indexed() {
                from = self.add_indexed(from, index, count);
            }
        }
        for &(row, count) in &other.overflow {
            self.add_overflow(row, count as usize);
        }
        self.add_totals(
            other.minimum_time_us,
            other.maximum_time_us,
            other.total_time_us,
            other.counter,
        );
    }

    /// Add a batch's response times for one request name, all from this
    /// second: each bucket into its row, and the unrounded minimum,
    /// maximum, total and count, as
    /// [`GooseRequestMetricTimingData::merge`] adds them.
    pub(crate) fn merge_timing(&mut self, other: &GooseRequestMetricTimingData) {
        for (&bucket, &count) in &other.times_us {
            self.add_bucket(bucket_row(bucket), count);
        }
        self.add_totals(
            other.minimum_time_us,
            other.maximum_time_us,
            other.total_time_us,
            other.counter,
        );
    }

    /// Once the second is unlikely to grow: move the counts of an open
    /// second into the compact list, allocated once at its size, and release
    /// spare capacity. Returns the emptied counts of the open second, for
    /// the next second to reuse.
    fn pack(&mut self) -> Option<Box<[usize; FIXED_BUCKETS]>> {
        let mut open = self.open.take()?;
        if self.entries.is_empty() {
            let buckets = open.iter().filter(|&&count| count > 0).count();
            self.entries.reserve_exact(buckets);
        }
        let mut from = 0;
        for (index, count) in open.iter_mut().enumerate() {
            if *count > 0 {
                from = self.add_indexed(from, index, *count);
                *count = 0;
            }
        }
        self.entries.shrink_to_fit();
        self.overflow.shrink_to_fit();
        Some(open)
    }

    /// Count a packed histogram by fixed index again, in `counts`, which
    /// must be all zero, until the next [`pack`](Self::pack).
    fn reopen(&mut self, mut counts: Box<[usize; FIXED_BUCKETS]>) {
        debug_assert!(self.open.is_none() && counts.iter().all(|&count| count == 0));
        for (index, count) in self.indexed() {
            counts[index] += count;
        }
        self.entries = Vec::new();
        self.open = Some(counts);
    }

    /// The heatmap's rows of this second: each non empty row as
    /// `index, count`, flattened, by ascending index.
    fn heat_cells(&self) -> Vec<u64> {
        let mut cells: Vec<u64> = Vec::new();
        for (index, count) in self.buckets() {
            let (index, count) = (index as u64, count as u64);
            // A row past COUNT_MAX continues in the next entry.
            match cells.len().checked_sub(2).map(|at| cells[at]) {
                Some(last) if last == index => *cells.last_mut().expect("a count") += count,
                _ => cells.extend([index, count]),
            }
        }
        cells
    }

    /// Non empty fixed rows as `(index, count)`, fastest first. A row past
    /// [`COUNT_MAX`] appears more than once, consecutively, unless the
    /// second is open.
    fn indexed(&self) -> impl Iterator<Item = (usize, usize)> + '_ {
        let open = self.open.iter().flat_map(|open| {
            open.iter()
                .enumerate()
                .filter(|&(_, &count)| count > 0)
                .map(|(index, &count)| (index, count))
        });
        let packed = self
            .entries
            .iter()
            .map(|&entry| ((entry >> COUNT_BITS) as usize, (entry & COUNT_MAX) as usize));
        open.chain(packed)
    }

    /// Non empty rows, fixed and past them, as `(index, count)`, fastest
    /// first. A row past [`COUNT_MAX`] may appear more than once,
    /// consecutively.
    fn buckets(&self) -> impl Iterator<Item = (usize, usize)> + '_ {
        self.indexed().chain(
            self.overflow
                .iter()
                .map(|&(row, count)| (row, count as usize)),
        )
    }

    /// [`Self::percentile_us`] in whole milliseconds, truncated (unit
    /// tests).
    #[cfg(test)]
    pub(crate) fn percentile(&self, percent: f32) -> usize {
        (self.percentile_us(percent) / 1000) as usize
    }

    /// The response time `percent` of these requests finished within, in
    /// microseconds: the middle of its row, kept between the fastest and
    /// slowest, by the rule the whole run's percentiles follow.
    pub(crate) fn percentile_us(&self, percent: f32) -> u64 {
        crate::metrics::response_time_percentile(
            self.buckets()
                .map(|(index, count)| (display_value(index), count)),
            self.counter,
            self.minimum_time_us,
            self.maximum_time_us,
            percent,
        )
    }
}

/// Heatmap row `index` as `[time, lowest, highest]` in microseconds.
fn row_entry_us(index: usize) -> [u64; 3] {
    let (lowest, highest) = display_range(index);
    [display_value(index), lowest, highest]
}

/// The heatmap's row table, `[time, lowest, highest]` in microseconds
/// for each index from 0 to `last`, and always every fixed index.
fn bucket_table_us(last: usize) -> Vec<[u64; 3]> {
    (0..=last.max(FIXED_BUCKETS - 1))
        .map(row_entry_us)
        .collect()
}

/// A new generation for a set of latency histograms: unique in the process
/// and, starting from the clock, unlikely to repeat one a client kept from an
/// earlier Goose.
fn next_heatmap_generation() -> u64 {
    static NEXT: OnceLock<AtomicU64> = OnceLock::new();
    NEXT.get_or_init(|| {
        let now_ms = std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .map_or(1, |since| since.as_millis() as u64);
        AtomicU64::new(now_ms * 1000)
    })
    .fetch_add(1, AtomicOrdering::Relaxed)
}

/// One second of a [`LatencyRing`].
#[derive(Clone)]
struct RingSlot {
    second: usize,
    /// The histograms' revision when this second last changed.
    rev: u64,
    data: SecondHistogram,
}

/// Most columns the report's heatmap keeps for all requests.
const ALL_REQUESTS_COLUMNS: usize = 600;
/// Most columns the report's heatmap keeps for each request name with a ring.
const REQUEST_COLUMNS: usize = 60;

/// A series' response times over the whole run, for the report's heatmap:
/// one histogram for each column of a width in seconds that
/// [`LatencyHistograms`] keeps, from its origin. When a series fills its
/// budget, adjacent pairs merge and the width doubles, so memory stops
/// growing however long the run. The newest two columns stay open, counted
/// by fixed index as a ring's newest seconds are, so folding a second into
/// them is additions.
#[derive(Clone, Default)]
pub(crate) struct Columns {
    columns: Vec<SecondHistogram>,
    /// Columns whose histograms are still open.
    open: Vec<usize>,
    /// Emptied counts of packed columns, for new columns to count in.
    #[allow(clippy::vec_box)]
    spare: Vec<Box<[usize; FIXED_BUCKETS]>>,
}

impl Columns {
    /// The histogram of `column`. A column after every other opens, and
    /// those two or more before it pack.
    fn at(&mut self, column: usize) -> &mut SecondHistogram {
        if column >= self.columns.len() {
            self.columns
                .resize_with(column + 1, SecondHistogram::default);
        }
        if column + 1 == self.columns.len() && !self.open.contains(&column) {
            let (columns, spare) = (&mut self.columns, &mut self.spare);
            self.open.retain(|&older| {
                if older + 2 > column {
                    return true;
                }
                spare.extend(columns[older].pack());
                false
            });
            let counts = self
                .spare
                .pop()
                .unwrap_or_else(|| Box::new([0; FIXED_BUCKETS]));
            self.columns[column].reopen(counts);
            self.open.push(column);
        }
        &mut self.columns[column]
    }

    /// The highest heatmap bucket index any column uses.
    fn last_bucket(&self) -> Option<usize> {
        self.columns
            .iter()
            .filter_map(|column| {
                let cells = column.heat_cells();
                cells.len().checked_sub(2).map(|at| cells[at] as usize)
            })
            .max()
    }

    /// Merge adjacent pairs of columns, for a width twice as wide.
    fn double(&mut self) {
        for column in self.open.drain(..) {
            self.spare.extend(self.columns[column].pack());
        }
        let mut pairs = std::mem::take(&mut self.columns).into_iter();
        while let Some(mut first) = pairs.next() {
            if let Some(second) = pairs.next() {
                first.merge(&second);
                first.entries.shrink_to_fit();
            }
            self.columns.push(first);
        }
    }
}

/// Where a ring's seconds go once folded: its own [`Columns`], if it has
/// them, and for a measured ring all requests' columns, each with its width
/// in seconds, all counted from `origin`. Passed in by the ring's caller in
/// [`LatencyHistograms`], which keeps the origin and the widths.
pub(crate) struct ColumnSink<'a> {
    origin: usize,
    width: usize,
    all: Option<(&'a mut Columns, usize)>,
}

impl ColumnSink<'_> {
    /// For a ring that feeds only its own columns, `width` seconds wide.
    fn own(origin: usize, width: usize) -> Self {
        ColumnSink {
            origin,
            width,
            all: None,
        }
    }

    /// For a ring with no columns to feed (unit tests).
    #[cfg(test)]
    fn none() -> Self {
        Self::own(0, 1)
    }

    /// Apply `add` to the column holding `second` in `own`, the ring's own
    /// columns, and in all requests' columns. A second before the origin
    /// goes to the first column.
    fn feed(
        &mut self,
        own: Option<&mut Columns>,
        second: usize,
        mut add: impl FnMut(&mut SecondHistogram),
    ) {
        let since = second.saturating_sub(self.origin);
        if let Some(own) = own {
            add(own.at(since / self.width));
        }
        if let Some((all, width)) = self.all.as_mut() {
            add(all.at(since / *width));
        }
    }
}

/// One response time histogram for each of the last [`SERIES_WINDOW_SECS`]
/// seconds. Second `s` lives in slot `s % len`, tagged with `s` so a slot that
/// still holds an older second is recognized and reused, and with the
/// revision of its last change so a heatmap can send only what changed. The
/// newest seconds stay open, counted by fixed index, until a second two later
/// begins. A ring with whole run columns folds each second into them once,
/// when it packs, and sends anything written to a second after that, or too
/// old for the ring, to them directly.
#[derive(Clone)]
pub(crate) struct LatencyRing {
    slots: Vec<Option<RingSlot>>,
    /// Seconds whose histograms are still open.
    open: Vec<usize>,
    /// Emptied counts of packed seconds, for new seconds to count in, so a
    /// ring does not allocate and free them every second. Boxed, as a
    /// histogram holds them, so they move between the two without copying.
    #[allow(clippy::vec_box)]
    spare: Vec<Box<[usize; FIXED_BUCKETS]>>,
    /// This ring's whole run columns, for the report's heatmap.
    columns: Option<Columns>,
}

impl LatencyRing {
    pub(crate) fn new() -> Self {
        Self::with_columns(false)
    }

    /// A ring that keeps whole run columns when `columns` is set.
    fn with_columns(columns: bool) -> Self {
        LatencyRing {
            slots: (0..SERIES_WINDOW_SECS).map(|_| None).collect(),
            open: Vec::new(),
            spare: Vec::new(),
            columns: columns.then(Columns::default),
        }
    }

    /// The histogram for `second`, replacing an older second's, marked as
    /// changed at `rev`. `None` when the slot already holds a newer second:
    /// the sample is too old to show. Seconds packed here fold into `sink`.
    fn slot_mut(
        &mut self,
        second: usize,
        rev: u64,
        sink: &mut ColumnSink<'_>,
    ) -> Option<&mut SecondHistogram> {
        // The window is a constant, so the remainder needs no division.
        let len = SERIES_WINDOW_SECS as usize;
        debug_assert_eq!(self.slots.len(), len);
        let at = second % len;
        match &self.slots[at] {
            Some(slot) if slot.second > second => return None,
            Some(slot) if slot.second == second => {}
            _ => {
                // A new second has begun: those two or more seconds back
                // have all but their slowest requests, so pack them.
                let (slots, spare, columns) = (&mut self.slots, &mut self.spare, &mut self.columns);
                self.open.retain(|&older| {
                    if older + 2 > second {
                        return true;
                    }
                    if let Some(slot) = &mut slots[older % len] {
                        if slot.second == older {
                            Self::pack_slot(slot, spare, columns.as_mut(), sink);
                        }
                    }
                    false
                });
                let data = match self.spare.pop() {
                    Some(counts) => SecondHistogram::open_with(counts),
                    None => SecondHistogram::open(),
                };
                self.slots[at] = Some(RingSlot { second, rev, data });
                self.open.push(second);
            }
        }
        self.slots[at].as_mut().map(|slot| {
            slot.rev = rev;
            &mut slot.data
        })
    }

    /// Pack an open second and fold it into the columns.
    #[allow(clippy::vec_box)]
    fn pack_slot(
        slot: &mut RingSlot,
        spare: &mut Vec<Box<[usize; FIXED_BUCKETS]>>,
        columns: Option<&mut Columns>,
        sink: &mut ColumnSink<'_>,
    ) {
        spare.extend(slot.data.pack());
        let data = &slot.data;
        sink.feed(columns, slot.second, |column| column.merge(data));
    }

    /// Pack every open second, folding each into the columns, so they hold
    /// every time written so far.
    fn pack_open(&mut self, sink: &mut ColumnSink<'_>) {
        let len = SERIES_WINDOW_SECS as usize;
        for older in self.open.drain(..) {
            if let Some(slot) = &mut self.slots[older % len] {
                if slot.second == older {
                    Self::pack_slot(slot, &mut self.spare, self.columns.as_mut(), sink);
                }
            }
        }
    }

    /// Apply `add` to `second`'s histogram, at `rev`; and to the columns
    /// when the second is already packed, and so folded, or too old for the
    /// ring.
    fn write(
        &mut self,
        second: usize,
        rev: u64,
        sink: &mut ColumnSink<'_>,
        mut add: impl FnMut(&mut SecondHistogram),
    ) {
        let feeds = self.columns.is_some() || sink.all.is_some();
        let folded = match self.slot_mut(second, rev, sink) {
            Some(histogram) => {
                add(histogram);
                histogram.open.is_none()
            }
            None => true,
        };
        if feeds && folded {
            sink.feed(self.columns.as_mut(), second, add);
        }
    }

    /// Record one response time in whole milliseconds in `second`, at
    /// `rev`, for a ring with no columns to feed (unit tests).
    #[cfg(test)]
    pub(crate) fn record(&mut self, second: usize, time: u64, rev: u64) {
        self.record_to(
            second,
            time.saturating_mul(1000),
            rev,
            &mut ColumnSink::none(),
        );
    }

    /// Record one response time in microseconds in `second`, at `rev`.
    fn record_to(&mut self, second: usize, us: u64, rev: u64, sink: &mut ColumnSink<'_>) {
        self.write(second, rev, sink, |histogram| histogram.record_us(us));
    }

    /// Add a batch's response times for one request name to `second`, at
    /// `rev`.
    fn merge_timing_to(
        &mut self,
        second: usize,
        other: &GooseRequestMetricTimingData,
        rev: u64,
        sink: &mut ColumnSink<'_>,
    ) {
        self.write(second, rev, sink, |histogram| histogram.merge_timing(other));
    }

    /// Add a batch's response times for one request name to `second`, at
    /// `rev` (unit tests).
    #[cfg(test)]
    pub(crate) fn merge_timing(
        &mut self,
        second: usize,
        other: &GooseRequestMetricTimingData,
        rev: u64,
    ) {
        self.merge_timing_to(second, other, rev, &mut ColumnSink::none());
    }

    /// Forget every second, and empty the columns.
    pub(crate) fn clear(&mut self) {
        for slot in self.slots.iter_mut() {
            *slot = None;
        }
        self.open.clear();
        if let Some(columns) = self.columns.as_mut() {
            *columns = Columns::default();
        }
    }

    /// Fold every packed second from `from` on into the columns; open
    /// seconds fold in as they pack.
    fn fold_packed(&mut self, from: usize, sink: &mut ColumnSink<'_>) {
        let (slots, columns) = (&self.slots, &mut self.columns);
        for slot in slots.iter().flatten() {
            if slot.second >= from && slot.data.open.is_none() {
                sink.feed(columns.as_mut(), slot.second, |column| {
                    column.merge(&slot.data)
                });
            }
        }
    }

    /// [`Self::slot`], to build and keep its heatmap cells.
    fn slot_at_mut(&mut self, second: usize) -> Option<&mut RingSlot> {
        self.slots[second % SERIES_WINDOW_SECS as usize]
            .as_mut()
            .filter(|slot| slot.second == second)
    }

    fn slot(&self, second: usize) -> Option<&RingSlot> {
        self.slots[second % SERIES_WINDOW_SECS as usize]
            .as_ref()
            .filter(|slot| slot.second == second)
    }

    /// The histogram for `second`, if the ring still holds it (unit tests).
    #[cfg(test)]
    pub(crate) fn get(&self, second: usize) -> Option<&SecondHistogram> {
        self.slot(second).map(|slot| &slot.data)
    }
}

/// Where a heatmap's measured columns come from.
trait MeasuredColumns {
    /// The revision `second` last changed at, if it has response times.
    fn rev(&self, second: usize) -> Option<u64>;
    /// `second`'s heatmap cells.
    fn cells(&mut self, second: usize) -> Vec<u64>;
}

impl MeasuredColumns for LatencyRing {
    fn rev(&self, second: usize) -> Option<u64> {
        self.slot(second).map(|slot| slot.rev)
    }

    fn cells(&mut self, second: usize) -> Vec<u64> {
        self.slot_at_mut(second)
            .map_or_else(Vec::new, |slot| slot.data.heat_cells())
    }
}

/// All requests: every name's ring and the ring of names without one,
/// added up for a column only when a heatmap asks for it.
struct AllRequests<'a> {
    rings: &'a [LatencyRing],
    others: &'a LatencyRing,
    /// Each second's cells as of the revision they were built at.
    cells: &'a mut [Option<(usize, u64, Vec<u64>)>],
}

impl AllRequests<'_> {
    fn slots(&self, second: usize) -> impl Iterator<Item = &RingSlot> + '_ {
        self.rings
            .iter()
            .chain([self.others])
            .filter_map(move |ring| ring.slot(second))
    }
}

impl MeasuredColumns for AllRequests<'_> {
    fn rev(&self, second: usize) -> Option<u64> {
        self.slots(second).map(|slot| slot.rev).max()
    }

    fn cells(&mut self, second: usize) -> Vec<u64> {
        let Some(rev) = self.rev(second) else {
            return Vec::new();
        };
        let at = second % self.cells.len();
        match &self.cells[at] {
            Some((kept, built, cells)) if *kept == second && *built == rev => cells.clone(),
            _ => {
                let mut all = SecondHistogram::open();
                for slot in self.slots(second) {
                    all.merge(&slot.data);
                }
                let cells = all.heat_cells();
                self.cells[at] = Some((second, rev, cells.clone()));
                cells
            }
        }
    }
}

/// Per second response time histograms for the dashboard's heatmap: each of
/// the first [`MAX_REQUEST_SERIES`] request names, the names after them
/// together, all requests as their sum, and the same for synthetic times
/// while coordinated omission mitigation is on. A batch's response times for
/// a name go to one ring; the all requests column of a second is only added
/// up when a heatmap needs it.
#[derive(Clone)]
pub(crate) struct LatencyHistograms {
    /// Requests whose names have no ring of their own.
    others: LatencyRing,
    /// The all requests heatmap's cells, kept by second (see [`AllRequests`]).
    all_cells: Vec<Option<(usize, u64, Vec<u64>)>>,
    /// Each admitted request name's ring, in the order names were admitted.
    rings: Vec<LatencyRing>,
    /// Admitted request names and the index of their ring in `rings`.
    by_request: HashMap<String, usize>,
    synthetic: Option<(LatencyRing, HashMap<String, LatencyRing>)>,
    /// Changes when the rings are cleared, so a client starts over.
    generation: u64,
    /// Counts changes: each record or merge marks the seconds it changes
    /// with the next value.
    rev: u64,
    /// All requests' whole run columns for the report's heatmap, when the
    /// rings keep columns: each name's are in its ring, and synthetic times'
    /// in theirs.
    all_columns: Option<Columns>,
    /// The second every series' columns count from: 0, or the second of the
    /// last metrics reset on the series clock.
    origin: usize,
    /// Seconds in a column of all requests, measured and synthetic.
    all_width: usize,
    /// Seconds in a column of each request name, measured and synthetic.
    request_width: usize,
    /// Most columns of all requests and of each request name.
    budgets: (usize, usize),
    /// The first second that fits in no column at the current widths; past
    /// every second when no columns are kept.
    limit: usize,
}

impl LatencyHistograms {
    /// `synthetic` keeps rings for coordinated omission mitigation's
    /// synthetic times too; pass it when mitigation is not `disabled`.
    /// `columns` keeps whole run columns for the report's heatmap.
    pub(crate) fn new(synthetic: bool, columns: bool) -> Self {
        let mut histograms = LatencyHistograms {
            others: LatencyRing::new(),
            all_cells: (0..SERIES_WINDOW_SECS).map(|_| None).collect(),
            rings: Vec::new(),
            by_request: HashMap::new(),
            synthetic: synthetic.then(|| (LatencyRing::with_columns(columns), HashMap::new())),
            generation: next_heatmap_generation(),
            rev: 0,
            all_columns: columns.then(Columns::default),
            origin: 0,
            all_width: 1,
            request_width: 1,
            budgets: (ALL_REQUESTS_COLUMNS, REQUEST_COLUMNS),
            limit: usize::MAX,
        };
        histograms.set_limit();
        histograms
    }

    /// [`Self::new`] keeping columns, with budgets of `all` columns for all
    /// requests and `request` for each name, so a test sees several merges.
    #[cfg(test)]
    pub(crate) fn with_budgets(synthetic: bool, all: usize, request: usize) -> Self {
        let mut histograms = Self::new(synthetic, true);
        histograms.budgets = (all, request);
        histograms.set_limit();
        histograms
    }

    /// Recompute the first second that fits in no column.
    fn set_limit(&mut self) {
        let (all, request) = self.budgets;
        self.limit = match self.all_columns {
            Some(_) => {
                (self.origin + all * self.all_width).min(self.origin + request * self.request_width)
            }
            None => usize::MAX,
        };
    }

    /// Before writing `second`: double the widths until it fits in a
    /// column. Every second a ring later folds was written, and so checked,
    /// here, and widths never narrow until the next reset.
    #[inline(always)]
    fn fit_columns(&mut self, second: usize) {
        if second >= self.limit {
            self.widen(second);
        }
    }

    /// Double the width of every series of a budget `second` does not fit,
    /// as often as it takes.
    #[cold]
    fn widen(&mut self, second: usize) {
        let (all, request) = self.budgets;
        while second >= self.origin + all * self.all_width {
            self.all_width *= 2;
            let synthetic = self.synthetic.as_mut().map(|(all, _)| all);
            for columns in self
                .all_columns
                .iter_mut()
                .chain(synthetic.and_then(|ring| ring.columns.as_mut()))
            {
                columns.double();
            }
        }
        while second >= self.origin + request * self.request_width {
            self.request_width *= 2;
            let synthetic = self
                .synthetic
                .iter_mut()
                .flat_map(|(_, by_request)| by_request.values_mut());
            for ring in self.rings.iter_mut().chain(synthetic) {
                if let Some(columns) = ring.columns.as_mut() {
                    columns.double();
                }
            }
        }
        self.set_limit();
    }

    /// The revision of a change about to be made.
    fn next_rev(&mut self) -> u64 {
        self.rev += 1;
        self.rev
    }

    /// The index of `key`'s ring, admitting the name while there is room.
    fn ring_index(&mut self, key: &str) -> Option<usize> {
        if let Some(&index) = self.by_request.get(key) {
            return Some(index);
        }
        if self.rings.len() >= MAX_REQUEST_SERIES {
            return None;
        }
        self.rings
            .push(LatencyRing::with_columns(self.all_columns.is_some()));
        self.by_request
            .insert(key.to_string(), self.rings.len() - 1);
        Some(self.rings.len() - 1)
    }

    /// `key`'s pending response times in `entry`, allocated the first time
    /// the request has a response time to record, which is when its ring is
    /// assigned: so the first [`MAX_REQUEST_SERIES`] names seen get rings,
    /// in the order they are seen.
    fn pending<'a>(&mut self, key: &str, entry: &'a mut KeySeries) -> &'a mut PendingLatency {
        let ring = match entry.pending {
            Some(_) => None,
            None => self.ring_index(key),
        };
        entry
            .pending
            .get_or_insert_with(|| Box::new(PendingLatency::new(ring)))
    }

    /// The ring of the name with ring `index`, or of the names without one,
    /// and where its seconds fold: its own columns, at the request width,
    /// and all requests' columns.
    fn ring(&mut self, index: Option<usize>) -> (&mut LatencyRing, ColumnSink<'_>) {
        let ring = match index {
            Some(index) => &mut self.rings[index],
            None => &mut self.others,
        };
        let all_width = self.all_width;
        let sink = ColumnSink {
            origin: self.origin,
            width: self.request_width,
            all: self.all_columns.as_mut().map(|all| (all, all_width)),
        };
        (ring, sink)
    }

    #[cfg(test)]
    fn record(&mut self, key: &str, second: usize, us: u64) {
        self.fit_columns(second);
        let rev = self.next_rev();
        let index = self.ring_index(key);
        let (ring, mut sink) = self.ring(index);
        ring.record_to(second, us, rev, &mut sink);
    }

    /// Record one response time in microseconds for `key`, whose entry is
    /// `entry`.
    fn record_in(&mut self, key: &str, entry: &mut KeySeries, second: usize, us: u64) {
        self.fit_columns(second);
        let index = self.pending(key, entry).ring;
        let rev = self.next_rev();
        let (ring, mut sink) = self.ring(index);
        ring.record_to(second, us, rev, &mut sink);
    }

    /// Merge a batch's timing data for `key` in `second` into the key's
    /// pending counts, moving what they held to its ring first when `second`
    /// is past both seconds they hold. Times for a second before them go to
    /// the ring directly.
    fn merge_pending(
        &mut self,
        key: &str,
        entry: &mut KeySeries,
        second: usize,
        data: &GooseRequestMetricTimingData,
    ) {
        self.fit_columns(second);
        let pending = self.pending(key, entry);
        match self.pending_at(pending, second) {
            Some(at) => pending.add(at, data),
            None => self.merge_older(pending, second, data),
        }
    }

    /// Which of the two seconds `key`'s pending counts hold is `second`,
    /// after moving what they held to its ring when `second` is past both;
    /// `None` when `second` is before them.
    fn pending_at(&mut self, pending: &mut PendingLatency, second: usize) -> Option<usize> {
        if !pending.held {
            pending.held = true;
            pending.second = second;
        }
        if second < pending.second {
            return None;
        }
        if second > pending.second + 1 {
            self.move_pending(pending);
            pending.second = second;
        }
        Some(second - pending.second)
    }

    /// Merge timing data for a second before those `key`'s pending counts
    /// hold straight into its ring.
    fn merge_older(
        &mut self,
        pending: &PendingLatency,
        second: usize,
        data: &GooseRequestMetricTimingData,
    ) {
        let rev = self.next_rev();
        let (ring, mut sink) = self.ring(pending.ring);
        ring.merge_timing_to(second, data, rev, &mut sink);
    }

    /// Move `pending` to its request's ring.
    fn move_pending(&mut self, pending: &mut PendingLatency) {
        let mut rev = self.rev;
        let (ring, mut sink) = self.ring(pending.ring);
        pending.move_to(ring, &mut sink, || {
            rev += 1;
            rev
        });
        self.rev = rev;
    }

    /// Pack every ring's open seconds, folding them into the columns, when
    /// columns are kept.
    fn pack_open(&mut self) {
        if self.all_columns.is_none() {
            return;
        }
        for index in (0..self.rings.len()).map(Some).chain([None]) {
            let (ring, mut sink) = self.ring(index);
            ring.pack_open(&mut sink);
        }
        let (origin, all_width, request_width) = (self.origin, self.all_width, self.request_width);
        if let Some((all, by_request)) = self.synthetic.as_mut() {
            all.pack_open(&mut ColumnSink::own(origin, all_width));
            for ring in by_request.values_mut() {
                ring.pack_open(&mut ColumnSink::own(origin, request_width));
            }
        }
    }

    /// All requests in `second`: every ring's histogram for it added up, if
    /// any ring holds it.
    #[cfg(test)]
    pub(crate) fn all_second(&self, second: usize) -> Option<SecondHistogram> {
        let mut slots = self
            .rings
            .iter()
            .chain([&self.others])
            .filter_map(|ring| ring.slot(second))
            .peekable();
        slots.peek()?;
        let mut all = SecondHistogram::default();
        for slot in slots {
            all.merge(&slot.data);
        }
        Some(all)
    }

    /// Record a synthetic time, in microseconds. A name gets a synthetic
    /// ring only if it has a measured one.
    fn record_synthetic(&mut self, key: &str, second: usize, us: u64) {
        self.fit_columns(second);
        let rev = self.next_rev();
        let (origin, columns) = (self.origin, self.all_columns.is_some());
        let Some((all, by_request)) = self.synthetic.as_mut() else {
            return;
        };
        all.record_to(
            second,
            us,
            rev,
            &mut ColumnSink::own(origin, self.all_width),
        );
        if !self.by_request.contains_key(key) {
            return;
        }
        let mut sink = ColumnSink::own(origin, self.request_width);
        if let Some(ring) = by_request.get_mut(key) {
            ring.record_to(second, us, rev, &mut sink);
        } else {
            let mut ring = LatencyRing::with_columns(columns);
            ring.record_to(second, us, rev, &mut sink);
            by_request.insert(key.to_string(), ring);
        }
    }

    /// Start or stop keeping whole run columns for the report's heatmap
    /// while the run goes on, `now` being the current second. Started, the
    /// columns count from the oldest second the rings can hold and take every
    /// second they hold; stopped, the columns go.
    fn keep_columns(&mut self, keep: bool, now: usize) {
        if keep == self.all_columns.is_some() {
            return;
        }
        let columns = || keep.then(Columns::default);
        self.all_columns = columns();
        for ring in self.rings.iter_mut() {
            ring.columns = columns();
        }
        if let Some((all, by_request)) = self.synthetic.as_mut() {
            all.columns = columns();
            for ring in by_request.values_mut() {
                ring.columns = columns();
            }
        }
        self.origin = now.saturating_sub(SERIES_WINDOW_SECS as usize - 1);
        self.all_width = 1;
        self.request_width = 1;
        self.set_limit();
        if !keep {
            return;
        }
        self.fit_columns(now);
        let origin = self.origin;
        for index in (0..self.rings.len()).map(Some).chain([None]) {
            let (ring, mut sink) = self.ring(index);
            ring.fold_packed(origin, &mut sink);
        }
        let (all_width, request_width) = (self.all_width, self.request_width);
        if let Some((all, by_request)) = self.synthetic.as_mut() {
            all.fold_packed(origin, &mut ColumnSink::own(origin, all_width));
            for ring in by_request.values_mut() {
                ring.fold_packed(origin, &mut ColumnSink::own(origin, request_width));
            }
        }
    }

    /// Clear every ring and its columns and forget every name, so names are
    /// admitted again, and start a new generation. Columns count from
    /// `origin`, the series second of the reset, one second wide.
    fn clear(&mut self, origin: usize) {
        self.others.clear();
        self.all_cells.iter_mut().for_each(|cells| *cells = None);
        self.rings.clear();
        self.by_request.clear();
        if let Some((all, by_request)) = self.synthetic.as_mut() {
            all.clear();
            by_request.clear();
        }
        if let Some(columns) = self.all_columns.as_mut() {
            *columns = Columns::default();
        }
        self.origin = origin;
        self.all_width = 1;
        self.request_width = 1;
        self.set_limit();
        self.generation = next_heatmap_generation();
    }

    /// The heatmap of one measured ring and, when `co_active` and synthetic
    /// rings are kept, its synthetic counterpart, over `start..=end`: only
    /// the seconds changed after `since` when it is a cursor of these
    /// histograms, otherwise every second with data.
    #[allow(clippy::too_many_arguments)]
    ///
    /// `generation` and `rev` are the histograms' own, passed in so the
    /// rings can be borrowed to build and keep each column's cells.
    fn heatmap(
        (generation, rev): (u64, u64),
        measured: &mut impl MeasuredColumns,
        mut synthetic: Option<&mut LatencyRing>,
        start: usize,
        end: usize,
        hold_from: usize,
        since: Option<HeatmapCursor>,
    ) -> Heatmap {
        let since = match since {
            Some(cursor) if cursor.generation == generation && cursor.rev <= rev => cursor.rev,
            _ => 0,
        };
        let mut columns = Vec::new();
        let mut last_index = FIXED_BUCKETS - 1;
        for second in start..=end {
            let real = measured.rev(second);
            let added = synthetic
                .as_deref_mut()
                .and_then(|ring| ring.slot_at_mut(second));
            if !real.is_some_and(|real| real > since) && !added.iter().any(|slot| slot.rev > since)
            {
                continue;
            }
            let real = match real {
                Some(_) => measured.cells(second),
                None => Vec::new(),
            };
            let added = added.map_or_else(Vec::new, |slot| slot.data.heat_cells());
            for cells in [&real, &added] {
                if let Some(&index) = cells.iter().rev().nth(1) {
                    last_index = last_index.max(index as usize);
                }
            }
            columns.push(HeatmapColumn {
                second: second as u64,
                measured: real,
                synthetic: added,
            });
        }
        Heatmap {
            generation,
            rev,
            since,
            start_second: start as u64,
            end_second: end as u64,
            hold_from: hold_from as u64,
            buckets_us: bucket_table_us(last_index),
            columns,
        }
    }

    fn heatmap_all(
        &mut self,
        start: usize,
        end: usize,
        hold_from: usize,
        since: Option<HeatmapCursor>,
        co_active: bool,
    ) -> Heatmap {
        let synthetic = self
            .synthetic
            .as_mut()
            .filter(|_| co_active)
            .map(|(all, _)| all);
        let mut all = AllRequests {
            rings: &self.rings,
            others: &self.others,
            cells: &mut self.all_cells,
        };
        Self::heatmap(
            (self.generation, self.rev),
            &mut all,
            synthetic,
            start,
            end,
            hold_from,
            since,
        )
    }

    fn heatmap_request(
        &mut self,
        key: &str,
        start: usize,
        end: usize,
        hold_from: usize,
        since: Option<HeatmapCursor>,
        co_active: bool,
    ) -> Option<Heatmap> {
        let measured = &mut self.rings[*self.by_request.get(key)?];
        let synthetic = self
            .synthetic
            .as_mut()
            .filter(|_| co_active)
            .and_then(|(_, by_request)| by_request.get_mut(key));
        Some(Self::heatmap(
            (self.generation, self.rev),
            measured,
            synthetic,
            start,
            end,
            hold_from,
            since,
        ))
    }
}

/// The report's response time heatmap, embedded in the report as JSON for
/// its chart script: every series with whole run columns, each column's
/// cells and percentiles worked out here. `S` is the series: a
/// [`SeriesStream`] for the report, which builds each as it is written.
#[derive(Serialize)]
struct ReportHeatmap<S> {
    /// When the test started, in milliseconds since the epoch, as the
    /// report's other graphs count it.
    start: i64,
    /// The Goose host's offset from UTC at the start, in milliseconds, so
    /// the chart shows times in its zone, as the other graphs do.
    offset: i64,
    /// The second, from the start, that column 0 begins at.
    origin: usize,
    /// `[time, lowest, highest]` of each bucket index used, in
    /// microseconds.
    buckets_us: Vec<[u64; 3]>,
    series: S,
}

/// The heatmap's series, built one at a time as they are serialized, so
/// only one series' columns are held at once.
struct SeriesStream<'a> {
    latency: &'a LatencyHistograms,
    co_active: bool,
}

impl Serialize for SeriesStream<'_> {
    fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
        serializer.collect_seq(
            self.latency
                .report_sources(self.co_active)
                .map(ReportSeries::new),
        )
    }
}

/// One series of the report's heatmap.
#[derive(Serialize)]
struct ReportSeries<'a> {
    /// `All requests`, or a request's `METHOD name`.
    name: &'a str,
    /// Seconds in each column.
    width: usize,
    measured: ReportView,
    /// Measured and synthetic times together, when coordinated omission
    /// mitigation recorded synthetic times and the series has columns of
    /// them.
    adjusted: Option<ReportView>,
}

/// Where a series of the report's heatmap comes from: its name, its column
/// width, its measured columns, and its synthetic columns when it offers
/// an Adjusted view.
type ReportSource<'a> = (&'a str, usize, &'a Columns, Option<&'a Columns>);

impl<'a> ReportSeries<'a> {
    fn new((name, width, measured, added): ReportSource<'a>) -> Self {
        ReportSeries {
            name,
            width,
            measured: ReportView::new(measured, None),
            adjusted: added.map(|added| ReportView::new(measured, Some(added))),
        }
    }
}

/// The columns of one view of a series, and the sparsest and busiest cell
/// counts its colours are scaled over.
#[derive(Serialize)]
struct ReportView {
    min: usize,
    max: usize,
    /// Each column with requests, flattened: its index, its p50, p95 and
    /// p99 in microseconds, then each non empty bucket as its index, its
    /// count and the count's place from 0 (`min`) to 1000 (`max`) on a
    /// logarithmic scale.
    columns: Vec<Vec<usize>>,
}

impl ReportView {
    /// The view of `measured` and, when given, `synthetic` columns added to
    /// them.
    fn new(measured: &Columns, synthetic: Option<&Columns>) -> Self {
        let mut columns = Vec::new();
        let mut adjusted;
        let synthetic_column =
            |index: usize| synthetic.and_then(|columns| columns.columns.get(index));
        let len = measured
            .columns
            .len()
            .max(synthetic.map_or(0, |columns| columns.columns.len()));
        for index in 0..len {
            let measured = measured.columns.get(index);
            let column = match synthetic_column(index) {
                Some(added) => {
                    adjusted = measured.cloned().unwrap_or_default();
                    adjusted.merge(added);
                    &adjusted
                }
                None => match measured {
                    Some(measured) => measured,
                    None => continue,
                },
            };
            if column.counter == 0 {
                continue;
            }
            let mut flat = vec![
                index,
                column.percentile_us(0.5) as usize,
                column.percentile_us(0.95) as usize,
                column.percentile_us(0.99) as usize,
            ];
            for pair in column.heat_cells().chunks(2) {
                flat.extend([pair[0] as usize, pair[1] as usize, 0]);
            }
            columns.push(flat);
        }
        let counts = || {
            columns
                .iter()
                .flat_map(|column| column[4..].chunks(3).map(|cell| cell[1]))
        };
        let (min, max) = (counts().min().unwrap_or(0), counts().max().unwrap_or(0));
        for column in columns.iter_mut() {
            for cell in column[4..].chunks_mut(3) {
                cell[2] = Self::level(cell[1], min, max);
            }
        }
        ReportView { min, max, columns }
    }

    /// Where `count` sits from 0, for `min`, to 1000, for `max`, on a
    /// logarithmic scale, as the dashboard places its colours. When every
    /// cell holds the same count, all are the highest.
    fn level(count: usize, min: usize, max: usize) -> usize {
        if max <= min {
            return 1000;
        }
        let place = (count as f64 / min as f64).ln() / (max as f64 / min as f64).ln();
        (place.clamp(0.0, 1.0) * 1000.0).round() as usize
    }
}

impl LatencyHistograms {
    /// Where each series of the report's heatmap comes from: all requests
    /// first, then each request name with columns, by name. `co_active`
    /// says coordinated omission mitigation recorded synthetic times.
    fn report_sources(&self, co_active: bool) -> impl Iterator<Item = ReportSource<'_>> {
        let synthetic = self.synthetic.as_ref().filter(|_| co_active);
        // All requests' synthetic columns outlive a reset empty, and then
        // offer no view; a name's synthetic ring exists only once it has a
        // time.
        let all = self.all_columns.as_ref().map(|all| {
            let added = synthetic
                .and_then(|(all, _)| all.columns.as_ref())
                .filter(|columns| columns.columns.iter().any(|column| column.counter > 0));
            ("All requests", self.all_width, all, added)
        });
        let mut names: Vec<(&String, &usize)> = self.by_request.iter().collect();
        names.sort();
        let names = names.into_iter().filter_map(move |(name, &index)| {
            let measured = self.rings[index].columns.as_ref()?;
            let added = synthetic
                .and_then(|(_, by_request)| by_request.get(name))
                .and_then(|ring| ring.columns.as_ref());
            Some((name.as_str(), self.request_width, measured, added))
        });
        all.into_iter().chain(names)
    }

    /// The report's heatmap, when whole run columns are kept, with `series`
    /// as its series.
    fn report_heatmap_with<S>(
        &self,
        co_active: bool,
        started: DateTime<Utc>,
        series: S,
    ) -> Option<ReportHeatmap<S>> {
        self.all_columns.as_ref()?;
        let last = self
            .report_sources(co_active)
            .flat_map(|(_, _, measured, added)| [Some(measured), added])
            .flatten()
            .filter_map(Columns::last_bucket)
            .max()
            .unwrap_or(0);
        Some(ReportHeatmap {
            start: started.timestamp() * 1000,
            offset: i64::from(
                Local
                    .offset_from_utc_datetime(&started.naive_utc())
                    .local_minus_utc(),
            ) * 1000,
            origin: self.origin,
            buckets_us: bucket_table_us(last),
            series,
        })
    }

    /// The report's heatmap with every series built (unit tests).
    #[cfg(test)]
    fn report_heatmap(
        &self,
        co_active: bool,
        started: DateTime<Utc>,
    ) -> Option<ReportHeatmap<Vec<ReportSeries<'_>>>> {
        let series = self
            .report_sources(co_active)
            .map(ReportSeries::new)
            .collect();
        self.report_heatmap_with(co_active, started, series)
    }
}

/// Writes JSON that is safe inside a `<script>` element: `<`, `>` and `&`
/// escaped, so no string in it can close the element or start markup, and
/// U+2028 and U+2029 escaped, which older script parsers end a line at.
/// Only strings can hold them, so only string fragments are checked.
struct ScriptFormatter;

impl serde_json::ser::Formatter for ScriptFormatter {
    fn write_string_fragment<W: ?Sized + std::io::Write>(
        &mut self,
        writer: &mut W,
        fragment: &str,
    ) -> std::io::Result<()> {
        let mut from = 0;
        for (at, c) in fragment.char_indices() {
            let escaped = match c {
                '<' => "\\u003c",
                '>' => "\\u003e",
                '&' => "\\u0026",
                '\u{2028}' => "\\u2028",
                '\u{2029}' => "\\u2029",
                _ => continue,
            };
            writer.write_all(&fragment.as_bytes()[from..at])?;
            writer.write_all(escaped.as_bytes())?;
            from = at + c.len_utf8();
        }
        writer.write_all(&fragment.as_bytes()[from..])
    }
}

/// The report heatmap's chart script. It reads the data from the JSON
/// element before it and draws the selected series with an ECharts
/// `custom` series, one rectangle per cell. Request names reach the page
/// only as the text of the select's options.
const REPORT_HEATMAP_SCRIPT: &str = include_str!("report/heatmap.js");

/// The report heatmap card's markup before its JSON.
const REPORT_HEATMAP_HEAD: &str = r#"<div class="graph heatmap">
                <style>
                    .graph.heatmap {
                        break-inside: avoid;
                    }
                    .heatmap-controls button {
                        border: 1px solid #2c664f;
                        background: white;
                        padding: 0.2em 0.8em;
                    }
                    .heatmap-controls button.active {
                        background: #2c664f;
                        color: white;
                    }
                    .heatmap-legend {
                        display: flex;
                        align-items: center;
                        gap: 0.5em;
                        width: 1000px;
                        font-size: 0.9em;
                    }
                    .heatmap-ramp {
                        display: inline-block;
                        width: 200px;
                        height: 0.8em;
                        print-color-adjust: exact;
                        -webkit-print-color-adjust: exact;
                    }
                </style>
                <h3>Response time heatmap</h3>
                <div class="heatmap-controls">
                    <label>Series <select id="graph-latency-heatmap-series"></select></label>
                    <span id="graph-latency-heatmap-views" hidden>
                        <button type="button" data-view="adjusted">Adjusted</button>
                        <button type="button" data-view="measured">Measured</button>
                    </span>
                </div>
                <p id="graph-latency-heatmap-width" class="heatmap-note"></p>
                <div id="graph-latency-heatmap" style="width: 1000px; height:500px; background: white;"></div>
                <div class="heatmap-legend">
                    <span id="graph-latency-heatmap-min"></span>
                    <span id="graph-latency-heatmap-ramp" class="heatmap-ramp"></span>
                    <span id="graph-latency-heatmap-max"></span>
                </div>
                <script type="application/json" id="graph-latency-heatmap-data">"#;
/// The markup between the card's JSON and its chart script.
const REPORT_HEATMAP_MIDDLE: &str = r#"</script>
                <script type="text/javascript">"#;
/// The markup after the card's chart script.
const REPORT_HEATMAP_TAIL: &str = r#"</script>
            </div>"#;

/// The report heatmap's card, with `heatmap` as its JSON, or nothing when it
/// can't be written.
pub(crate) fn heatmap_card<H: Serialize + ?Sized>(heatmap: &H) -> String {
    let mut card = REPORT_HEATMAP_HEAD.as_bytes().to_vec();
    let mut serializer = serde_json::Serializer::with_formatter(&mut card, ScriptFormatter);
    if heatmap.serialize(&mut serializer).is_err() {
        return String::new();
    }
    card.extend_from_slice(REPORT_HEATMAP_MIDDLE.as_bytes());
    card.extend_from_slice(REPORT_HEATMAP_SCRIPT.as_bytes());
    card.extend_from_slice(REPORT_HEATMAP_TAIL.as_bytes());
    card.shrink_to_fit();
    String::from_utf8(card).unwrap_or_default()
}

/// The per second data the HTML report graphs, as the JSON report holds it
/// with `--report-graph-data`: each series as its first second and its
/// values, request names in order, and the heatmap as the HTML report embeds
/// it.
#[derive(Serialize)]
pub(crate) struct ReportGraphs<'a> {
    requests_per_second: BTreeMap<&'a str, &'a TimeSeries<u32, u32>>,
    errors_per_second: BTreeMap<&'a str, &'a TimeSeries<u32, u32>>,
    average_response_time: BTreeMap<&'a str, &'a TimeSeries<MovingAverage, f32>>,
    transactions_per_second: &'a TimeSeries<usize, usize>,
    scenarios_per_second: &'a TimeSeries<usize, usize>,
    users_per_second: &'a TimeSeries<usize, usize>,
    #[serde(skip_serializing_if = "Option::is_none")]
    heatmap: Option<ReportGraphsHeatmap<'a>>,
}

/// The heatmap in the JSON report: built from the histograms, or read back
/// from a JSON report.
enum ReportGraphsHeatmap<'a> {
    Live(ReportHeatmap<SeriesStream<'a>>),
    Recorded(&'a serde_json::Value),
}

impl Serialize for ReportGraphsHeatmap<'_> {
    fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
        match self {
            ReportGraphsHeatmap::Live(heatmap) => heatmap.serialize(serializer),
            ReportGraphsHeatmap::Recorded(heatmap) => heatmap.serialize(serializer),
        }
    }
}

/// The graph data of a JSON report read back, to render its HTML report.
#[derive(Deserialize)]
pub(crate) struct RecordedGraphs {
    #[serde(default)]
    requests_per_second: HashMap<String, TimeSeries<u32, u32>>,
    #[serde(default)]
    errors_per_second: HashMap<String, TimeSeries<u32, u32>>,
    #[serde(default)]
    average_response_time: HashMap<String, TimeSeries<MovingAverage, f32>>,
    #[serde(default = "TimeSeries::new")]
    transactions_per_second: TimeSeries<usize, usize>,
    #[serde(default = "TimeSeries::new")]
    scenarios_per_second: TimeSeries<usize, usize>,
    #[serde(default = "TimeSeries::new")]
    users_per_second: TimeSeries<usize, usize>,
    #[serde(default)]
    heatmap: Option<serde_json::Value>,
}

impl RecordedGraphs {
    /// The graph data, and the heatmap as the JSON report holds it.
    pub(crate) fn into_parts(self) -> (GraphData, Option<serde_json::Value>) {
        let items = |series: HashMap<String, TimeSeries<u32, u32>>| ItemsPerSecond {
            series: series
                .into_iter()
                .map(|(name, counts)| {
                    (
                        name,
                        KeySeries {
                            counts,
                            pending: None,
                        },
                    )
                })
                .collect(),
            origin: 0,
        };
        let graph_data = GraphData {
            requests_per_second: items(self.requests_per_second),
            errors_per_second: items(self.errors_per_second),
            average_response_time_per_second: self.average_response_time,
            transactions_per_second: self.transactions_per_second,
            scenarios_per_second: self.scenarios_per_second,
            users_per_second: self.users_per_second,
            latency: None,
        };
        (graph_data, self.heatmap)
    }
}

/// Used to collect graph data during a load test. Cloned for Save report now,
/// which writes a report while the run goes on.
#[derive(Clone)]
pub(crate) struct GraphData {
    /// Counts requests per second for each request type.
    requests_per_second: ItemsPerSecond,
    /// Counts errors per second.
    errors_per_second: ItemsPerSecond,
    /// Maintains average response time per second.
    average_response_time_per_second: HashMap<String, TimeSeries<MovingAverage, f32>>,
    /// Number of transactions at the end of each second of the test.
    transactions_per_second: TimeSeries<usize, usize>,
    /// Number of scenarios at the end of each second of the test.
    scenarios_per_second: TimeSeries<usize, usize>,
    /// Number of users at the end of each second of the test.
    users_per_second: TimeSeries<usize, usize>,
    /// Response time histograms by second for the dashboard's heatmap.
    /// Only the metrics processor's copy has them, and only while the
    /// dashboard tracks percentiles over time.
    latency: Option<LatencyHistograms>,
}

impl GraphData {
    /// Create a new GraphData object.
    pub(crate) fn new() -> Self {
        trace!("new graph");
        GraphData {
            requests_per_second: ItemsPerSecond::new(),
            errors_per_second: ItemsPerSecond::new(),
            average_response_time_per_second: HashMap::new(),
            transactions_per_second: TimeSeries::new(),
            scenarios_per_second: TimeSeries::new(),
            users_per_second: TimeSeries::new(),
            latency: None,
        }
    }

    /// Create GraphData for the metrics processor: with latency histograms
    /// when `latency_series` is set, synthetic ones too when
    /// `synthetic_latency` (coordinated omission mitigation is on), and
    /// whole run columns for the report's heatmap when `latency_columns`.
    pub(crate) fn for_metrics_processor(
        latency_series: bool,
        synthetic_latency: bool,
        latency_columns: bool,
    ) -> Self {
        let mut graph_data = GraphData::new();
        if latency_series {
            graph_data.latency = Some(LatencyHistograms::new(synthetic_latency, latency_columns));
        }
        graph_data
    }

    /// Reset per-second *rate* series after the increase→maintain metrics reset.
    ///
    /// Request/error/latency/transaction/scenario buckets restart so running
    /// metrics and HTML report rates reflect steady-state load only.
    ///
    /// **Active users are a level over the whole run**, not a rate: the users
    /// series is preserved so the live dashboard (and HTML active-users graph)
    /// still show the initial ramp and any later increases/decreases. Wiping
    /// users and reseeding second 0 with the final count made the chart look
    /// like the test had always been at full load.
    ///
    /// When dashboard pruning has already advanced the users series `origin`,
    /// rate series are recreated with that same origin so the next sample at
    /// absolute second *S* does not expand vectors from 0..S.
    ///
    /// The heatmap's whole run columns restart from `series_elapsed_secs`,
    /// the reset's second on the series clock.
    pub(crate) fn reset_rate_series(&mut self, series_elapsed_secs: usize) {
        let origin = self.users_per_second.origin;
        self.requests_per_second = ItemsPerSecond::with_origin(origin);
        self.errors_per_second = ItemsPerSecond::with_origin(origin);
        self.average_response_time_per_second = HashMap::new();
        self.transactions_per_second = TimeSeries::with_origin(origin);
        self.scenarios_per_second = TimeSeries::with_origin(origin);
        if let Some(latency) = self.latency.as_mut() {
            latency.clear(series_elapsed_secs);
        }
    }

    /// Record one measured response time in whole milliseconds for the
    /// heatmap, without counting a request (unit tests).
    #[cfg(test)]
    pub(crate) fn record_latency(&mut self, key: &str, second: usize, time: u64) {
        self.record_latency_us(key, second, time.saturating_mul(1000));
    }

    /// [`Self::record_latency`] in microseconds (unit tests).
    #[cfg(test)]
    pub(crate) fn record_latency_us(&mut self, key: &str, second: usize, us: u64) {
        if let Some(latency) = self.latency.as_mut() {
            latency.record(key, second, us);
        }
    }

    /// [`Self::record_request_per_second_and_latency_us`] with the response
    /// time in whole milliseconds (unit tests).
    #[cfg(test)]
    pub(crate) fn record_request_per_second_and_latency(
        &mut self,
        key: &str,
        second: usize,
        latency: Option<u64>,
    ) {
        self.record_request_per_second_and_latency_us(
            key,
            second,
            latency.map(|time| time.saturating_mul(1000)),
        );
    }

    /// Count one request in `second` and, when `latency` (in microseconds)
    /// is given and the histograms are kept, record its response time for
    /// the heatmap.
    pub(crate) fn record_request_per_second_and_latency_us(
        &mut self,
        key: &str,
        second: usize,
        latency: Option<u64>,
    ) {
        let data = self.requests_per_second.increment(key, second, 1);
        debug!(
            "incremented second {second} for requests per second counter: {}",
            data.get(second)
        );
        if let (Some(time), Some(histograms)) = (latency, self.latency.as_mut()) {
            histograms.record_in(key, data, second, time);
        }
    }

    /// Merge a batch's count of requests in `second` and, when the
    /// histograms are kept and `times` is given, their response times (the
    /// batch's timing data for this request, every time from `second`),
    /// keeping them beside the count the same lookup finds.
    pub(crate) fn record_requests_per_second_and_latency_batch(
        &mut self,
        key: &str,
        second: usize,
        count: u32,
        times: Option<&GooseRequestMetricTimingData>,
    ) {
        let data = self.requests_per_second.increment(key, second, count);
        if let (Some(times), Some(histograms)) = (times, self.latency.as_mut()) {
            if times.counter > 0 {
                histograms.merge_pending(key, data, second, times);
            }
        }
    }

    /// Count a batch's requests for `key` in `second`, and merge their
    /// response times (the batch's timing data for the request, all from
    /// `second`) into `whole_run`, the request's timing data for the whole
    /// run, and, when the histograms are kept, into its pending counts,
    /// walking the batch's buckets once for both.
    pub(crate) fn record_requests_per_second_merging_latency(
        &mut self,
        key: &str,
        second: usize,
        data: &GooseRequestMetricTimingData,
        whole_run: &mut GooseRequestMetricTimingData,
    ) {
        let entry = self
            .requests_per_second
            .increment(key, second, data.counter as u32);
        let Some(histograms) = self.latency.as_mut().filter(|_| data.counter > 0) else {
            return whole_run.merge(data);
        };
        let pending = histograms.pending(key, entry);
        match histograms.pending_at(pending, second) {
            Some(at) => {
                pending.add_totals(at, data);
                whole_run.merge_also(data, |bucket, count| pending.add_bucket(at, bucket, count));
            }
            None => {
                histograms.merge_older(pending, second, data);
                whole_run.merge(data);
            }
        }
    }

    /// Move every request's pending response times to its ring, so the
    /// rings hold every time merged so far.
    fn move_pending_latency(&mut self) {
        let Some(histograms) = self.latency.as_mut() else {
            return;
        };
        for entry in self.requests_per_second.series.values_mut() {
            if let Some(pending) = entry.pending.as_deref_mut().filter(|pending| pending.held) {
                histograms.move_pending(pending);
            }
        }
    }

    /// Before a batch writes response times from seconds up to `second`:
    /// widen the heatmap's whole run columns if they do not reach it.
    pub(crate) fn fit_latency_columns(&mut self, second: usize) {
        if let Some(histograms) = self.latency.as_mut() {
            histograms.fit_columns(second);
        }
    }

    /// The first second of the run the graph data holds: 0, unless the
    /// dashboard's window dropped older seconds.
    pub(crate) fn first_second(&self) -> usize {
        self.users_per_second.origin
    }

    /// Whether the heatmap keeps whole run columns (unit tests).
    #[cfg(test)]
    pub(crate) fn has_latency_columns_for_test(&self) -> bool {
        self.latency
            .as_ref()
            .is_some_and(|latency| latency.all_columns.is_some())
    }

    /// Start or stop keeping the report heatmap's whole run columns while the
    /// run goes on, as Save report is turned on or off; `now` is the current
    /// second on the series clock.
    pub(crate) fn keep_latency_columns(&mut self, keep: bool, now: usize) {
        if keep {
            // Every response time held moves to its ring first, so the
            // columns take it.
            self.move_pending_latency();
        }
        if let Some(histograms) = self.latency.as_mut() {
            histograms.keep_columns(keep, now);
        }
    }

    /// At the end of the run: move every pending response time to its ring
    /// and pack every open second, so the heatmap's whole run columns hold
    /// every time recorded.
    pub(crate) fn finish_latency_columns(&mut self) {
        self.move_pending_latency();
        if let Some(histograms) = self.latency.as_mut() {
            histograms.pack_open();
        }
    }

    /// The report's response time heatmap card, or nothing when whole run
    /// columns are not kept. `co_active` says coordinated omission
    /// mitigation recorded synthetic times, so the card offers Adjusted and
    /// Measured views; `started` is when the test started.
    pub(crate) fn get_latency_heatmap_markup(
        &self,
        co_active: bool,
        started: DateTime<Utc>,
    ) -> String {
        // A run with no requests since the start or the last reset has no
        // heatmap to show.
        let Some(latency) = self.latency.as_ref().filter(|latency| {
            latency
                .all_columns
                .as_ref()
                .is_some_and(|all| all.columns.iter().any(|column| column.counter > 0))
        }) else {
            return String::new();
        };
        let Some(heatmap) =
            latency.report_heatmap_with(co_active, started, SeriesStream { latency, co_active })
        else {
            return String::new();
        };
        // The JSON goes straight into the card, one series at a time, so
        // no other copy of it, or of every series' columns, is ever held.
        heatmap_card(&heatmap)
    }

    /// The report's heatmap, for the JSON report, when whole run columns are
    /// kept and hold a response time.
    fn report_heatmap(
        &self,
        co_active: bool,
        started: DateTime<Utc>,
    ) -> Option<ReportHeatmap<SeriesStream<'_>>> {
        let latency = self.latency.as_ref().filter(|latency| {
            latency
                .all_columns
                .as_ref()
                .is_some_and(|all| all.columns.iter().any(|column| column.counter > 0))
        })?;
        latency.report_heatmap_with(co_active, started, SeriesStream { latency, co_active })
    }

    /// The per second data the HTML report graphs, for the JSON report. A
    /// heatmap read back from a JSON report, `recorded`, goes in as it is.
    pub(crate) fn report_graphs<'a>(
        &'a self,
        co_active: bool,
        started: DateTime<Utc>,
        recorded: Option<&'a serde_json::Value>,
    ) -> ReportGraphs<'a> {
        let counts = |items: &'a ItemsPerSecond| {
            items
                .series
                .iter()
                .map(|(name, series)| (name.as_str(), &series.counts))
                .collect()
        };
        ReportGraphs {
            requests_per_second: counts(&self.requests_per_second),
            errors_per_second: counts(&self.errors_per_second),
            average_response_time: self
                .average_response_time_per_second
                .iter()
                .map(|(name, series)| (name.as_str(), series))
                .collect(),
            transactions_per_second: &self.transactions_per_second,
            scenarios_per_second: &self.scenarios_per_second,
            users_per_second: &self.users_per_second,
            heatmap: match recorded {
                Some(recorded) => Some(ReportGraphsHeatmap::Recorded(recorded)),
                None => self
                    .report_heatmap(co_active, started)
                    .map(ReportGraphsHeatmap::Live),
            },
        }
    }

    /// Whether response times are kept by second for the heatmaps.
    pub(crate) fn records_latency(&self) -> bool {
        self.latency.is_some()
    }

    /// Record one synthetic response time from coordinated omission
    /// mitigation, in whole milliseconds as mitigation makes them, when
    /// synthetic histograms are kept.
    pub(crate) fn record_synthetic_latency(&mut self, key: &str, second: usize, time: u64) {
        if let Some(latency) = self.latency.as_mut() {
            latency.record_synthetic(key, second, time.saturating_mul(1000));
        }
    }

    /// Whether synthetic response times are kept for the heatmap.
    pub(crate) fn records_synthetic_latency(&self) -> bool {
        self.latency
            .as_ref()
            .is_some_and(|latency| latency.synthetic.is_some())
    }

    /// Keys (`"METHOD name"`) of the requests with their own latency series.
    pub(crate) fn latency_series_keys(&self) -> HashSet<&str> {
        self.latency
            .as_ref()
            .map(|latency| latency.by_request.keys().map(String::as_str).collect())
            .unwrap_or_default()
    }

    /// The latency histograms, if kept (unit tests).
    #[cfg(test)]
    pub(crate) fn latency_histograms(&mut self) -> Option<&LatencyHistograms> {
        self.latency.as_ref()
    }

    /// The measured histogram for `second`: all requests when `key` is
    /// `None`, otherwise `key`'s own ring (unit tests).
    #[cfg(test)]
    pub(crate) fn latency_second_for_test(
        &mut self,
        key: Option<&str>,
        second: usize,
    ) -> Option<SecondHistogram> {
        self.move_pending_latency();
        let latency = self.latency.as_ref()?;
        match key {
            None => latency.all_second(second),
            Some(key) => latency.rings[*latency.by_request.get(key)?]
                .get(second)
                .cloned(),
        }
    }

    /// GraphData keeping whole run columns with budgets of `all` columns for
    /// all requests and `request` for each name, and synthetic columns too
    /// when `synthetic` (unit tests).
    #[cfg(test)]
    pub(crate) fn with_latency_budgets(synthetic: bool, all: usize, request: usize) -> Self {
        let mut graph = GraphData::new();
        graph.latency = Some(LatencyHistograms::with_budgets(synthetic, all, request));
        graph
    }

    /// The whole run columns of all requests (`key` `None`) or of `key`, as
    /// `(column, requests)` for each non empty column, and their width
    /// (unit tests).
    #[cfg(test)]
    pub(crate) fn latency_columns_for_test(
        &self,
        key: Option<&str>,
    ) -> Option<(usize, Vec<(usize, usize)>)> {
        let latency = self.latency.as_ref()?;
        let (columns, width) = match key {
            None => (latency.all_columns.as_ref()?, latency.all_width),
            Some(key) => (
                latency.rings[*latency.by_request.get(key)?]
                    .columns
                    .as_ref()?,
                latency.request_width,
            ),
        };
        let counts = columns
            .columns
            .iter()
            .enumerate()
            .filter(|(_, column)| column.counter > 0)
            .map(|(index, column)| (index, column.counter))
            .collect();
        Some((width, counts))
    }

    /// The average response time for `key` in `second`, if recorded (unit
    /// tests).
    #[cfg(test)]
    pub(crate) fn average_in_second_for_test(&self, key: &str, second: usize) -> Option<f32> {
        self.average_response_time_per_second
            .get(key)
            .map(|series| series.get(second))
            .filter(|average| average.count > 0)
            .map(|average| average.average)
    }

    /// How many response times the average for `key` in `second` holds
    /// (unit tests).
    #[cfg(test)]
    pub(crate) fn average_count_in_second_for_test(&self, key: &str, second: usize) -> u32 {
        self.average_response_time_per_second
            .get(key)
            .map_or(0, |series| series.get(second).count)
    }

    /// The requests per second count for `key` in `second` (unit tests).
    #[cfg(test)]
    pub(crate) fn requests_in_second_for_test(&self, key: &str, second: usize) -> u32 {
        self.requests_per_second
            .series
            .get(key)
            .map_or(0, |series| series.get(second))
    }

    /// The errors per second count for `key` in `second` (unit tests).
    #[cfg(test)]
    pub(crate) fn errors_in_second_for_test(&self, key: &str, second: usize) -> u32 {
        self.errors_per_second
            .series
            .get(key)
            .map_or(0, |series| series.get(second))
    }

    /// Last absolute second with a users sample, if any.
    pub(crate) fn users_last_absolute_second(&self) -> Option<usize> {
        self.users_per_second.last_absolute_second()
    }

    /// Record requests per second metric, without latency (unit tests).
    #[cfg(test)]
    pub(crate) fn record_requests_per_second(&mut self, key: &str, second: usize) {
        let value = self
            .requests_per_second
            .initialize_or_increment(key, second, 1);
        debug!("incremented second {second} for requests per second counter: {value}");
    }

    /// Record errors per second metric.
    pub(crate) fn record_errors_per_second(&mut self, key: &str, second: usize) {
        let value = self
            .errors_per_second
            .initialize_or_increment(key, second, 1);

        debug!("incremented second {second} for errors per second counter: {value}");
    }

    /// Take back an error recorded in `second`, for a failed request that
    /// `set_success()` marked a success. Does nothing when the second is no
    /// longer held.
    pub(crate) fn remove_error_per_second(&mut self, key: &str, second: usize) {
        if let Some(series) = self.errors_per_second.series.get_mut(key) {
            series.decrement(second);
        }
    }

    /// Record average response time per second metric.
    #[cfg(test)]
    pub(crate) fn record_average_response_time_per_second(
        &mut self,
        key: String,
        second: usize,
        response_time: u64,
    ) {
        self.record_average_response_time_per_second_us(
            key,
            second,
            response_time.saturating_mul(1000),
        );
    }

    /// Record one response time, in microseconds, in the average response
    /// time per second, which is in milliseconds.
    pub(crate) fn record_average_response_time_per_second_us(
        &mut self,
        key: String,
        second: usize,
        us: u64,
    ) {
        let origin = self.users_per_second.origin;
        let data = self
            .average_response_time_per_second
            .entry(key)
            .or_insert_with(|| TimeSeries::with_origin(origin));
        data.increase_value(second, (us as f64 / 1000.0) as f32);

        debug!(
            "updated second {} for average response time per second: {}",
            second,
            data.get(second).average
        );
    }

    /// Record transactions per second metric.
    pub(crate) fn record_transactions_per_second(&mut self, second: usize) {
        self.transactions_per_second.increase_value(second, 1);

        debug!(
            "incremented second {} for transactions per second counter: {}",
            second,
            self.transactions_per_second.get(second)
        );
    }

    /// Record scenarios per second metric.
    pub(crate) fn record_scenarios_per_second(&mut self, second: usize) {
        self.scenarios_per_second.increase_value(second, 1);

        debug!(
            "incremented second {} for scenarios per second counter: {}",
            second,
            self.scenarios_per_second.get(second)
        );
    }

    /// Record pre-aggregated average response time for batch merging.
    ///
    /// Performs a proper weighted merge of the batch's total response time and
    /// count into the per-second moving average, avoiding the precision loss
    /// that would result from truncating the average to an integer. `next`
    /// is the batch's total response time and count for `second + 1`, merged
    /// through the same lookup.
    pub(crate) fn record_average_response_time_per_second_batch(
        &mut self,
        key: &str,
        second: usize,
        total_time: f64,
        count: u32,
    ) {
        if count == 0 {
            return;
        }
        let origin = self.users_per_second.origin;
        let data = self
            .average_response_time_per_second
            .entry(key.to_string())
            .or_insert_with(|| TimeSeries::with_origin(origin));
        let batch_avg = MovingAverage {
            count,
            average: total_time as f32 / count as f32,
        };
        data.merge_value(second, &batch_avg);
    }

    /// Record multiple transactions per second for batch merging.
    pub(crate) fn record_transactions_per_second_batch(&mut self, second: usize, count: usize) {
        self.transactions_per_second.increase_value(second, count);
    }

    /// Record multiple scenarios per second for batch merging.
    pub(crate) fn record_scenarios_per_second_batch(&mut self, second: usize, count: usize) {
        self.scenarios_per_second.increase_value(second, count);
    }

    /// Records number of users for a current second.
    pub(crate) fn record_users_per_second(&mut self, users: usize, second: usize) {
        self.users_per_second.set_and_maintain_last(second, users);
    }

    /// Record active users at `second` only if time is not going backwards.
    ///
    /// Used by the dashboard snapshot path to pin the current level at "now"
    /// without rewinding past samples (which would recreate mid-ramp V-glitches
    /// when combined with gap-fill).
    pub(crate) fn record_users_monotonic(&mut self, users: usize, second: usize) {
        if let Some(last) = self.users_per_second.last_absolute_second() {
            if second < last {
                return;
            }
        }
        self.users_per_second.set_and_maintain_last(second, users);
    }

    /// Convenience wrapper for unit tests (no end-second hint).
    ///
    /// Production paths always pass a series-clock hint via
    /// [`Self::export_series_window_until`].
    #[cfg(test)]
    pub(crate) fn export_series_window(&mut self, window_secs: u32) -> SeriesWindow {
        self.export_series_window_until(window_secs, None)
    }

    /// [`Self::export_series_window_with_heatmap`] for a client with no
    /// heatmap yet and coordinated omission mitigation without events (unit
    /// tests).
    #[cfg(test)]
    pub(crate) fn export_series_window_until(
        &mut self,
        window_secs: u32,
        end_second_hint: Option<usize>,
    ) -> SeriesWindow {
        self.export_series_window_with_heatmap(window_secs, end_second_hint, false, None)
    }

    /// The trailing window's first and last second, as
    /// [`Self::export_series_window_with_heatmap`] defines them, or `None`
    /// before anything is recorded.
    fn series_window_bounds(
        &self,
        window_secs: u32,
        end_second_hint: Option<usize>,
    ) -> Option<(usize, usize)> {
        if window_secs == 0 {
            return None;
        }
        let mut end_second: Option<usize> = end_second_hint;
        for series in self.requests_per_second.series.values() {
            if let Some(last) = series.last_absolute_second() {
                end_second = Some(end_second.map_or(last, |e| e.max(last)));
            }
        }
        if let Some(last) = self.users_per_second.last_absolute_second() {
            end_second = Some(end_second.map_or(last, |e| e.max(last)));
        }
        let end_second = end_second?;
        Some((
            end_second.saturating_sub(window_secs as usize - 1),
            end_second,
        ))
    }

    /// First second the heatmap does not draw yet: the newest seconds may
    /// still be missing their slowest requests.
    fn latency_hold_from(end_second: usize, end_second_hint: Option<usize>) -> usize {
        end_second_hint.unwrap_or(end_second).saturating_sub(1)
    }

    /// Export a trailing aggregate series window for the live dashboard.
    ///
    /// Normative algorithm:
    /// 1. `end_second` = max second index across RPS keys, users series, and
    ///    optional `end_second_hint` (series clock "now" from the main loop)
    /// 2. `start_second` = `end_second.saturating_sub(window_secs - 1)`
    /// 3. Allocate fixed-length RPS/FPS/latency buffers; for each per-key series
    ///    add its overlapping range into those buffers (and accumulate
    ///    request-count-weighted latency the same way)
    /// 4. Missing RPS/error/latency seconds contribute 0
    /// 5. **Users are a level, not a rate**: missing samples (including trailing
    ///    seconds where request metrics advanced past the last `RecordUsers`)
    ///    hold the last known active-user count — never drop to 0 solely because
    ///    the users series lags RPS by a few seconds.
    /// 6. With latency histograms kept, `heatmap` covers the same seconds:
    ///    the seconds that changed after `heatmap_since` when it is a cursor
    ///    of these histograms, otherwise every second, with synthetic times
    ///    only when `co_active` (coordinated omission mitigation has recorded
    ///    an event) and synthetic histograms are kept. Without latency
    ///    histograms it is `None`.
    ///
    /// `end_second_hint` extends the window through series-clock "now" when the
    /// last stored sample is a few seconds older (main-loop cadence / no recent
    /// requests).
    pub(crate) fn export_series_window_with_heatmap(
        &mut self,
        window_secs: u32,
        end_second_hint: Option<usize>,
        co_active: bool,
        heatmap_since: Option<HeatmapCursor>,
    ) -> SeriesWindow {
        self.move_pending_latency();
        let Some((start_second, end_second)) =
            self.series_window_bounds(window_secs, end_second_hint)
        else {
            let mut empty = SeriesWindow::empty();
            empty.heatmap = self
                .latency
                .as_mut()
                .map(|latency| latency.heatmap_all(0, 0, 0, heatmap_since, co_active));
            return empty;
        };
        let len = end_second - start_second + 1;

        // Allocate fixed-length outputs once, then accumulate per key into the
        // overlapping range. This is O(stored_samples_in_window) rather than
        // O(window_secs × unique_keys), which matters at ~1 Hz with many request
        // names.
        let mut rps = vec![0.0f64; len];
        let mut fps = vec![0.0f64; len];
        let mut avg_latency_ms = vec![0.0f64; len];
        let mut latency_weight = vec![0u64; len];

        for series in self.requests_per_second.series.values() {
            accumulate_rate_series_into_window(series, start_second, end_second, &mut rps);
        }
        for series in self.errors_per_second.series.values() {
            accumulate_rate_series_into_window(series, start_second, end_second, &mut fps);
        }

        // Weighted latency: for each key, walk the overlap of its RPS and latency
        // series with the export window and add into fixed output vectors.
        for (key, latency_series) in &self.average_response_time_per_second {
            let Some(rps_series) = self.requests_per_second.series.get(key) else {
                continue;
            };
            if latency_series.data.is_empty() || rps_series.data.is_empty() {
                continue;
            }
            let series_start = latency_series
                .origin
                .max(rps_series.origin)
                .max(start_second);
            let series_end = (latency_series.origin + latency_series.data.len() - 1)
                .min(rps_series.origin + rps_series.data.len() - 1)
                .min(end_second);
            if series_start > series_end {
                continue;
            }
            for s in series_start..=series_end {
                let count = rps_series.data[s - rps_series.origin] as u64;
                if count == 0 {
                    continue;
                }
                let avg = latency_series.data[s - latency_series.origin].average as f64;
                let idx = s - start_second;
                avg_latency_ms[idx] += avg * count as f64;
                latency_weight[idx] += count;
            }
        }
        for i in 0..len {
            if latency_weight[i] > 0 {
                avg_latency_ms[i] /= latency_weight[i] as f64;
            }
        }

        // Level hold: do not paint "0 users" for trailing seconds where only
        // RPS has advanced (main-loop RecordUsers lags drain_pending). Users are
        // a single series so O(window) is fine.
        let mut users = Vec::with_capacity(len);
        for s in start_second..=end_second {
            users.push(self.users_per_second.get_held(s) as u64);
        }

        let hold_from = Self::latency_hold_from(end_second, end_second_hint);
        let heatmap = self.latency.as_mut().map(|latency| {
            latency.heatmap_all(
                start_second,
                end_second,
                hold_from,
                heatmap_since,
                co_active,
            )
        });

        SeriesWindow {
            start_second: start_second as u64,
            rps,
            fps,
            users,
            avg_latency_ms,
            heatmap,
        }
    }

    /// One request's heatmap for `GET /api/v1/series`, over the same seconds
    /// as [`Self::export_series_window_with_heatmap`] and sent the same way.
    /// `None` when the request has no histograms of its own.
    pub(crate) fn export_request_series(
        &mut self,
        method: String,
        name: String,
        window_secs: u32,
        end_second_hint: Option<usize>,
        co_active: bool,
        since: Option<HeatmapCursor>,
    ) -> Option<RequestSeries> {
        self.latency.as_ref()?;
        self.move_pending_latency();
        let key = format!("{method} {name}");
        let (start_second, end_second) = self
            .series_window_bounds(window_secs, end_second_hint)
            .unwrap_or((0, 0));
        let latency = self.latency.as_mut()?;
        let heatmap = latency.heatmap_request(
            &key,
            start_second,
            end_second,
            Self::latency_hold_from(end_second, end_second_hint),
            since,
            co_active,
        )?;
        Some(RequestSeries {
            method,
            name,
            heatmap,
        })
    }

    /// Drop series samples older than the trailing `keep_secs` window.
    ///
    /// Used when the live dashboard is enabled without `--report-file`, so
    /// multi-hour soaks do not retain per-second history that the dashboard
    /// never exports (export window is [`SERIES_WINDOW_SECS`](crate::metrics::dashboard_snapshot::SERIES_WINDOW_SECS)).
    /// No-op when `keep_secs == 0`.
    ///
    /// After length-based pruning, all per-key series are **aligned** to
    /// `users_per_second.origin`. Length-only prune leaves idle keys (short
    /// `data`, origin still 0) alone; if such a key becomes active again at a
    /// large absolute second, `increase_value` would expand from the stale
    /// origin and defeat the memory bound. Alignment drops pre-window samples
    /// and advances each series origin to the retained window.
    pub(crate) fn prune_to_window(&mut self, keep_secs: u32) {
        let keep = keep_secs as usize;
        if keep == 0 {
            return;
        }
        for series in self.requests_per_second.series.values_mut() {
            series.prune_to_window(keep);
        }
        for series in self.errors_per_second.series.values_mut() {
            series.prune_to_window(keep);
        }
        for series in self.average_response_time_per_second.values_mut() {
            series.prune_to_window(keep);
        }
        self.transactions_per_second.prune_to_window(keep);
        self.scenarios_per_second.prune_to_window(keep);
        self.users_per_second.prune_to_window(keep);

        // Align every series (and the ItemsPerSecond new-key origin) to the
        // retained users window so idle keys cannot re-expand from second 0.
        let origin = self.users_per_second.origin;
        for series in self.requests_per_second.series.values_mut() {
            series.align_origin_to(origin);
        }
        for series in self.errors_per_second.series.values_mut() {
            series.align_origin_to(origin);
        }
        for series in self.average_response_time_per_second.values_mut() {
            series.align_origin_to(origin);
        }
        self.transactions_per_second.align_origin_to(origin);
        self.scenarios_per_second.align_origin_to(origin);
        self.requests_per_second.origin = origin;
        self.errors_per_second.origin = origin;
    }

    /// Max stored samples across RPS/users series (unit tests / diagnostics).
    #[cfg(test)]
    pub(crate) fn max_series_len_for_test(&self) -> usize {
        let mut max_len = self.users_per_second.data.len();
        for series in self.requests_per_second.series.values() {
            max_len = max_len.max(series.data.len());
        }
        for series in self.errors_per_second.series.values() {
            max_len = max_len.max(series.data.len());
        }
        max_len
            .max(self.transactions_per_second.data.len())
            .max(self.scenarios_per_second.data.len())
    }

    /// Generate active users graph.
    pub(crate) fn get_active_users_graph(&self, granular_data: bool) -> Graph<'_, usize, usize> {
        self.create_graph_from_single_data(
            "graph-active-users",
            "Active users #",
            granular_data,
            &self.users_per_second,
        )
    }

    /// Generate requests per second graph.
    pub(crate) fn get_requests_per_second_graph(&self, granular_data: bool) -> Graph<'_, u32, u32> {
        self.requests_per_second
            .graph("graph-rps", "Requests #", granular_data)
    }

    /// Generate average response time graph.
    pub(crate) fn get_average_response_time_graph(
        &self,
        granular_data: bool,
    ) -> Graph<'_, MovingAverage, f32> {
        self.create_graph_from_data(
            "graph-avg-response-time",
            "Response time [ms]",
            granular_data,
            &self.average_response_time_per_second,
        )
    }

    /// Generate active transactions graph.
    pub(crate) fn get_transactions_per_second_graph(
        &self,
        granular_data: bool,
    ) -> Graph<'_, usize, usize> {
        self.create_graph_from_single_data(
            "graph-tps",
            "Transactions #",
            granular_data,
            &self.transactions_per_second,
        )
    }

    /// Generate active scenarios graph.
    pub(crate) fn get_scenarios_per_second_graph(
        &self,
        granular_data: bool,
    ) -> Graph<'_, usize, usize> {
        self.create_graph_from_single_data(
            "graph-sps",
            "Scenarios #",
            granular_data,
            &self.scenarios_per_second,
        )
    }

    /// Generate errors per second graph.
    pub(crate) fn get_errors_per_second_graph(&self, granular_data: bool) -> Graph<'_, u32, u32> {
        self.errors_per_second
            .graph("graph-eps", "Errors #", granular_data)
    }

    /// Creates a Graph from granular data.
    fn create_graph_from_data<
        'a,
        T: Clone + TimeSeriesValue<T, U>,
        U: Serialize + Copy + PartialEq + PartialOrd,
    >(
        &self,
        html_id: &'a str,
        y_axis_label: &'a str,
        granular_data: bool,
        data: &'a HashMap<String, TimeSeries<T, U>>,
    ) -> Graph<'a, T, U> {
        Graph::new(html_id, y_axis_label, granular_data, data)
    }

    /// Creates a Graph from single (just total numbers, not granular) data.
    fn create_graph_from_single_data<
        'a,
        T: Clone + TimeSeriesValue<T, U>,
        U: Serialize + Copy + PartialEq + PartialOrd,
    >(
        &self,
        html_id: &'a str,
        y_axis_label: &'a str,
        granular_data: bool,
        data: &'a TimeSeries<T, U>,
    ) -> Graph<'a, T, U> {
        Graph {
            html_id,
            y_axis_label,
            granular_data,
            left_out: None,
            data: vec![("Total", data)],
        }
    }
}

/// Defines the HTML graph data.
#[derive(Debug)]
pub(crate) struct Graph<'a, T: Clone + TimeSeriesValue<T, U>, U: Serialize + Copy> {
    /// HTML ID of the graph's main wrapper.
    html_id: &'a str,
    /// Label of the y axis.
    y_axis_label: &'a str,
    /// Indicates whether the granular data should be displayed on graphs.
    granular_data: bool,
    /// Graph data, borrowed from [`GraphData`] in the iteration order of its
    /// map, so rendering a report does not copy every series.
    data: Vec<(&'a str, &'a TimeSeries<T, U>)>,
    /// A second, from the start of the run, that every line leaves out.
    left_out: Option<usize>,
}

impl<'a, T: Clone + TimeSeriesValue<T, U>, U: Serialize + Copy + PartialEq + PartialOrd>
    Graph<'a, T, U>
{
    /// Creates a new Graph object.
    #[allow(clippy::too_many_arguments)]
    fn new(
        html_id: &'a str,
        y_axis_label: &'a str,
        granular_data: bool,
        data: &'a HashMap<String, TimeSeries<T, U>>,
    ) -> Graph<'a, T, U> {
        Graph {
            html_id,
            y_axis_label,
            granular_data,
            left_out: None,
            data: data
                .iter()
                .map(|(label, series)| (label.as_str(), series))
                .collect(),
        }
    }

    /// Leaves out the run's final second, `partial`, when the run stopped
    /// partway through it: a count there holds only part of a second, so the
    /// line would end in a drop. Every line leaves out the same second, so a
    /// request whose last one came earlier keeps its last full second.
    pub(crate) fn without_partial_second(mut self, partial: Option<usize>) -> Self {
        self.left_out = partial;
        self
    }

    /// The series with this label, for tests.
    #[cfg(test)]
    fn series(&self, label: &str) -> Option<&TimeSeries<T, U>> {
        self.data
            .iter()
            .find(|(l, _)| *l == label)
            .map(|(_, series)| *series)
    }

    /// Helper function to build HTML charts powered by the
    /// [ECharts](https://echarts.apache.org) library.
    pub(crate) fn get_markup(
        self,
        history: &[TestPlanHistory],
        test_started_time: DateTime<Utc>,
    ) -> String {
        let mut steps = String::new();
        for step in history.windows(2) {
            let started = Local
                .timestamp_opt(step[0].timestamp.timestamp(), 0)
                // @TODO: Error handling
                .unwrap()
                .format("%Y-%m-%d %H:%M:%S")
                .to_string();
            let stopped = Local
                .timestamp_opt(step[1].timestamp.timestamp(), 0)
                // @TODO: Error handling
                .unwrap()
                .format("%Y-%m-%d %H:%M:%S")
                .to_string();
            match &step[0].action {
                // For increasing show the current number of users to the new number of users.
                TestPlanStepAction::Increasing => {
                    let _ = write!(
                        steps,
                        r#"[
                            {{
                                xAxis: '{started}',
                                itemStyle: {{ borderColor: 'rgba(44, 102, 79, 0.25)', borderWidth: 1 }},
                            }},
                            {{
                                xAxis: '{started}'
                            }}
                        ],
                        [
                            {{
                                xAxis: '{started}',
                                itemStyle: {{ color: 'rgba(44, 102, 79, 0.05)' }},
                            }},
                            {{
                                xAxis: '{stopped}'
                            }}
                        ],
                        [
                            {{
                                xAxis: '{stopped}',
                                itemStyle: {{ borderColor: 'rgba(44, 102, 79, 0.25)', borderWidth: 1 }},
                            }},
                            {{
                                xAxis: '{stopped}'
                            }}
                        ],"#,
                    );
                }
                // For decreasing show the new number of users from the current number of users.
                TestPlanStepAction::Decreasing | TestPlanStepAction::Canceling => {
                    let _ = write!(
                        steps,
                        r#"[
                            {{
                                xAxis: '{started}',
                                itemStyle: {{ borderColor: 'rgba(179, 65, 65, 0.25)', borderWidth: 1 }},
                            }},
                            {{
                                xAxis: '{started}'
                            }}
                        ],
                        [
                            {{
                                xAxis: '{started}',
                                itemStyle: {{ color: 'rgba(179, 65, 65, 0.05)' }},
                            }},
                            {{
                                xAxis: '{stopped}'
                            }}
                        ],
                        [
                            {{
                                xAxis: '{stopped}',
                                itemStyle: {{ borderColor: 'rgba(179, 65, 65, 0.25)', borderWidth: 1 }},
                            }},
                            {{
                                xAxis: '{stopped}'
                            }}
                        ],"#,
                    );
                }
                _ => {}
            }
        }

        let mut total_values: TimeSeries<T, U> = TimeSeries::new();
        if self.data.is_empty() {
            "<!-- no data, no legend -->".to_string()
        } else {
            let (legend, main_label, main_values, other_values) = if self.data.len() > 1 {
                // If we are dealing with a metric with granular data we need to calculate totals.
                for (_, single_data) in &self.data {
                    total_values.add_time_series(single_data);
                }

                // We will have multiple lines. We need to prepare the legend section on the graph
                // and create data series for all of them.
                let mut legend = vec!["Total"];

                let mut other_values = String::new();
                if self.granular_data {
                    // In order for this to sort correctly we need to flip label and time series since tuples
                    // are sorted lexicographically so we want time series to be the first element of the tuple.
                    for (sub_data, label) in self
                        .data
                        .iter()
                        .map(|&(label, sub_data)| (sub_data, label))
                        .sorted()
                        .rev()
                    {
                        legend.push(label);

                        let formatted_line = format!(
                            r#"{{
                                name: '{label}',
                                type: 'line',
                                symbol: 'none',
                                sampling: 'lttb',
                                data: {values},
                            }},
                        "#,
                            label = label,
                            values = json!(self.add_timestamp_to_html_graph_data(
                                &sub_data.get_graph_data(),
                                sub_data.origin,
                                test_started_time
                            ))
                        );
                        other_values += formatted_line.as_str();
                    }
                    (
                        format!(
                            r#"legend: {{
                            type: '{legend_type}',
                            width: '75%',
                            data: {data},
                        }},"#,
                            legend_type = if self.data.len() > 4 {
                                "scroll"
                            } else {
                                "plain"
                            },
                            data = json!(legend)
                        ),
                        "Total",
                        &total_values,
                        other_values,
                    )
                } else {
                    ("".to_string(), "Total", &total_values, "".to_string())
                }
            } else {
                // If there is only one data series in the metric we simply display it.
                (
                    "".to_string(),
                    self.data[0].0,
                    self.data[0].1,
                    "".to_string(),
                )
            };

            format!(
                r#"<div class="graph">
                <div id="{html_id}" style="width: 1000px; height:500px; background: white;"></div>

                <script type="text/javascript">
                    var chartDom = document.getElementById('{html_id}');
                    var myChart = echarts.init(chartDom);

                    myChart.setOption({{
                        color: ['#2c664f', '#5470c6', '#91cc75', '#fac858', '#ee6666', '#73c0de', '#3ba272', '#fc8452', '#9a60b4', '#ea7ccc'],
                        tooltip: {{ trigger: 'axis' }},
                        toolbox: {{
                            feature: {{
                                dataZoom: {{ yAxisIndex: 'none' }},
                                restore: {{}},
                                saveAsImage: {{}}
                            }}
                        }},
                        dataZoom: [
                            {{
                                type: 'inside',
                                start: 0,
                                end: 100,
                                fillerColor: 'rgba(34, 80, 61, 0.25)',
                                selectedDataBackground: {{
                                    lineStyle: {{ color: '#2c664f' }},
                                    areaStyle: {{ color: '#378063' }}
                                }}
                            }},
                            {{
                                start: 0,
                                end: 100,
                                fillerColor: 'rgba(34, 80, 61, 0.25)',
                                selectedDataBackground: {{
                                    lineStyle: {{ color: '#2c664f' }},
                                    areaStyle: {{ color: '#378063' }}
                                }}
                            }},
                        ],
                        xAxis: {{ type: 'time' }},
                        yAxis: {{
                            name: '{y_axis_label}',
                            nameLocation: 'center',
                            nameRotate: 90,
                            nameGap: 45,
                            type: 'value'
                        }},
                        {legend}
                        series: [
                            {{
                                name: '{main_label}',
                                type: 'line',
                                symbol: 'none',
                                sampling: 'lttb',
                                lineStyle: {{ color: '#2c664f' }},
                                areaStyle: {{ color: '#378063' }},
                                markArea: {{
                                    data: [
                                        {steps}
                                    ]
                                }},
                                data: {main_values},
                            }},
                            {other_values}
                        ]
                    }});
                </script>
            </div>"#,
                html_id = self.html_id,
                main_values = json!(self.add_timestamp_to_html_graph_data(
                    &main_values.get_graph_data(),
                    main_values.origin,
                    test_started_time
                )),
                y_axis_label = self.y_axis_label,
                main_label = main_label,
                legend = legend,
                other_values = other_values,
                steps = steps,
            )
        }
    }

    /// Adds timestamps to the graph data series to ensure correct time display on x axis.
    ///
    /// Will take a vector of (generally numerical) values and convert them into tuples where
    /// the second element will be the data point and the first element will be formatted time
    /// it belongs to.
    fn add_timestamp_to_html_graph_data(
        &self,
        data: &[Option<U>],
        origin: usize,
        started: DateTime<Utc>,
    ) -> Vec<(String, U)> {
        // A second no clock can show, from an origin read back from a JSON
        // report, is left out rather than drawn.
        data.iter()
            .enumerate()
            .filter_map(|(index, value)| {
                let second = origin
                    .checked_add(index)
                    .filter(|&second| Some(second) != self.left_out)
                    .and_then(|second| i64::try_from(second).ok())
                    .and_then(|second| second.checked_add(started.timestamp()))?;
                let time = Local.timestamp_opt(second, 0).single()?;
                value.map(|data| (time.format("%Y-%m-%d %H:%M:%S").to_string(), data))
            })
            .collect::<Vec<_>>()
    }
}

/// Add one rate series' samples into a fixed-length export window buffer.
///
/// Only visits the absolute-second overlap between the series storage and
/// `[start_second, end_second]`. Missing buckets stay at their pre-zeroed value.
fn accumulate_rate_series_into_window(
    series: &TimeSeries<u32, u32>,
    start_second: usize,
    end_second: usize,
    out: &mut [f64],
) {
    if series.data.is_empty() {
        return;
    }
    let series_end = series.origin + series.data.len() - 1;
    let overlap_start = start_second.max(series.origin);
    let overlap_end = end_second.min(series_end);
    if overlap_start > overlap_end {
        return;
    }
    for s in overlap_start..=overlap_end {
        out[s - start_second] += series.data[s - series.origin] as f64;
    }
}

/// Data structure to represent time series data.
#[derive(Debug, Clone)]
struct TimeSeries<T: TimeSeriesValue<T, U>, U> {
    /// Time series data.
    ///
    /// Each element represents the value for one second in the time series.
    /// `data[i]` holds the sample for absolute second `origin + i`.
    ///
    /// Stored as a [`VecDeque`] so dashboard pruning can drop a prefix with
    /// amortized O(dropped) cost (`pop_front`) instead of O(retained) shifts
    /// from `Vec::drain(0..n)` on every 1 Hz prune across many request keys.
    data: VecDeque<T>,
    /// Absolute second corresponding to `data[0]`. Non-zero only after pruning
    /// (dashboard-only long soaks). Report-file collection never prunes, so this
    /// stays 0 and indices remain absolute seconds from test start.
    origin: usize,
    /// Total value of the time series (sum of all elements).
    total: T,
    /// Phantom data indicates to the compiler that the "U" generic data type has zero size.
    phantom: PhantomData<U>,
}

impl<T: Clone + TimeSeriesValue<T, U>, U: PartialEq + PartialOrd> Ord for TimeSeries<T, U> {
    fn cmp(&self, other: &Self) -> Ordering {
        let self_total = self.total();
        let other_total = other.total();

        if self_total > other_total {
            Ordering::Greater
        } else if self_total < other_total {
            Ordering::Less
        } else {
            Ordering::Equal
        }
    }
}

impl<T: Clone + TimeSeriesValue<T, U>, U: PartialEq + PartialOrd> PartialOrd for TimeSeries<T, U> {
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        Some(self.cmp(other))
    }
}

impl<T: Clone + TimeSeriesValue<T, U>, U: PartialEq> Eq for TimeSeries<T, U> {}

impl<T: Clone + TimeSeriesValue<T, U>, U: PartialEq> PartialEq for TimeSeries<T, U> {
    fn eq(&self, other: &Self) -> bool {
        self.total() == other.total()
    }
}

impl<T: Clone + TimeSeriesValue<T, U>, U> TimeSeries<T, U> {
    /// Creates a new TimeSeries object.
    fn new() -> TimeSeries<T, U> {
        Self::with_origin(0)
    }

    /// Creates an empty series whose first bucket is absolute second `origin`.
    ///
    /// Used after dashboard pruning / rate reset so inserts at absolute second *S*
    /// store at index `S - origin` instead of expanding from 0.
    fn with_origin(origin: usize) -> TimeSeries<T, U> {
        TimeSeries {
            data: VecDeque::new(),
            origin,
            phantom: PhantomData,
            total: T::initial_value(),
        }
    }

    /// Last absolute second that has a stored sample, if any.
    fn last_absolute_second(&self) -> Option<usize> {
        if self.data.is_empty() {
            None
        } else {
            Some(self.origin + self.data.len() - 1)
        }
    }

    /// Drop samples older than the trailing `keep_secs` window (by absolute second).
    ///
    /// After pruning, `origin` advances and `data` holds only the retained window so
    /// multi-hour dashboard-only soaks do not grow unbounded. No-op when the series
    /// is already within the window.
    ///
    /// Uses repeated [`VecDeque::pop_front`] so cost is O(dropped) rather than
    /// O(retained) from a front `Vec::drain` — important when many per-key series
    /// are pruned at ~1 Hz after already sitting at the keep window.
    ///
    /// **Limitation:** when `data.len() <= keep_secs` this is a no-op even if the
    /// series still has a stale low `origin` (idle keys). Callers that need a
    /// shared window must follow up with [`Self::align_origin_to`].
    fn prune_to_window(&mut self, keep_secs: usize) {
        if keep_secs == 0 || self.data.len() <= keep_secs {
            return;
        }
        let drop = self.data.len() - keep_secs;
        for _ in 0..drop {
            self.data.pop_front();
        }
        self.origin = self.origin.saturating_add(drop);
        // `total` still reflects historical increments (including dropped samples).
        // Dashboard export uses per-second buckets only; HTML reports never prune.
    }

    /// Advance `origin` to `target_origin`, dropping any samples strictly before it.
    ///
    /// Complements length-based [`Self::prune_to_window`]: idle per-key series keep
    /// a short `data` buffer and never length-prune, so without this step a late
    /// reappearance at absolute second *S* would expand from the stale origin.
    /// No-op when `target_origin <= self.origin`.
    fn align_origin_to(&mut self, target_origin: usize) {
        if target_origin <= self.origin {
            return;
        }
        if self.data.is_empty() {
            self.origin = target_origin;
            return;
        }
        let last = self.origin + self.data.len() - 1;
        if last < target_origin {
            // Entire series is before the retained window.
            self.data.clear();
            self.origin = target_origin;
            return;
        }
        let drop = target_origin - self.origin;
        for _ in 0..drop {
            self.data.pop_front();
        }
        self.origin = target_origin;
    }

    /// Increases the the value for a given second.
    fn increase_value(&mut self, second: usize, value: U) {
        // Samples that fall before the pruned window are dropped (dashboard-only).
        if second < self.origin {
            return;
        }
        self.expand(second, T::initial_value());
        let idx = second - self.origin;
        self.data[idx].increase_value(&value);
        self.total.increase_value(&value);
    }

    /// Merges a pre-aggregated value into a given second.
    fn merge_value(&mut self, second: usize, value: &T) {
        // Samples that fall before the pruned window are dropped (dashboard-only).
        if second < self.origin {
            return;
        }
        self.expand(second, T::initial_value());
        let idx = second - self.origin;
        self.data[idx].merge(value);
        self.total.merge(value);
    }

    /// Adds another time series.
    fn add_time_series(&mut self, other: &TimeSeries<T, U>) {
        for (offset, other_item) in other.data.iter().enumerate() {
            let second = other.origin + offset;
            if second < self.origin {
                continue;
            }
            self.expand(second, T::initial_value());
            let idx = second - self.origin;
            self.data.get_mut(idx).unwrap().merge(other_item);
            self.total.merge(other_item);
        }
    }

    /// Sets a value for a given second and maintains last recorded value if
    /// there is a gap in the time series.
    fn set_and_maintain_last(&mut self, second: usize, value: U) {
        if second < self.origin {
            return;
        }
        self.expand(second, self.last());
        self.total.increase_value(&value);
        let idx = second - self.origin;
        self.data[idx].set_value(value);
    }

    /// Returns a value for a given second.
    fn get(&self, second: usize) -> T {
        if second < self.origin {
            return T::initial_value();
        }
        match self.data.get(second - self.origin) {
            Some(value) => value.clone(),
            None => T::initial_value(),
        }
    }

    /// Level-style read: hold the nearest known sample when `second` is outside
    /// the stored range (before origin or past the last sample).
    ///
    /// Used for active-user counts, which are continuous levels. Rate series
    /// (RPS/errors) correctly use [`Self::get`] so missing buckets stay 0.
    fn get_held(&self, second: usize) -> T {
        if self.data.is_empty() {
            return T::initial_value();
        }
        if second < self.origin {
            return self.data[0].clone();
        }
        let last_abs = self.origin + self.data.len() - 1;
        if second > last_abs {
            return self.last();
        }
        self.data[second - self.origin].clone()
    }

    /// Returns the last value in the time series or initial value if the time
    /// series is empty.
    fn last(&self) -> T {
        match self.data.back() {
            Some(last) => last.clone(),
            None => T::initial_value(),
        }
    }

    /// Gets time series suitable for usage in HTML graphs (generally each value
    /// becomes some kind of a scalar).
    ///
    /// Report generation never prunes (`origin == 0`), so the returned vector is
    /// indexed by absolute second from test start. If pruning were used with
    /// reports, callers would need to pad `origin` leading empties; that path is
    /// intentionally unsupported.
    fn get_graph_data(&self) -> Vec<Option<U>> {
        self.data
            .iter()
            .map(|value| value.get_graph_value())
            .collect::<Vec<_>>()
    }

    /// Expands vectors that collect per-second data for HTML report graphs with a
    /// default value.
    ///
    /// We need to do that since we don't know for how long the load test will run
    /// and we can't initialize these vectors at the beginning. It is also
    /// better to do it as we go to save memory.
    ///
    /// `second` is absolute (from test start). Storage index is `second - origin`.
    fn expand(&mut self, second: usize, initial: T) {
        debug_assert!(second >= self.origin);
        let idx = second - self.origin;
        if self.data.len() <= idx {
            for _ in 0..(idx - self.data.len() + 1) {
                self.data.push_back(initial.clone());
                self.total.merge(&initial);
            }
        };
    }

    /// Gets time series total value (sum of all values).
    fn total(&self) -> U {
        self.total.get_total_value()
    }
}

/// A series in the JSON report: its first second and its values.
impl<T: Serialize + TimeSeriesValue<T, U>, U> Serialize for TimeSeries<T, U> {
    fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
        use serde::ser::SerializeStruct;
        let mut series = serializer.serialize_struct("TimeSeries", 2)?;
        series.serialize_field("origin", &self.origin)?;
        series.serialize_field("data", &self.data)?;
        series.end()
    }
}

/// A series read back from the JSON report, its total worked out again.
impl<'de, T, U> Deserialize<'de> for TimeSeries<T, U>
where
    T: Deserialize<'de> + Clone + TimeSeriesValue<T, U>,
{
    fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
        #[derive(Deserialize)]
        struct Stored<T> {
            #[serde(default)]
            origin: usize,
            data: VecDeque<T>,
        }
        let stored = Stored::<T>::deserialize(deserializer)?;
        let mut total = T::initial_value();
        for value in &stored.data {
            total.merge(value);
        }
        Ok(TimeSeries {
            data: stored.data,
            origin: stored.origin,
            total,
            phantom: PhantomData,
        })
    }
}

impl TimeSeries<u32, u32> {
    /// Takes one back from a given second, stopping at zero. Does nothing for
    /// a second the series does not hold.
    fn decrement(&mut self, second: usize) {
        if second < self.origin {
            return;
        }
        if let Some(held) = self.data.get_mut(second - self.origin) {
            if *held > 0 {
                *held -= 1;
                self.total -= 1;
            }
        }
    }
}

/// Defines a single value in a TimeSeries.
pub trait TimeSeriesValue<T, U> {
    /// Initial ("zero") value.
    fn initial_value() -> T;
    /// Adds the given value to the current value.
    fn increase_value(&mut self, value: &U);
    /// Sets the value (and drops existing one if present).
    fn set_value(&mut self, value: U);
    /// Merges (adds) another TimeSeriesValue.
    fn merge(&mut self, other: &T);
    /// Gets representation of the value suitable for HTML graphs (generally a scalar).
    fn get_graph_value(&self) -> Option<U>;
    fn get_total_value(&self) -> U;
}

impl TimeSeriesValue<usize, usize> for usize {
    fn initial_value() -> usize {
        0
    }
    fn increase_value(&mut self, value: &usize) {
        *self += *value;
    }
    fn set_value(&mut self, value: usize) {
        *self = value;
    }
    fn merge(&mut self, other: &usize) {
        *self += *other;
    }
    fn get_graph_value(&self) -> Option<usize> {
        match *self == 0 {
            true => None,
            false => Some(*self),
        }
    }
    fn get_total_value(&self) -> usize {
        *self
    }
}

impl TimeSeriesValue<u32, u32> for u32 {
    fn initial_value() -> u32 {
        0
    }
    fn increase_value(&mut self, value: &u32) {
        *self += *value;
    }
    fn set_value(&mut self, value: u32) {
        *self = value;
    }
    fn merge(&mut self, other: &u32) {
        *self += *other;
    }
    fn get_graph_value(&self) -> Option<u32> {
        match *self == 0 {
            true => None,
            false => Some(*self),
        }
    }
    fn get_total_value(&self) -> u32 {
        *self
    }
}

impl TimeSeriesValue<MovingAverage, f32> for MovingAverage {
    fn initial_value() -> MovingAverage {
        MovingAverage::new()
    }
    fn increase_value(&mut self, value: &f32) {
        self.add_item(value);
    }
    fn set_value(&mut self, _: f32) {
        panic!("TimeSeriesValue::set() is not supported for MovingAverage");
    }
    fn merge(&mut self, other: &MovingAverage) {
        let total_count = self.count + other.count;
        if total_count == 0 {
            self.average = 0.;
        } else {
            self.average = self.average * (self.count as f32 / total_count as f32)
                + other.average * (other.count as f32 / total_count as f32);
        };
        self.count = total_count;
    }
    fn get_graph_value(&self) -> Option<f32> {
        match self.average == 0f32 {
            true => None,
            false => Some(self.average),
        }
    }
    fn get_total_value(&self) -> f32 {
        self.average
    }
}

/// Data structure to maintain moving averages.
///
/// It will maintain the current average and the number of data items that
/// were used to compute it. The JSON report holds it as `[count, average]`.
#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
#[serde(from = "(u32, f32)", into = "(u32, f32)")]
pub(crate) struct MovingAverage {
    /// Number of data items that were used to compute the current average.
    count: u32,
    /// Current average.
    average: f32,
}

impl From<(u32, f32)> for MovingAverage {
    fn from((count, average): (u32, f32)) -> Self {
        MovingAverage { count, average }
    }
}

impl From<MovingAverage> for (u32, f32) {
    fn from(average: MovingAverage) -> Self {
        (average.count, average.average)
    }
}

impl MovingAverage {
    /// Create a new MovingAverage object.
    fn new() -> Self {
        MovingAverage {
            count: 0,
            average: 0.,
        }
    }

    /// Adds a new data item and calculates the new average.
    fn add_item(&mut self, item: &f32) {
        self.count += 1;
        self.average += (*item - self.average) / self.count as f32;
    }
}

impl Default for MovingAverage {
    /// Creates an empty moving average.
    fn default() -> MovingAverage {
        MovingAverage::new()
    }
}

impl Ord for MovingAverage {
    fn cmp(&self, other: &Self) -> Ordering {
        if self.average > other.average {
            Ordering::Greater
        } else if self.average < other.average {
            Ordering::Less
        } else {
            Ordering::Equal
        }
    }
}

impl PartialOrd for MovingAverage {
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        Some(self.cmp(other))
    }
}

impl PartialEq for MovingAverage {
    fn eq(&self, other: &Self) -> bool {
        self.average == other.average
    }
}

impl Eq for MovingAverage {}

#[cfg(test)]
mod test {
    use super::*;

    #[test]
    fn test_graph_setters() {
        let mut graph = GraphData::new();
        graph.requests_per_second.insert(
            "GET /",
            TimeSeries {
                origin: 0,
                data: VecDeque::from(vec![123, 234, 345, 456, 567]),
                phantom: PhantomData,
                total: 0,
            },
        );
        graph.users_per_second = TimeSeries {
            origin: 0,
            data: VecDeque::from(vec![345, 456, 567, 123, 234]),
            phantom: PhantomData,
            total: 0,
        };
        graph.average_response_time_per_second.insert(
            "GET /".to_string(),
            TimeSeries {
                origin: 0,
                data: VecDeque::from(vec![
                    MovingAverage {
                        count: 123,
                        average: 1.23,
                    },
                    MovingAverage {
                        count: 234,
                        average: 2.34,
                    },
                    MovingAverage {
                        count: 345,
                        average: 3.45,
                    },
                    MovingAverage {
                        count: 456,
                        average: 4.56,
                    },
                    MovingAverage {
                        count: 567,
                        average: 5.67,
                    },
                ]),
                phantom: PhantomData,
                total: MovingAverage {
                    count: 0,
                    average: 0.,
                },
            },
        );
        graph.transactions_per_second = TimeSeries {
            origin: 0,
            data: VecDeque::from(vec![345, 123, 234, 456, 567]),
            phantom: PhantomData,
            total: 0,
        };

        graph.scenarios_per_second = TimeSeries {
            origin: 0,
            data: VecDeque::from(vec![345, 123, 234, 456, 567]),
            phantom: PhantomData,
            total: 0,
        };

        graph.errors_per_second.insert(
            "GET /",
            TimeSeries {
                origin: 0,
                data: VecDeque::from(vec![567, 123, 234, 345, 456]),
                phantom: PhantomData,
                total: 0,
            },
        );

        let rps_graph = graph.get_requests_per_second_graph(true);
        let expected_time_series: TimeSeries<u32, u32> = TimeSeries {
            origin: 0,
            data: VecDeque::from(vec![123, 234, 345, 456, 567]),
            phantom: PhantomData,
            total: 0,
        };
        assert_eq!(
            rps_graph.series("GET /").unwrap().clone(),
            expected_time_series
        );
        assert_eq!(rps_graph.html_id, "graph-rps");
        assert_eq!(rps_graph.y_axis_label, "Requests #");

        let users_graph = graph.get_active_users_graph(true);
        let expected_time_series: TimeSeries<usize, usize> = TimeSeries {
            origin: 0,
            data: VecDeque::from(vec![345, 456, 567, 123, 234]),
            phantom: PhantomData,
            total: 0,
        };
        assert_eq!(
            users_graph.series("Total").unwrap().clone(),
            expected_time_series
        );
        assert_eq!(users_graph.html_id, "graph-active-users");
        assert_eq!(users_graph.y_axis_label, "Active users #");

        let avg_rt_graph = graph.get_average_response_time_graph(true);
        let expected_time_series: TimeSeries<MovingAverage, f32> = TimeSeries {
            origin: 0,
            data: VecDeque::from(vec![
                MovingAverage {
                    count: 123,
                    average: 1.23,
                },
                MovingAverage {
                    count: 234,
                    average: 2.34,
                },
                MovingAverage {
                    count: 345,
                    average: 3.45,
                },
                MovingAverage {
                    count: 456,
                    average: 4.56,
                },
                MovingAverage {
                    count: 567,
                    average: 5.67,
                },
            ]),
            phantom: PhantomData,
            total: MovingAverage {
                count: 0,
                average: 0.,
            },
        };
        assert_eq!(
            avg_rt_graph.series("GET /").unwrap().clone(),
            expected_time_series
        );
        assert_eq!(avg_rt_graph.html_id, "graph-avg-response-time");
        assert_eq!(avg_rt_graph.y_axis_label, "Response time [ms]");

        let transactions_graph = graph.get_transactions_per_second_graph(true);
        let expected_time_series: TimeSeries<usize, usize> = TimeSeries {
            origin: 0,
            data: VecDeque::from(vec![345, 123, 234, 456, 567]),
            phantom: PhantomData,
            total: 0,
        };
        assert_eq!(
            transactions_graph.series("Total").unwrap().clone(),
            expected_time_series
        );
        assert_eq!(transactions_graph.html_id, "graph-tps");
        assert_eq!(transactions_graph.y_axis_label, "Transactions #");

        let scenarios_graph = graph.get_scenarios_per_second_graph(true);
        let expected_time_series: TimeSeries<usize, usize> = TimeSeries {
            origin: 0,
            data: VecDeque::from(vec![345, 123, 234, 456, 567]),
            phantom: PhantomData,
            total: 0,
        };
        assert_eq!(
            scenarios_graph.series("Total").unwrap().clone(),
            expected_time_series
        );
        assert_eq!(scenarios_graph.html_id, "graph-sps");
        assert_eq!(scenarios_graph.y_axis_label, "Scenarios #");

        let errors_graph = graph.get_errors_per_second_graph(true);
        let expected_time_series: TimeSeries<u32, u32> = TimeSeries {
            origin: 0,
            data: VecDeque::from(vec![567, 123, 234, 345, 456]),
            phantom: PhantomData,
            total: 0,
        };

        assert_eq!(
            errors_graph.series("GET /").unwrap().clone(),
            expected_time_series
        );
        assert_eq!(errors_graph.html_id, "graph-eps");
        assert_eq!(errors_graph.y_axis_label, "Errors #");
    }

    #[test]
    fn test_record_requests_per_second() {
        // Should be initialized with empty requests per second vector.
        let mut graph = GraphData::new();
        assert_eq!(graph.requests_per_second.len(), 0);

        graph.record_requests_per_second("GET /", 0);
        graph.record_requests_per_second("GET /", 0);
        graph.record_requests_per_second("GET /", 0);
        graph.record_requests_per_second("GET /", 1);
        graph.record_requests_per_second("GET /", 2);
        graph.record_requests_per_second("GET /", 2);
        graph.record_requests_per_second("GET /", 2);
        graph.record_requests_per_second("GET /", 2);
        graph.record_requests_per_second("GET /", 2);
        assert_eq!(
            graph.requests_per_second.get("GET /").unwrap().data.len(),
            3
        );
        assert_eq!(graph.requests_per_second.get("GET /").unwrap().data[0], 3);
        assert_eq!(graph.requests_per_second.get("GET /").unwrap().data[1], 1);
        assert_eq!(graph.requests_per_second.get("GET /").unwrap().data[2], 5);
        assert_eq!(graph.requests_per_second.get("GET /").unwrap().total(), 9);

        graph.record_requests_per_second("GET /", 100);
        graph.record_requests_per_second("GET /", 100);
        graph.record_requests_per_second("GET /", 100);
        graph.record_requests_per_second("GET /", 0);
        graph.record_requests_per_second("GET /", 1);
        graph.record_requests_per_second("GET /", 2);
        graph.record_requests_per_second("GET /", 5);
        assert_eq!(
            graph.requests_per_second.get("GET /").unwrap().data.len(),
            101
        );
        assert_eq!(graph.requests_per_second.get("GET /").unwrap().data[0], 4);
        assert_eq!(graph.requests_per_second.get("GET /").unwrap().data[1], 2);
        assert_eq!(graph.requests_per_second.get("GET /").unwrap().data[2], 6);
        assert_eq!(graph.requests_per_second.get("GET /").unwrap().data[3], 0);
        assert_eq!(graph.requests_per_second.get("GET /").unwrap().data[4], 0);
        assert_eq!(graph.requests_per_second.get("GET /").unwrap().data[5], 1);
        assert_eq!(graph.requests_per_second.get("GET /").unwrap().data[100], 3);
        for second in 6..100 {
            assert_eq!(
                graph.requests_per_second.get("GET /").unwrap().data[second],
                0
            );
        }
        assert_eq!(graph.requests_per_second.get("GET /").unwrap().total(), 16);

        graph.record_requests_per_second("GET /user", 0);
        graph.record_requests_per_second("GET /user", 1);
        graph.record_requests_per_second("GET /user", 1);
        graph.record_requests_per_second("GET /", 2);
        graph.record_requests_per_second("GET /", 5);
        assert_eq!(
            graph
                .requests_per_second
                .get("GET /user")
                .unwrap()
                .data
                .len(),
            2
        );
        assert_eq!(
            graph.requests_per_second.get("GET /user").unwrap().data[0],
            1
        );
        assert_eq!(
            graph.requests_per_second.get("GET /user").unwrap().data[1],
            2
        );

        assert_eq!(
            graph.requests_per_second.get("GET /").unwrap().data.len(),
            101
        );
        assert_eq!(graph.requests_per_second.get("GET /").unwrap().data[0], 4);
        assert_eq!(graph.requests_per_second.get("GET /").unwrap().data[1], 2);
        assert_eq!(graph.requests_per_second.get("GET /").unwrap().data[2], 7);
        assert_eq!(graph.requests_per_second.get("GET /").unwrap().data[3], 0);
        assert_eq!(graph.requests_per_second.get("GET /").unwrap().data[4], 0);
        assert_eq!(graph.requests_per_second.get("GET /").unwrap().data[5], 2);
        assert_eq!(graph.requests_per_second.get("GET /").unwrap().data[100], 3);
        for second in 6..100 {
            assert_eq!(
                graph.requests_per_second.get("GET /").unwrap().data[second],
                0
            );
        }
        assert_eq!(graph.requests_per_second.get("GET /").unwrap().total(), 18);
        assert_eq!(
            graph.requests_per_second.get("GET /user").unwrap().total(),
            3
        );

        graph.record_requests_per_second("GET /user", 100);
        graph.record_requests_per_second("GET /user", 0);
        graph.record_requests_per_second("GET /user", 1);
        graph.record_requests_per_second("GET /", 0);
        graph.record_requests_per_second("GET /", 1);
        assert_eq!(
            graph
                .requests_per_second
                .get("GET /user")
                .unwrap()
                .data
                .len(),
            101
        );
        assert_eq!(
            graph.requests_per_second.get("GET /user").unwrap().data[0],
            2
        );
        assert_eq!(
            graph.requests_per_second.get("GET /user").unwrap().data[1],
            3
        );
        assert_eq!(
            graph.requests_per_second.get("GET /user").unwrap().data[100],
            1
        );
        for second in 6..100 {
            assert_eq!(
                graph.requests_per_second.get("GET /user").unwrap().data[second],
                0
            );
        }

        assert_eq!(
            graph.requests_per_second.get("GET /").unwrap().data.len(),
            101
        );
        assert_eq!(graph.requests_per_second.get("GET /").unwrap().data[0], 5);
        assert_eq!(graph.requests_per_second.get("GET /").unwrap().data[1], 3);
        assert_eq!(graph.requests_per_second.get("GET /").unwrap().data[2], 7);
        assert_eq!(graph.requests_per_second.get("GET /").unwrap().data[3], 0);
        assert_eq!(graph.requests_per_second.get("GET /").unwrap().data[4], 0);
        assert_eq!(graph.requests_per_second.get("GET /").unwrap().data[5], 2);
        assert_eq!(graph.requests_per_second.get("GET /").unwrap().data[100], 3);
        for second in 6..100 {
            assert_eq!(
                graph.requests_per_second.get("GET /").unwrap().data[second],
                0
            );
        }
        assert_eq!(graph.requests_per_second.get("GET /").unwrap().total(), 20);
        assert_eq!(
            graph.requests_per_second.get("GET /user").unwrap().total(),
            6
        );
    }

    #[test]
    fn test_record_errors_per_second() {
        // Should be initialized with empty errors per second vector.
        let mut graph = GraphData::new();
        assert_eq!(graph.errors_per_second.len(), 0);

        graph.record_errors_per_second("GET /", 0);
        graph.record_errors_per_second("GET /", 0);
        graph.record_errors_per_second("GET /", 0);
        graph.record_errors_per_second("GET /", 1);
        graph.record_errors_per_second("GET /", 2);
        graph.record_errors_per_second("GET /", 2);
        graph.record_errors_per_second("GET /", 2);
        graph.record_errors_per_second("GET /", 2);
        graph.record_errors_per_second("GET /", 2);
        assert_eq!(graph.errors_per_second.get("GET /").unwrap().data.len(), 3);
        assert_eq!(graph.errors_per_second.get("GET /").unwrap().data[0], 3);
        assert_eq!(graph.errors_per_second.get("GET /").unwrap().data[1], 1);
        assert_eq!(graph.errors_per_second.get("GET /").unwrap().data[2], 5);
        assert_eq!(graph.errors_per_second.get("GET /").unwrap().total(), 9);

        graph.record_errors_per_second("GET /", 100);
        graph.record_errors_per_second("GET /", 100);
        graph.record_errors_per_second("GET /", 100);
        graph.record_errors_per_second("GET /", 0);
        graph.record_errors_per_second("GET /", 1);
        graph.record_errors_per_second("GET /", 2);
        graph.record_errors_per_second("GET /", 5);
        assert_eq!(
            graph.errors_per_second.get("GET /").unwrap().data.len(),
            101
        );
        assert_eq!(graph.errors_per_second.get("GET /").unwrap().data[0], 4);
        assert_eq!(graph.errors_per_second.get("GET /").unwrap().data[1], 2);
        assert_eq!(graph.errors_per_second.get("GET /").unwrap().data[2], 6);
        assert_eq!(graph.errors_per_second.get("GET /").unwrap().data[3], 0);
        assert_eq!(graph.errors_per_second.get("GET /").unwrap().data[4], 0);
        assert_eq!(graph.errors_per_second.get("GET /").unwrap().data[5], 1);
        assert_eq!(graph.errors_per_second.get("GET /").unwrap().data[100], 3);
        for second in 6..100 {
            assert_eq!(
                graph.errors_per_second.get("GET /").unwrap().data[second],
                0
            );
        }
        assert_eq!(graph.errors_per_second.get("GET /").unwrap().total(), 16);
    }

    #[test]
    fn test_remove_error_per_second() {
        let mut graph = GraphData::new();
        graph.record_errors_per_second("GET /", 2);
        graph.record_errors_per_second("GET /", 2);

        graph.remove_error_per_second("GET /", 2);
        assert_eq!(graph.errors_in_second_for_test("GET /", 2), 1);
        graph.remove_error_per_second("GET /", 2);
        graph.remove_error_per_second("GET /", 2);
        assert_eq!(graph.errors_in_second_for_test("GET /", 2), 0);
        assert_eq!(graph.errors_per_second.get("GET /").unwrap().total(), 0);

        // A second past the series and a key it does not have change nothing.
        graph.remove_error_per_second("GET /", 50);
        assert_eq!(graph.errors_per_second.get("GET /").unwrap().data.len(), 3);
        graph.remove_error_per_second("GET /other", 2);
        assert_eq!(graph.errors_per_second.len(), 1);

        // Nor does a second before a pruned series' origin.
        graph.record_errors_per_second("GET /", 9);
        graph
            .errors_per_second
            .series
            .get_mut("GET /")
            .unwrap()
            .prune_to_window(2);
        graph.remove_error_per_second("GET /", 2);
        assert_eq!(graph.errors_in_second_for_test("GET /", 9), 1);
    }

    #[test]
    #[allow(clippy::float_cmp)]
    fn test_record_average_response_time_per_second() {
        // Should be initialized with empty average response time per second vector.
        let mut graph = GraphData::new();
        assert_eq!(graph.average_response_time_per_second.len(), 0);

        graph.record_average_response_time_per_second("GET /".to_string(), 0, 5);
        graph.record_average_response_time_per_second("GET /".to_string(), 0, 4);
        graph.record_average_response_time_per_second("GET /".to_string(), 0, 3);
        graph.record_average_response_time_per_second("GET /".to_string(), 1, 1);
        graph.record_average_response_time_per_second("GET /".to_string(), 2, 4);
        graph.record_average_response_time_per_second("GET /".to_string(), 2, 8);
        graph.record_average_response_time_per_second("GET /".to_string(), 2, 12);
        graph.record_average_response_time_per_second("GET /".to_string(), 2, 4);
        graph.record_average_response_time_per_second("GET /".to_string(), 2, 4);
        assert_eq!(
            graph
                .average_response_time_per_second
                .get("GET /")
                .unwrap()
                .data
                .len(),
            3
        );
        assert_eq!(
            graph
                .average_response_time_per_second
                .get("GET /")
                .unwrap()
                .data[0]
                .average,
            4.
        );
        assert_eq!(
            graph
                .average_response_time_per_second
                .get("GET /")
                .unwrap()
                .data[1]
                .average,
            1.
        );
        assert_eq!(
            graph
                .average_response_time_per_second
                .get("GET /")
                .unwrap()
                .data[2]
                .average,
            6.4
        );
        assert_eq!(
            graph
                .average_response_time_per_second
                .get("GET /")
                .unwrap()
                .total(),
            5.0000005
        );

        graph.record_average_response_time_per_second("GET /".to_string(), 100, 5);
        graph.record_average_response_time_per_second("GET /".to_string(), 100, 9);
        graph.record_average_response_time_per_second("GET /".to_string(), 100, 7);
        graph.record_average_response_time_per_second("GET /".to_string(), 0, 2);
        graph.record_average_response_time_per_second("GET /".to_string(), 1, 2);
        graph.record_average_response_time_per_second("GET /".to_string(), 2, 5);
        graph.record_average_response_time_per_second("GET /".to_string(), 5, 2);
        assert_eq!(
            graph
                .average_response_time_per_second
                .get("GET /")
                .unwrap()
                .data
                .len(),
            101
        );
        assert_eq!(
            graph
                .average_response_time_per_second
                .get("GET /")
                .unwrap()
                .data[0]
                .average,
            3.5
        );
        assert_eq!(
            graph
                .average_response_time_per_second
                .get("GET /")
                .unwrap()
                .data[1]
                .average,
            1.5
        );
        assert_eq!(
            graph
                .average_response_time_per_second
                .get("GET /")
                .unwrap()
                .data[2]
                .average,
            6.166667
        );
        assert_eq!(
            graph
                .average_response_time_per_second
                .get("GET /")
                .unwrap()
                .data[3]
                .average,
            0.
        );
        assert_eq!(
            graph
                .average_response_time_per_second
                .get("GET /")
                .unwrap()
                .data[4]
                .average,
            0.
        );
        assert_eq!(
            graph
                .average_response_time_per_second
                .get("GET /")
                .unwrap()
                .data[5]
                .average,
            2.
        );
        assert_eq!(
            graph
                .average_response_time_per_second
                .get("GET /")
                .unwrap()
                .data[100]
                .average,
            7.
        );
        for second in 6..100 {
            assert_eq!(
                graph
                    .average_response_time_per_second
                    .get("GET /")
                    .unwrap()
                    .data[second]
                    .average,
                0.
            );
        }
        assert_eq!(
            graph
                .average_response_time_per_second
                .get("GET /")
                .unwrap()
                .total(),
            4.8125005
        );
    }

    #[test]
    #[allow(clippy::float_cmp)]
    fn test_record_average_response_time_per_second_batch() {
        let mut graph = GraphData::new();

        // Batch merge: 3 requests with total_time 60.0 → avg 20.0
        graph.record_average_response_time_per_second_batch("GET /", 0, 60.0, 3);
        let avg = graph
            .average_response_time_per_second
            .get("GET /")
            .unwrap()
            .get(0);
        assert_eq!(avg.average, 20.0);
        assert_eq!(avg.count, 3);

        // Add a single individual request with response time 40.
        graph.record_average_response_time_per_second("GET /".to_string(), 0, 40);
        let avg = graph
            .average_response_time_per_second
            .get("GET /")
            .unwrap()
            .get(0);
        // Weighted merge: (3 * 20 + 1 * 40) / 4 = 25.0
        assert_eq!(avg.average, 25.0);
        assert_eq!(avg.count, 4);

        // Another batch at a different second.
        graph.record_average_response_time_per_second_batch("GET /", 5, 100.0, 2);
        let avg = graph
            .average_response_time_per_second
            .get("GET /")
            .unwrap()
            .get(5);
        assert_eq!(avg.average, 50.0);
        assert_eq!(avg.count, 2);

        // Merge a second batch into the same second.
        graph.record_average_response_time_per_second_batch("GET /", 5, 200.0, 2);
        let avg = graph
            .average_response_time_per_second
            .get("GET /")
            .unwrap()
            .get(5);
        // Weighted: (2 * 50 + 2 * 100) / 4 = 75.0
        assert_eq!(avg.average, 75.0);
        assert_eq!(avg.count, 4);
    }

    #[test]
    fn test_record_transactions_per_second() {
        // Should be initialized with empty transactions per second vector.
        let mut graph = GraphData::new();
        assert_eq!(graph.transactions_per_second.data.len(), 0);

        graph.record_transactions_per_second(0);
        graph.record_transactions_per_second(0);
        graph.record_transactions_per_second(0);
        graph.record_transactions_per_second(1);
        graph.record_transactions_per_second(2);
        graph.record_transactions_per_second(2);
        graph.record_transactions_per_second(2);
        graph.record_transactions_per_second(2);
        graph.record_transactions_per_second(2);
        assert_eq!(graph.transactions_per_second.data.len(), 3);
        assert_eq!(graph.transactions_per_second.data[0], 3);
        assert_eq!(graph.transactions_per_second.data[1], 1);
        assert_eq!(graph.transactions_per_second.data[2], 5);
        assert_eq!(graph.transactions_per_second.total(), 9);

        graph.record_transactions_per_second(100);
        graph.record_transactions_per_second(100);
        graph.record_transactions_per_second(100);
        graph.record_transactions_per_second(0);
        graph.record_transactions_per_second(1);
        graph.record_transactions_per_second(2);
        graph.record_transactions_per_second(5);
        assert_eq!(graph.transactions_per_second.data.len(), 101);
        assert_eq!(graph.transactions_per_second.data[0], 4);
        assert_eq!(graph.transactions_per_second.data[1], 2);
        assert_eq!(graph.transactions_per_second.data[2], 6);
        assert_eq!(graph.transactions_per_second.data[3], 0);
        assert_eq!(graph.transactions_per_second.data[4], 0);
        assert_eq!(graph.transactions_per_second.data[5], 1);
        assert_eq!(graph.transactions_per_second.data[100], 3);
        for second in 6..100 {
            assert_eq!(graph.transactions_per_second.data[second], 0);
        }
        assert_eq!(graph.transactions_per_second.total(), 16);
    }

    #[test]
    fn test_record_users_per_second() {
        // Should be initialized with empty transactions per second vector.
        let mut graph = GraphData::new();
        assert_eq!(graph.users_per_second.data.len(), 0);

        graph.record_users_per_second(1, 0);
        graph.record_users_per_second(1, 1);
        graph.record_users_per_second(2, 2);
        assert_eq!(graph.users_per_second.data.len(), 3);
        assert_eq!(graph.users_per_second.data[0], 1);
        assert_eq!(graph.users_per_second.data[1], 1);
        assert_eq!(graph.users_per_second.data[2], 2);
        assert_eq!(graph.users_per_second.total(), 6);

        graph.record_users_per_second(5, 5);
        graph.record_users_per_second(10, 37);
        assert_eq!(graph.users_per_second.data.len(), 38);
        assert_eq!(graph.users_per_second.data[0], 1);
        assert_eq!(graph.users_per_second.data[1], 1);
        assert_eq!(graph.users_per_second.data[2], 2);
        assert_eq!(graph.users_per_second.data[3], 2);
        assert_eq!(graph.users_per_second.data[4], 2);
        assert_eq!(graph.users_per_second.data[5], 5);
        assert_eq!(graph.users_per_second.data[37], 10);
        for second in 6..36 {
            assert_eq!(graph.users_per_second.data[second], 5);
        }
        assert_eq!(graph.users_per_second.total(), 187);
    }

    #[test]
    fn test_export_series_window_empty() {
        let mut graph = GraphData::new();
        let window = graph.export_series_window(300);
        assert_eq!(window, SeriesWindow::empty());
        assert_eq!(window.start_second, 0);
        assert!(window.rps.is_empty());
        assert!(window.fps.is_empty());
        assert!(window.users.is_empty());
        assert!(window.avg_latency_ms.is_empty());
    }

    #[test]
    fn test_export_series_window_multi_key_reduction() {
        let mut graph = GraphData::new();

        // Key A: 2 requests at second 0 (latency 10ms each), 1 request at second 2 (latency 30ms).
        graph.record_requests_per_second("GET /a", 0);
        graph.record_requests_per_second("GET /a", 0);
        graph.record_average_response_time_per_second("GET /a".to_string(), 0, 10);
        graph.record_average_response_time_per_second("GET /a".to_string(), 0, 10);
        graph.record_requests_per_second("GET /a", 2);
        graph.record_average_response_time_per_second("GET /a".to_string(), 2, 30);

        // Key B: 3 requests at second 2 only (latency 50ms), disjoint from A's second-0 traffic.
        graph.record_requests_per_second("GET /b", 2);
        graph.record_requests_per_second("GET /b", 2);
        graph.record_requests_per_second("GET /b", 2);
        graph.record_average_response_time_per_second("GET /b".to_string(), 2, 50);
        graph.record_average_response_time_per_second("GET /b".to_string(), 2, 50);
        graph.record_average_response_time_per_second("GET /b".to_string(), 2, 50);

        // One error on key B at second 2.
        graph.record_errors_per_second("GET /b", 2);

        graph.record_users_per_second(1, 0);
        graph.record_users_per_second(4, 2);

        let window = graph.export_series_window(300);
        assert_eq!(window.start_second, 0);
        assert_eq!(window.rps.len(), 3);
        assert_eq!(window.fps.len(), 3);
        assert_eq!(window.users.len(), 3);
        assert_eq!(window.avg_latency_ms.len(), 3);

        // Second 0: 2 RPS from A only; latency 10ms.
        assert!((window.rps[0] - 2.0).abs() < f64::EPSILON);
        assert!((window.fps[0] - 0.0).abs() < f64::EPSILON);
        assert_eq!(window.users[0], 1);
        assert!((window.avg_latency_ms[0] - 10.0).abs() < 0.001);

        // Second 1: empty → 0 (gap filled by users maintain-last, but RPS/latency 0).
        assert!((window.rps[1] - 0.0).abs() < f64::EPSILON);
        assert!((window.fps[1] - 0.0).abs() < f64::EPSILON);
        // users_per_second.set_and_maintain_last fills gap with last value (1).
        assert_eq!(window.users[1], 1);
        assert!((window.avg_latency_ms[1] - 0.0).abs() < f64::EPSILON);

        // Second 2: 1 (A) + 3 (B) = 4 RPS; 1 error; weighted latency = (30*1 + 50*3)/4 = 45.
        assert!((window.rps[2] - 4.0).abs() < f64::EPSILON);
        assert!((window.fps[2] - 1.0).abs() < f64::EPSILON);
        assert_eq!(window.users[2], 4);
        assert!((window.avg_latency_ms[2] - 45.0).abs() < 0.001);
    }

    #[test]
    fn test_export_series_window_trailing_window_and_empty_seconds() {
        let mut graph = GraphData::new();
        // Activity only at seconds 0 and 5; window of 3 ending at 5 → seconds 3,4,5.
        graph.record_requests_per_second("GET /", 0);
        graph.record_requests_per_second("GET /", 5);
        graph.record_requests_per_second("GET /", 5);
        graph.record_average_response_time_per_second("GET /".to_string(), 0, 100);
        graph.record_average_response_time_per_second("GET /".to_string(), 5, 20);
        graph.record_average_response_time_per_second("GET /".to_string(), 5, 40);
        graph.record_users_per_second(2, 0);
        graph.record_users_per_second(8, 5);

        let window = graph.export_series_window(3);
        assert_eq!(window.start_second, 3);
        assert_eq!(window.rps.len(), 3);
        // Seconds 3 and 4 empty for RPS → 0.
        assert!((window.rps[0] - 0.0).abs() < f64::EPSILON);
        assert!((window.rps[1] - 0.0).abs() < f64::EPSILON);
        assert!((window.rps[2] - 2.0).abs() < f64::EPSILON);
        assert!((window.avg_latency_ms[0] - 0.0).abs() < f64::EPSILON);
        assert!((window.avg_latency_ms[1] - 0.0).abs() < f64::EPSILON);
        // Average of 20 and 40 = 30.
        assert!((window.avg_latency_ms[2] - 30.0).abs() < 0.001);
        // Users: 2 at second 0, then set_and_maintain fills 1..4 with 2 when
        // recording 8 at second 5. Window seconds 3,4,5 → [2, 2, 8].
        assert_eq!(window.users, vec![2, 2, 8]);
    }

    #[test]
    fn test_export_series_users_hold_when_rps_leads() {
        // Regression: Active users chart dropped to 0 near the right edge because
        // end_second came from RPS while users series lagged (RecordUsers is
        // main-loop only; drain_pending can advance RPS further).
        let mut graph = GraphData::new();
        for s in 0..=50 {
            graph.record_users_per_second(10, s);
        }
        // RPS continues through second 56 with no further RecordUsers.
        for s in 0..=56 {
            graph.record_requests_per_second("GET /", s);
        }

        let window = graph.export_series_window(300);
        assert_eq!(window.start_second, 0);
        assert_eq!(window.rps.len(), 57);
        assert_eq!(window.users.len(), 57);
        // Trailing seconds past last RecordUsers must hold 10, not drop to 0.
        for (i, &u) in window.users.iter().enumerate() {
            assert_eq!(u, 10, "users[{i}] should hold last known count 10, got {u}");
        }
        // RPS still present at the end (not all zeros).
        assert!(window.rps[56] > 0.0);
    }

    #[test]
    fn test_export_series_users_monotonic_ramp_without_forward_stamp() {
        // Regression: stamping active_users forward to the RPS/duration second
        // (set_and_maintain_last gap-fill with a stale count, then partial
        // overwrites from real RecordUsers) produced V-shaped glitches mid-ramp.
        // Only sequential RecordUsers samples should define the series; export
        // holds the last known level past the end.
        let mut graph = GraphData::new();
        // Steady 10 users.
        for s in 0..=60 {
            graph.record_users_per_second(10, s);
        }
        // Clean increase 10 → 20 over seconds 61..70.
        for (s, users) in (61..=70).zip(11..=20) {
            graph.record_users_per_second(users, s);
        }
        // RPS leads past the last users sample (as drain_pending does).
        for s in 0..=78 {
            graph.record_requests_per_second("GET /", s);
        }

        let window = graph.export_series_window(300);
        assert_eq!(window.users.len(), 79);
        // Steady region.
        for s in 0..=60 {
            assert_eq!(window.users[s], 10, "steady users[{s}]");
        }
        // Monotonic ramp — no dips.
        for s in 61..=70 {
            let expected = (s - 60 + 10) as u64; // 11..20
            assert_eq!(window.users[s], expected, "ramp users[{s}]");
            if s > 61 {
                assert!(
                    window.users[s] >= window.users[s - 1],
                    "ramp must be non-decreasing at {s}: {} < {}",
                    window.users[s],
                    window.users[s - 1]
                );
            }
        }
        // Trailing hold at 20 (not 0, not a dip).
        for s in 71..=78 {
            assert_eq!(window.users[s], 20, "hold users[{s}]");
        }
    }

    #[test]
    fn test_out_of_order_users_write_creates_v_glitch() {
        // Documents why user stamps must be monotonic in time: gap-fill with a
        // jump forward, then overwriting intermediate seconds, leaves stale dips.
        let mut graph = GraphData::new();
        graph.record_users_per_second(10, 60);
        // Bad stamp: jump to second 70 with live count 20 (fills 61..69 with 10).
        graph.record_users_per_second(20, 70);
        // Real hatch samples only overwrite some intermediate seconds.
        graph.record_users_per_second(15, 65);
        graph.record_users_per_second(18, 68);

        let window = graph.export_series_window(300);
        // 60=10, 61-64=10 (stale fill), 65=15, 66-67=10 (stale!), 68=18, 69=10, 70=20
        assert_eq!(window.users[60], 10);
        assert_eq!(window.users[65], 15);
        assert_eq!(
            window.users[66], 10,
            "stale gap-fill creates a dip after 15"
        );
        assert_eq!(window.users[68], 18);
        assert_eq!(
            window.users[69], 10,
            "stale gap-fill creates a dip before 20"
        );
        assert_eq!(window.users[70], 20);
        // Confirm the V: 15 → 10 → 18 is non-monotonic mid-ramp.
        assert!(window.users[66] < window.users[65] && window.users[66] < window.users[68]);
    }

    #[test]
    fn test_record_users_monotonic_rejects_backwards_writes() {
        let mut graph = GraphData::new();
        graph.record_users_per_second(10, 5);
        graph.record_users_per_second(20, 10);
        // Would create a V-glitch if applied — must be ignored.
        graph.record_users_monotonic(15, 7);
        let window = graph.export_series_window(300);
        assert_eq!(window.users[7], 10, "backwards write must not rewrite past");
        assert_eq!(window.users[10], 20);
        // Forward monotonic stamp is allowed and pins the edge.
        graph.record_users_monotonic(25, 12);
        let window = graph.export_series_window(300);
        assert_eq!(window.users[10], 20);
        assert_eq!(window.users[11], 20);
        assert_eq!(window.users[12], 25);
    }

    #[test]
    fn test_reset_rate_series_preserves_users_history() {
        let mut graph = GraphData::new();
        for (s, users) in (0..=5).zip(1..=6) {
            graph.record_users_per_second(users, s);
        }
        graph.record_requests_per_second("GET /", 3);
        graph.record_errors_per_second("GET /", 3);
        graph.record_average_response_time_per_second("GET /".into(), 3, 42);
        graph.record_transactions_per_second(3);
        graph.record_scenarios_per_second(3);

        graph.reset_rate_series(0);

        let window = graph.export_series_window(300);
        // Users ramp intact.
        assert_eq!(window.users, vec![1, 2, 3, 4, 5, 6]);
        // Rate series wiped.
        assert!(window.rps.iter().all(|&v| v == 0.0));
        assert!(window.fps.iter().all(|&v| v == 0.0));
        assert!(window.avg_latency_ms.iter().all(|&v| v == 0.0));
    }

    #[test]
    fn test_reset_rate_series_preserves_pruned_origin() {
        // After a long dashboard-only soak, prune advances origin. A subsequent
        // increase→maintain reset must not recreate rate series at origin=0 or the
        // next absolute-second sample allocates a multi-hour-sized vector.
        let mut graph = GraphData::new();
        for s in 0..=900 {
            graph.record_users_per_second(10, s);
            graph.record_requests_per_second("GET /", s);
        }
        graph.prune_to_window(600);
        assert!(graph.users_per_second.origin > 0);
        let origin_before = graph.users_per_second.origin;

        graph.reset_rate_series(0);
        assert_eq!(graph.requests_per_second.origin, origin_before);
        assert_eq!(graph.transactions_per_second.origin, origin_before);

        // Sample at a late absolute second: storage length stays near the window,
        // not ~absolute second from 0.
        let late = origin_before + 50;
        graph.record_requests_per_second("GET /new", late);
        let stored = graph
            .requests_per_second
            .series
            .get("GET /new")
            .expect("series")
            .data
            .len();
        assert!(
            stored <= 51,
            "expected compact storage after origin-preserving reset, got len={stored}",
            stored = stored
        );
    }

    #[test]
    fn test_prune_aligns_idle_key_origin_on_reappear() {
        // Idle request names only receive early samples, so length-based prune
        // leaves their origin at 0. After a long soak, re-activating that key
        // must not expand from absolute second 0.
        let mut graph = GraphData::new();
        graph.record_requests_per_second("GET /idle", 0);
        graph.record_requests_per_second("GET /idle", 1);
        graph.record_errors_per_second("GET /idle", 0);
        graph.record_average_response_time_per_second("GET /idle".into(), 0, 10);

        for s in 0..=900 {
            graph.record_users_per_second(10, s);
            graph.record_requests_per_second("GET /active", s);
            graph.record_transactions_per_second(s);
            graph.record_scenarios_per_second(s);
        }
        graph.prune_to_window(600);

        let origin = graph.users_per_second.origin;
        assert!(origin > 0, "users origin should advance after prune");
        assert_eq!(
            graph
                .requests_per_second
                .series
                .get("GET /idle")
                .expect("idle rps")
                .origin,
            origin
        );
        assert_eq!(
            graph
                .errors_per_second
                .series
                .get("GET /idle")
                .expect("idle errors")
                .origin,
            origin
        );
        assert_eq!(
            graph
                .average_response_time_per_second
                .get("GET /idle")
                .expect("idle latency")
                .origin,
            origin
        );

        let late = origin + 50;
        graph.record_requests_per_second("GET /idle", late);
        graph.record_errors_per_second("GET /idle", late);
        graph.record_average_response_time_per_second("GET /idle".into(), late, 12);

        for (label, len) in [
            (
                "rps",
                graph
                    .requests_per_second
                    .series
                    .get("GET /idle")
                    .unwrap()
                    .data
                    .len(),
            ),
            (
                "errors",
                graph
                    .errors_per_second
                    .series
                    .get("GET /idle")
                    .unwrap()
                    .data
                    .len(),
            ),
            (
                "latency",
                graph
                    .average_response_time_per_second
                    .get("GET /idle")
                    .unwrap()
                    .data
                    .len(),
            ),
        ] {
            assert!(
                len <= 51,
                "idle {label} series must stay compact after reappear, got len={len}",
                label = label,
                len = len
            );
        }
    }

    #[test]
    fn test_export_series_window_until_extends_to_hint() {
        let mut graph = GraphData::new();
        graph.record_users_per_second(7, 2);
        // No RPS; without hint end would be second 2.
        let window = graph.export_series_window_until(300, Some(8));
        assert_eq!(window.users.len(), 9); // seconds 0..8
        assert_eq!(window.users[2], 7);
        // Hold last known level through the hint ("now").
        for s in 3..=8 {
            assert_eq!(window.users[s], 7, "hold users[{s}]");
        }
    }

    /// GraphData with latency histograms, synthetic ones too when `synthetic`.
    fn latency_graph(synthetic: bool) -> GraphData {
        GraphData::for_metrics_processor(true, synthetic, false)
    }

    fn histograms(graph: &mut GraphData) -> &LatencyHistograms {
        graph.move_pending_latency();
        graph.latency_histograms().expect("latency histograms")
    }

    #[test]
    fn latency_batch_merge_equals_direct_recording() {
        let times: [(usize, u64); 6] = [(3, 12), (3, 140), (3, 1400), (4, 7), (4, 90), (4, 2600)];
        // Two batches, so the second finds the request's ring through the
        // index cached on its requests per second entry.
        let mut batches: [HashMap<usize, GooseRequestMetricTimingData>; 2] = Default::default();
        for (i, &(second, time)) in times.iter().enumerate() {
            batches[i % 2].entry(second).or_default().record_time(time);
        }
        let mut merged = latency_graph(false);
        for batch in &batches {
            for (second, data) in batch {
                merged.record_requests_per_second_and_latency_batch(
                    "GET /a",
                    *second,
                    data.counter as u32,
                    Some(data),
                );
            }
        }
        let mut direct = latency_graph(false);
        for &(second, time) in &times {
            direct.record_request_per_second_and_latency("GET /a", second, Some(time));
        }
        assert_eq!(
            merged.requests_per_second.get("GET /a").unwrap().data,
            direct.requests_per_second.get("GET /a").unwrap().data
        );
        for second in [3, 4] {
            let merged = histograms(&mut merged);
            let direct = histograms(&mut direct);
            assert_eq!(merged.all_second(second), direct.all_second(second));
            assert_eq!(
                merged.rings[merged.by_request["GET /a"]].get(second),
                direct.rings[direct.by_request["GET /a"]].get(second)
            );
            let mut expected = SecondHistogram::default();
            for &(_, time) in times.iter().filter(|(s, _)| *s == second) {
                expected.record(time);
            }
            assert_eq!(merged.all_second(second), Some(expected.clone()));
        }
    }

    /// Batches merged through a request's pending counts, in and out of
    /// order, with a jump of several seconds, an earlier second and a time
    /// past 60 seconds, give the rings and the heatmap the same response
    /// times as recording each request directly, whenever they are read.
    #[test]
    fn pending_latency_equals_direct_recording() {
        let batches: [(usize, &[u64]); 8] = [
            (5, &[3, 40]),
            (6, &[7]),
            (5, &[61_500, 2]),
            (6, &[0, 900]),
            (9, &[12]),
            (4, &[33]),
            (10, &[5, 5]),
            (9, &[250]),
        ];
        let mut merged = latency_graph(false);
        let mut direct = latency_graph(false);
        for (i, &(second, times)) in batches.iter().enumerate() {
            let mut data = GooseRequestMetricTimingData::new(None);
            for &time in times {
                data.record_time(time);
                direct.record_request_per_second_and_latency("GET /a", second, Some(time));
            }
            merged.record_requests_per_second_and_latency_batch(
                "GET /a",
                second,
                data.counter as u32,
                Some(&data),
            );
            // Read part way through as well as at the end.
            if i == 3 {
                let merged = histograms(&mut merged);
                let direct = histograms(&mut direct);
                for second in 4..=6 {
                    assert_eq!(merged.all_second(second), direct.all_second(second));
                }
            }
        }
        let window = |graph: &mut GraphData| {
            graph
                .export_series_window_until(300, Some(12))
                .heatmap
                .unwrap()
                .columns
        };
        assert_eq!(window(&mut merged), window(&mut direct));
        for second in 3..=11 {
            let merged = histograms(&mut merged);
            let direct = histograms(&mut direct);
            assert_eq!(
                merged.all_second(second),
                direct.all_second(second),
                "{second}"
            );
            assert_eq!(
                merged.rings[merged.by_request["GET /a"]].get(second),
                direct.rings[direct.by_request["GET /a"]].get(second),
                "{second}"
            );
        }
    }

    #[test]
    fn second_histogram_matches_timing_data() {
        // Every row maps to its own index and back, and the rows meet end to
        // end, past the fixed ones too.
        for index in 0..FIXED_BUCKETS + 50 {
            let (lowest, highest) = display_range(index);
            assert_eq!(display_index(lowest), index, "{index}");
            assert_eq!(display_index(highest), index, "{index}");
            assert_eq!(display_index(highest + 1), index + 1, "{index}");
            assert_eq!(display_index(display_value(index)), index, "{index}");
        }
        assert_eq!(display_index(60_999_999), FIXED_BUCKETS - 1);

        // The same totals as the whole run timing data, each bucket in the
        // row of its last microsecond, and each percentile in the row the
        // whole run's percentile bucket is drawn in, including times past 61
        // seconds (a longer --timeout).
        let mut times: Vec<u64> = Vec::new();
        let mut seed: u64 = 7;
        for _ in 0..5000 {
            seed = seed
                .wrapping_mul(6364136223846793005)
                .wrapping_add(1442695040888963407);
            times.push((seed >> 33) % 90_000 / (1 + (seed >> 60)));
        }
        let mut reference = crate::metrics::GooseRequestMetricTimingData::default();
        let mut histogram = SecondHistogram::default();
        for &time in &times {
            reference.record_time(time);
            histogram.record(time);
        }
        assert!(!histogram.overflow.is_empty());
        let buckets: Vec<(usize, usize)> = histogram.buckets().collect();
        let mut expected: Vec<(usize, usize)> = Vec::new();
        for (&bucket, &count) in &reference.times_us {
            let row = display_index(crate::metrics::buckets::bucket_end(bucket) - 1);
            match expected.last_mut() {
                Some(last) if last.0 == row => last.1 += count,
                _ => expected.push((row, count)),
            }
        }
        assert_eq!(buckets, expected);
        assert_eq!(histogram.counter, reference.counter);
        assert_eq!(histogram.total_time_us, reference.total_time_us);
        assert_eq!(histogram.minimum_time_us, reference.minimum_time_us);
        assert_eq!(histogram.maximum_time_us, reference.maximum_time_us);
        for percent in [0.0, 0.5, 0.95, 0.99, 1.0] {
            assert_eq!(
                display_index(histogram.percentile_us(percent)),
                bucket_row(crate::metrics::buckets::bucket_floor(
                    reference.percentile_us(percent)
                )),
                "p{percent}"
            );
        }

        // Batches merged into an empty histogram give the same histogram.
        let (first, second) = times.split_at(times.len() / 3);
        let mut via_batch = SecondHistogram::default();
        for part in [first, second] {
            let mut batch = SecondHistogram::default();
            for &time in part {
                batch.record(time);
            }
            via_batch.merge(&batch);
        }
        assert_eq!(via_batch, histogram);

        // A bucket past the most one entry holds continues in the next.
        let mut full = SecondHistogram::default();
        full.add_indexed(0, 5, COUNT_MAX as usize + 10);
        full.add_indexed(0, 5, 3);
        full.add_indexed(0, 4, 1);
        assert_eq!(
            full.buckets().collect::<Vec<_>>(),
            vec![(4, 1), (5, COUNT_MAX as usize), (5, 13)]
        );

        // Merging two halves equals recording everything.
        let (first, second) = times.split_at(times.len() / 2);
        let mut a = SecondHistogram::default();
        first.iter().for_each(|&time| a.record(time));
        let mut b = SecondHistogram::default();
        second.iter().for_each(|&time| b.record(time));
        a.merge(&b);
        assert_eq!(a, histogram);
    }

    #[test]
    fn open_second_equals_packed_second() {
        let mut times: Vec<u64> = Vec::new();
        let mut seed: u64 = 11;
        for _ in 0..3000 {
            seed = seed
                .wrapping_mul(6364136223846793005)
                .wrapping_add(1442695040888963407);
            times.push((seed >> 33) % 90_000 / (1 + (seed >> 60)));
        }
        let mut packed = SecondHistogram::default();
        times.iter().for_each(|&time| packed.record(time));

        // Recorded and merged into an open second, read open and packed.
        let mut open = SecondHistogram::open();
        let (first, second) = times.split_at(times.len() / 2);
        first.iter().for_each(|&time| open.record(time));
        let mut batch = SecondHistogram::default();
        second.iter().for_each(|&time| batch.record(time));
        open.merge(&batch);
        assert!(open.entries.is_empty());
        assert_eq!(open, packed);
        assert_eq!(
            open.buckets().collect::<Vec<_>>(),
            packed.buckets().collect::<Vec<_>>()
        );
        for percent in [0.0, 0.5, 0.95, 0.99, 1.0] {
            assert_eq!(open.percentile(percent), packed.percentile(percent));
        }
        open.pack();
        assert!(open.open.is_none());
        assert_eq!(open.entries, packed.entries);
        assert_eq!(open.overflow, packed.overflow);

        // A packed second keeps taking late responses.
        open.record(7);
        packed.record(7);
        assert_eq!(open.entries, packed.entries);

        // An open bucket past the most one entry holds packs into two.
        let mut full = SecondHistogram::open();
        full.add_bucket(5, COUNT_MAX as usize + 10);
        full.add_bucket(4, 1);
        full.pack();
        assert_eq!(
            full.buckets().collect::<Vec<_>>(),
            vec![(4, 1), (5, COUNT_MAX as usize), (5, 10)]
        );
    }

    /// The first [`MAX_REQUEST_SERIES`] request names the metrics thread
    /// sees with a response time get rings, in the order it sees them, even
    /// when their pending counts first move to a ring in another order
    /// (#713).
    #[test]
    fn rings_go_to_the_first_names_seen() {
        let mut graph = GraphData::for_metrics_processor(true, false, false);
        let mut times = GooseRequestMetricTimingData::new(None);
        times.record_time(25);
        let mut whole_run = GooseRequestMetricTimingData::new(None);
        let names: Vec<String> = (0..150).map(|i| format!("GET /n{i}")).collect();
        // First sightings in order, all in second 0.
        for name in &names {
            graph.record_requests_per_second_merging_latency(name, 0, &times, &mut whole_run);
        }
        // Then second 5 in reverse order, which moves each name's pending
        // counts to its ring, last name first.
        for name in names.iter().rev() {
            graph.record_requests_per_second_merging_latency(name, 5, &times, &mut whole_run);
        }
        let histograms = graph.latency.as_ref().unwrap();
        let mut ringed: Vec<&String> = histograms.by_request.keys().collect();
        ringed.sort_by_key(|name| names.iter().position(|n| n == *name));
        let first: Vec<&String> = names.iter().take(MAX_REQUEST_SERIES).collect();
        assert_eq!(ringed, first);
        // In the order seen.
        for (i, name) in names.iter().take(MAX_REQUEST_SERIES).enumerate() {
            assert_eq!(histograms.by_request[name], i, "{}", name);
        }
    }

    /// A request name's entry stays small, and its pending response times
    /// are allocated only when the heatmap records one: never without the
    /// heatmap, and never for an error (#713).
    #[test]
    fn pending_latency_is_allocated_only_for_the_heatmap() {
        assert!(
            std::mem::size_of::<KeySeries>() <= 64,
            "KeySeries is {} bytes",
            std::mem::size_of::<KeySeries>()
        );
        let mut times = GooseRequestMetricTimingData::new(None);
        times.record_time(12);
        times.record_time(70_000);
        for heatmap in [false, true] {
            let mut graph = GraphData::for_metrics_processor(heatmap, heatmap, false);
            let mut whole_run = GooseRequestMetricTimingData::new(None);
            for second in 0..5 {
                graph.record_requests_per_second_merging_latency(
                    "GET /a",
                    second,
                    &times,
                    &mut whole_run,
                );
                graph.record_requests_per_second_and_latency_batch(
                    "GET /b",
                    second,
                    2,
                    Some(&times),
                );
                graph.record_request_per_second_and_latency("GET /c", second, Some(9));
                graph.record_errors_per_second("GET /c 500 Internal Server Error", second);
            }
            graph.move_pending_latency();
            for (key, entry) in &graph.requests_per_second.series {
                assert_eq!(entry.pending.is_some(), heatmap, "{}", key);
            }
            assert_eq!(graph.errors_per_second.series.len(), 1);
            for (key, entry) in &graph.errors_per_second.series {
                assert!(entry.pending.is_none(), "{}", key);
            }
        }
    }

    #[test]
    fn latency_ring_packs_seconds_two_back() {
        let mut ring = LatencyRing::new();
        ring.record(10, 20, 1);
        ring.record(11, 20, 2);
        let is_open = |ring: &LatencyRing, second| ring.get(second).unwrap().open.is_some();
        assert!(is_open(&ring, 10) && is_open(&ring, 11));
        ring.record(12, 20, 3);
        assert!(!is_open(&ring, 10));
        assert!(is_open(&ring, 11) && is_open(&ring, 12));
        // A late response to a packed second still counts, and marks it
        // changed.
        ring.record(10, 30, 4);
        assert_eq!(ring.get(10).unwrap().counter, 2);
        assert_eq!(ring.slot(10).unwrap().rev, 4);
        assert_eq!(ring.slot(11).unwrap().rev, 2);
        // After a gap every older open second is packed.
        ring.record(40, 20, 5);
        assert!(!is_open(&ring, 11) && !is_open(&ring, 12));
        assert_eq!(ring.open, vec![40]);
        ring.clear();
        assert!(ring.open.is_empty());
    }

    #[test]
    fn latency_ring_drops_samples_older_than_its_slot() {
        let mut ring = LatencyRing::new();
        ring.record(400, 25, 1);
        // Second 100 maps to the same slot as 400 (300 slots).
        ring.record(100, 10, 2);
        ring.merge_timing(
            100,
            &{
                let mut data = GooseRequestMetricTimingData::default();
                data.record_time(10);
                data
            },
            3,
        );
        assert!(ring.get(100).is_none());
        let held = ring.get(400).expect("slot keeps second 400");
        assert_eq!(held.counter, 1);
        assert_eq!(held.maximum_time_us, 25_000);
        // A sample too old to keep does not mark the slot changed.
        assert_eq!(ring.slot(400).unwrap().rev, 1);
        // A newer second replaces the older one.
        ring.record(700, 5, 4);
        assert!(ring.get(400).is_none());
        assert_eq!(ring.get(700).map(|d| d.counter), Some(1));
    }

    /// The `(index, count)` pairs of `second`'s measured buckets.
    fn measured_pairs(heatmap: &Heatmap, second: u64) -> Vec<(u64, u64)> {
        heatmap
            .column(second)
            .map(|column| HeatmapColumn::pairs(&column.measured))
            .unwrap_or_default()
    }

    /// Rows from 5 to 61 seconds are 1 second wide, so a 5.1 s time is in
    /// the 5 to 6 s row, not a 5 to 7 s one.
    #[test]
    fn heatmap_rows_from_five_seconds_are_one_second_wide() {
        assert_eq!(
            display_range(display_index(5_100_000)),
            (5_000_000, 5_999_999)
        );
        assert_eq!(
            display_range(display_index(60_999_999)),
            (60_000_000, 60_999_999)
        );
        assert_eq!(display_index(60_999_999), FIXED_BUCKETS - 1);
        assert_eq!(FIXED_BUCKETS, 237);
    }

    #[test]
    fn heatmap_bucket_tables_follow_the_rows() {
        let table = bucket_table_us(FIXED_BUCKETS);
        assert_eq!(table.len(), FIXED_BUCKETS + 1);
        // Every measured time from 0 to past 61 seconds falls in exactly the
        // row whose range holds it, and the fixed rows meet end to end.
        for (index, entry) in table.iter().enumerate() {
            let [value, lowest, highest] = *entry;
            assert!(lowest <= value && value <= highest, "{:?}", entry);
            for time in [lowest, value, highest] {
                assert_eq!(display_index(time), index, "{entry:?}");
            }
            if index > 0 && index < FIXED_BUCKETS {
                assert_eq!(table[index - 1][2] + 1, lowest, "{entry:?}");
            }
        }
        for (index, entry) in [
            (0, [50, 0, 99]),
            (9, [950, 900, 999]),
            (10, [1_500, 1_000, 1_999]),
            (72, [63_500, 63_000, 63_999]),
            (73, [65_000, 64_000, 65_999]),
            (91, [105_000, 100_000, 109_999]),
            (131, [525_000, 500_000, 549_999]),
            (141, [1_050_000, 1_000_000, 1_099_999]),
            (145, [1_450_000, 1_400_000, 1_499_999]),
            (146, [1_550_000, 1_500_000, 1_599_999]),
            (181, [5_500_000, 5_000_000, 5_999_999]),
            (182, [6_500_000, 6_000_000, 6_999_999]),
            (236, [60_500_000, 60_000_000, 60_999_999]),
            (237, [61_170_848, 61_000_000, 61_341_695]),
        ] {
            assert_eq!(table[index], entry, "index {index}");
        }
        assert_eq!(bucket_table_us(0).len(), FIXED_BUCKETS);
    }

    /// Every fixed row takes at least one timing data bucket, so none stays
    /// empty under a spread of times.
    #[test]
    fn every_fixed_row_takes_a_bucket() {
        let mut taken = [false; FIXED_BUCKETS];
        let mut bucket = 0;
        while bucket < FIXED_END_US {
            if let Some(row) = taken.get_mut(bucket_row(bucket)) {
                *row = true;
            }
            bucket = crate::metrics::buckets::bucket_end(bucket);
        }
        let empty: Vec<usize> = (0..FIXED_BUCKETS).filter(|&row| !taken[row]).collect();
        assert!(empty.is_empty(), "rows no bucket lands in: {:?}", empty);
    }

    /// A column's cells are kept from one heatmap to the next, and rebuilt
    /// when its second changes.
    #[test]
    fn heatmap_columns_rebuild_only_when_changed() {
        let mut graph = latency_graph(false);
        graph.record_requests_per_second("GET /", 1);
        graph.record_latency("GET /", 1, 5);
        let first = graph
            .export_series_window_until(300, Some(4))
            .heatmap
            .unwrap();
        // 5 ms is row 14, past the ten rows under a millisecond.
        assert_eq!(measured_pairs(&first, 1), [(14, 1)]);
        let again = graph
            .export_series_window_until(300, Some(4))
            .heatmap
            .unwrap();
        assert_eq!(again.columns, first.columns);
        graph.record_latency("GET /", 1, 6);
        let changed = graph
            .export_series_window_until(300, Some(4))
            .heatmap
            .unwrap();
        assert_eq!(measured_pairs(&changed, 1), [(14, 1), (15, 1)]);
    }

    #[test]
    fn heatmap_sends_only_changed_seconds() {
        let mut graph = latency_graph(false);
        for (second, time) in [(1, 5), (2, 7), (2, 7), (2, 120)] {
            graph.record_requests_per_second("GET /", second);
            graph.record_latency("GET /", second, time);
        }
        let first = graph
            .export_series_window_until(300, Some(4))
            .heatmap
            .unwrap();
        assert_eq!(first.since, 0);
        assert_eq!(
            (first.start_second, first.end_second, first.hold_from),
            (0, 4, 3)
        );
        assert_eq!(first.columns.len(), 2);
        assert_eq!(measured_pairs(&first, 1), [(14, 1)]);
        // 7 ms is row 16; 120 ms is in the 120 to 130 ms row, index 93.
        assert_eq!(measured_pairs(&first, 2), [(16, 2), (93, 1)]);
        assert!(first
            .columns
            .iter()
            .all(|column| column.synthetic.is_empty()));
        assert_eq!(first.buckets_us.len(), FIXED_BUCKETS);

        // Nothing changed: no columns.
        let cursor = HeatmapCursor {
            generation: first.generation,
            rev: first.rev,
        };
        let same = graph
            .export_series_window_with_heatmap(300, Some(4), false, Some(cursor))
            .heatmap
            .unwrap();
        assert_eq!((same.since, same.rev), (first.rev, first.rev));
        assert!(same.columns.is_empty());

        // A late response to second 1 and a new second 3: only those, whole.
        graph.record_latency("GET /", 1, 6);
        graph.record_latency("GET /", 3, 75_000);
        let delta = graph
            .export_series_window_with_heatmap(300, Some(4), false, Some(cursor))
            .heatmap
            .unwrap();
        assert_eq!(delta.since, first.rev);
        assert_eq!(
            delta
                .columns
                .iter()
                .map(|column| column.second)
                .collect::<Vec<_>>(),
            [1, 3]
        );
        assert_eq!(measured_pairs(&delta, 1), [(14, 1), (15, 1)]);
        // Past 61 seconds a row per timing data bucket: 75 s is index 256,
        // and the table grows to cover it.
        assert_eq!(measured_pairs(&delta, 3), [(256, 1)]);
        assert_eq!(delta.buckets_us.len(), 257);
        assert_eq!(delta.buckets_us[256], [74_973_184, 74_448_896, 75_497_471]);

        // A cursor of another generation, or from the future, gets the whole
        // window.
        for stale in [
            HeatmapCursor {
                generation: cursor.generation + 1,
                rev: cursor.rev,
            },
            HeatmapCursor {
                generation: cursor.generation,
                rev: delta.rev + 1,
            },
        ] {
            let whole = graph
                .export_series_window_with_heatmap(300, Some(4), false, Some(stale))
                .heatmap
                .unwrap();
            assert_eq!(whole.since, 0);
            assert_eq!(whole.columns.len(), 3);
        }

        // A metrics reset starts a new generation with no columns.
        graph.reset_rate_series(0);
        let reset = graph
            .export_series_window_with_heatmap(300, Some(4), false, Some(cursor))
            .heatmap
            .unwrap();
        assert_ne!(reset.generation, cursor.generation);
        assert_eq!(reset.since, 0);
        assert!(reset.columns.is_empty());
    }

    #[test]
    fn heatmap_slow_second_and_split_buckets() {
        let mut graph = latency_graph(false);
        for _ in 0..10 {
            graph.record_requests_per_second("GET /slow", 5);
            graph.record_latency("GET /slow", 5, 1400);
        }
        let heatmap = graph
            .export_series_window_until(300, Some(7))
            .heatmap
            .unwrap();
        assert_eq!(measured_pairs(&heatmap, 5), [(145, 10)]);

        // A bucket past the most one packed entry holds is still one cell.
        let mut full = SecondHistogram::default();
        full.add_indexed(0, 5, COUNT_MAX as usize + 10);
        full.add_indexed(0, 4, 1);
        assert_eq!(full.heat_cells(), [4, 1, 5, COUNT_MAX as u64 + 10]);
    }

    #[test]
    fn heatmap_window_gaps_and_hold() {
        let mut graph = latency_graph(false);
        for second in [2, 4, 9, 10] {
            graph.record_requests_per_second("GET /", second);
            graph.record_latency("GET /", second, 20);
        }
        let heatmap = graph
            .export_series_window_until(300, Some(10))
            .heatmap
            .unwrap();
        // Seconds without requests have no column.
        assert_eq!(
            heatmap.columns.iter().map(|c| c.second).collect::<Vec<_>>(),
            [2, 4, 9, 10]
        );
        // The two newest seconds, from hint - 1, are held back.
        assert_eq!(heatmap.hold_from, 9);
        // A request filed one second past the hint extends the window; the
        // hold still starts at hint - 1.
        graph.record_requests_per_second("GET /", 11);
        graph.record_latency("GET /", 11, 20);
        let heatmap = graph
            .export_series_window_until(300, Some(10))
            .heatmap
            .unwrap();
        assert_eq!((heatmap.end_second, heatmap.hold_from), (11, 9));
        // Nothing recorded yet: an empty heatmap, not none.
        let empty = latency_graph(false)
            .export_series_window_until(300, None)
            .heatmap
            .unwrap();
        assert!(empty.columns.is_empty());
    }

    #[test]
    fn latency_keeps_first_hundred_names() {
        let mut graph = latency_graph(true);
        for i in 0..=MAX_REQUEST_SERIES {
            graph.record_latency(&format!("GET /{i}"), 1, 10);
        }
        let latency = histograms(&mut graph);
        assert_eq!(latency.by_request.len(), MAX_REQUEST_SERIES);
        assert!(!latency.by_request.contains_key("GET /100"));
        // The 101st name still counts toward all requests.
        assert_eq!(
            latency.all_second(1).map(|d| d.counter),
            Some(MAX_REQUEST_SERIES + 1)
        );
        // No synthetic ring for a name without a measured one.
        graph.record_synthetic_latency("GET /100", 1, 500);
        graph.record_synthetic_latency("GET /0", 1, 500);
        let (all, by_request) = histograms(&mut graph).synthetic.as_ref().unwrap();
        assert_eq!(all.get(1).map(|d| d.counter), Some(2));
        assert!(by_request.contains_key("GET /0"));
        assert!(!by_request.contains_key("GET /100"));

        let keys = graph.latency_series_keys();
        assert_eq!(keys.len(), MAX_REQUEST_SERIES);
        assert!(keys.contains("GET /99"));
        assert!(!keys.contains("GET /100"));

        // A metrics reset empties both maps and every ring, so names are
        // admitted again.
        graph.reset_rate_series(0);
        let latency = histograms(&mut graph);
        assert!(latency.by_request.is_empty());
        assert!(latency.all_second(1).is_none());
        let (all, by_request) = latency.synthetic.as_ref().unwrap();
        assert!(by_request.is_empty());
        assert!(all.get(1).is_none());
        graph.record_latency("GET /100", 2, 10);
        assert!(graph.latency_series_keys().contains("GET /100"));
    }

    /// The heatmap's JSON in the worst case, every row in every second of
    /// the window: what changes in a second stays within 200 KB and a 60
    /// second synthetic backfill within 220 KB. A whole window, sent once to
    /// a client with no heatmap, can exceed both, which is why only changes
    /// are sent.
    #[test]
    fn heatmap_worst_case_sizes() {
        let mut graph = latency_graph(true);
        let every_bucket = |count: usize| {
            let mut data = GooseRequestMetricTimingData::default();
            for index in 0..FIXED_BUCKETS {
                data.times_us.insert(
                    crate::metrics::buckets::bucket_floor(display_value(index)),
                    count,
                );
            }
            data.counter = FIXED_BUCKETS * count;
            data.maximum_time_us = 60_000_000;
            data
        };
        let data = every_bucket(12_345);
        for second in 0..300 {
            graph.record_requests_per_second_and_latency_batch(
                "GET /",
                second,
                data.counter as u32,
                Some(&data),
            );
        }
        let size = |heatmap: &Heatmap| serde_json::to_vec(heatmap).unwrap().len();
        let whole = graph
            .export_series_window_with_heatmap(300, Some(299), true, None)
            .heatmap
            .unwrap();
        assert_eq!(whole.columns.len(), 300);
        let cursor = HeatmapCursor {
            generation: whole.generation,
            rev: whole.rev,
        };
        // The next second: a new column, and late responses to the two
        // before it.
        for second in [298, 299, 300] {
            graph.record_requests_per_second_and_latency_batch(
                "GET /",
                second,
                data.counter as u32,
                Some(&data),
            );
        }
        let next = graph
            .export_series_window_with_heatmap(300, Some(300), true, Some(cursor))
            .heatmap
            .unwrap();
        assert_eq!(next.columns.len(), 3);
        // A 60 second stall backfilled with synthetic times in every bucket.
        let cursor = HeatmapCursor {
            generation: next.generation,
            rev: next.rev,
        };
        for second in 241..=300 {
            for index in 0..FIXED_BUCKETS {
                graph.record_synthetic_latency("GET /", second, display_value(index) / 1000);
            }
        }
        let backfill = graph
            .export_series_window_with_heatmap(300, Some(300), true, Some(cursor))
            .heatmap
            .unwrap();
        assert_eq!(backfill.columns.len(), 60);
        eprintln!(
            "worst case heatmap JSON: whole window {} bytes, a second's changes {} bytes, a 60 s backfill {} bytes",
            size(&whole),
            size(&next),
            size(&backfill)
        );
        assert!(size(&next) <= 200 * 1024);
        assert!(size(&backfill) <= 220 * 1024);
        assert!(size(&whole) > 200 * 1024);
    }

    #[test]
    fn heatmap_off_exports_none() {
        let mut graph = GraphData::new();
        graph.record_requests_per_second("GET /", 1);
        graph.record_latency("GET /", 1, 10);
        let window = graph.export_series_window_with_heatmap(300, Some(3), true, None);
        assert_eq!(window.rps.len(), 4);
        assert_eq!(window.heatmap, None);
        assert!(graph.latency_series_keys().is_empty());
        assert!(graph
            .export_request_series("GET".into(), "/".into(), 300, Some(3), true, None)
            .is_none());
    }

    #[test]
    fn heatmap_request_series_export() {
        let mut graph = latency_graph(true);
        for second in [1, 2] {
            for _ in 0..3 {
                graph.record_requests_per_second("GET /a", second);
                graph.record_latency("GET /a", second, 30);
            }
            graph.record_requests_per_second("GET /b", second);
            graph.record_latency("GET /b", second, 900);
        }
        graph.record_synthetic_latency("GET /a", 1, 4000);
        let series = graph
            .export_request_series("GET".into(), "/a".into(), 300, Some(5), true, None)
            .expect("series for /a");
        assert_eq!(series.method, "GET");
        assert_eq!(series.name, "/a");
        let heatmap = &series.heatmap;
        assert_eq!((heatmap.start_second, heatmap.end_second), (0, 5));
        assert_eq!(measured_pairs(heatmap, 1), [(39, 3)]);
        assert_eq!(measured_pairs(heatmap, 2), [(39, 3)]);
        // 4 s is index 171.
        assert_eq!(
            HeatmapColumn::pairs(&heatmap.column(1).unwrap().synthetic),
            [(171, 1)]
        );
        // Not co_active: no synthetic times.
        let series = graph
            .export_request_series("GET".into(), "/a".into(), 300, Some(5), false, None)
            .unwrap();
        assert!(series
            .heatmap
            .columns
            .iter()
            .all(|c| c.synthetic.is_empty()));
        // All requests: both names.
        let all = graph
            .export_series_window_until(300, Some(5))
            .heatmap
            .unwrap();
        assert_eq!(all.counts(1), (4, 0));
        assert!(graph
            .export_request_series("GET".into(), "/missing".into(), 300, Some(5), true, None)
            .is_none());
    }

    #[test]
    fn test_moving_average() {
        let mut moving_average = MovingAverage::new();
        assert_eq!(
            moving_average,
            MovingAverage {
                count: 0,
                average: 0.
            }
        );

        moving_average.add_item(&1.23);
        assert_eq!(
            moving_average,
            MovingAverage {
                count: 1,
                average: 1.23
            }
        );

        moving_average.add_item(&2.34);
        assert_eq!(
            moving_average,
            MovingAverage {
                count: 2,
                average: 1.785
            }
        );

        moving_average.add_item(&89.23);
        assert_eq!(
            moving_average,
            MovingAverage {
                count: 3,
                average: 30.933332
            }
        );

        moving_average.add_item(&12.34);
        assert_eq!(
            moving_average,
            MovingAverage {
                count: 4,
                average: 26.285
            }
        );
    }

    #[test]
    fn test_moving_average_cmp() {
        assert!(
            MovingAverage {
                count: 0,
                average: 0.
            } < MovingAverage {
                count: 0,
                average: 0.1,
            }
        );

        assert!(
            MovingAverage {
                count: 0,
                average: 1.1,
            } > MovingAverage {
                count: 0,
                average: 0.1,
            }
        );

        assert_eq!(
            MovingAverage {
                count: 1,
                average: 1.1,
            },
            MovingAverage {
                count: 2,
                average: 1.1,
            }
        );

        assert!(
            MovingAverage {
                count: 0,
                average: 1.1,
            } != MovingAverage {
                count: 0,
                average: 1.0,
            }
        );
    }

    #[test]
    fn test_add_timestamp_to_html_graph_data() {
        let data = vec![Some(123), Some(234), Some(345), Some(456), Some(567)];
        let empty = HashMap::new();
        let graph: Graph<usize, usize> = Graph::new("html_id", "Label", true, &empty);

        // A series that starts later, as after Save report is turned on during
        // a run, is drawn from its own first second.
        let later = graph.add_timestamp_to_html_graph_data(
            &data[2..],
            2,
            Utc.with_ymd_and_hms(2021, 12, 14, 15, 12, 23).unwrap(),
        );
        let all = graph.add_timestamp_to_html_graph_data(
            &data,
            0,
            Utc.with_ymd_and_hms(2021, 12, 14, 15, 12, 23).unwrap(),
        );
        assert_eq!(later, all[2..]);

        assert_eq!(
            graph.add_timestamp_to_html_graph_data(
                &data,
                0,
                Utc.with_ymd_and_hms(2021, 12, 14, 15, 12, 23).unwrap()
            ),
            vec![
                (
                    Local
                        .timestamp_opt(
                            Utc.with_ymd_and_hms(2021, 12, 14, 15, 12, 23)
                                .unwrap()
                                .timestamp(),
                            0
                        )
                        .unwrap()
                        .format("%Y-%m-%d %H:%M:%S")
                        .to_string(),
                    123
                ),
                (
                    Local
                        .timestamp_opt(
                            Utc.with_ymd_and_hms(2021, 12, 14, 15, 12, 24)
                                .unwrap()
                                .timestamp(),
                            0
                        )
                        .unwrap()
                        .format("%Y-%m-%d %H:%M:%S")
                        .to_string(),
                    234
                ),
                (
                    Local
                        .timestamp_opt(
                            Utc.with_ymd_and_hms(2021, 12, 14, 15, 12, 25)
                                .unwrap()
                                .timestamp(),
                            0
                        )
                        .unwrap()
                        .format("%Y-%m-%d %H:%M:%S")
                        .to_string(),
                    345
                ),
                (
                    Local
                        .timestamp_opt(
                            Utc.with_ymd_and_hms(2021, 12, 14, 15, 12, 26)
                                .unwrap()
                                .timestamp(),
                            0
                        )
                        .unwrap()
                        .format("%Y-%m-%d %H:%M:%S")
                        .to_string(),
                    456
                ),
                (
                    Local
                        .timestamp_opt(
                            Utc.with_ymd_and_hms(2021, 12, 14, 15, 12, 27)
                                .unwrap()
                                .timestamp(),
                            0
                        )
                        .unwrap()
                        .format("%Y-%m-%d %H:%M:%S")
                        .to_string(),
                    567
                )
            ]
        );
    }

    fn expected_graph_html_prefix(
        html_id: &str,
        y_axis_label: &str,
        main_series_label: &str,
    ) -> String {
        format!(
            r#"<div class="graph">
                <div id="{html_id}" style="width: 1000px; height:500px; background: white;"></div>

                <script type="text/javascript">
                    var chartDom = document.getElementById('{html_id}');
                    var myChart = echarts.init(chartDom);

                    myChart.setOption({{
                        color: ['#2c664f', '#5470c6', '#91cc75', '#fac858', '#ee6666', '#73c0de', '#3ba272', '#fc8452', '#9a60b4', '#ea7ccc'],
                        tooltip: {{ trigger: 'axis' }},
                        toolbox: {{
                            feature: {{
                                dataZoom: {{ yAxisIndex: 'none' }},
                                restore: {{}},
                                saveAsImage: {{}}
                            }}
                        }},
                        dataZoom: [
                            {{
                                type: 'inside',
                                start: 0,
                                end: 100,
                                fillerColor: 'rgba(34, 80, 61, 0.25)',
                                selectedDataBackground: {{
                                    lineStyle: {{ color: '#2c664f' }},
                                    areaStyle: {{ color: '#378063' }}
                                }}
                            }},
                            {{
                                start: 0,
                                end: 100,
                                fillerColor: 'rgba(34, 80, 61, 0.25)',
                                selectedDataBackground: {{
                                    lineStyle: {{ color: '#2c664f' }},
                                    areaStyle: {{ color: '#378063' }}
                                }}
                            }},
                        ],
                        xAxis: {{ type: 'time' }},
                        yAxis: {{
                            name: '{y_axis_label}',
                            nameLocation: 'center',
                            nameRotate: 90,
                            nameGap: 45,
                            type: 'value'
                        }},
                        
                        series: [
                            {{
                                name: '{main_series_label}',
                                type: 'line',
                                symbol: 'none',
                                sampling: 'lttb',
                                lineStyle: {{ color: '#2c664f' }},
                                areaStyle: {{ color: '#378063' }},
                                markArea: {{
"#
        )
    }

    #[test]
    fn test_graph_markup() {
        let expected_prefix = expected_graph_html_prefix("graph-rps", "Requests #", "GET /");

        let data: TimeSeries<usize, usize> = TimeSeries {
            origin: 0,
            data: VecDeque::from(vec![123, 111, 99, 134]),
            phantom: PhantomData,
            total: 0,
        };

        let mut graph = HashMap::new();
        graph.insert("GET /".to_string(), data);

        let mut expected = expected_prefix.to_owned();
        expected += format!(
            r#"                                    data: [
                                        
                                    ]
                                }},
                                data: [["{data_series_prefix}:32",123],["{data_series_prefix}:33",111],["{data_series_prefix}:34",99],["{data_series_prefix}:35",134]],
                            }},
                            
                        ]
                    }});
                </script>
            </div>"#,
            data_series_prefix = Local
                .timestamp_opt(Utc.with_ymd_and_hms(2021, 11, 21, 21, 20, 32).unwrap().timestamp(), 0)
                .unwrap()
                .format("%Y-%m-%d %H:%M")
        ).as_str();

        assert_eq!(
            Graph::new("graph-rps", "Requests #", true, &graph,).get_markup(
                &Vec::new(),
                Utc.with_ymd_and_hms(2021, 11, 21, 21, 20, 32).unwrap()
            ),
            expected
        );

        // It should make no difference if we disable granular graphs, since we only have one
        // request.
        assert_eq!(
            Graph::new("graph-rps", "Requests #", false, &graph,).get_markup(
                &Vec::new(),
                Utc.with_ymd_and_hms(2021, 11, 21, 21, 20, 32).unwrap()
            ),
            expected
        );

        let mut expected = expected_prefix;
        expected += format!(
            r#"                                    data: [
                                        [
                            {{
                                xAxis: '{increasing}',
                                itemStyle: {{ borderColor: 'rgba(44, 102, 79, 0.25)', borderWidth: 1 }},
                            }},
                            {{
                                xAxis: '{increasing}'
                            }}
                        ],
                        [
                            {{
                                xAxis: '{increasing}',
                                itemStyle: {{ color: 'rgba(44, 102, 79, 0.05)' }},
                            }},
                            {{
                                xAxis: '{decreasing}'
                            }}
                        ],
                        [
                            {{
                                xAxis: '{decreasing}',
                                itemStyle: {{ borderColor: 'rgba(44, 102, 79, 0.25)', borderWidth: 1 }},
                            }},
                            {{
                                xAxis: '{decreasing}'
                            }}
                        ],[
                            {{
                                xAxis: '{decreasing}',
                                itemStyle: {{ borderColor: 'rgba(179, 65, 65, 0.25)', borderWidth: 1 }},
                            }},
                            {{
                                xAxis: '{decreasing}'
                            }}
                        ],
                        [
                            {{
                                xAxis: '{decreasing}',
                                itemStyle: {{ color: 'rgba(179, 65, 65, 0.05)' }},
                            }},
                            {{
                                xAxis: '{cancelling}'
                            }}
                        ],
                        [
                            {{
                                xAxis: '{cancelling}',
                                itemStyle: {{ borderColor: 'rgba(179, 65, 65, 0.25)', borderWidth: 1 }},
                            }},
                            {{
                                xAxis: '{cancelling}'
                            }}
                        ],[
                            {{
                                xAxis: '{cancelling}',
                                itemStyle: {{ borderColor: 'rgba(179, 65, 65, 0.25)', borderWidth: 1 }},
                            }},
                            {{
                                xAxis: '{cancelling}'
                            }}
                        ],
                        [
                            {{
                                xAxis: '{cancelling}',
                                itemStyle: {{ color: 'rgba(179, 65, 65, 0.05)' }},
                            }},
                            {{
                                xAxis: '{finishing}'
                            }}
                        ],
                        [
                            {{
                                xAxis: '{finishing}',
                                itemStyle: {{ borderColor: 'rgba(179, 65, 65, 0.25)', borderWidth: 1 }},
                            }},
                            {{
                                xAxis: '{finishing}'
                            }}
                        ],
                                    ]
                                }},
                                data: [["{data_series_prefix}:32",123],["{data_series_prefix}:33",111],["{data_series_prefix}:34",99],["{data_series_prefix}:35",134]],
                            }},
                            
                        ]
                    }});
                </script>
            </div>"#,
            data_series_prefix = Local
                .timestamp_opt(Utc.with_ymd_and_hms(2021, 11, 21, 21, 20, 32).unwrap().timestamp(), 0)
                .unwrap()
                .format("%Y-%m-%d %H:%M"),
            increasing = Local
                .timestamp_opt(Utc.with_ymd_and_hms(2021, 11, 21, 21, 20, 32).unwrap().timestamp(), 0)
                .unwrap()
                .format("%Y-%m-%d %H:%M:%S"),
            decreasing = Local
                .timestamp_opt(Utc.with_ymd_and_hms(2021, 11, 21, 21, 20, 33).unwrap().timestamp(), 0)
                .unwrap()
                .format("%Y-%m-%d %H:%M:%S"),
            cancelling = Local
                .timestamp_opt(Utc.with_ymd_and_hms(2021, 11, 21, 21, 20, 34).unwrap().timestamp(), 0)
                .unwrap()
                .format("%Y-%m-%d %H:%M:%S"),
            finishing = Local
                .timestamp_opt(Utc.with_ymd_and_hms(2021, 11, 21, 21, 20, 35).unwrap().timestamp(), 0)
                .unwrap()
                .format("%Y-%m-%d %H:%M:%S"),
        ).as_str();

        let steps = vec![
            TestPlanHistory {
                action: TestPlanStepAction::Increasing,
                timestamp: Utc.with_ymd_and_hms(2021, 11, 21, 21, 20, 32).unwrap(),
                users: 123,
            },
            TestPlanHistory {
                action: TestPlanStepAction::Decreasing,
                timestamp: Utc.with_ymd_and_hms(2021, 11, 21, 21, 20, 33).unwrap(),
                users: 123,
            },
            TestPlanHistory {
                action: TestPlanStepAction::Canceling,
                timestamp: Utc.with_ymd_and_hms(2021, 11, 21, 21, 20, 34).unwrap(),
                users: 123,
            },
            TestPlanHistory {
                action: TestPlanStepAction::Finished,
                timestamp: Utc.with_ymd_and_hms(2021, 11, 21, 21, 20, 35).unwrap(),
                users: 123,
            },
        ];

        assert_eq!(
            Graph::new("graph-rps", "Requests #", true, &graph,).get_markup(
                &steps,
                Utc.with_ymd_and_hms(2021, 11, 21, 21, 20, 32).unwrap()
            ),
            expected
        );

        // It should make no difference if we disable granular graphs, since we only have one
        // request.
        assert_eq!(
            Graph::new("graph-rps", "Requests #", false, &graph,).get_markup(
                &steps,
                Utc.with_ymd_and_hms(2021, 11, 21, 21, 20, 32).unwrap()
            ),
            expected
        );

        let user_data: TimeSeries<usize, usize> = TimeSeries {
            origin: 0,
            data: VecDeque::from(vec![23, 12, 44, 22]),
            phantom: PhantomData,
            total: 0,
        };
        graph.insert("GET /user".to_string(), user_data);

        let markup = Graph::new("graph-rps", "Requests #", true, &graph).get_markup(
            &Vec::new(),
            Utc.with_ymd_and_hms(2021, 11, 21, 21, 20, 32).unwrap(),
        );
        let expected_legend = r#"
                        legend: {
                            type: 'plain',
                            width: '75%',
                            data: ["Total","GET /"#;
        assert!(
            markup.contains(expected_legend),
            "legend {} not found in {}",
            expected_legend,
            markup
        );

        let expected_line = format!(
            r#"{{
                                name: 'Total',
                                type: 'line',
                                symbol: 'none',
                                sampling: 'lttb',
                                lineStyle: {{ color: '#2c664f' }},
                                areaStyle: {{ color: '#378063' }},
                                markArea: {{
                                    data: [
                                        
                                    ]
                                }},
                                data: [["{data_series_prefix}:32",146],["{data_series_prefix}:33",123],["{data_series_prefix}:34",143],["{data_series_prefix}:35",156]],
                            }},"#,
            data_series_prefix = Local
                .timestamp_opt(
                    Utc.with_ymd_and_hms(2021, 11, 21, 21, 20, 32)
                        .unwrap()
                        .timestamp(),
                    0
                )
                .unwrap()
                .format("%Y-%m-%d %H:%M"),
        );
        assert!(
            markup.contains(expected_line.as_str()),
            "line {} not found in {}",
            expected_line,
            markup
        );

        let expected_line = format!(
            r#"{{
                                name: 'GET /',
                                type: 'line',
                                symbol: 'none',
                                sampling: 'lttb',
                                data: [["{data_series_prefix}:32",123],["{data_series_prefix}:33",111],["{data_series_prefix}:34",99],["{data_series_prefix}:35",134]],
                            }},"#,
            data_series_prefix = Local
                .timestamp_opt(
                    Utc.with_ymd_and_hms(2021, 11, 21, 21, 20, 32)
                        .unwrap()
                        .timestamp(),
                    0
                )
                .unwrap()
                .format("%Y-%m-%d %H:%M"),
        );
        assert!(
            markup.contains(expected_line.as_str()),
            "line {} not found in {}",
            expected_line,
            markup
        );

        let expected_line = format!(
            r#"{{
                                name: 'GET /user',
                                type: 'line',
                                symbol: 'none',
                                sampling: 'lttb',
                                data: [["{data_series_prefix}:32",23],["{data_series_prefix}:33",12],["{data_series_prefix}:34",44],["{data_series_prefix}:35",22]],
                            }},"#,
            data_series_prefix = Local
                .timestamp_opt(
                    Utc.with_ymd_and_hms(2021, 11, 21, 21, 20, 32)
                        .unwrap()
                        .timestamp(),
                    0
                )
                .unwrap()
                .format("%Y-%m-%d %H:%M"),
        );
        assert!(
            markup.contains(expected_line.as_str()),
            "line {} not found in {}",
            expected_line,
            markup
        );

        // Now same graph with granular data disabled.
        let expected_nongranular_prefix =
            expected_graph_html_prefix("graph-rps", "Requests #", "Total");
        let mut expected = expected_nongranular_prefix.to_owned();
        expected += format!(
            r#"                                    data: [
                                        
                                    ]
                                }},
                                data: [["{data_series_prefix}:32",146],["{data_series_prefix}:33",123],["{data_series_prefix}:34",143],["{data_series_prefix}:35",156]],
                            }},
                            
                        ]
                    }});
                </script>
            </div>"#,
            data_series_prefix = Local
                .timestamp_opt(Utc.with_ymd_and_hms(2021, 11, 21, 21, 20, 32).unwrap().timestamp(), 0)
                .unwrap()
                .format("%Y-%m-%d %H:%M")
        ).as_str();

        assert_eq!(
            Graph::new("graph-rps", "Requests #", false, &graph,).get_markup(
                &Vec::new(),
                Utc.with_ymd_and_hms(2021, 11, 21, 21, 20, 32).unwrap()
            ),
            expected
        );

        let more_data: TimeSeries<usize, usize> = TimeSeries {
            origin: 0,
            data: VecDeque::from(vec![1, 1, 1, 1]),
            phantom: PhantomData,
            total: 0,
        };
        graph.insert("GET /one".to_string(), more_data.clone());
        graph.insert("GET /two".to_string(), more_data.clone());
        graph.insert("GET /three".to_string(), more_data);

        let markup = Graph::new("graph-rps", "Requests #", true, &graph).get_markup(
            &Vec::new(),
            Utc.with_ymd_and_hms(2021, 11, 21, 21, 20, 32).unwrap(),
        );
        let expected_legend = r#"
                        legend: {
                            type: 'scroll',
                            width: '75%',
                            data: ["Total","GET /"#;
        assert!(
            markup.contains(expected_legend),
            "legend {} not found in {}",
            expected_legend,
            markup
        );

        let expected_line = format!(
            r#"{{
                                name: 'Total',
                                type: 'line',
                                symbol: 'none',
                                sampling: 'lttb',
                                lineStyle: {{ color: '#2c664f' }},
                                areaStyle: {{ color: '#378063' }},
                                markArea: {{
                                    data: [
                                        
                                    ]
                                }},
                                data: [["{data_series_prefix}:32",149],["{data_series_prefix}:33",126],["{data_series_prefix}:34",146],["{data_series_prefix}:35",159]],
                            }},"#,
            data_series_prefix = Local
                .timestamp_opt(
                    Utc.with_ymd_and_hms(2021, 11, 21, 21, 20, 32)
                        .unwrap()
                        .timestamp(),
                    0
                )
                .unwrap()
                .format("%Y-%m-%d %H:%M"),
        );
        assert!(
            markup.contains(expected_line.as_str()),
            "line {} not found in {}",
            expected_line,
            markup
        );

        let expected_line = format!(
            r#"{{
                                name: 'GET /',
                                type: 'line',
                                symbol: 'none',
                                sampling: 'lttb',
                                data: [["{data_series_prefix}:32",123],["{data_series_prefix}:33",111],["{data_series_prefix}:34",99],["{data_series_prefix}:35",134]],
                            }},"#,
            data_series_prefix = Local
                .timestamp_opt(
                    Utc.with_ymd_and_hms(2021, 11, 21, 21, 20, 32)
                        .unwrap()
                        .timestamp(),
                    0
                )
                .unwrap()
                .format("%Y-%m-%d %H:%M"),
        );
        assert!(
            markup.contains(expected_line.as_str()),
            "line {} not found in {}",
            expected_line,
            markup
        );

        let expected_line = format!(
            r#"{{
                                name: 'GET /user',
                                type: 'line',
                                symbol: 'none',
                                sampling: 'lttb',
                                data: [["{data_series_prefix}:32",23],["{data_series_prefix}:33",12],["{data_series_prefix}:34",44],["{data_series_prefix}:35",22]],
                            }},"#,
            data_series_prefix = Local
                .timestamp_opt(
                    Utc.with_ymd_and_hms(2021, 11, 21, 21, 20, 32)
                        .unwrap()
                        .timestamp(),
                    0
                )
                .unwrap()
                .format("%Y-%m-%d %H:%M"),
        );
        assert!(
            markup.contains(expected_line.as_str()),
            "line {} not found in {}",
            expected_line,
            markup
        );

        let expected_line = format!(
            r#"{{
                                name: 'GET /one',
                                type: 'line',
                                symbol: 'none',
                                sampling: 'lttb',
                                data: [["{data_series_prefix}:32",1],["{data_series_prefix}:33",1],["{data_series_prefix}:34",1],["{data_series_prefix}:35",1]],
                            }},"#,
            data_series_prefix = Local
                .timestamp_opt(
                    Utc.with_ymd_and_hms(2021, 11, 21, 21, 20, 32)
                        .unwrap()
                        .timestamp(),
                    0
                )
                .unwrap()
                .format("%Y-%m-%d %H:%M"),
        );
        assert!(
            markup.contains(expected_line.as_str()),
            "line {} not found in {}",
            expected_line,
            markup
        );

        let expected_line = format!(
            r#"{{
                                name: 'GET /two',
                                type: 'line',
                                symbol: 'none',
                                sampling: 'lttb',
                                data: [["{data_series_prefix}:32",1],["{data_series_prefix}:33",1],["{data_series_prefix}:34",1],["{data_series_prefix}:35",1]],
                            }},"#,
            data_series_prefix = Local
                .timestamp_opt(
                    Utc.with_ymd_and_hms(2021, 11, 21, 21, 20, 32)
                        .unwrap()
                        .timestamp(),
                    0
                )
                .unwrap()
                .format("%Y-%m-%d %H:%M"),
        );
        assert!(
            markup.contains(expected_line.as_str()),
            "line {} not found in {}",
            expected_line,
            markup
        );

        let expected_line = format!(
            r#"{{
                                name: 'GET /three',
                                type: 'line',
                                symbol: 'none',
                                sampling: 'lttb',
                                data: [["{data_series_prefix}:32",1],["{data_series_prefix}:33",1],["{data_series_prefix}:34",1],["{data_series_prefix}:35",1]],
                            }},"#,
            data_series_prefix = Local
                .timestamp_opt(
                    Utc.with_ymd_and_hms(2021, 11, 21, 21, 20, 32)
                        .unwrap()
                        .timestamp(),
                    0
                )
                .unwrap()
                .format("%Y-%m-%d %H:%M"),
        );
        assert!(
            markup.contains(expected_line.as_str()),
            "line {} not found in {}",
            expected_line,
            markup
        );

        // Now same graph with granular data disabled.
        let mut expected = expected_nongranular_prefix;
        expected += format!(
            r#"                                    data: [
                                        
                                    ]
                                }},
                                data: [["{data_series_prefix}:32",149],["{data_series_prefix}:33",126],["{data_series_prefix}:34",146],["{data_series_prefix}:35",159]],
                            }},
                            
                        ]
                    }});
                </script>
            </div>"#,
            data_series_prefix = Local
                .timestamp_opt(Utc.with_ymd_and_hms(2021, 11, 21, 21, 20, 32).unwrap().timestamp(), 0)
                .unwrap()
                .format("%Y-%m-%d %H:%M")
        ).as_str();

        assert_eq!(
            Graph::new("graph-rps", "Requests #", false, &graph,).get_markup(
                &Vec::new(),
                Utc.with_ymd_and_hms(2021, 11, 21, 21, 20, 32).unwrap()
            ),
            expected
        );
    }

    // --- The report heatmap's whole run columns (#746) ---

    fn latency(graph: &GraphData) -> &LatencyHistograms {
        graph.latency.as_ref().expect("latency histograms")
    }

    /// The non empty columns of `columns`, by index.
    fn filled(columns: &Columns) -> Vec<(usize, SecondHistogram)> {
        columns
            .columns
            .iter()
            .enumerate()
            .filter(|(_, column)| column.counter > 0)
            .map(|(index, column)| (index, column.clone()))
            .collect()
    }

    /// Columns of `width` seconds from second 0, built directly from
    /// `(second, time)` pairs.
    fn reference_columns(times: &[(usize, u64)], width: usize) -> Vec<(usize, SecondHistogram)> {
        let mut columns: std::collections::BTreeMap<usize, SecondHistogram> = Default::default();
        for &(second, time) in times {
            columns.entry(second / width).or_default().record(time);
        }
        columns.into_iter().collect()
    }

    /// A seeded response time: mostly fast, some slow, a few past 60 s.
    fn seeded_time(state: &mut u64) -> u64 {
        *state = state
            .wrapping_mul(6364136223846793005)
            .wrapping_add(1442695040888963407);
        match (*state >> 33) % 100 {
            0 => 61_000 + (*state >> 40) % 5_000,
            1..=9 => 200 + (*state >> 40) % 3_000,
            _ => (*state >> 40) % 120,
        }
    }

    /// Merging columns as a budget fills gives the same counts and the
    /// same p50, p95 and p99 as building the wider columns directly from
    /// the raw times, after several merges of both budgets.
    #[test]
    fn merged_columns_match_columns_built_directly() {
        let mut graph = GraphData::with_latency_budgets(false, 4, 2);
        let mut state = 746;
        let mut times = Vec::new();
        let mut by_name: HashMap<&str, Vec<(usize, u64)>> = HashMap::new();
        for second in 0..40 {
            for (i, &name) in ["GET /a", "GET /b"].iter().enumerate() {
                let mut batch = GooseRequestMetricTimingData::new(None);
                for _ in 0..(3 + i * 2) {
                    let time = seeded_time(&mut state);
                    batch.record_time(time);
                    times.push((second, time));
                    by_name.entry(name).or_default().push((second, time));
                }
                graph.record_requests_per_second_and_latency_batch(
                    name,
                    second,
                    batch.counter as u32,
                    Some(&batch),
                );
            }
            // One request at a time too.
            let time = seeded_time(&mut state);
            graph.record_request_per_second_and_latency("GET /a", second, Some(time));
            times.push((second, time));
            by_name.entry("GET /a").or_default().push((second, time));
        }
        graph.finish_latency_columns();
        let latency = latency(&graph);
        // 40 seconds in 4 columns: 1, 2, 4, 8 then 16 seconds wide; in 2
        // columns, 32 seconds wide.
        assert_eq!((latency.all_width, latency.request_width), (16, 32));
        let all = latency.all_columns.as_ref().unwrap();
        assert!(all.columns.len() <= 4);
        let expected = reference_columns(&times, 16);
        assert_eq!(filled(all), expected);
        for (name, times) in &by_name {
            let columns = latency.rings[latency.by_request[*name]]
                .columns
                .as_ref()
                .unwrap();
            assert!(columns.columns.len() <= 2);
            assert_eq!(filled(columns), reference_columns(times, 32), "{name}");
        }
        for ((_, merged), (_, direct)) in filled(all).iter().zip(&expected) {
            for percent in [0.5, 0.95, 0.99] {
                assert_eq!(merged.percentile(percent), direct.percentile(percent));
            }
        }
    }

    /// A write to a second the ring already packed, and so folded, or that
    /// the ring refuses as too old, reaches the columns once: no time is
    /// lost or counted twice.
    #[test]
    fn late_writes_reach_the_columns_once() {
        let mut graph = GraphData::with_latency_budgets(false, 1_000, 1_000);
        let one = |time: u64| {
            let mut data = GooseRequestMetricTimingData::new(None);
            data.record_time(time);
            data
        };
        let batch = |graph: &mut GraphData, second: usize, time: u64| {
            graph.record_requests_per_second_and_latency_batch(
                "GET /a",
                second,
                1,
                Some(&one(time)),
            );
        };
        batch(&mut graph, 10, 5);
        // Seconds 11 to 13 move 10 to the ring, and 13 packs it.
        for second in 11..=13 {
            batch(&mut graph, second, 5);
            graph.move_pending_latency();
        }
        // A late batch for packed second 10, then one directly to it.
        batch(&mut graph, 10, 7);
        graph.record_request_per_second_and_latency("GET /a", 10, Some(9));
        // Second 20 leaves the ring once second 320 takes its slot.
        batch(&mut graph, 20, 5);
        batch(&mut graph, 320, 5);
        graph.move_pending_latency();
        batch(&mut graph, 20, 11);
        graph.finish_latency_columns();
        let latency = latency(&graph);
        let all = latency.all_columns.as_ref().unwrap();
        let own = latency.rings[0].columns.as_ref().unwrap();
        for columns in [all, own] {
            let mut ten = SecondHistogram::default();
            for time in [5, 7, 9] {
                ten.record(time);
            }
            assert_eq!(columns.columns[10], ten);
            let mut twenty = SecondHistogram::default();
            twenty.record(5);
            twenty.record(11);
            assert_eq!(columns.columns[20], twenty);
            let total: usize = columns.columns.iter().map(|column| column.counter).sum();
            assert_eq!(total, 9);
        }
        // The ring still shows second 10 as it was, for the dashboard.
        assert_eq!(latency.rings[0].get(10).map(|data| data.counter), Some(3));
    }

    /// Synthetic times land in the columns of the second they belong to:
    /// for a second the ring no longer holds, and in columns that have
    /// merged, both for all requests and for the name; the adjusted view
    /// adds them to the measured columns.
    #[test]
    fn synthetic_times_land_in_their_columns() {
        let mut graph = GraphData::with_latency_budgets(true, 100, 50);
        for second in 0..700 {
            graph.record_request_per_second_and_latency("GET /a", second, Some(10));
        }
        // Second 305 takes slot 5 in the synthetic rings, so they refuse
        // second 5, which goes to the columns directly.
        graph.record_synthetic_latency("GET /a", 305, 1_000);
        graph.record_synthetic_latency("GET /a", 5, 2_000);
        // Second 653 packs 650, so a later time for 650 is a late write.
        graph.record_synthetic_latency("GET /a", 650, 3_000);
        graph.record_synthetic_latency("GET /a", 653, 1_000);
        graph.record_synthetic_latency("GET /a", 650, 3_000);
        // A name without a measured ring adds only to all requests.
        graph.record_synthetic_latency("GET /none", 653, 1_000);
        graph.finish_latency_columns();
        let latency = latency(&graph);
        assert_eq!((latency.all_width, latency.request_width), (8, 16));
        let (all, by_request) = latency.synthetic.as_ref().unwrap();
        let synthetic_all = all.columns.as_ref().unwrap();
        let synthetic_a = by_request["GET /a"].columns.as_ref().unwrap();
        let counts = |columns: &Columns| -> Vec<(usize, usize)> {
            filled(columns)
                .into_iter()
                .map(|(index, column)| (index, column.counter))
                .collect()
        };
        assert_eq!(counts(synthetic_all), [(0, 1), (305 / 8, 1), (650 / 8, 4)]);
        assert_eq!(counts(synthetic_a), [(0, 1), (305 / 16, 1), (650 / 16, 3)]);
        assert!(!by_request.contains_key("GET /none"));
        assert_eq!(synthetic_all.columns[0].maximum_time_us, 2_000_000);
        assert_eq!(synthetic_a.columns[650 / 16].maximum_time_us, 3_000_000);

        let heatmap = latency.report_heatmap(true, Utc::now()).unwrap();
        let all = &heatmap.series[0];
        let adjusted = all.adjusted.as_ref().unwrap();
        // Column 0 holds 8 measured seconds and one synthetic time.
        let column = &adjusted.columns[0];
        assert_eq!(column[0], 0);
        let total: usize = column[4..].chunks(3).map(|cell| cell[1]).sum();
        assert_eq!(total, 9);
        let measured: usize = all.measured.columns[0][4..]
            .chunks(3)
            .map(|cell| cell[1])
            .sum();
        assert_eq!(measured, 8);
        // Without mitigation events there is no adjusted view.
        let heatmap = latency.report_heatmap(false, Utc::now()).unwrap();
        assert!(heatmap
            .series
            .iter()
            .all(|series| series.adjusted.is_none()));
    }

    /// After a reset that cleared every synthetic time, no series offers
    /// an Adjusted view, even with mitigation events recorded before it.
    #[test]
    fn no_adjusted_view_without_synthetic_times() {
        let mut graph = GraphData::with_latency_budgets(true, 600, 120);
        graph.record_request_per_second_and_latency("GET /a", 1, Some(10));
        graph.record_synthetic_latency("GET /a", 1, 900);
        graph.reset_rate_series(5);
        graph.record_request_per_second_and_latency("GET /a", 6, Some(10));
        graph.finish_latency_columns();
        let heatmap = latency(&graph).report_heatmap(true, Utc::now()).unwrap();
        assert_eq!(heatmap.series.len(), 2);
        assert!(heatmap
            .series
            .iter()
            .all(|series| series.adjusted.is_none()));
    }

    /// With budgets that fill at different seconds, each series widens when
    /// its own budget fills: no series ever holds more columns than its
    /// budget, after any write.
    #[test]
    fn each_budget_widens_its_own_series() {
        let mut graph = GraphData::with_latency_budgets(true, 6, 2);
        for second in 0..40 {
            graph.record_request_per_second_and_latency("GET /a", second, Some(10));
            graph.record_synthetic_latency("GET /a", second, 500);
            let latency = latency(&graph);
            let own = latency.rings[0].columns.as_ref().unwrap();
            let synthetic = latency.synthetic.as_ref().unwrap();
            let synthetic_own = synthetic.1["GET /a"].columns.as_ref().unwrap();
            let synthetic_all = synthetic.0.columns.as_ref().unwrap();
            assert!(
                own.columns.len() <= 2 && synthetic_own.columns.len() <= 2,
                "{}",
                second
            );
            assert!(
                latency.all_columns.as_ref().unwrap().columns.len() <= 6,
                "{}",
                second
            );
            assert!(synthetic_all.columns.len() <= 6, "{}", second);
        }
        let latency = latency(&graph);
        assert_eq!((latency.all_width, latency.request_width), (8, 32));
    }

    /// A synthetic time from a second later than any measured one widens
    /// the columns before it is written.
    #[test]
    fn a_synthetic_time_fits_its_second_in_the_columns() {
        let mut graph = GraphData::with_latency_budgets(true, 4, 2);
        graph.record_request_per_second_and_latency("GET /a", 0, Some(10));
        graph.record_synthetic_latency("GET /a", 9, 500);
        let latency = latency(&graph);
        assert_eq!((latency.all_width, latency.request_width), (4, 8));
        let synthetic = latency.synthetic.as_ref().unwrap();
        assert!(synthetic.0.columns.as_ref().unwrap().columns.len() <= 4);
        assert!(
            synthetic.1["GET /a"]
                .columns
                .as_ref()
                .unwrap()
                .columns
                .len()
                <= 2
        );
    }

    /// A reset also empties the columns synthetic times were folded into:
    /// none survive into the new run's Adjusted view.
    #[test]
    fn a_reset_clears_folded_synthetic_times() {
        let mut graph = GraphData::with_latency_budgets(true, 600, 120);
        for second in 0..5 {
            graph.record_request_per_second_and_latency("GET /a", second, Some(10));
            graph.record_synthetic_latency("GET /a", second, 900);
        }
        graph.finish_latency_columns();
        graph.reset_rate_series(10);
        graph.record_request_per_second_and_latency("GET /a", 11, Some(10));
        graph.finish_latency_columns();
        let heatmap = latency(&graph).report_heatmap(true, Utc::now()).unwrap();
        assert!(heatmap
            .series
            .iter()
            .all(|series| series.adjusted.is_none()));
    }

    /// A count's place on the colour scale is logarithmic, as the
    /// dashboard's: 10 between 1 and 100 sits in the middle.
    #[test]
    fn report_heatmap_colours_on_a_log_scale() {
        assert_eq!(ReportView::level(1, 1, 100), 0);
        assert_eq!(ReportView::level(10, 1, 100), 500);
        assert_eq!(ReportView::level(100, 1, 100), 1000);
        // Every cell holding the same count is the highest.
        assert_eq!(ReportView::level(7, 7, 7), 1000);
    }

    /// A run with no requests since the last reset shows no heatmap card.
    #[test]
    fn no_card_without_requests() {
        let mut graph = GraphData::with_latency_budgets(false, 600, 120);
        graph.finish_latency_columns();
        assert_eq!(graph.get_latency_heatmap_markup(false, Utc::now()), "");
        graph.record_request_per_second_and_latency("GET /a", 1, Some(10));
        graph.finish_latency_columns();
        assert!(graph
            .get_latency_heatmap_markup(false, Utc::now())
            .contains("Response time heatmap"));
    }

    /// A name first seen after the per name columns widened gets columns of
    /// the same width, so its measured and synthetic columns line up with
    /// every other name's.
    #[test]
    fn a_late_name_gets_the_current_width() {
        let mut graph = GraphData::with_latency_budgets(true, 10, 5);
        for second in 0..40 {
            graph.record_request_per_second_and_latency("GET /early", second, Some(10));
        }
        graph.record_request_per_second_and_latency("GET /late", 39, Some(10));
        graph.record_synthetic_latency("GET /late", 38, 500);
        graph.finish_latency_columns();
        let latency = latency(&graph);
        assert_eq!(latency.request_width, 8);
        let late = latency.rings[latency.by_request["GET /late"]]
            .columns
            .as_ref()
            .unwrap();
        assert_eq!(
            filled(late).iter().map(|(i, _)| *i).collect::<Vec<_>>(),
            [4]
        );
        let synthetic = latency.synthetic.as_ref().unwrap().1["GET /late"]
            .columns
            .as_ref()
            .unwrap();
        assert_eq!(
            filled(synthetic)
                .iter()
                .map(|(i, _)| *i)
                .collect::<Vec<_>>(),
            [4]
        );
    }

    /// A reset empties the columns, restores one second columns and starts
    /// them at its second: a time from before it, a request in flight at
    /// the reset, goes to column 0, once.
    #[test]
    fn reset_clears_the_columns() {
        let mut graph = GraphData::with_latency_budgets(false, 4, 4);
        for second in 0..20 {
            graph.record_request_per_second_and_latency("GET /a", second, Some(10));
        }
        assert!(latency(&graph).all_width > 1);
        graph.reset_rate_series(20);
        let latency_after = latency(&graph);
        assert_eq!(
            (
                latency_after.origin,
                latency_after.all_width,
                latency_after.request_width
            ),
            (20, 1, 1)
        );
        assert!(filled(latency_after.all_columns.as_ref().unwrap()).is_empty());
        graph.record_request_per_second_and_latency("GET /a", 19, Some(30));
        graph.record_request_per_second_and_latency("GET /a", 21, Some(10));
        graph.finish_latency_columns();
        let latency = latency(&graph);
        let all = latency.all_columns.as_ref().unwrap();
        let counts: Vec<(usize, usize)> = filled(all)
            .into_iter()
            .map(|(index, column)| (index, column.counter))
            .collect();
        assert_eq!(counts, [(0, 1), (1, 1)]);
        assert_eq!(all.columns[0].maximum_time_us, 30_000);
        // The report places column 0 at the reset's second.
        assert_eq!(
            latency.report_heatmap(false, Utc::now()).unwrap().origin,
            20
        );
    }

    /// The first 100 names get columns of their own; later names count
    /// only toward all requests, and the report lists only names with
    /// columns.
    #[test]
    fn only_the_first_names_get_columns() {
        let mut graph = GraphData::with_latency_budgets(false, 600, 120);
        for name in 0..105 {
            graph.record_request_per_second_and_latency(&format!("GET /{name}"), 1, Some(10));
        }
        graph.finish_latency_columns();
        let latency = latency(&graph);
        assert_eq!(latency.rings.len(), MAX_REQUEST_SERIES);
        assert!(latency.rings.iter().all(|ring| ring
            .columns
            .as_ref()
            .is_some_and(|columns| columns.columns[1].counter == 1)));
        assert_eq!(
            latency.all_columns.as_ref().unwrap().columns[1].counter,
            105
        );
        let heatmap = latency.report_heatmap(false, Utc::now()).unwrap();
        assert_eq!(heatmap.series.len(), MAX_REQUEST_SERIES + 1);
        assert!(heatmap
            .series
            .iter()
            .all(|series| series.name != "GET /104"));
    }

    /// At the end of the run the columns hold every time: the last two
    /// seconds, still open in the rings, and a name last requested 10
    /// seconds before the end, whose times waited in its pending counts.
    #[test]
    fn the_end_of_the_run_is_in_the_columns() {
        let mut graph = GraphData::with_latency_budgets(false, 600, 120);
        let one = |time: u64| {
            let mut data = GooseRequestMetricTimingData::new(None);
            data.record_time(time);
            data
        };
        for second in 0..=30 {
            graph.record_requests_per_second_and_latency_batch("GET /a", second, 1, Some(&one(10)));
            if second <= 20 {
                graph.record_requests_per_second_and_latency_batch(
                    "GET /b",
                    second,
                    1,
                    Some(&one(20)),
                );
            }
        }
        graph.finish_latency_columns();
        let latency = latency(&graph);
        let columns = |key: &str| {
            latency.rings[latency.by_request[key]]
                .columns
                .as_ref()
                .unwrap()
        };
        for second in 0..=30 {
            assert_eq!(columns("GET /a").columns[second].counter, 1, "{second}");
            let all = &latency.all_columns.as_ref().unwrap().columns[second];
            assert_eq!(all.counter, if second <= 20 { 2 } else { 1 }, "{second}");
        }
        assert_eq!(columns("GET /b").columns.len(), 21);
        assert_eq!(columns("GET /b").columns[20].counter, 1);
    }

    /// A report whose graphs start after the run did, as after Save report
    /// is turned on during a run, draws them from their first second, also
    /// when it is rendered again from its JSON report.
    #[test]
    fn graphs_that_start_late_keep_their_first_second() {
        let mut graph = GraphData::new();
        for second in 0..=900 {
            graph.record_users_per_second(10, second);
            graph.record_requests_per_second("GET /a", second);
            graph.record_requests_per_second("GET /b", second);
        }
        graph.prune_to_window(600);
        let origin = graph.first_second();
        assert!(origin > 0);
        let started = Utc.with_ymd_and_hms(2026, 10, 22, 10, 0, 0).unwrap();
        let at = |second: usize| {
            Local
                .timestamp_opt(started.timestamp() + second as i64, 0)
                .unwrap()
                .format("%Y-%m-%d %H:%M:%S")
                .to_string()
        };
        let check = |graph: &GraphData| {
            for markup in [
                graph.get_active_users_graph(false).get_markup(&[], started),
                graph
                    .get_requests_per_second_graph(true)
                    .get_markup(&[], started),
            ] {
                assert!(markup.contains(&at(origin)), "{}", markup);
                assert!(!markup.contains(&at(origin - 1)), "{}", markup);
            }
        };
        check(&graph);
        let json = serde_json::to_string(&graph.report_graphs(false, started, None)).unwrap();
        let (again, _) = serde_json::from_str::<RecordedGraphs>(&json)
            .unwrap()
            .into_parts();
        assert_eq!(again.first_second(), origin);
        check(&again);
    }

    /// Without whole run columns, as for the dashboard with Save report off,
    /// the rings work as before and the report has no heatmap.
    #[test]
    fn no_columns_without_a_report() {
        let configuration =
            crate::GooseConfiguration::parse_args_default(&["--dashboard"]).unwrap();
        assert!(configuration.records_latency_series());
        let mut graph = GraphData::for_metrics_processor(
            configuration.records_latency_series(),
            configuration.records_synthetic_latency(),
            false,
        );
        for second in 0..10 {
            graph.record_request_per_second_and_latency("GET /a", second, Some(10));
        }
        graph.finish_latency_columns();
        let latency = latency(&graph);
        assert!(latency.all_columns.is_none());
        assert!(latency.rings.iter().all(|ring| ring.columns.is_none()));
        assert_eq!(latency.limit, usize::MAX);
        assert_eq!(graph.get_latency_heatmap_markup(false, Utc::now()), "");

        // A report under the off switch keeps no histograms at all.
        for args in [
            vec!["--no-percentiles-over-time"],
            vec!["--no-percentiles-over-time", "--report-file", "r.html"],
        ] {
            let configuration = crate::GooseConfiguration::parse_args_default(&args).unwrap();
            let graph = GraphData::for_metrics_processor(
                configuration.records_latency_series(),
                configuration.records_synthetic_latency(),
                configuration.records_latency_columns(),
            );
            assert!(!graph.records_latency(), "{:?}", args);
        }
    }

    /// Each column's p50, p95 and p99 in the report's data are the ones
    /// Rust computes from the same counts, for the measured and the
    /// adjusted view, and the count's place on the colour scale runs from
    /// 0 for the sparsest cell to 1000 for the busiest.
    #[test]
    fn report_heatmap_percentiles_come_from_the_columns() {
        let mut graph = GraphData::with_latency_budgets(true, 8, 4);
        let mut state = 2026;
        let mut measured = Vec::new();
        let mut synthetic = Vec::new();
        for second in 0..30 {
            for _ in 0..20 {
                let time = seeded_time(&mut state);
                graph.record_request_per_second_and_latency("GET /a", second, Some(time));
                measured.push((second, time));
            }
            if second % 7 == 0 {
                graph.record_synthetic_latency("GET /a", second, 4_000);
                synthetic.push((second, 4_000));
            }
        }
        graph.finish_latency_columns();
        let heatmap = latency(&graph).report_heatmap(true, Utc::now()).unwrap();
        let all = &heatmap.series[0];
        assert_eq!(all.width, 4);
        let check = |view: &ReportView, times: &[(usize, u64)], width: usize| {
            let expected = reference_columns(times, width);
            assert_eq!(view.columns.len(), expected.len());
            for (column, (index, direct)) in view.columns.iter().zip(&expected) {
                assert_eq!(column[0], *index);
                assert_eq!(
                    column[1..4],
                    [
                        direct.percentile_us(0.5) as usize,
                        direct.percentile_us(0.95) as usize,
                        direct.percentile_us(0.99) as usize
                    ]
                );
                let cells: Vec<u64> = column[4..]
                    .chunks(3)
                    .flat_map(|cell| [cell[0] as u64, cell[1] as u64])
                    .collect();
                assert_eq!(cells, direct.heat_cells());
            }
            let levels: Vec<usize> = view
                .columns
                .iter()
                .flat_map(|column| column[4..].chunks(3).map(|cell| (cell[1], cell[2])))
                .filter(|&(count, _)| count == view.min || count == view.max)
                .map(|(count, level)| {
                    if count == view.min {
                        level
                    } else {
                        1000 - level
                    }
                })
                .collect();
            assert!(levels.iter().all(|&level| level == 0));
        };
        check(&all.measured, &measured, 4);
        let both: Vec<(usize, u64)> = measured.iter().chain(&synthetic).copied().collect();
        check(all.adjusted.as_ref().unwrap(), &both, 4);
        let a = &heatmap.series[1];
        assert_eq!((a.name, a.width), ("GET /a", 8));
        check(&a.measured, &measured, 8);
        check(a.adjusted.as_ref().unwrap(), &both, 8);
    }

    /// The card's JSON, written one series at a time, is the heatmap with
    /// every series built, which the other tests check.
    #[test]
    fn card_json_is_the_built_heatmap() {
        let mut graph = GraphData::with_latency_budgets(true, 4, 2);
        let mut state = 763;
        for second in 0..20 {
            for name in ["GET /a", "GET /b"] {
                graph.record_request_per_second_and_latency(
                    name,
                    second,
                    Some(seeded_time(&mut state)),
                );
            }
            if second % 3 == 0 {
                graph.record_synthetic_latency("GET /a", second, 2_500);
            }
        }
        graph.finish_latency_columns();
        let started = Utc::now();
        let markup = graph.get_latency_heatmap_markup(true, started);
        let start = markup.find("graph-latency-heatmap-data\">").unwrap() + 28;
        let end = start + markup[start..].find("</script>").unwrap();
        let card: serde_json::Value = serde_json::from_str(&markup[start..end]).unwrap();
        let built = latency(&graph).report_heatmap(true, started).unwrap();
        assert_eq!(card, serde_json::to_value(&built).unwrap());
        assert_eq!(card["series"].as_array().unwrap().len(), 3);
        assert!(!card["series"][1]["adjusted"].is_null());
    }

    /// A hostile request name stays text: the report's JSON has no `<`,
    /// `>`, `&`, U+2028 or U+2029, parses back to the same name, and the
    /// script builds the select from it with `textContent` only.
    #[test]
    fn report_heatmap_escapes_request_names() {
        let name = "GET /</script><img src=x onerror=alert(1)>\"'&\u{2028}\u{2029}";
        let mut graph = GraphData::with_latency_budgets(false, 600, 120);
        graph.record_request_per_second_and_latency(name, 1, Some(10));
        graph.finish_latency_columns();
        let markup = graph.get_latency_heatmap_markup(false, Utc::now());
        let start = markup.find("graph-latency-heatmap-data\">").unwrap() + 28;
        let end = start + markup[start..].find("</script>").unwrap();
        let json = &markup[start..end];
        for unsafe_char in ['<', '>', '&', '\u{2028}', '\u{2029}'] {
            assert!(!json.contains(unsafe_char), "{:?} in {}", unsafe_char, json);
        }
        let parsed: serde_json::Value = serde_json::from_str(json).unwrap();
        assert_eq!(parsed["series"][1]["name"], name);
        assert!(!REPORT_HEATMAP_SCRIPT.contains("innerHTML"));
        assert!(REPORT_HEATMAP_SCRIPT.contains("option.textContent = series.name"));
    }
}

/// The heatmap's rows under a millisecond and its percentile lines in
/// microseconds (#585).
#[cfg(test)]
mod microsecond_tests {
    use super::*;
    use crate::metrics::dashboard_snapshot::HeatmapColumn;

    /// The default `--timeout`, 60 seconds, in microseconds.
    const DEFAULT_TIMEOUT_US: u64 = 60_000_000;

    #[test]
    fn display_rows_are_100_microseconds_under_a_millisecond() {
        for us in 0..1000 {
            assert_eq!(display_index(us), (us / 100) as usize, "{us} µs");
        }
        assert_eq!(display_index(1000), 10);
        // Every time to the default timeout has a row the packed index holds.
        let mut previous = 0;
        for us in (0..=DEFAULT_TIMEOUT_US).step_by(997) {
            let index = display_index(us);
            assert!(index >= previous, "row falls at {} µs", us);
            assert!(index < 256, "{} µs has row {}", us, index);
            previous = index;
        }
        assert!(display_index(DEFAULT_TIMEOUT_US) < 256);
    }

    #[test]
    fn heatmap_rows_under_a_millisecond_are_filled() {
        let mut graph = GraphData::for_metrics_processor(true, false, false);
        for us in [50, 400, 400, 950] {
            graph.record_requests_per_second("GET /", 1);
            graph.record_latency_us("GET /", 1, us);
        }
        let heatmap = graph
            .export_series_window_until(300, Some(4))
            .heatmap
            .unwrap();
        let column = heatmap.column(1).expect("second 1");
        let pairs = HeatmapColumn::pairs(&column.measured);
        assert_eq!(pairs.len(), 3, "{pairs:?}");
        for ((index, count), (us, expected)) in pairs.iter().zip([(50, 1), (400, 2), (950, 1)]) {
            let [_, lowest, highest] = heatmap.buckets_us[*index as usize];
            assert!(
                lowest <= us && us <= highest,
                "{} µs in {} to {}",
                us,
                lowest,
                highest
            );
            assert_eq!(highest - lowest, 99);
            assert_eq!(*count, expected);
        }
    }

    #[test]
    fn heatmap_percentiles_follow_the_whole_run_to_a_row() {
        let mut histogram = SecondHistogram::default();
        let mut whole = GooseRequestMetricTimingData::default();
        let mut seed: u64 = 585;
        for i in 0..10_000u64 {
            seed = seed
                .wrapping_mul(6364136223846793005)
                .wrapping_add(1442695040888963407);
            let us = if i % 10 < 7 {
                30 + (seed >> 33) % 66
            } else {
                400 + (seed >> 33) % 501
            };
            histogram.record_us(us);
            whole.record_time_us(us);
        }
        for percent in [0.5, 0.95, 0.99] {
            let row = histogram.percentile_us(percent);
            assert_eq!(
                display_index(row),
                bucket_row(crate::metrics::buckets::bucket_floor(
                    whole.percentile_us(percent)
                )),
                "p{percent}"
            );
            assert!(row < 1000);
        }
    }
}