Repository navigation
Expand file tree
/
Copy pathBenchmarks.rake
More file actions
444 lines (395 loc) · 13.9 KB
/
Copy pathBenchmarks.rake
File metadata and controls
444 lines (395 loc) · 13.9 KB
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
require "json"
require "socket"
require "timeout"
require "time"
class FpsBenchmarkClient
def initialize(socket_path)
@socket = UNIXSocket.new(socket_path)
@next_id = 0
end
def request(command)
@next_id += 1
@socket.puts(JSON.generate({ id: @next_id }.merge(command)))
ready = IO.select([@socket], nil, nil, 30)
raise "ascdraw did not respond within 30 seconds" unless ready
line = @socket.gets
raise "ascdraw closed the automation connection" unless line
response = JSON.parse(line)
unless response["id"] == @next_id
raise "automation response ID #{response["id"]} did not match #{@next_id}"
end
raise response.fetch("error", "automation command failed") unless response["ok"]
response["result"]
end
def close
@socket.close
end
end
module FpsBenchmark
module_function
def point_on_circle(t, radius)
angle = 2.0 * Math::PI * t
x = radius * Math.cos(angle)
y = radius * Math.sin(angle)
[x, y]
end
def run
operations = positive_integer("FPS_OPERATIONS", 300)
warmup = nonnegative_integer("FPS_WARMUP", 20)
report_dir = ENV.fetch("FPS_REPORT_DIR", "target/benchmarks/fps")
fixture = WorkspaceFixture.path(default: 1)
FileUtils.mkdir_p(report_dir)
Dir.mktmpdir("ascdraw-fps") do |temporary_dir|
socket_path = File.join(temporary_dir, "control.sock")
document_path = WorkspaceFixture.materialize(fixture, temporary_dir)
saved_document_path = WorkspaceFixture.materialize(
fixture,
temporary_dir,
basename: "saved-workspace",
)
log_path = File.join(report_dir, "ascdraw.log")
File.open(log_path, "w") do |log|
reports = run_editor(socket_path, document_path, log) do |client|
run_scenarios(client, warmup, operations) +
[measure_zoom(client, "zoom-file", warmup, operations)]
end
minimum_zoom_path = WorkspaceFixture.materialize(
fixture,
temporary_dir,
basename: "minimum-zoom-scroll",
)
reports << run_editor(socket_path, minimum_zoom_path, log) do |client|
zoom_to_minimum(client)
measure_scroll(client, "minimum-zoom-scroll", warmup, operations)
end
zoom_return_path = WorkspaceFixture.materialize(
fixture,
temporary_dir,
basename: "minimum-zoom-scroll-return",
)
reports << run_editor(socket_path, zoom_return_path, log) do |client|
measure_zoom_scroll_return(
client,
"minimum-zoom-scroll-return",
warmup,
operations,
)
end
reports.concat(
run_editor(socket_path, saved_document_path, log) do |client|
run_scenarios(client, warmup, operations, "saved-file-")
end,
)
empty_home = File.join(temporary_dir, "empty-home")
FileUtils.mkdir_p(empty_home)
reports << run_editor(socket_path, nil, log, { "HOME" => empty_home }) do |client|
measure_zoom(client, "zoom-empty", warmup, operations)
end
write_reports(report_dir, reports, warmup, operations, fixture)
print_summary(reports, report_dir)
end
end
end
def run_mini
operations = positive_integer("FPS_MINI_OPERATIONS", 30)
warmup = nonnegative_integer("FPS_MINI_WARMUP", 0)
report_dir = ENV.fetch("FPS_MINI_REPORT_DIR", "target/benchmarks/fps-mini")
fixture = WorkspaceFixture.path(default: 1)
FileUtils.mkdir_p(report_dir)
Dir.mktmpdir("ascdraw-fps-mini") do |temporary_dir|
socket_path = File.join(temporary_dir, "control.sock")
document_path = WorkspaceFixture.materialize(fixture, temporary_dir)
log_path = File.join(report_dir, "ascdraw.log")
File.open(log_path, "w") do |log|
reports = run_editor(socket_path, document_path, log) do |client|
run_scenarios(client, warmup, operations, include_scroll: false)
end
write_reports(report_dir, reports, warmup, operations, fixture)
print_summary(reports, report_dir)
end
end
end
def run_zoom
operations = positive_integer("ZOOM_OPERATIONS", 1_200)
warmup = nonnegative_integer("ZOOM_WARMUP", 0)
report_dir = ENV.fetch("ZOOM_REPORT_DIR", "target/benchmarks/zoom")
fixture = WorkspaceFixture.path(default: 1)
FileUtils.mkdir_p(report_dir)
Dir.mktmpdir("ascdraw-zoom") do |temporary_dir|
socket_path = File.join(temporary_dir, "control.sock")
document_path = WorkspaceFixture.materialize(fixture, temporary_dir)
log_path = File.join(report_dir, "ascdraw.log")
File.open(log_path, "w") do |log|
report = run_editor(socket_path, document_path, log) do |client|
measure_zoom(client, "zoom-file", warmup, operations)
end
reports = [report]
write_reports(report_dir, reports, warmup, operations, fixture)
print_summary(reports, report_dir)
end
end
end
def run_editor(socket_path, document_path, log, environment = {})
arguments = [
"target/release/ascdraw",
"--automation-socket",
socket_path,
]
arguments << document_path if document_path
pid = Process.spawn(
environment,
*arguments,
out: log,
err: [:child, :out],
)
client = nil
begin
client = connect(socket_path, pid)
yield client
ensure
begin
client&.request(command: "shutdown")
ensure
client&.close
begin
wait_for_exit(pid)
ensure
stop_process(pid)
end
end
end
end
def connect(socket_path, pid)
deadline = Process.clock_gettime(Process::CLOCK_MONOTONIC) + 15
loop do
if File.socket?(socket_path)
client = FpsBenchmarkClient.new(socket_path)
result = client.request(command: "ping")
return client if result["ready"]
client.close
end
if Process.waitpid(pid, Process::WNOHANG)
raise "ascdraw exited before the automation socket became ready"
end
raise "ascdraw did not become ready within 15 seconds" if Process.clock_gettime(Process::CLOCK_MONOTONIC) >= deadline
sleep 0.05
end
end
def run_scenarios(client, warmup, operations, name_prefix = "", include_scroll: true)
reports = []
if include_scroll
reports << measure_scroll(client, "#{name_prefix}scroll", warmup, operations)
end
client.request(command: "key", key: "i", count: 1)
characters = "□■▫▪◆◊·∙•●△▽◁▷▲▼◀▶↑↓░▒▓█▘▝▖▗▌▐▞▚▛▜α×▪+ø◦◯Øø╳╱╲÷×±←→▵▿◃▹▴▾◂▸▙▟▀▄█β▪↓+ø¤◇☆★※↕↔▏▎▍▋▊▉▔δ▪↑↓+ø▁▂▃▅▆▇▕γ▪↑+ø".chars
character_index = 0
inserts = 0
reports << measure(client, "#{name_prefix}text", warmup, operations) do
client.request(command: "text", text: characters[character_index])
character_index = (character_index + 1) % characters.length
inserts += 1
if (inserts % 10).zero?
client.request(command: "key", key: "left", count: 10)
client.request(command: "key", key: "down", count: 1)
end
end
client.request(command: "key", key: "escape", count: 1)
client.request(command: "key", key: "1", count: 1)
client.request(command: "key", key: "2", count: 1)
spiral_directions = ["right", "up", "left", "down"]
direction_index = 0
segment_length = 1
steps_in_segment = 0
reports << measure(client, "#{name_prefix}line", warmup, operations) do
client.request(
command: "key",
key: spiral_directions[direction_index],
modifiers: { control: true },
count: 1,
)
steps_in_segment += 1
if steps_in_segment == segment_length
steps_in_segment = 0
direction_index = (direction_index + 1) % spiral_directions.length
segment_length += 1
end
end
reports
end
def measure_zoom(client, name, warmup, operations)
minimum = 5.0
maximum = 48.0
step = 0.25
current = minimum
direction = 1.0
client.request(command: "zoom", delta: -1_000.0)
client.request(command: "zoom", delta: minimum - 4.0)
measure(client, name, warmup, operations) do
client.request(command: "zoom", delta: step * direction)
current += step * direction
direction = -1.0 if current >= maximum
direction = 1.0 if current <= minimum
end
end
def measure_scroll(client, name, warmup, operations)
deltas = circular_scroll_deltas
measure(client, name, warmup, operations) do
x, y = deltas.next
client.request(command: "scroll", x: x, y: y, steps: 1)
end
end
def measure_zoom_scroll_return(client, name, warmup, operations)
if warmup.positive?
zoom_steps = zoom_to_minimum(client)
scroll(client, circular_scroll_deltas, warmup)
zoom_by_steps(client, 0.25, zoom_steps)
end
client.request(command: "metrics", reset: true)
started = Process.clock_gettime(Process::CLOCK_MONOTONIC)
zoom_steps = zoom_to_minimum(client)
scroll(client, circular_scroll_deltas, operations)
zoom_by_steps(client, 0.25, zoom_steps)
elapsed = Process.clock_gettime(Process::CLOCK_MONOTONIC) - started
metrics = client.request(command: "metrics", reset: false)
measured_operations = operations + zoom_steps * 2 + 1
{
"scenario" => name,
"operations" => measured_operations,
"elapsed_seconds" => elapsed,
"operations_per_second" => measured_operations / elapsed,
"metrics" => metrics,
}
end
def circular_scroll_deltas
Enumerator.new do |deltas|
points = 100
radius = 40.0
step = 0
previous = point_on_circle(0.0, radius)
loop do
point = point_on_circle((step % points).fdiv(points - 1), radius)
step += 1
deltas << [point[0] - previous[0], point[1] - previous[1]]
previous = point
end
end
end
def scroll(client, deltas, count)
count.times do
x, y = deltas.next
client.request(command: "scroll", x: x, y: y, steps: 1)
end
end
def zoom_to_minimum(client)
successful_steps = 0
loop do
result = client.request(command: "zoom", delta: -0.25)
return successful_steps unless zoom_changed?(result)
successful_steps += 1
raise "minimum zoom was not reached within 1,000 steps" if successful_steps >= 1_000
end
end
def zoom_by_steps(client, delta, count)
count.times do
result = client.request(command: "zoom", delta: delta)
unless zoom_changed?(result)
raise "zoom stopped before returning to the initial scale"
end
end
end
def zoom_changed?(result)
return result if result == true || result == false
!result.is_a?(Hash) || result.fetch("changed", true)
end
def measure(client, name, warmup, operations, &operation)
warmup.times(&operation)
client.request(command: "metrics", reset: true)
started = Process.clock_gettime(Process::CLOCK_MONOTONIC)
operations.times(&operation)
elapsed = Process.clock_gettime(Process::CLOCK_MONOTONIC) - started
metrics = client.request(command: "metrics", reset: false)
{
"scenario" => name,
"operations" => operations,
"elapsed_seconds" => elapsed,
"operations_per_second" => operations / elapsed,
"metrics" => metrics,
}
end
def write_reports(report_dir, reports, warmup, operations, fixture)
reports.each do |report|
path = File.join(report_dir, "#{report.fetch("scenario")}.json")
File.write(path, JSON.pretty_generate(report) + "\n")
end
summary = {
"generated_at" => Time.now.utc.iso8601,
"build" => "release",
"warmup_operations" => warmup,
"measured_operations" => operations,
"fixture" => fixture,
"scenarios" => reports,
}
File.write(File.join(report_dir, "summary.json"), JSON.pretty_generate(summary) + "\n")
end
def print_summary(reports, report_dir)
scenario_width = [18, *reports.map { |report| report.fetch("scenario").length }].max
row_format = "%-#{scenario_width}s %10.1f %10.2fms %10.2fms %10.2fms %10.2fms %9.1f%% %9.1f%%"
puts
puts "FPS benchmark"
puts format("%-#{scenario_width}s %10s %12s %12s %12s %12s %10s %10s", "scenario", "FPS", "frame p95", "grid p95", "minimap p95", "event p95", ">8.33ms", ">16.67ms")
reports.each do |report|
metrics = report.fetch("metrics")
puts format(
row_format,
report.fetch("scenario"),
metrics.fetch("frame_rate"),
metrics.dig("frames", "p95_ms"),
metrics.dig("grid", "p95_ms"),
metrics.dig("minimap", "p95_ms"),
metrics.dig("event_to_submit", "p95_ms"),
metrics.fetch("over_8_33_ms_percent"),
metrics.fetch("over_16_67_ms_percent"),
)
end
puts "Reports: #{report_dir}"
end
def wait_for_exit(pid)
Timeout.timeout(10) { Process.wait(pid) }
rescue Timeout::Error
raise "ascdraw did not exit within 10 seconds"
end
def stop_process(pid)
Process.kill("TERM", pid)
Process.wait(pid)
rescue Errno::ESRCH, Errno::ECHILD
nil
end
def positive_integer(name, default)
value = Integer(ENV.fetch(name, default.to_s), 10)
raise "#{name} must be greater than zero" unless value.positive?
value
rescue ArgumentError
raise "#{name} must be an integer"
end
def nonnegative_integer(name, default)
value = Integer(ENV.fetch(name, default.to_s), 10)
raise "#{name} must not be negative" if value.negative?
value
rescue ArgumentError
raise "#{name} must be an integer"
end
end
namespace :benchmark do
desc "Run native-window editing, minimum-zoom scrolling, zoom-return, and reopen FPS benchmarks"
task fps: :build_release do
FpsBenchmark.run
end
desc "Run a short native-window text and line FPS benchmark"
task fps_mini: :build_release do
FpsBenchmark.run_mini
end
desc "Run a fixture-backed native-window zoom benchmark"
task zoom: :build_release do
FpsBenchmark.run_zoom
end
end