-
Notifications
You must be signed in to change notification settings - Fork 4
/
term_perf.zig
187 lines (160 loc) · 5.64 KB
/
term_perf.zig
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
const std = @import("std");
const tui = @import("./tui.zig");
const fs = std.fs;
const fmt = std.fmt;
const heap = std.heap;
const mem = std.mem;
const io = std.io;
const time = std.time;
const posix = std.posix;
const terminal = tui.terminal;
const print = std.debug.print;
const BW = tui.BufferedStdOut;
const Draw = tui.Draw;
const FSStats = struct {
bytes_per_row: f64,
num_rows: f64,
num_cols: f64,
fill_per_row: f64,
flush_per_row: f64,
};
const RenderStats = struct {
bytes_per_row: f64,
num_rows: f64,
num_cols: f64,
fill_per_row: f64,
flush_per_row: f64,
clear_screen: f64,
fill_screen: f64,
total: f64,
};
const TimeLog = std.ArrayList(RenderStats);
pub fn main() !void {
var bak: posix.termios = undefined;
try terminal.enableRawMode(&bak);
defer terminal.disableRawMode(&bak) catch {};
var writer = BW.init();
writer.use_csi_sync = true;
// writer.use_dcs_sync = true;
var draw = Draw{ .writer = &writer };
try draw.hideCursor();
defer draw.showCursor() catch {};
var timelog = TimeLog.init(std.heap.page_allocator);
defer timelog.deinit();
try loop(&draw, false, &timelog);
print_timelog(&timelog);
}
pub fn print_timelog(timelog: *TimeLog) void {
var totals: RenderStats = .{
.bytes_per_row = 0,
.num_rows = 0,
.num_cols = 0,
.fill_per_row = 0,
.flush_per_row = 0,
.clear_screen = 0,
.fill_screen = 0,
.total = 0,
};
for (0..timelog.items.len) |i| {
const log = timelog.items[i];
totals.bytes_per_row += log.bytes_per_row;
totals.num_rows += log.num_rows;
totals.num_cols += log.num_cols;
totals.fill_per_row += log.fill_per_row;
totals.flush_per_row += log.flush_per_row;
totals.clear_screen += log.clear_screen;
totals.fill_screen += log.fill_screen;
totals.total += log.total;
}
const len: f64 = @floatFromInt(timelog.items.len);
std.debug.print("num_entries : {d:14.0}\n", .{len});
std.debug.print("num_rows : {d:14.0}\n", .{totals.num_rows / len});
std.debug.print("num_cols : {d:14.0}\n", .{totals.num_cols / len});
std.debug.print("bytes_per_row : {d:14.0} B\n", .{totals.bytes_per_row / len});
std.debug.print("fill_per_row : {d:14.3} µs\n", .{totals.fill_per_row / len});
std.debug.print("flush_per_row : {d:14.3} µs\n", .{totals.flush_per_row / len});
std.debug.print("clear_screen : {d:14.3} µs\n", .{totals.clear_screen / len});
std.debug.print("fill_screen : {d:14.3} µs\n", .{totals.fill_screen / len});
std.debug.print("average_total : {d:14.3} µs\n", .{totals.total / len});
std.debug.print("total : {d:14.3} ms\n", .{totals.total / 1000});
}
pub fn loop(draw: *Draw, is_manual: bool, timelog: *TimeLog) !void {
var reader = io.getStdIn().reader();
var char: u8 = 32;
var total: usize = 0;
while (true) {
const start = time.nanoTimestamp();
try draw.moveCursor(0, 0);
const fs_start = time.nanoTimestamp();
const fs_times = try fillScreenChar(char, draw.writer);
const fs_end = time.nanoTimestamp();
var input_diff: i128 = 0;
if (is_manual) {
const input_start = time.nanoTimestamp();
while (true) {
const b = try reader.readByte();
switch (b) {
'j' => char +|= 1,
'k' => char -|= 1,
'q' => return,
else => continue,
}
break;
}
input_diff = time.nanoTimestamp() - input_start;
} else {
char +|= 1;
total +|= 1;
}
const clear_start = time.nanoTimestamp();
// try draw.clearScreen();
const total_end = time.nanoTimestamp();
const clear_diff: f64 = @floatFromInt(total_end - clear_start);
const total_diff: f64 = @floatFromInt((total_end - start) - input_diff);
const fs_diff: f64 = @floatFromInt(fs_end - fs_start);
try timelog.append(.{
.bytes_per_row = fs_times.bytes_per_row,
.num_rows = fs_times.num_rows,
.num_cols = fs_times.num_cols,
.fill_per_row = fs_times.fill_per_row,
.flush_per_row = fs_times.flush_per_row,
.clear_screen = clear_diff / 1000,
.fill_screen = fs_diff / 1000,
.total = total_diff / 1000,
});
if (total == 256) break;
}
}
pub fn fillScreenChar(char: u8, writer_: *BW) !FSStats {
var writer = writer_;
const c: u8 = switch (char) {
0...32 => char + 32,
33...126 => char,
127...255 => (char % 32) + 32,
};
const size = terminal.getTerminalSize();
writer.buffered();
const start_fill = time.nanoTimestamp();
for (0..(size.rows - 1)) |r| {
for (0..size.cols) |_| {
_ = try writer.print("\x1b[K\x1b[{d}m{c}\x1b[0m", .{ (c +| r) % 7 + 31, c });
}
_ = try writer.write("\n");
}
const end_fill = time.nanoTimestamp();
const bytes: f64 = @floatFromInt(writer.end);
try writer.flush();
writer.unbuffered();
const end_flush = time.nanoTimestamp();
const diff_fill: f64 = @floatFromInt(end_fill - start_fill);
const diff_flush: f64 = @floatFromInt(end_flush - end_fill);
const cols: f64 = @floatFromInt(size.cols);
const rows: f64 = @floatFromInt(size.rows);
return .{
.bytes_per_row = bytes / rows,
.num_cols = cols,
.num_rows = rows,
.fill_per_row = (diff_fill / rows) / 1000,
.flush_per_row = (diff_flush / rows) / 1000,
};
}