Debian Codegraph @

truncate_bench_test.go

raw symbols SCIP
1 package displaywidth
2
3 import "testing"
4
5 var csOptions = Options{ControlSequences: true}
6
7 // Inputs for benchmarking truncation with trailing escape sequence preservation
8 var (
9 // Short colored text with reset
10 shortANSI = "\x1b[31mhello world\x1b[0m"
11 // Multiple stacked SGR sequences
12 stackedANSI = "\x1b[1m\x1b[31m\x1b[42mhello world, this is some longer text\x1b[0m"
13 // Many interleaved color changes
14 interleavedANSI = "hello \x1b[31mworld \x1b[32mfoo \x1b[33mbar \x1b[34mbaz \x1b[35mqux \x1b[36mend\x1b[0m"
15 // Plain text (no escape sequences) — baseline
16 plainText = "hello world, this is some plain text without escapes"
17 )
18
19 func BenchmarkTruncateString(b *testing.B) {
20 benchmarks := []struct {
21 name string
22 input string
23 options Options
24 }{
25 {"plain/default", plainText, defaultOptions},
26 {"plain/ControlSequences", plainText, csOptions},
27 {"short_ANSI/default", shortANSI, defaultOptions},
28 {"short_ANSI/ControlSequences", shortANSI, csOptions},
29 {"stacked_ANSI/ControlSequences", stackedANSI, csOptions},
30 {"interleaved_ANSI/ControlSequences", interleavedANSI, csOptions},
31 }
32
33 for _, bm := range benchmarks {
34 b.Run(bm.name, func(b *testing.B) {
35 b.ReportAllocs()
36 for i := 0; i < b.N; i++ {
37 _ = bm.options.TruncateString(bm.input, 5, "...")
38 }
39 })
40 }
41 }
42
43 var tail = []byte("...")
44
45 func BenchmarkTruncateBytes(b *testing.B) {
46 benchmarks := []struct {
47 name string
48 input []byte
49 options Options
50 }{
51 {"plain/default", []byte(plainText), defaultOptions},
52 {"plain/ControlSequences", []byte(plainText), csOptions},
53 {"short_ANSI/default", []byte(shortANSI), defaultOptions},
54 {"short_ANSI/ControlSequences", []byte(shortANSI), csOptions},
55 {"stacked_ANSI/ControlSequences", []byte(stackedANSI), csOptions},
56 {"interleaved_ANSI/ControlSequences", []byte(interleavedANSI), csOptions},
57 }
58
59 for _, bm := range benchmarks {
60 b.Run(bm.name, func(b *testing.B) {
61 b.ReportAllocs()
62 for i := 0; i < b.N; i++ {
63 _ = bm.options.TruncateBytes(bm.input, 5, tail)
64 }
65 })
66 }
67 }