-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathscreen_bench_test.go
More file actions
609 lines (560 loc) · 16 KB
/
Copy pathscreen_bench_test.go
File metadata and controls
609 lines (560 loc) · 16 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
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
package glyph
import (
"bytes"
"testing"
)
// mockWriter discards output but counts bytes
type mockWriter struct {
n int
}
func (w *mockWriter) Write(p []byte) (int, error) {
w.n += len(p)
return len(p), nil
}
// BenchmarkFlushFullScreen benchmarks flushing when entire screen changed
func BenchmarkFlushFullScreen(b *testing.B) {
w := &mockWriter{}
s := &Screen{
width: 120,
height: 40,
back: NewBuffer(120, 40),
front: NewBuffer(120, 40),
buf: bytes.Buffer{},
writer: w,
}
// Fill back buffer with content
for y := 0; y < 40; y++ {
for x := 0; x < 120; x++ {
s.back.Set(x, y, Cell{Rune: 'A', Style: DefaultStyle()})
}
}
b.ResetTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
// Reset front buffer to force full redraw
s.front.Clear()
// Mark back buffer dirty so flush will check all rows
s.back.MarkAllDirty()
w.n = 0
s.Flush()
}
b.ReportMetric(float64(w.n), "bytes/op")
}
// BenchmarkFlushSparseChanges benchmarks flushing with only a few changed cells
func BenchmarkFlushSparseChanges(b *testing.B) {
w := &mockWriter{}
s := &Screen{
width: 120,
height: 40,
back: NewBuffer(120, 40),
front: NewBuffer(120, 40),
buf: bytes.Buffer{},
writer: w,
}
// Fill both buffers identically
for y := 0; y < 40; y++ {
for x := 0; x < 120; x++ {
cell := Cell{Rune: 'A', Style: DefaultStyle()}
s.back.Set(x, y, cell)
s.front.Set(x, y, cell)
}
}
b.ResetTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
// Change just 10 cells on different rows
for j := 0; j < 10; j++ {
s.back.Set(j*10, j*4, Cell{Rune: rune('0' + (i+j)%10), Style: DefaultStyle()})
}
w.n = 0
s.Flush()
}
b.ReportMetric(float64(w.n), "bytes/op")
}
// BenchmarkFlushOneLineChanged benchmarks flushing with one line changed
func BenchmarkFlushOneLineChanged(b *testing.B) {
w := &mockWriter{}
s := &Screen{
width: 120,
height: 40,
back: NewBuffer(120, 40),
front: NewBuffer(120, 40),
buf: bytes.Buffer{},
writer: w,
}
// Fill both buffers identically
for y := 0; y < 40; y++ {
for x := 0; x < 120; x++ {
cell := Cell{Rune: 'A', Style: DefaultStyle()}
s.back.Set(x, y, cell)
s.front.Set(x, y, cell)
}
}
b.ResetTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
// Change one entire line
for x := 0; x < 120; x++ {
s.back.Set(x, 20, Cell{Rune: rune('0' + (i+x)%10), Style: DefaultStyle()})
}
w.n = 0
s.Flush()
}
b.ReportMetric(float64(w.n), "bytes/op")
}
// BenchmarkFlushNoChanges benchmarks flushing when nothing changed
func BenchmarkFlushNoChanges(b *testing.B) {
w := &mockWriter{}
s := &Screen{
width: 120,
height: 40,
back: NewBuffer(120, 40),
front: NewBuffer(120, 40),
buf: bytes.Buffer{},
writer: w,
}
// Fill both buffers identically
for y := 0; y < 40; y++ {
for x := 0; x < 120; x++ {
cell := Cell{Rune: 'A', Style: DefaultStyle()}
s.back.Set(x, y, cell)
s.front.Set(x, y, cell)
}
}
b.ResetTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
w.n = 0
s.Flush()
}
b.ReportMetric(float64(w.n), "bytes/op")
}
// BenchmarkFlushColor16NoForceRGB flushes a Color16-filled screen without forceRGB.
// baseline: compact ANSI escape codes.
func BenchmarkFlushColor16NoForceRGB(b *testing.B) {
w := &mockWriter{}
s := &Screen{
width: 120,
height: 40,
back: NewBuffer(120, 40),
front: NewBuffer(120, 40),
buf: bytes.Buffer{},
writer: w,
}
style := Style{FG: Green, BG: Black}
for y := range 40 {
for x := range 120 {
s.back.Set(x, y, Cell{Rune: 'A', Style: style})
}
}
b.ResetTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
s.front.Clear()
s.back.MarkAllDirty()
s.Flush()
w.n = 0
s.FlushBuffer()
}
b.ReportMetric(float64(w.n), "bytes/op")
}
// BenchmarkFlushColor16ForceRGB flushes the same screen with forceRGB=true.
// measures the cost of upgrading Color16 → true color at flush time.
func BenchmarkFlushColor16ForceRGB(b *testing.B) {
w := &mockWriter{}
s := &Screen{
width: 120,
height: 40,
back: NewBuffer(120, 40),
front: NewBuffer(120, 40),
buf: bytes.Buffer{},
writer: w,
forceRGB: true,
}
style := Style{FG: Green, BG: Black}
for y := range 40 {
for x := range 120 {
s.back.Set(x, y, Cell{Rune: 'A', Style: style})
}
}
b.ResetTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
s.front.Clear()
s.back.MarkAllDirty()
s.Flush()
w.n = 0
s.FlushBuffer()
}
b.ReportMetric(float64(w.n), "bytes/op")
}
// BenchmarkResolveColor16Pass measures the pre-pass that patches Color16 RGB values.
func BenchmarkResolveColor16Pass(b *testing.B) {
buf := NewBuffer(120, 40)
style := Style{FG: Green, BG: Yellow}
for y := range 40 {
for x := range 120 {
buf.Set(x, y, Cell{Rune: 'A', Style: style})
}
}
b.ResetTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
resolveColor16(buf, 120, 40)
}
}
// BenchmarkVignetteTransition measures the first-frame cost when vignette activates —
// full screen rewrite with unique true-color per cell.
func BenchmarkVignetteTransition(b *testing.B) {
w := &mockWriter{}
s := &Screen{
width: 120,
height: 40,
back: NewBuffer(120, 40),
front: NewBuffer(120, 40),
buf: bytes.Buffer{},
writer: w,
forceRGB: true,
}
// pre-compute the vignette output into a stable buffer
renderBuf := NewBuffer(120, 40)
style := Style{FG: Green, BG: Black}
for y := range 40 {
for x := range 120 {
renderBuf.Set(x, y, Cell{Rune: 'A', Style: style})
}
}
resolveColor16(renderBuf, 120, 40)
SEVignette().Strength(1.0).Apply(renderBuf, PostContext{Width: 120, Height: 40})
b.ResetTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
// simulate: front has pre-vignette state, back has post-vignette
s.front.Clear()
copy(s.back.cells, renderBuf.cells)
s.back.MarkAllDirty()
s.Flush()
w.n = 0
s.FlushBuffer()
}
b.ReportMetric(float64(w.n), "bytes/op")
}
// BenchmarkVignetteSteadyState measures the per-frame cost when vignette is stable —
// diff should find nothing changed and write nothing.
func BenchmarkVignetteSteadyState(b *testing.B) {
w := &mockWriter{}
s := &Screen{
width: 120,
height: 40,
back: NewBuffer(120, 40),
front: NewBuffer(120, 40),
buf: bytes.Buffer{},
writer: w,
forceRGB: true,
}
// pre-compute vignette output
renderBuf := NewBuffer(120, 40)
style := Style{FG: Green, BG: Black}
for y := range 40 {
for x := range 120 {
renderBuf.Set(x, y, Cell{Rune: 'A', Style: style})
}
}
resolveColor16(renderBuf, 120, 40)
SEVignette().Strength(1.0).Apply(renderBuf, PostContext{Width: 120, Height: 40})
// prime both buffers with the same vignette output (steady state)
copy(s.back.cells, renderBuf.cells)
copy(s.front.cells, renderBuf.cells)
b.ResetTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
copy(s.back.cells, renderBuf.cells)
s.back.MarkAllDirty()
s.Flush()
w.n = 0
s.FlushBuffer()
}
b.ReportMetric(float64(w.n), "bytes/op")
}
// BenchmarkPlasmaFrame measures one animated plasma frame — every cell gets a
// unique quantized RGB per frame. Measures steady-state throughput (frames/sec proxy).
func BenchmarkPlasmaFrame(b *testing.B) {
w := &mockWriter{}
s := &Screen{
width: 120,
height: 40,
back: NewBuffer(120, 40),
front: NewBuffer(120, 40),
buf: bytes.Buffer{},
writer: w,
forceRGB: true,
}
style := Style{FG: RGB(100, 100, 100), BG: RGB(10, 10, 30)}
for y := range 40 {
for x := range 120 {
s.back.Set(x, y, Cell{Rune: 'A', Style: style})
s.front.Set(x, y, Cell{Rune: 'A', Style: style})
}
}
plasma := SETint(RGB(200, 100, 50)).Strength(0.8)
ctx := PostContext{Width: 120, Height: 40}
b.ResetTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
// new frame: render content into a fresh buffer, apply plasma, flush
renderBuf := NewBuffer(120, 40)
for y := range 40 {
for x := range 120 {
renderBuf.Set(x, y, Cell{Rune: 'A', Style: style})
}
}
ctx.Time += 33 * 1e6 // 33ms advance
plasma.Apply(renderBuf, ctx)
copy(s.back.cells, renderBuf.cells)
s.back.MarkAllDirty()
s.front.Clear() // simulate prior frame having different content
s.Flush()
w.n = 0
s.FlushBuffer()
}
b.ReportMetric(float64(w.n), "bytes/op")
}
// ---------------------------------------------------------------------------
// Phase-isolation benchmarks — proves where frame time actually goes.
//
// The three phases of a rendered frame:
// Phase 1 — Effect: resolveColor16 + Effect passes (pure Go, CPU-bound)
// Phase 2 — Diff: Flush() — cell comparison + escape-sequence building (pure Go)
// Phase 3 — Write: FlushBuffer() — single Write() syscall to terminal (I/O-bound)
//
// mockWriter makes Phase 3 essentially free (~ns), so BenchmarkPlasmaFrame measures
// Phase 1+2 only. Comparing that against a 33ms frame budget (30fps) shows how much
// headroom our Go code leaves for the terminal.
// ---------------------------------------------------------------------------
// BenchmarkPlasmaComputeOnly measures only Phase 1: effect computation.
// No diff, no write. Pure Go CPU cost.
func BenchmarkPlasmaComputeOnly(b *testing.B) {
buf := NewBuffer(120, 40)
style := Style{FG: RGB(204, 204, 204), BG: RGB(10, 10, 30)}
for y := range 40 {
for x := range 120 {
buf.Set(x, y, Cell{Rune: 'A', Style: style})
}
}
plasma := SETint(RGB(200, 100, 50)).Strength(0.8)
ctx := PostContext{Width: 120, Height: 40}
b.ResetTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
ctx.Time += 33 * 1e6
plasma.Apply(buf, ctx)
}
}
// BenchmarkPlasmaFlushBuildOnly measures only Phase 2: diff + escape building.
// Uses a pre-computed plasma frame so effect cost is excluded.
// mockWriter makes the Write free, isolating the diff algorithm.
func BenchmarkPlasmaFlushBuildOnly(b *testing.B) {
w := &mockWriter{}
s := &Screen{
width: 120,
height: 40,
back: NewBuffer(120, 40),
front: NewBuffer(120, 40),
buf: bytes.Buffer{},
writer: w,
forceRGB: true,
}
// pre-build a plasma frame
style := Style{FG: RGB(204, 204, 204), BG: RGB(10, 10, 30)}
renderBuf := NewBuffer(120, 40)
for y := range 40 {
for x := range 120 {
renderBuf.Set(x, y, Cell{Rune: 'A', Style: style})
}
}
SETint(RGB(200, 100, 50)).Strength(0.8).Apply(renderBuf, PostContext{Width: 120, Height: 40, Time: 1e9})
b.ResetTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
copy(s.back.cells, renderBuf.cells)
s.back.MarkAllDirty()
s.front.Clear()
s.Flush() // diff + escape build only
w.n = 0
s.FlushBuffer() // mockWriter — free
}
b.ReportMetric(float64(w.n), "bytes/op")
}
// BenchmarkWriteThroughput measures Phase 3 in isolation: how fast can we push
// bytes through Write() when the terminal is the only variable.
// Run with mockWriter (this file) vs piped to /dev/null at the shell to compare:
// go test -bench=BenchmarkWriteThroughput -benchtime=3s | tee /dev/null
// The difference between the two is terminal parsing overhead.
func BenchmarkWriteThroughput(b *testing.B) {
// realistic plasma-frame-sized payload: pre-built escape sequence buffer
w := &mockWriter{}
s := &Screen{
width: 120,
height: 40,
back: NewBuffer(120, 40),
front: NewBuffer(120, 40),
buf: bytes.Buffer{},
writer: w,
forceRGB: true,
}
style := Style{FG: RGB(204, 204, 204), BG: RGB(10, 10, 30)}
renderBuf := NewBuffer(120, 40)
for y := range 40 {
for x := range 120 {
renderBuf.Set(x, y, Cell{Rune: 'A', Style: style})
}
}
SETint(RGB(200, 100, 50)).Strength(0.8).Apply(renderBuf, PostContext{Width: 120, Height: 40, Time: 1e9})
copy(s.back.cells, renderBuf.cells)
s.back.MarkAllDirty()
s.front.Clear()
s.Flush()
prebuilt := make([]byte, s.buf.Len())
copy(prebuilt, s.buf.Bytes())
b.SetBytes(int64(len(prebuilt)))
b.ResetTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
w.Write(prebuilt) // isolated Write — no Go computation
}
b.ReportMetric(float64(w.n)/float64(b.N), "bytes/op")
}
// BenchmarkWriteIntToBuf benchmarks integer formatting
func BenchmarkWriteIntToBuf(b *testing.B) {
s := &Screen{
buf: bytes.Buffer{},
}
b.ResetTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
s.buf.Reset()
s.writeIntToBuf(12345)
}
}
// BenchmarkAppendInt benchmarks the appendInt helper
func BenchmarkAppendInt(b *testing.B) {
var scratch [32]byte
b.ResetTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
buf := scratch[:0]
buf = appendInt(buf, 12345)
}
}
// ---------------------------------------------------------------------------
// Effect dispatch overhead benchmarks
//
// All three benchmarks run identical per-cell work (desaturate luminance blend)
// on a 120×40 buffer. The only variable is how the inner function is called:
// Direct — loop inlined, no function call at all
// Closure — func variable (current Effect model)
// Method — concrete type with a method (potential inlining via devirtualisation)
//
// Compare ns/op to isolate call overhead from computation cost.
// ---------------------------------------------------------------------------
// BenchmarkEffectDispatchDirect is the baseline: the inner loop written inline.
// This gives the minimum achievable time — pure computation, zero dispatch cost.
func BenchmarkEffectDispatchDirect(b *testing.B) {
buf := NewBuffer(120, 40)
for y := range 40 {
for x := range 120 {
buf.Set(x, y, Cell{Rune: 'X', Style: Style{FG: RGB(180, 120, 60)}})
}
}
ctx := PostContext{Width: 120, Height: 40}
b.ResetTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
for y := range ctx.Height {
base := y * buf.width
for x := range ctx.Width {
c := &buf.cells[base+x]
fg := resolveFG(c.Style.FG, ctx)
lum := uint8((uint32(fg.R)*299 + uint32(fg.G)*587 + uint32(fg.B)*114) / 1000)
c.Style.FG = RGB(lum, lum, lum)
}
}
}
}
// BenchmarkEffectDispatchClosure calls the same work via a func variable —
// the current Effect model. Measures the indirect call overhead.
func BenchmarkEffectDispatchClosure(b *testing.B) {
buf := NewBuffer(120, 40)
for y := range 40 {
for x := range 120 {
buf.Set(x, y, Cell{Rune: 'X', Style: Style{FG: RGB(180, 120, 60)}})
}
}
ctx := PostContext{Width: 120, Height: 40}
effect := SEDesaturate().Strength(1.0) // interface dispatch — same math as Direct
b.ResetTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
effect.Apply(buf, ctx)
}
}
// desatShader is a concrete type implementing the same desaturate logic.
// Go can potentially devirtualise/inline method calls on concrete types.
type desatShader struct{}
func (desatShader) run(buf *Buffer, ctx PostContext) {
for y := range ctx.Height {
base := y * buf.width
for x := range ctx.Width {
c := &buf.cells[base+x]
fg := resolveFG(c.Style.FG, ctx)
lum := uint8((uint32(fg.R)*299 + uint32(fg.G)*587 + uint32(fg.B)*114) / 1000)
c.Style.FG = RGB(lum, lum, lum)
}
}
}
// BenchmarkEffectDispatchMethod calls the same work via a concrete method.
// If Go can inline through the method call, this will match Direct.
func BenchmarkEffectDispatchMethod(b *testing.B) {
buf := NewBuffer(120, 40)
for y := range 40 {
for x := range 120 {
buf.Set(x, y, Cell{Rune: 'X', Style: Style{FG: RGB(180, 120, 60)}})
}
}
ctx := PostContext{Width: 120, Height: 40}
shader := desatShader{}
b.ResetTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
shader.run(buf, ctx)
}
}
// desatShader.Apply satisfies Effect — used to benchmark interface dispatch.
func (desatShader) Apply(buf *Buffer, ctx PostContext) {
for y := range ctx.Height {
base := y * buf.width
for x := range ctx.Width {
c := &buf.cells[base+x]
fg := resolveFG(c.Style.FG, ctx)
lum := uint8((uint32(fg.R)*299 + uint32(fg.G)*587 + uint32(fg.B)*114) / 1000)
c.Style.FG = RGB(lum, lum, lum)
}
}
}
// BenchmarkEffectDispatchInterface calls via an interface — what a concrete
// Effect interface design would do when iterating a []Effect pipeline.
func BenchmarkEffectDispatchInterface(b *testing.B) {
buf := NewBuffer(120, 40)
for y := range 40 {
for x := range 120 {
buf.Set(x, y, Cell{Rune: 'X', Style: Style{FG: RGB(180, 120, 60)}})
}
}
ctx := PostContext{Width: 120, Height: 40}
var pp Effect = desatShader{}
b.ResetTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
pp.Apply(buf, ctx)
}
}