Files
pipelines/internal/termstate/bench_test.go
T
alan e13cb99ade Terminals: keep screen state on the server; snapshot on reload and gaps
A headless xterm-compatible emulator (internal/termstate) follows each
machine terminal's PTY output under the same lock as the 1 MiB tail. A
terminal_output from before the retained tail (new page, reload, gap)
now gets output_snapshot: an escape stream that resets the client and
restores scrollback (1000 lines), main screen, the alternate screen when
in use (via ?1049h so the program's exit brings the shell back), saved
cursors, tabs, scroll region, cursor incl. pending wrap, modes (mouse
tracking/encoding, bracketed paste, DECCKM, keypad, autowrap, origin,
focus, ...), charsets, pen and title, as of output_end; frames continue
there. Reads inside the tail stay raw.

No Go library was adequate: charmbracelet/x/vt and vito/midterm scroll at
0.2-4 MB/s and hide modes/pen/region; vt10x is unmaintained. termstate
runs at 160-200 MB/s.

The app treats a snapshot reply as a replay without the dropped-output
line, and every replay now starts with RIS so stale modes end.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-26 00:56:49 +00:00

61 lines
1.7 KiB
Go

package termstate
import (
"fmt"
"math/rand"
"strings"
"testing"
)
// Workloads of the throughput the terminal output path sustains (about 30
// MB/s): `seq`, a full-screen program redrawing coloured frames, long
// wrapping lines of mixed text.
func workloads() map[string][]byte {
var b strings.Builder
for i := 1; b.Len() < 8<<20; i++ {
fmt.Fprintf(&b, "%d\r\n", i)
}
seq := b.String()
b.Reset()
r := rand.New(rand.NewSource(1))
b.WriteString("\x1b[?1049h")
for b.Len() < 8<<20 {
fmt.Fprintf(&b, "\x1b[%d;%dH\x1b[38;5;%dm\x1b[48;2;%d;%d;%dm%s\x1b[0m", r.Intn(50)+1, r.Intn(150)+1, r.Intn(256), r.Intn(256), r.Intn(256), r.Intn(256), strings.Repeat("x", r.Intn(40)))
}
tui := b.String()
b.Reset()
for b.Len() < 8<<20 {
b.WriteString("the quick brown fox jumps over the lazy dog ünïcödé 日本語 ")
}
return map[string][]byte{"seq": []byte(seq), "tui": []byte(tui), "text": []byte(b.String())}
}
func BenchmarkWrite(b *testing.B) {
loads := workloads()
for _, name := range []string{"seq", "tui", "text"} {
for _, size := range [][2]int{{80, 24}, {200, 50}} {
data := loads[name]
b.Run(fmt.Sprintf("%s/%dx%d", name, size[0], size[1]), func(b *testing.B) {
b.SetBytes(int64(len(data)))
for b.Loop() {
term := New(size[0], size[1], DefaultScrollback)
for i := 0; i < len(data); i += 32 << 10 {
term.Write(data[i:min(i+32<<10, len(data))])
}
}
})
}
}
}
// A snapshot of a full scrollback of wide rows.
func BenchmarkSnapshot(b *testing.B) {
term := New(200, 50, DefaultScrollback)
term.Write(workloads()["text"])
snap, _ := term.Snapshot()
b.ReportMetric(float64(len(snap)), "bytes")
for b.Loop() {
term.Snapshot()
}
}