| 1 |
package runewidth |
| 2 |
|
| 3 |
import ( |
| 4 |
"testing" |
| 5 |
"unicode/utf8" |
| 6 |
) |
| 7 |
|
| 8 |
var benchSink int |
| 9 |
|
| 10 |
// |
| 11 |
// RuneWidth |
| 12 |
// |
| 13 |
|
| 14 |
func benchRuneWidth(b *testing.B, eastAsianWidth bool, start, stop rune, want int) int { |
| 15 |
b.Helper() |
| 16 |
n := 0 |
| 17 |
b.Run("regular", func(b *testing.B) { |
| 18 |
got := -1 |
| 19 |
c := NewCondition() |
| 20 |
c.EastAsianWidth = eastAsianWidth |
| 21 |
b.ReportAllocs() |
| 22 |
b.ResetTimer() |
| 23 |
for i := 0; i < b.N; i++ { |
| 24 |
got = n |
| 25 |
for r := start; r < stop; r++ { |
| 26 |
n += c.RuneWidth(r) |
| 27 |
} |
| 28 |
got = n - got |
| 29 |
} |
| 30 |
if want != 0 && got != want { // some extra checks |
| 31 |
b.Errorf("got %d, want %d\n", got, want) |
| 32 |
} |
| 33 |
}) |
| 34 |
b.Run("lut", func(b *testing.B) { |
| 35 |
got := -1 |
| 36 |
n = 0 |
| 37 |
c := NewCondition() |
| 38 |
c.EastAsianWidth = eastAsianWidth |
| 39 |
c.CreateLUT() |
| 40 |
b.ReportAllocs() |
| 41 |
b.ResetTimer() |
| 42 |
for i := 0; i < b.N; i++ { |
| 43 |
got = n |
| 44 |
for r := start; r < stop; r++ { |
| 45 |
n += c.RuneWidth(r) |
| 46 |
} |
| 47 |
got = n - got |
| 48 |
} |
| 49 |
if want != 0 && got != want { // some extra checks |
| 50 |
b.Errorf("got %d, want %d\n", got, want) |
| 51 |
} |
| 52 |
}) |
| 53 |
return n |
| 54 |
} |
| 55 |
func BenchmarkRuneWidthAll(b *testing.B) { |
| 56 |
benchSink = benchRuneWidth(b, false, 0, utf8.MaxRune+1, 1292760) |
| 57 |
} |
| 58 |
func BenchmarkRuneWidth768(b *testing.B) { |
| 59 |
benchSink = benchRuneWidth(b, false, 0, 0x300, 702) |
| 60 |
} |
| 61 |
func BenchmarkRuneWidthAllEastAsian(b *testing.B) { |
| 62 |
benchSink = benchRuneWidth(b, true, 0, utf8.MaxRune+1, 1431130) |
| 63 |
} |
| 64 |
func BenchmarkRuneWidth768EastAsian(b *testing.B) { |
| 65 |
benchSink = benchRuneWidth(b, true, 0, 0x300, 794) |
| 66 |
} |
| 67 |
|
| 68 |
// |
| 69 |
// String1Width - strings which consist of a single rune |
| 70 |
// |
| 71 |
|
| 72 |
func benchString1Width(b *testing.B, eastAsianWidth bool, start, stop rune, want int) int { |
| 73 |
b.Helper() |
| 74 |
n := 0 |
| 75 |
ss := make([]string, stop-start) |
| 76 |
for r := start; r < stop; r++ { |
| 77 |
ss = append(ss, string(r)) |
| 78 |
} |
| 79 |
|
| 80 |
b.Run("regular", func(b *testing.B) { |
| 81 |
got := -1 |
| 82 |
c := NewCondition() |
| 83 |
c.EastAsianWidth = eastAsianWidth |
| 84 |
b.ResetTimer() |
| 85 |
b.ReportAllocs() |
| 86 |
for i := 0; i < b.N; i++ { |
| 87 |
got = n |
| 88 |
for _, s := range ss { |
| 89 |
n += c.StringWidth(s) |
| 90 |
} |
| 91 |
got = n - got |
| 92 |
} |
| 93 |
if want != 0 && got != want { // some extra checks |
| 94 |
b.Errorf("got %d, want %d\n", got, want) |
| 95 |
} |
| 96 |
}) |
| 97 |
b.Run("lut", func(b *testing.B) { |
| 98 |
got := -1 |
| 99 |
n = 0 |
| 100 |
c := NewCondition() |
| 101 |
c.EastAsianWidth = eastAsianWidth |
| 102 |
c.CreateLUT() |
| 103 |
b.ResetTimer() |
| 104 |
b.ReportAllocs() |
| 105 |
for i := 0; i < b.N; i++ { |
| 106 |
got = n |
| 107 |
for _, s := range ss { |
| 108 |
n += c.StringWidth(s) |
| 109 |
} |
| 110 |
got = n - got |
| 111 |
} |
| 112 |
if want != 0 && got != want { // some extra checks |
| 113 |
b.Errorf("got %d, want %d\n", got, want) |
| 114 |
} |
| 115 |
}) |
| 116 |
return n |
| 117 |
} |
| 118 |
func BenchmarkString1WidthAll(b *testing.B) { |
| 119 |
benchSink = benchString1Width(b, false, 0, utf8.MaxRune+1, 1294808) |
| 120 |
} |
| 121 |
func BenchmarkString1Width768(b *testing.B) { |
| 122 |
benchSink = benchString1Width(b, false, 0, 0x300, 702) |
| 123 |
} |
| 124 |
func BenchmarkString1WidthAllEastAsian(b *testing.B) { |
| 125 |
benchSink = benchString1Width(b, true, 0, utf8.MaxRune+1, 1435226) |
| 126 |
} |
| 127 |
func BenchmarkString1Width768EastAsian(b *testing.B) { |
| 128 |
benchSink = benchString1Width(b, true, 0, 0x300, 794) |
| 129 |
} |
| 130 |
|
| 131 |
// tables |
| 132 |
func benchTable(b *testing.B, tbl table) int { |
| 133 |
n := 0 |
| 134 |
for i := 0; i < b.N; i++ { |
| 135 |
for r := rune(0); r <= utf8.MaxRune; r++ { |
| 136 |
if inTable(r, tbl) { |
| 137 |
n++ |
| 138 |
} |
| 139 |
} |
| 140 |
} |
| 141 |
return n |
| 142 |
} |
| 143 |
|
| 144 |
func BenchmarkTablePrivate(b *testing.B) { |
| 145 |
benchSink = benchTable(b, private) |
| 146 |
} |
| 147 |
func BenchmarkTableNonprint(b *testing.B) { |
| 148 |
benchSink = benchTable(b, nonprint) |
| 149 |
} |
| 150 |
func BenchmarkTableCombining(b *testing.B) { |
| 151 |
benchSink = benchTable(b, combining) |
| 152 |
} |
| 153 |
func BenchmarkTableDoublewidth(b *testing.B) { |
| 154 |
benchSink = benchTable(b, doublewidth) |
| 155 |
} |
| 156 |
func BenchmarkTableAmbiguous(b *testing.B) { |
| 157 |
benchSink = benchTable(b, ambiguous) |
| 158 |
} |
| 159 |
func BenchmarkTableEmoji(b *testing.B) { |
| 160 |
benchSink = benchTable(b, emoji) |
| 161 |
} |
| 162 |
func BenchmarkTableNarrow(b *testing.B) { |
| 163 |
benchSink = benchTable(b, narrow) |
| 164 |
} |
| 165 |
func BenchmarkTableNeutral(b *testing.B) { |
| 166 |
benchSink = benchTable(b, neutral) |
| 167 |
} |
| 168 |
|
| 169 |
func BenchmarkFillLeftShort(b *testing.B) { |
| 170 |
for i := 0; i < b.N; i++ { |
| 171 |
FillLeft("test", 16) |
| 172 |
} |
| 173 |
} |
| 174 |
|
| 175 |
func BenchmarkFillLeftLong(b *testing.B) { |
| 176 |
for i := 0; i < b.N; i++ { |
| 177 |
FillLeft("test", 64) |
| 178 |
} |
| 179 |
} |
| 180 |
|
| 181 |
func BenchmarkFillRightShort(b *testing.B) { |
| 182 |
for i := 0; i < b.N; i++ { |
| 183 |
FillRight("test", 16) |
| 184 |
} |
| 185 |
} |
| 186 |
|
| 187 |
func BenchmarkFillRightLong(b *testing.B) { |
| 188 |
for i := 0; i < b.N; i++ { |
| 189 |
FillRight("test", 64) |
| 190 |
} |
| 191 |
} |