summaryrefslogtreecommitdiff
path: root/src/FrameStats.zig
blob: 2287002eb71e6a20e16890598671ffdf53d66a8e (plain) (blame)
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
//! The frame-timing probe a game keeps: for every frame the pump draws, how
//! long its update, its render and its present took on the clock, the gap
//! since the frame before, and what the shell's own motion (a scroll's
//! picture) showed in it. A frame that something was
//! animating into and that came more than half a display period late is
//! jank: the eye sees the motion stop for a refresh. The records go in a
//! fixed ring embedded in `Pardes`, nothing allocated per frame, and cost
//! three reads of the monotonic clock a frame, so the probe is always on:
//! `Debug` shows the last second of it in its box, and `PARDES_FRAMES=path`
//! writes the whole ring and its summary there when the session ends.
//!
//! Each frame also carries its input-to-present latency (from the first
//! input it answers, the shell's timestamp of the key or wheel event, to
//! the end of its present), where its work went (`Zone`: the core's body
//! text, rows and colours; the shell's instance build, word shaping and
//! glyph rasterizing) and the view it showed, so a spike is attributed and
//! a scroll's throughput is counted in rows actually presented.
//!
//! `PARDES_FRAMES_DEMO=scroll` (or `stress`) runs a timed script of scroll
//! inputs (`named`) in a pixel shell (the GUI feeds them through its own
//! wheel and key paths) and then ends the session: a timedemo, so two
//! builds are compared on the same motion, on a real display.
const std = @import("std");
const pardes = @import("pardes.zig");
const animation = @import("animation.zig");

const FrameStats = @This();

/// 4.5 minutes at 60 Hz: the longest timedemo fits whole (1.3 MB).
pub const capacity = 16384;

pub const Frame = struct {
    /// When the frame's step began, the input that woke it read: the
    /// monotonic clock, ns.
    start_ns: u64 = 0,
    /// Events, effects and the shell's poll; the core's render; the
    /// shell's present (its instance build and GPU submission). µs.
    update_us: u32 = 0,
    render_us: u32 = 0,
    present_us: u32 = 0,
    /// From the previous drawn frame's present to this one's, µs.
    gap_us: u32 = 0,
    /// Something was moving into this frame: the gap should be a period.
    moving: bool = false,
    /// The shell had no image to draw into (a busy swapchain).
    skipped: bool = false,
    /// The shell's motion in it: a scroll's picture offset from the text,
    /// rows (0 at rest), or a focused PDF's strip as drawn, pixels down
    /// the document; and where it drew the caret, its top in rows (the
    /// jitter check: a caret on a fixed row must not wobble).
    scroll: f32 = 0,
    caret: f32 = 0,
    /// From the first input this frame answers to its present's end, µs;
    /// 0 when it answers none.
    latency_us: u32 = 0,
    /// The focused pane's first line: with `scroll`, where the picture is.
    view: i32 = 0,
    /// µs a frame spent in each zone, and how many words the shell shaped
    /// and glyphs it rasterized anew.
    zones: [zone_count]u32 = @splat(0),
    shaped: u16 = 0,
    rastered: u16 = 0,
    /// Bytes a cell shell wrote to its terminal for the frame.
    bytes: u32 = 0,
    /// PDF page rasters that landed in it (rendered on the loop, or taken
    /// from a lane): the frame a page lands on.
    landed: u16 = 0,
};

/// Where a frame's work goes, as a game's profiler splits its budget.
/// The gui's are nested: shape and raster inside build; a soft present's
/// wait for the GPU, its upload of the frame and its flip after it.
pub const Zone = enum(u8) { body_text, body_rows, body_color, gui_build, gui_shape, gui_raster, gui_wait, gui_upload, gui_flip };
pub const zone_count = std.enums.values(Zone).len;

ring: [capacity]Frame = @splat(.{}),
/// Frames recorded since the start; the ring holds the last `capacity`.
count: u64 = 0,
/// The display's refresh period, as the shell knows it.
period_ns: u64 = animation.frame_ns,
/// The end of the last drawn frame's present, and whether anything was
/// still moving then (the next frame was owed at once).
last_end_ns: u64 = 0,
armed: bool = false,
/// Since the start: frames that came late into a motion, and frames whose
/// own work (update, render, present) took longer than a period.
jank: u64 = 0,
over_budget: u64 = 0,
skipped: u64 = 0,
/// What the shell sets while it draws the frame (Frame.scroll), and the
/// pump (view).
scroll: f32 = 0,
caret: f32 = 0,
view: i32 = 0,
/// The first input not yet answered by a drawn frame, monotonic ns.
input_ns: u64 = 0,
/// This frame's zones so far.
zone_us: [zone_count]u32 = @splat(0),
shaped: u16 = 0,
rastered: u16 = 0,
bytes: u32 = 0,
landed: u16 = 0,
/// When the timedemo started, monotonic ns (0: none ran), and which.
demo_ns: u64 = 0,
demo_name: []const u8 = "",
/// The frame being timed: its marks, the monotonic clock's ns.
mark_start: u64 = 0,
mark_update: u64 = 0,
mark_render: u64 = 0,

/// An input arrived at `at_ns`: the next drawn frame answers it.
pub fn input(stats: *FrameStats, at_ns: u64) void {
    if (stats.input_ns == 0 or at_ns < stats.input_ns) stats.input_ns = at_ns;
}

/// The pump had nothing to draw: what came in changed nothing on screen,
/// so no frame answers it.
pub fn idle(stats: *FrameStats) void {
    stats.input_ns = 0;
    // Nor was a frame owed after all (a motion that ended between frames):
    // the next frame's gap is no motion's.
    stats.armed = false;
}

/// Adds the time since `from_ns` to `which`.
pub fn zone(stats: *FrameStats, which: Zone, from_ns: u64) void {
    const us: u32 = @intCast(@min(std.math.maxInt(u32), (nowNs() -| from_ns) / std.time.ns_per_us));
    stats.zone_us[@backingInt(which)] +|= us;
}

pub fn nowNs() u64 {
    if (comptime !pardes.hosted) return 0;
    var ts: std.c.timespec = undefined;
    if (std.c.clock_gettime(.MONOTONIC, &ts) != 0) return 0;
    return @as(u64, @intCast(ts.sec)) *| std.time.ns_per_s +| @as(u64, @intCast(ts.nsec));
}

/// Closes the frame whose marks are set: `end_ns` is when its present
/// returned, `moving` whether anything still moves (a frame is owed at
/// once), `skipped` whether the shell drew nothing.
pub fn record(stats: *FrameStats, end_ns: u64, moving: bool, skipped: bool) void {
    const us = struct {
        fn of(from: u64, to: u64) u32 {
            return @intCast(@min(std.math.maxInt(u32), (to -| from) / std.time.ns_per_us));
        }
    };
    const gap: u32 = if (stats.last_end_ns == 0) 0 else us.of(stats.last_end_ns, end_ns);
    const frame: Frame = .{
        .start_ns = stats.mark_start,
        .update_us = us.of(stats.mark_start, stats.mark_update),
        .render_us = us.of(stats.mark_update, stats.mark_render),
        .present_us = us.of(stats.mark_render, end_ns),
        .gap_us = gap,
        .moving = stats.armed,
        .skipped = skipped,
        .scroll = stats.scroll,
        .caret = stats.caret,
        .latency_us = if (stats.input_ns == 0) 0 else @max(1, us.of(stats.input_ns, end_ns)),
        .view = stats.view,
        .zones = stats.zone_us,
        .shaped = stats.shaped,
        .rastered = stats.rastered,
        .bytes = stats.bytes,
        .landed = stats.landed,
    };
    stats.bytes = 0;
    stats.landed = 0;
    stats.input_ns = 0;
    stats.zone_us = @splat(0);
    stats.shaped = 0;
    stats.rastered = 0;
    // Half a period late is a refresh the motion missed.
    if (stats.armed and @as(u64, gap) * std.time.ns_per_us * 2 > stats.period_ns * 3) stats.jank += 1;
    if (@as(u64, frame.update_us + frame.render_us + frame.present_us) * std.time.ns_per_us > stats.period_ns) stats.over_budget += 1;
    if (skipped) stats.skipped += 1;
    stats.ring[stats.count % capacity] = frame;
    stats.count += 1;
    stats.last_end_ns = end_ns;
    stats.armed = moving;
}

/// The last `n` frames, oldest first, as two slices of the ring.
pub fn last(stats: *const FrameStats, n: usize) [2][]const Frame {
    const have: usize = @intCast(@min(stats.count, capacity));
    const take = @min(n, have);
    const end: usize = @intCast(stats.count % capacity);
    if (take <= end) return .{ stats.ring[end - take .. end], &.{} };
    return .{ stats.ring[capacity - (take - end) ..], stats.ring[0..end] };
}

/// Order statistics of one of a run of frames' times, µs: its median,
/// 99th percentile and worst; over the moving frames only for a gap.
pub const Spread = struct {
    n: usize = 0,
    p50: u32 = 0,
    p90: u32 = 0,
    p99: u32 = 0,
    max: u32 = 0,
    mean: f64 = 0,

    pub const What = enum { work, update, render, present, gap, latency };

    pub fn of(frames: [2][]const Frame, comptime what: What) Spread {
        var values: [capacity]u32 = undefined;
        var n: usize = 0;
        var sum: f64 = 0;
        for (frames) |part| for (part) |f| {
            const v: u32 = switch (what) {
                .work => f.update_us + f.render_us + f.present_us,
                .update => f.update_us,
                .render => f.render_us,
                .present => f.present_us,
                .gap => if (f.moving) f.gap_us else continue,
                .latency => if (f.latency_us != 0) f.latency_us else continue,
            };
            values[n] = v;
            n += 1;
            sum += @floatFromInt(v);
        };
        if (n == 0) return .{};
        std.mem.sortUnstable(u32, values[0..n], {}, std.sort.asc(u32));
        return .{
            .n = n,
            .p50 = values[n / 2],
            .p90 = values[@min(n - 1, n * 90 / 100)],
            .p99 = values[@min(n - 1, n * 99 / 100)],
            .max = values[n - 1],
            .mean = sum / @as(f64, @floatFromInt(n)),
        };
    }
};

/// The Debug box's lines: the last second's work, gaps and input latency,
/// and the jank counter since the start, in ms.
pub fn summary(stats: *const FrameStats, buf: []u8) []const u8 {
    const recent = stats.last(@intCast(@max(1, std.time.ns_per_s / @max(1, stats.period_ns))));
    const work = Spread.of(recent, .work);
    const gap = Spread.of(recent, .gap);
    const latency = Spread.of(stats.last(capacity / 16), .latency);
    const ms = struct {
        fn of(v: u32) f64 {
            return @as(f64, @floatFromInt(v)) / 1000;
        }
    };
    return std.fmt.bufPrint(buf, "frame  {d:.2}/{d:.2}/{d:.2} ms\ngap    {d:.2}/{d:.2} ms jank {d}\ninput  {d:.2}/{d:.2} ms", .{
        ms.of(work.p50), ms.of(work.p99), ms.of(work.max), ms.of(gap.p50), ms.of(gap.max), stats.jank, ms.of(latency.p50), ms.of(latency.p90),
    }) catch "";
}

/// Writes the summary and every frame the ring holds to `path`: a header
/// of `# ` lines, then one tab-separated line a frame.
pub fn dump(stats: *const FrameStats, path: [:0]const u8) void {
    if (comptime !pardes.hosted) return;
    const fd = std.c.open(path, .{ .ACCMODE = .WRONLY, .CREAT = true, .TRUNC = true, .CLOEXEC = true }, @as(std.c.mode_t, 0o644));
    if (fd < 0) return;
    defer _ = std.c.close(fd);
    var buf: [256]u8 = undefined;
    const all = stats.last(capacity);
    const write = struct {
        fn line(f: c_int, b: []u8, comptime fmt: []const u8, args: anytype) void {
            const text = std.fmt.bufPrint(b, fmt, args) catch return;
            _ = std.c.write(f, text.ptr, text.len);
        }
    };
    write.line(fd, &buf, "# frames {d} period_us {d} jank {d} over_budget {d} skipped {d}\n", .{ stats.count, stats.period_ns / std.time.ns_per_us, stats.jank, stats.over_budget, stats.skipped });
    inline for (comptime std.enums.values(Spread.What)) |what| {
        const s = Spread.of(all, what);
        write.line(fd, &buf, "# {t} n {d} mean {d:.0} p50 {d} p90 {d} p99 {d} max {d}\n", .{ what, s.n, s.mean, s.p50, s.p90, s.p99, s.max });
    }
    write.line(fd, &buf, "# start_ms\tupdate_us\trender_us\tpresent_us\tgap_us\tmoving\tskipped\tscroll\n", .{});
    const t0 = if (all[0].len != 0) all[0][0].start_ns else 0;
    // The monotonic clock is the machine's: a driver lines its own times
    // up with the frames' by this.
    write.line(fd, &buf, "# t0_ns {d}\n", .{t0});
    // The timedemo's phases, on the frames' clock.
    if (stats.demo_ns != 0) if (named(stats.demo_name)) |steps| for (steps) |step| switch (step.act) {
        .phase => |name| write.line(fd, &buf, "# phase {s} {d:.1}\n", .{ name, @as(f64, @floatFromInt((stats.demo_ns + @as(u64, step.at_ms) * std.time.ns_per_ms) -| t0)) / std.time.ns_per_ms }),
        else => {},
    };
    write.line(fd, &buf, "# start_ms\tupdate_us\trender_us\tpresent_us\tgap_us\tmoving\tskipped\tscroll\tcaret\tlatency_us\tview", .{});
    inline for (comptime std.enums.values(Zone)) |which| write.line(fd, &buf, "\t{t}_us", .{which});
    write.line(fd, &buf, "\tshaped\trastered\tbytes\tlanded\n", .{});
    for (all) |part| for (part) |f| {
        write.line(fd, &buf, "{d:.3}\t{d}\t{d}\t{d}\t{d}\t{d}\t{d}\t{d:.4}\t{d:.4}\t{d}\t{d}", .{
            @as(f64, @floatFromInt(f.start_ns -| t0)) / std.time.ns_per_ms, f.update_us, f.render_us, f.present_us, f.gap_us,
            @intFromBool(f.moving), @intFromBool(f.skipped), f.scroll, f.caret, f.latency_us, f.view,
        });
        for (f.zones) |us| write.line(fd, &buf, "\t{d}", .{us});
        write.line(fd, &buf, "\t{d}\t{d}\t{d}\t{d}\n", .{ f.shaped, f.rastered, f.bytes, f.landed });
    };
}

/// One input of the scroll timedemo, `at_ms` after the first frame.
pub const Step = struct {
    at_ms: u32,
    act: union(enum) {
        /// Wheel notches, down positive, as a mouse's.
        wheel: i8,
        /// A touchpad's scroll, lines down, a fraction (no notch).
        touch: f32,
        key: pardes.Key,
        /// A named stretch of the run starts (the dump lists them).
        phase: []const u8,
        /// A press and release of `button` over the focused pane, `at` of
        /// the way down its body, on its rail or in its text.
        click: struct { button: pardes.Mouse.Button, at: f32, rail: bool },
        /// Wheel notches over the focused pane's rail (the gutter).
        rail_wheel: i8,
        /// Text pasted (bracketed, as a terminal's paste arrives).
        paste: []const u8,
        /// A line run as Exec runs it, in the focused pane (a builtin, a
        /// setting, else a shell command), the pane it opens focused: what
        /// opens the other kinds of pane.
        exec: []const u8,
        /// The run is over: the session ends.
        end,
    },
};

/// Every kind of scroll a user makes, on a long file from its top: single
/// notches, a fast spin of the wheel both ways (notches 8 ms apart, two a
/// frame: each retargets the one before), half pages, pages, the far jumps
/// to the end (ge) and back (gg), half pages faster than they settle, and the cursor
/// moving a line, a word and, in insert mode, a column at a time.
/// The timedemo PARDES_FRAMES_DEMO names, if any.
pub fn named(name: []const u8) ?[]const Step {
    if (std.mem.eql(u8, name, "scroll")) return &demo;
    if (std.mem.eql(u8, name, "stress")) return &stress;
    if (std.mem.eql(u8, name, "kinds")) return &kinds;
    if (std.mem.eql(u8, name, "panes")) return &panes;
    if (std.mem.eql(u8, name, "pdf-scroll")) return &pdf_scroll;
    if (std.mem.eql(u8, name, "pdf-sdm")) return &pdf_sdm;
    if (std.mem.eql(u8, name, "one")) return &one;
    if (std.mem.eql(u8, name, "keys")) return &keystrokes;
    if (std.mem.eql(u8, name, "jumps")) return &jumps;
    if (std.mem.eql(u8, name, "longlines")) return &longlines;
    if (std.mem.eql(u8, name, "clip-flick")) return &clip_flick;
    if (std.mem.eql(u8, name, "clip-far")) return &clip_far;
    if (std.mem.eql(u8, name, "clip-cursor")) return &clip_cursor;
    if (std.mem.eql(u8, name, "clip-query")) return &clip_query;
    if (std.mem.eql(u8, name, "clip-terminal")) return &clip_terminal;
    if (std.mem.eql(u8, name, "clip-pages")) return &clip_pages;
    return null;
}

pub const demo = blk: {
    @setEvalBranchQuota(20_000);
    var steps: [128]Step = undefined;
    var n: usize = 0;
    var t: u32 = 1500;
    steps[n] = .{ .at_ms = t, .act = .{ .phase = "notches" } };
    n += 1;
    for (0..10) |_| {
        steps[n] = .{ .at_ms = t, .act = .{ .wheel = 1 } };
        n += 1;
        t += 150;
    }
    t += 400;
    steps[n] = .{ .at_ms = t, .act = .{ .phase = "spin" } };
    n += 1;
    for ([_]i8{ 1, -1 }) |dir| {
        for (0..24) |_| {
            steps[n] = .{ .at_ms = t, .act = .{ .wheel = dir } };
            n += 1;
            t += 8;
        }
        t += 800;
    }
    const ctrl = struct {
        fn key(cp: u21) pardes.Key {
            return .{ .cp = cp, .ctrl = true };
        }
    };
    steps[n] = .{ .at_ms = t, .act = .{ .phase = "pages" } };
    n += 1;
    for ([_]u21{ 'd', 'd', 'd', 'u', 'u', 'f', 'b' }) |cp| {
        steps[n] = .{ .at_ms = t, .act = .{ .key = ctrl.key(cp) } };
        n += 1;
        t += 600;
    }
    for ([_]u21{ 'g', 'e' }, [_][]const u8{ "g", "e" }, 0..) |cp, text, i| {
        steps[n] = .{ .at_ms = t + 20 * i, .act = .{ .key = .{ .cp = cp, .text = text } } };
        n += 1;
    }
    t += 800;
    for (0..2) |i| {
        steps[n] = .{ .at_ms = t + 20 * i, .act = .{ .key = .{ .cp = 'g', .text = "g" } } };
        n += 1;
    }
    t += 800;
    steps[n] = .{ .at_ms = t, .act = .{ .phase = "retarget" } };
    n += 1;
    for (0..4) |_| {
        steps[n] = .{ .at_ms = t, .act = .{ .key = ctrl.key('d') } };
        n += 1;
        t += 80;
    }
    t += 700;
    steps[n] = .{ .at_ms = t, .act = .{ .phase = "cursor" } };
    n += 1;
    for ([_]struct { u21, []const u8, u32 }{ .{ 'j', "j", 60 }, .{ 'w', "w", 90 } }) |run| {
        for (0..10) |_| {
            steps[n] = .{ .at_ms = t, .act = .{ .key = .{ .cp = run[0], .text = run[1] } } };
            n += 1;
            t += run[2];
        }
        t += 400;
    }
    steps[n] = .{ .at_ms = t, .act = .{ .key = .{ .cp = 'i', .text = "i" } } };
    n += 1;
    t += 300;
    for ([_]struct { u21, u32 }{ .{ pardes.Key.right, 60 }, .{ pardes.Key.down, 100 } }) |run| {
        for (0..8) |_| {
            steps[n] = .{ .at_ms = t, .act = .{ .key = .{ .cp = run[0] } } };
            n += 1;
            t += run[1];
        }
        t += 300;
    }
    steps[n] = .{ .at_ms = t, .act = .{ .key = .{ .cp = pardes.Key.escape } } };
    steps[n + 1] = .{ .at_ms = t + 1000, .act = .end };
    n += 2;
    const out = steps[0..n].*;
    break :blk out;
};

/// The scrolls every kind of text and pane is put through (`kinds`,
/// `panes`), from `t`: notches, a spin, half pages, PageDown held, the far
/// jumps, the cursor down a held j. 7.5 s.
fn scrollSet(steps: []Step, n: *usize, t: *u32) void {
    const at = struct {
        fn add(s: []Step, k: *usize, ms: u32, act: @FieldType(Step, "act")) void {
            s[k.*] = .{ .at_ms = ms, .act = act };
            k.* += 1;
        }
    };
    for (0..6) |_| {
        at.add(steps, n, t.*, .{ .wheel = 1 });
        t.* += 150;
    }
    for (0..100) |_| {
        at.add(steps, n, t.*, .{ .wheel = 1 });
        t.* += 8;
    }
    t.* += 500;
    for (0..3) |_| {
        at.add(steps, n, t.*, .{ .key = .{ .cp = 'd', .ctrl = true } });
        t.* += 400;
    }
    for (0..45) |_| {
        at.add(steps, n, t.*, .{ .key = .{ .cp = pardes.Key.page_down } });
        t.* += 33;
    }
    t.* += 400;
    for ([_][]const u8{ "e", "g" }) |second| {
        at.add(steps, n, t.*, .{ .key = .{ .cp = 'g', .text = "g" } });
        at.add(steps, n, t.* + 20, .{ .key = .{ .cp = second[0], .text = second } });
        t.* += 600;
    }
    for (0..30) |_| {
        at.add(steps, n, t.*, .{ .key = .{ .cp = 'j', .text = "j" } });
        t.* += 33;
    }
    t.* += 500;
}

/// A PDF's strip put through what moves it (anim 11): a wheel's notches,
/// then spun; PageDown held; the far jumps ge and gg; a Look at a page and
/// Back; then slow: a notch at a time, and a touchpad's drag a pixel or so
/// a frame.
pub const pdf_scroll = blk: {
    @setEvalBranchQuota(20_000);
    var steps: [512]Step = undefined;
    var n: usize = 0;
    var t: u32 = 1500;
    const at = struct {
        fn add(s: []Step, k: *usize, ms: u32, act: @FieldType(Step, "act")) void {
            s[k.*] = .{ .at_ms = ms, .act = act };
            k.* += 1;
        }
    };
    at.add(&steps, &n, t, .{ .exec = "Look ../../docs/design.pdf" });
    t += 2000;
    at.add(&steps, &n, t, .{ .phase = "spin" });
    for (0..6) |_| {
        at.add(&steps, &n, t, .{ .wheel = 1 });
        t += 150;
    }
    for (0..60) |_| {
        at.add(&steps, &n, t, .{ .wheel = 1 });
        t += 8;
    }
    t += 600;
    for (0..30) |_| {
        at.add(&steps, &n, t, .{ .wheel = -1 });
        t += 8;
    }
    t += 600;
    at.add(&steps, &n, t, .{ .phase = "held-pagedown" });
    for (0..20) |_| {
        at.add(&steps, &n, t, .{ .key = .{ .cp = pardes.Key.page_down } });
        t += 33;
    }
    t += 600;
    at.add(&steps, &n, t, .{ .phase = "far-jump" });
    for ([_][]const u8{ "e", "g" }) |second| {
        at.add(&steps, &n, t, .{ .key = .{ .cp = 'g', .text = "g" } });
        at.add(&steps, &n, t + 20, .{ .key = .{ .cp = second[0], .text = second } });
        t += 800;
    }
    at.add(&steps, &n, t, .{ .exec = "Look ../../docs/design.pdf:12" });
    t += 800;
    at.add(&steps, &n, t, .{ .key = .{ .cp = 'o', .ctrl = true } });
    t += 800;
    at.add(&steps, &n, t, .{ .phase = "slow-notches" });
    for (0..8) |_| {
        at.add(&steps, &n, t, .{ .wheel = 1 });
        t += 300;
    }
    at.add(&steps, &n, t, .{ .phase = "slow-touch" });
    for (0..120) |_| {
        at.add(&steps, &n, t, .{ .touch = 0.06 });
        t += 16;
    }
    t += 600;
    at.add(&steps, &n, t, .{ .phase = "done" });
    at.add(&steps, &n, t, .end);
    const out = steps[0..n].*;
    break :blk out;
};

/// The same, on a manual of 5363 pages (the Intel SDM, combined volumes,
/// 27 MB: run where `sdm.pdf` is, a copy): its open, a wheel spun both
/// ways, PageDown held five seconds, a far Look to page 3000 and Back, gg,
/// a Look at page 4500, then slow notches and a touchpad's drag.
pub const pdf_sdm = blk: {
    @setEvalBranchQuota(40_000);
    var steps: [512]Step = undefined;
    var n: usize = 0;
    var t: u32 = 1500;
    const at = struct {
        fn add(s: []Step, k: *usize, ms: u32, act: @FieldType(Step, "act")) void {
            s[k.*] = .{ .at_ms = ms, .act = act };
            k.* += 1;
        }
    };
    at.add(&steps, &n, t, .{ .phase = "open" });
    at.add(&steps, &n, t, .{ .exec = "Look sdm.pdf" });
    t += 3000;
    at.add(&steps, &n, t, .{ .phase = "spin" });
    for (0..6) |_| {
        at.add(&steps, &n, t, .{ .wheel = 1 });
        t += 150;
    }
    for (0..60) |_| {
        at.add(&steps, &n, t, .{ .wheel = 1 });
        t += 8;
    }
    t += 600;
    for (0..30) |_| {
        at.add(&steps, &n, t, .{ .wheel = -1 });
        t += 8;
    }
    t += 600;
    at.add(&steps, &n, t, .{ .phase = "held-pagedown" });
    for (0..150) |_| {
        at.add(&steps, &n, t, .{ .key = .{ .cp = pardes.Key.page_down } });
        t += 33;
    }
    t += 800;
    at.add(&steps, &n, t, .{ .phase = "far-jump" });
    at.add(&steps, &n, t, .{ .exec = "Look sdm.pdf:3000" });
    t += 1200;
    at.add(&steps, &n, t, .{ .key = .{ .cp = 'o', .ctrl = true } });
    t += 1200;
    at.add(&steps, &n, t, .{ .key = .{ .cp = 'g', .text = "g" } });
    at.add(&steps, &n, t + 20, .{ .key = .{ .cp = 'g', .text = "g" } });
    t += 1200;
    at.add(&steps, &n, t, .{ .phase = "look-deep" });
    at.add(&steps, &n, t, .{ .exec = "Look sdm.pdf:4500" });
    t += 1500;
    at.add(&steps, &n, t, .{ .phase = "slow-notches" });
    for (0..8) |_| {
        at.add(&steps, &n, t, .{ .wheel = 1 });
        t += 300;
    }
    at.add(&steps, &n, t, .{ .phase = "slow-touch" });
    for (0..120) |_| {
        at.add(&steps, &n, t, .{ .touch = 0.06 });
        t += 16;
    }
    t += 600;
    at.add(&steps, &n, t, .{ .phase = "done" });
    at.add(&steps, &n, t, .end);
    const out = steps[0..n].*;
    break :blk out;
};

/// Keystroke-to-present, a phase a gesture, on the file the session opened:
/// test/perf.zig's --live sends these same steps to the tty through a pty
/// and --live-gui runs them here, so the two frontends are timed alike.
pub const keystrokes = blk: {
    @setEvalBranchQuota(50_000);
    var steps: [1024]Step = undefined;
    var n: usize = 0;
    var t: u32 = 1500;
    const at = struct {
        fn add(s: []Step, k: *usize, ms: u32, act: @FieldType(Step, "act")) void {
            s[k.*] = .{ .at_ms = ms, .act = act };
            k.* += 1;
        }
    };
    at.add(&steps, &n, t, .{ .phase = "key-down" });
    for (0..150) |_| {
        at.add(&steps, &n, t, .{ .key = .{ .cp = 'j', .text = "j" } });
        t += 30;
    }
    at.add(&steps, &n, t, .{ .phase = "key-right" });
    for (0..150) |i| {
        at.add(&steps, &n, t, .{ .key = if (i % 2 == 0) .{ .cp = 'l', .text = "l" } else .{ .cp = 'h', .text = "h" } });
        t += 30;
    }
    at.add(&steps, &n, t, .{ .phase = "edit-char" });
    at.add(&steps, &n, t, .{ .key = .{ .cp = 'i', .text = "i" } });
    t += 30;
    for (0..150) |_| {
        at.add(&steps, &n, t, .{ .key = .{ .cp = 'x', .text = "x" } });
        t += 30;
    }
    at.add(&steps, &n, t, .{ .key = .{ .cp = pardes.Key.escape } });
    t += 300;
    at.add(&steps, &n, t, .{ .phase = "page-down" });
    for (0..60) |_| {
        at.add(&steps, &n, t, .{ .key = .{ .cp = pardes.Key.page_down } });
        t += 50;
    }
    at.add(&steps, &n, t, .{ .phase = "wheel" });
    for (0..150) |_| {
        at.add(&steps, &n, t, .{ .wheel = 1 });
        t += 30;
    }
    t += 500;
    at.add(&steps, &n, t, .{ .phase = "done" });
    at.add(&steps, &n, t, .end);
    const out = steps[0..n].*;
    break :blk out;
};

/// `scrollSet` alone, on the file the session opened: one kind, for a
/// profiler's run.
pub const one = blk: {
    @setEvalBranchQuota(20_000);
    var steps: [512]Step = undefined;
    var n: usize = 0;
    var t: u32 = 1500;
    steps[n] = .{ .at_ms = t, .act = .{ .phase = "one" } };
    n += 1;
    scrollSet(&steps, &n, &t);
    steps[n] = .{ .at_ms = t, .act = .end };
    n += 1;
    const out = steps[0..n].*;
    break :blk out;
};

/// The samples' scenes (docs/next-anim.md), a few seconds each, recorded
/// with PARDES_FRAMES_CAPTURE: a wheel spun hard; Ctrl-d held then ge and
/// gg; j held then the thin caret typing and arrowing; a `/` typed and its
/// places stepped with Ctrl-n; a command's output then the wheel over it;
/// Ctrl-f a page at a time.
fn clipSteps(comptime which: usize, steps: *[256]Step) usize {
    @setEvalBranchQuota(100_000);
    var n: usize = 0;
    var t: u32 = 1200;
    const add = struct {
        fn step(s: []Step, k: *usize, at: *u32, gap: u32, act: @FieldType(Step, "act")) void {
            s[k.*] = .{ .at_ms = at.*, .act = act };
            k.* += 1;
            at.* += gap;
        }
    };
    switch (which) {
        0 => for (0..150) |_| add.step(steps, &n, &t, 12, .{ .wheel = 1 }),
        1 => {
            for (0..36) |_| add.step(steps, &n, &t, 33, .{ .key = .{ .cp = 'd', .ctrl = true } });
            t += 600;
            add.step(steps, &n, &t, 20, .{ .key = .{ .cp = 'g', .text = "g" } });
            add.step(steps, &n, &t, 900, .{ .key = .{ .cp = 'e', .text = "e" } });
            add.step(steps, &n, &t, 20, .{ .key = .{ .cp = 'g', .text = "g" } });
            add.step(steps, &n, &t, 900, .{ .key = .{ .cp = 'g', .text = "g" } });
        },
        2 => {
            for (0..45) |_| add.step(steps, &n, &t, 33, .{ .key = .{ .cp = 'j', .text = "j" } });
            t += 300;
            add.step(steps, &n, &t, 300, .{ .key = .{ .cp = 'i', .text = "i" } });
            for ("the quick brown fox jumps") |ch| add.step(steps, &n, &t, 60, .{ .key = .{ .cp = ch, .text = &.{ch} } });
            for (0..10) |_| add.step(steps, &n, &t, 70, .{ .key = .{ .cp = pardes.Key.left } });
            for (0..4) |_| add.step(steps, &n, &t, 90, .{ .key = .{ .cp = pardes.Key.up } });
            add.step(steps, &n, &t, 400, .{ .key = .{ .cp = pardes.Key.escape } });
        },
        3 => {
            for ("/pane.") |ch| add.step(steps, &n, &t, 180, .{ .key = .{ .cp = ch, .text = &.{ch} } });
            t += 300;
            for (0..8) |_| add.step(steps, &n, &t, 300, .{ .key = .{ .cp = 'n', .ctrl = true } });
        },
        4 => {
            add.step(steps, &n, &t, 1800, .{ .exec = "cat stress.zig" });
            for (0..60) |_| add.step(steps, &n, &t, 16, .{ .wheel = -1 });
            t += 400;
            for (0..12) |_| add.step(steps, &n, &t, 150, .{ .wheel = 1 });
        },
        else => for (0..8) |_| add.step(steps, &n, &t, 450, .{ .key = .{ .cp = 'f', .ctrl = true } }),
    }
    t += 800;
    add.step(steps, &n, &t, 0, .end);
    return n;
}

const clip_flick = blk: {
    var steps: [256]Step = undefined;
    const n = clipSteps(0, &steps);
    const out = steps[0..n].*;
    break :blk out;
};
const clip_far = blk: {
    var steps: [256]Step = undefined;
    const n = clipSteps(1, &steps);
    const out = steps[0..n].*;
    break :blk out;
};
const clip_cursor = blk: {
    var steps: [256]Step = undefined;
    const n = clipSteps(2, &steps);
    const out = steps[0..n].*;
    break :blk out;
};
const clip_query = blk: {
    var steps: [256]Step = undefined;
    const n = clipSteps(3, &steps);
    const out = steps[0..n].*;
    break :blk out;
};
const clip_terminal = blk: {
    var steps: [256]Step = undefined;
    const n = clipSteps(4, &steps);
    const out = steps[0..n].*;
    break :blk out;
};
const clip_pages = blk: {
    var steps: [256]Step = undefined;
    const n = clipSteps(5, &steps);
    const out = steps[0..n].*;
    break :blk out;
};

/// The cursor along long lines (a minified JS, a one-line JSON, six 360 KB
/// lines), Wrap on and then off (off, the view scrolls sideways): w and e
/// held, l held, $ and 0, f and t, gl and gh, typing at a line's end and in
/// its middle, a selection swept with held L and W.
pub const longlines = blk: {
    @setEvalBranchQuota(1_000_000);
    var steps: [8192]Step = undefined;
    var n: usize = 0;
    var t: u32 = 1500;
    const add = struct {
        fn step(s: []Step, k: *usize, at: *u32, gap: u32, act: @FieldType(Step, "act")) void {
            s[k.*] = .{ .at_ms = at.*, .act = act };
            k.* += 1;
            at.* += gap;
        }
        fn keys(s: []Step, k: *usize, at: *u32, gap: u32, text: []const u8, times: usize) void {
            for (0..times) |_| for (text, 0..) |_, i| step(s, k, at, gap, .{ .key = .{ .cp = text[i], .text = text[i .. i + 1] } });
        }
    };
    for ([_][]const u8{ "wrap", "nowrap" }) |mode| {
        if (std.mem.eql(u8, mode, "nowrap")) add.step(&steps, &n, &t, 300, .{ .exec = "Wrap" });
        for ([_][]const u8{ "minified.js", "oneline.json", "hugelines.txt" }) |file| {
            add.step(&steps, &n, &t, 1500, .{ .exec = "Look kinds/" ++ file });
            const tag = file ++ "-" ++ mode;
            add.keys(&steps, &n, &t, 400, "gg", 1);
            add.step(&steps, &n, &t, 0, .{ .phase = "w-held-" ++ tag });
            add.keys(&steps, &n, &t, 33, "w", 60);
            add.step(&steps, &n, &t, 0, .{ .phase = "e-held-" ++ tag });
            add.keys(&steps, &n, &t, 33, "e", 60);
            add.step(&steps, &n, &t, 0, .{ .phase = "l-held-" ++ tag });
            add.keys(&steps, &n, &t, 33, "l", 90);
            add.step(&steps, &n, &t, 0, .{ .phase = "dollar-zero-" ++ tag });
            add.keys(&steps, &n, &t, 600, "$0", 2);
            add.step(&steps, &n, &t, 0, .{ .phase = "f-t-" ++ tag });
            add.keys(&steps, &n, &t, 150, "f,", 8);
            add.keys(&steps, &n, &t, 150, "t ", 8);
            add.step(&steps, &n, &t, 0, .{ .phase = "gl-gh-" ++ tag });
            add.keys(&steps, &n, &t, 500, "glgh", 3);
            add.step(&steps, &n, &t, 0, .{ .phase = "insert-end-" ++ tag });
            add.keys(&steps, &n, &t, 300, "$a", 1);
            add.keys(&steps, &n, &t, 45, "typed at the end ", 2);
            add.step(&steps, &n, &t, 300, .{ .key = .{ .cp = pardes.Key.escape } });
            add.step(&steps, &n, &t, 0, .{ .phase = "insert-middle-" ++ tag });
            add.keys(&steps, &n, &t, 40, "0", 1);
            add.keys(&steps, &n, &t, 40, "40000l", 1);
            add.keys(&steps, &n, &t, 300, "i", 1);
            add.keys(&steps, &n, &t, 45, "typed in the middle ", 2);
            add.step(&steps, &n, &t, 300, .{ .key = .{ .cp = pardes.Key.escape } });
            add.step(&steps, &n, &t, 0, .{ .phase = "sweep-L-W-" ++ tag });
            add.keys(&steps, &n, &t, 33, "L", 40);
            add.keys(&steps, &n, &t, 33, "W", 30);
            // Del twice: the second throws the typing away.
            add.step(&steps, &n, &t, 300, .{ .exec = "Del" });
            add.step(&steps, &n, &t, 600, .{ .exec = "Del" });
        }
    }
    add.step(&steps, &n, &t, 0, .end);
    const out = steps[0..n].*;
    break :blk out;
};

/// What insert mode's paste pastes: 200 lines.
const pasted = blk: {
    const line = "a pasted line, one of two hundred\n";
    var out: [line.len * 200]u8 = undefined;
    for (0..200) |i| @memcpy(out[i * line.len ..][0..line.len], line);
    break :blk out;
};

/// Every other way a view moves, each its own phase, on the session's file
/// (a big one) and then on a small one: the counted jumps (12G, 9000G, gg),
/// the upward pages, a search's n/N, a query's Ctrl-n moving its owner's
/// view, `*`, Looks at `:40`, at a far line and at another file (a new
/// pane: nothing to glide from), the jumplist both ways, the rail's
/// buttons and the wheel over it, and insert mode: typing, arrows, Enter at
/// the bottom of the view, a 200-line paste.
pub const jumps = blk: {
    @setEvalBranchQuota(400_000);
    var steps: [2048]Step = undefined;
    var n: usize = 0;
    var t: u32 = 1500;
    const add = struct {
        fn step(s: []Step, k: *usize, at: *u32, gap: u32, act: @FieldType(Step, "act")) void {
            s[k.*] = .{ .at_ms = at.*, .act = act };
            k.* += 1;
            at.* += gap;
        }
        fn text(s: []Step, k: *usize, at: *u32, gap: u32, keys: []const u8) void {
            for (keys, 0..) |_, i| step(s, k, at, gap, .{ .key = .{ .cp = keys[i], .text = keys[i .. i + 1] } });
        }
        fn key(s: []Step, k: *usize, at: *u32, gap: u32, cp: u21, ctrl: bool, times: usize) void {
            for (0..times) |_| step(s, k, at, gap, .{ .key = .{ .cp = cp, .ctrl = ctrl } });
        }
    };
    for ([_][]const u8{ "big", "small" }) |size| {
        if (std.mem.eql(u8, size, "small")) add.step(&steps, &n, &t, 1200, .{ .exec = "Look kinds/guide.typ" });
        add.step(&steps, &n, &t, 0, .{ .phase = "counted-G-" ++ size });
        for ([_][]const u8{ "12G", "300G", "9000G", "gg", "ge", "gg" }) |keys| {
            add.text(&steps, &n, &t, 20, keys);
            t += 700;
        }
        add.step(&steps, &n, &t, 0, .{ .phase = "pages-up-" ++ size });
        add.text(&steps, &n, &t, 700, "ge");
        add.key(&steps, &n, &t, 500, 'u', true, 3);
        add.key(&steps, &n, &t, 500, 'b', true, 3);
        add.key(&steps, &n, &t, 500, pardes.Key.page_up, false, 3);
        add.step(&steps, &n, &t, 0, .{ .phase = "search-n-" ++ size });
        add.text(&steps, &n, &t, 700, "gg");
        add.text(&steps, &n, &t, 120, "/const ");
        add.key(&steps, &n, &t, 600, pardes.Key.enter, false, 1);
        add.text(&steps, &n, &t, 400, "nnnnnnNNNNNN");
        add.step(&steps, &n, &t, 0, .{ .phase = "rail-" ++ size });
        add.text(&steps, &n, &t, 700, "ge");
        for ([_]pardes.Mouse.Button{ .left, .left, .right, .right, .middle }, [_]f32{ 0.8, 0.3, 0.6, 0.9, 0.5 }) |button, at|
            add.step(&steps, &n, &t, 600, .{ .click = .{ .button = button, .at = at, .rail = true } });
        for (0..8) |_| add.step(&steps, &n, &t, 120, .{ .rail_wheel = 1 });
        t += 500;
        add.step(&steps, &n, &t, 0, .{ .phase = "query-ctrl-n-" ++ size });
        add.text(&steps, &n, &t, 120, "/pub fn");
        add.key(&steps, &n, &t, 250, 'n', true, 8);
        add.key(&steps, &n, &t, 250, 'p', true, 4);
        add.key(&steps, &n, &t, 700, pardes.Key.escape, false, 1);
        add.step(&steps, &n, &t, 0, .{ .phase = "star-" ++ size });
        add.text(&steps, &n, &t, 300, "ggww*");
        add.text(&steps, &n, &t, 500, "nnnn");
        add.step(&steps, &n, &t, 0, .{ .phase = "look-" ++ size });
        add.step(&steps, &n, &t, 900, .{ .exec = "Look :40" });
        add.step(&steps, &n, &t, 900, .{ .exec = "Look :7000" });
        add.step(&steps, &n, &t, 900, .{ .exec = "Look :200" });
        add.step(&steps, &n, &t, 0, .{ .phase = "jumplist-" ++ size });
        add.key(&steps, &n, &t, 600, 'o', true, 3);
        add.key(&steps, &n, &t, 600, 'i', true, 3);
        add.text(&steps, &n, &t, 40, " jo");
        t += 600;
        add.text(&steps, &n, &t, 40, " ji");
        t += 600;
        add.step(&steps, &n, &t, 0, .{ .phase = "insert-" ++ size });
        add.text(&steps, &n, &t, 300, "ggi");
        for (0..4) |_| add.text(&steps, &n, &t, 45, "the quick brown fox ");
        add.key(&steps, &n, &t, 60, pardes.Key.left, false, 12);
        add.key(&steps, &n, &t, 60, pardes.Key.right, false, 12);
        add.key(&steps, &n, &t, 60, pardes.Key.down, false, 6);
        // Enter after Enter until the view has to follow, a line at a time.
        add.key(&steps, &n, &t, 70, pardes.Key.enter, false, 60);
        add.step(&steps, &n, &t, 600, .{ .paste = &pasted });
        add.key(&steps, &n, &t, 700, pardes.Key.escape, false, 1);
    }
    add.step(&steps, &n, &t, 0, .{ .phase = "look-new-pane" });
    add.step(&steps, &n, &t, 1500, .{ .exec = "Look ../../src/layout.zig:9000" });
    add.step(&steps, &n, &t, 0, .end);
    const out = steps[0..n].*;
    break :blk out;
};

/// Each file of the corpus (`.scratch/tools/corpus.sh`) is opened by name,
/// relative to the first file's directory, and put through `scrollSet`;
/// the long-line ones a second time with Wrap off.
pub const kinds = blk: {
    @setEvalBranchQuota(200_000);
    var steps: [4096]Step = undefined;
    var n: usize = 0;
    var t: u32 = 1500;
    const files = [_][]const u8{ "generated-parser.c", "commands.rs", "tables.py", "jquery.js", "minified.js", "grammar.json", "oneline.json", "CHANGELOG.md", "cascade.html", "simdutf.cpp", "parser.ts", "fcnal-test.sh", "data.go", "guide.typ", "unicode.txt", "big.log", "words.txt", "hugelines.txt" };
    for (files ++ [_][]const u8{ "minified.js!", "oneline.json!", "hugelines.txt!" }) |file| {
        const unwrapped = file[file.len - 1] == '!';
        const name = if (unwrapped) file[0 .. file.len - 1] else file;
        // Wrap goes off once, before the second round.
        if (unwrapped and std.mem.eql(u8, name, "minified.js")) {
            steps[n] = .{ .at_ms = t, .act = .{ .exec = "Wrap" } };
            n += 1;
            t += 200;
        }
        steps[n] = .{ .at_ms = t, .act = .{ .exec = "Look kinds/" ++ name } };
        n += 1;
        t += 1200;
        steps[n] = .{ .at_ms = t, .act = .{ .phase = file } };
        n += 1;
        scrollSet(&steps, &n, &t);
        steps[n] = .{ .at_ms = t, .act = .{ .exec = "Del" } };
        n += 1;
        t += 400;
    }
    steps[n] = .{ .at_ms = t, .act = .end };
    n += 1;
    const out = steps[0..n].*;
    break :blk out;
};

/// The other kinds of pane, opened from the first file's directory, each
/// put through `scrollSet`: a command's fast output (the stress file, then
/// `yes`), a +Pager, a directory, a PDF, a picture, Help's output pane, a
/// +Diff, and the query pane typed into (its rows refilled each key, its
/// owner's places drawn).
pub const panes = blk: {
    @setEvalBranchQuota(200_000);
    var steps: [4096]Step = undefined;
    var n: usize = 0;
    var t: u32 = 1500;
    const Kind = struct { name: []const u8, open: []const u8, settle: u32 };
    for ([_]Kind{
        .{ .name = "command-cat", .open = "cat stress.zig", .settle = 300 },
        .{ .name = "command-yes", .open = "yes | head -200000", .settle = 300 },
        .{ .name = "pager", .open = "cat stress.zig | pardes -", .settle = 2500 },
        .{ .name = "directory", .open = "Look kinds/", .settle = 1200 },
        .{ .name = "pdf", .open = "Look ../../docs/design.pdf", .settle = 2000 },
        .{ .name = "image", .open = "Look ../../docs/screenshots/atelier.png", .settle = 1200 },
        .{ .name = "help", .open = "Help", .settle = 1200 },
        .{ .name = "changelog", .open = "Changelog", .settle = 1200 },
    }) |kind| {
        steps[n] = .{ .at_ms = t, .act = .{ .phase = kind.name } };
        n += 1;
        steps[n] = .{ .at_ms = t, .act = .{ .exec = kind.open } };
        n += 1;
        // A command's output streams in while it is scrolled.
        t += kind.settle;
        scrollSet(&steps, &n, &t);
        steps[n] = .{ .at_ms = t, .act = .{ .exec = "Del" } };
        n += 1;
        t += 600;
    }
    steps[n] = .{ .at_ms = t, .act = .{ .phase = "query-typing" } };
    n += 1;
    steps[n] = .{ .at_ms = t, .act = .{ .key = .{ .cp = '/', .text = "/" } } };
    n += 1;
    t += 300;
    for ("pane.vie") |ch| {
        steps[n] = .{ .at_ms = t, .act = .{ .key = .{ .cp = ch, .text = &.{ch} } } };
        n += 1;
        t += 150;
    }
    for (0..12) |_| {
        steps[n] = .{ .at_ms = t, .act = .{ .key = .{ .cp = 'n', .ctrl = true } } };
        n += 1;
        t += 120;
    }
    steps[n] = .{ .at_ms = t, .act = .{ .key = .{ .cp = pardes.Key.escape } } };
    n += 1;
    t += 800;
    steps[n] = .{ .at_ms = t, .act = .end };
    n += 1;
    const out = steps[0..n].*;
    break :blk out;
};

/// Throughput on a big file (layout.zig x23, 30k lines): each input held
/// for three seconds at a keyboard's repeat (33 ms) or a wheel spun hard (a
/// notch every 8 ms), so the picture has to keep up with more than it can
/// show: a spin, held Ctrl-d, held j, held PageDown, then the far jumps.
pub const stress = blk: {
    @setEvalBranchQuota(100_000);
    var steps: [1024]Step = undefined;
    var n: usize = 0;
    var t: u32 = 1500;
    const Held = struct { name: []const u8, key: ?pardes.Key, every: u32 };
    for ([_]Held{
        .{ .name = "spin", .key = null, .every = 8 },
        .{ .name = "held-ctrl-d", .key = .{ .cp = 'd', .ctrl = true }, .every = 33 },
        .{ .name = "held-j", .key = .{ .cp = 'j', .text = "j" }, .every = 33 },
        .{ .name = "held-pagedown", .key = .{ .cp = pardes.Key.page_down }, .every = 33 },
    }) |held| {
        // Each from the top.
        for ([_]u32{ 0, 20 }) |dt| {
            steps[n] = .{ .at_ms = t + dt, .act = .{ .key = .{ .cp = 'g', .text = "g" } } };
            n += 1;
        }
        t += 800;
        steps[n] = .{ .at_ms = t, .act = .{ .phase = held.name } };
        n += 1;
        const end = t + 3000;
        while (t < end) : (t += held.every) {
            steps[n] = .{ .at_ms = t, .act = if (held.key) |key| .{ .key = key } else .{ .wheel = 1 } };
            n += 1;
        }
        t += 1000;
    }
    steps[n] = .{ .at_ms = t, .act = .{ .phase = "far" } };
    n += 1;
    for (0..3) |_| {
        for ([_][]const u8{ "e", "g" }) |second| {
            for ([_][]const u8{ "g", second }, 0..) |text, i| {
                steps[n] = .{ .at_ms = t + 20 * @as(u32, i), .act = .{ .key = .{ .cp = text[0], .text = text } } };
                n += 1;
            }
            t += 700;
        }
    }
    steps[n] = .{ .at_ms = t + 500, .act = .end };
    n += 1;
    const out = steps[0..n].*;
    break :blk out;
};

test "frame stats: gaps into a motion count as jank, idle gaps do not, and the ring keeps the last frames" {
    var stats: FrameStats = .{ .period_ns = 16 * std.time.ns_per_ms };
    const ms = std.time.ns_per_ms;
    var t: u64 = 1000 * ms;
    // An idle frame, then a second of nothing: no jank, nothing was moving.
    stats.mark_start = t;
    stats.mark_update = t + ms;
    stats.mark_render = t + 2 * ms;
    stats.record(t + 3 * ms, false, false);
    t += 1000 * ms;
    // A frame after the pause starts a motion; then one on time, then one
    // two periods late.
    for ([_]u64{ 16, 16, 33 }) |late| {
        t += late * ms;
        stats.mark_start = t - ms;
        stats.mark_update = t - ms;
        stats.mark_render = t - ms;
        stats.record(t, true, false);
    }
    try std.testing.expectEqual(@as(u64, 1), stats.jank);
    try std.testing.expectEqual(@as(u64, 4), stats.count);
    const recent = stats.last(3);
    try std.testing.expectEqual(@as(u32, 1_013_000), recent[0][0].gap_us);
    try std.testing.expect(!recent[0][0].moving and recent[0][1].moving);
    try std.testing.expectEqual(@as(u32, 33_000), recent[0][2].gap_us);
    // Only the frames something moved into have a gap that counts.
    const gap = Spread.of(stats.last(capacity), .gap);
    try std.testing.expectEqual(@as(usize, 2), gap.n);
    try std.testing.expectEqual(@as(u32, 33_000), gap.max);
    // Round the ring: still the last ones, oldest first.
    for (0..capacity + 5) |i| {
        stats.mark_start = t;
        stats.mark_update = t;
        stats.mark_render = t;
        t += 16 * ms;
        stats.scroll = @floatFromInt(i);
        stats.record(t, true, false);
    }
    const tail = stats.last(10);
    try std.testing.expectEqual(@as(usize, 10), tail[0].len + tail[1].len);
    const newest = if (tail[1].len != 0) tail[1][tail[1].len - 1] else tail[0][tail[0].len - 1];
    try std.testing.expectEqual(@as(f32, capacity + 4), newest.scroll);
    // Each timedemo ends, and its steps are in time order.
    for ([_][]const Step{ &demo, &stress, &kinds, &panes, &one, &jumps, &longlines }) |steps| {
        try std.testing.expect(steps[steps.len - 1].act == .end);
        for (steps[1..], steps[0 .. steps.len - 1]) |b, a| try std.testing.expect(b.at_ms >= a.at_ms);
    }
    // An input is answered by the next drawn frame, and only by it.
    stats.input(t - 2 * ms);
    stats.mark_start = t;
    stats.mark_update = t;
    stats.mark_render = t;
    stats.record(t + 3 * ms, false, false);
    try std.testing.expectEqual(@as(u32, 5000), stats.last(1)[0][0].latency_us);
    stats.record(t + 4 * ms, false, false);
    try std.testing.expectEqual(@as(u32, 0), stats.last(1)[0][0].latency_us);
}