kitty/tools/cmd/benchmark/main_test.go
Kovid Goyal c9a99c093d
Make the ASCII and Unicode text benchmarks comparable
The two benchmarks now use text with the same structure: the same lines,
of the same widths in cells, ending in CRLF and with tabs at the same
positions, differing only in the characters used to fill them. Before,
the ASCII text had a bare newline about every 90 bytes, while the
Unicode text was long lines of mostly CJK text, so they were dominated
by different costs. Also report the rate at which cells are drawn for
them, since non-ASCII characters are encoded in more bytes.
2026-11-16 13:43:40 +05:30

61 lines
1.8 KiB
Go

// License: GPLv3 Copyright: 2026, Kovid Goyal, <kovid at kovidgoyal.net>
package benchmark
import (
"strings"
"testing"
"github.com/kovidgoyal/kitty/tools/wcswidth"
)
func line_widths(t *testing.T, text string) (widths []int, tabs []int) {
lines := strings.Split(strings.TrimSuffix(text, "\r\n"), "\r\n")
for _, line := range lines {
tab := -1
if i := strings.IndexByte(line, '\t'); i > -1 {
tab = wcswidth.Stringwidth(line[:i])
line = line[:i] + line[i+1:]
}
if strings.ContainsAny(line, "\t\r\n") {
t.Fatalf("Unexpected control characters in line: %q", line)
}
widths = append(widths, wcswidth.Stringwidth(line))
tabs = append(tabs, tab)
}
return
}
func TestBenchmarkTextPayloadsAreComparable(t *testing.T) {
a, acells := ascii_text()
u, ucells := unicode_text()
if acells != ucells {
t.Fatalf("The payloads have different numbers of cells: %d != %d", acells, ucells)
}
aw, at := line_widths(t, a)
uw, ut := line_widths(t, u)
if len(aw) != num_text_lines || len(uw) != num_text_lines {
t.Fatalf("Wrong number of lines: %d %d", len(aw), len(uw))
}
total := 0
for i := range aw {
if aw[i] != uw[i] || at[i] != ut[i] {
t.Fatalf("Line %d differs: width %d != %d or tab %d != %d", i, aw[i], uw[i], at[i], ut[i])
}
total += aw[i]
}
if total != acells {
t.Fatalf("Cell count %d does not match the widths of the lines: %d", acells, total)
}
// Every character except spaces and control characters must be non-ASCII,
// letters with combining marks are allowed to have an ASCII base
for _, g := range wcswidth.SplitIntoGraphemes(u) {
switch g {
case " ", "\t", "\r", "\n", "\r\n":
continue
}
if strings.IndexFunc(g, func(r rune) bool { return r >= 0x80 }) < 0 {
t.Fatalf("The unicode payload contains the ASCII character: %q", g)
}
}
}