Debian Codegraph @

pkg/twwidth/width_bench_test.go

raw
1 package twwidth
2
3 import (
4 "fmt"
5 "strconv"
6 "strings"
7 "testing"
8 )
9
10 var benchmarkStrings = map[string]string{
11 "SimpleASCII": "hello world, this is a test string.",
12 "ASCIIWithANSI": "\033[31mhello\033[0m \033[34mworld\033[0m, this is \033[1ma\033[0m test string.",
13 "EastAsian": "こんにちは世界、これはテスト文字列です。",
14 "EastAsianWithANSI": "\033[32mこんにちは\033[0m \033[35m世界\033[0m、これは\033[4mテスト\033[0m文字列です。",
15 "LongSimpleASCII": strings.Repeat("abcdefghijklmnopqrstuvwxyz ", 20),
16 "LongASCIIWithANSI": strings.Repeat("\033[31ma\033[32mb\033[33mc\033[34md\033[35me\033[36mf\033[0m ", 50),
17 }
18
19 func BenchmarkWidthFunction(b *testing.B) {
20 eastAsianSettings := []bool{false, true}
21
22 for name, str := range benchmarkStrings {
23 for _, eaSetting := range eastAsianSettings {
24 SetEastAsian(eaSetting)
25
26 b.Run(fmt.Sprintf("%s_EA%v_NoCache", name, eaSetting), func(b *testing.B) {
27 b.SetBytes(int64(len(str)))
28 b.ReportAllocs()
29 b.ResetTimer()
30 for i := 0; i < b.N; i++ {
31 _ = WidthNoCache(str)
32 }
33 })
34
35 b.Run(fmt.Sprintf("%s_EA%v_CacheMiss", name, eaSetting), func(b *testing.B) {
36 b.SetBytes(int64(len(str)))
37 b.ReportAllocs()
38 b.ResetTimer()
39 for i := 0; i < b.N; i++ {
40 _ = Width(str + strconv.Itoa(i))
41 }
42 })
43 resetGlobalCache()
44
45 b.Run(fmt.Sprintf("%s_EA%v_CacheHit", name, eaSetting), func(b *testing.B) {
46 b.SetBytes(int64(len(str)))
47 b.ReportAllocs()
48 b.ResetTimer()
49 if b.N > 0 {
50 _ = Width(str)
51 }
52 for i := 1; i < b.N; i++ {
53 _ = Width(str)
54 }
55 })
56 resetGlobalCache()
57 }
58 }
59 }