| 1 |
package comparison |
| 2 |
|
| 3 |
import ( |
| 4 |
"strings" |
| 5 |
"testing" |
| 6 |
|
| 7 |
"github.com/clipperhouse/displaywidth" |
| 8 |
"github.com/clipperhouse/displaywidth/testdata" |
| 9 |
"github.com/mattn/go-runewidth" |
| 10 |
"github.com/rivo/uniseg" |
| 11 |
) |
| 12 |
|
| 13 |
|
| 14 |
type TestCase struct { |
| 15 |
Name string |
| 16 |
Input string |
| 17 |
} |
| 18 |
|
| 19 |
|
| 20 |
func loadTestCases() ([]TestCase, int64, error) { |
| 21 |
file, err := testdata.TestCases() |
| 22 |
if err != nil { |
| 23 |
return nil, 0, err |
| 24 |
} |
| 25 |
|
| 26 |
var testCases []TestCase |
| 27 |
lines := strings.Split(string(file), "\n") |
| 28 |
|
| 29 |
for _, line := range lines { |
| 30 |
|
| 31 |
if line == "" || strings.HasPrefix(line, "#") { |
| 32 |
continue |
| 33 |
} |
| 34 |
|
| 35 |
|
| 36 |
if strings.HasSuffix(line, ":") { |
| 37 |
name := strings.TrimSuffix(line, ":") |
| 38 |
var input string |
| 39 |
|
| 40 |
switch name { |
| 41 |
case "newline": |
| 42 |
input = "\n" |
| 43 |
case "tab": |
| 44 |
input = "\t" |
| 45 |
case "carriage return": |
| 46 |
input = "\r" |
| 47 |
case "backspace": |
| 48 |
input = "\b" |
| 49 |
case "null": |
| 50 |
input = "\x00" |
| 51 |
case "del": |
| 52 |
input = "\x7f" |
| 53 |
case "Zero Width Space": |
| 54 |
input = "\u200b" |
| 55 |
case "Zero Width Joiner": |
| 56 |
input = "\u200d" |
| 57 |
case "Zero Width Non-Joiner": |
| 58 |
input = "\u200c" |
| 59 |
case "Empty string": |
| 60 |
input = "" |
| 61 |
case "Single space": |
| 62 |
input = " " |
| 63 |
case "Multiple spaces": |
| 64 |
input = " " |
| 65 |
case "Tab and newline": |
| 66 |
input = "\t\n" |
| 67 |
case "Mixed whitespace": |
| 68 |
input = " \t \n " |
| 69 |
default: |
| 70 |
|
| 71 |
input = name |
| 72 |
} |
| 73 |
|
| 74 |
testCases = append(testCases, TestCase{ |
| 75 |
Name: name, |
| 76 |
Input: input, |
| 77 |
}) |
| 78 |
} else { |
| 79 |
|
| 80 |
testCases = append(testCases, TestCase{ |
| 81 |
Name: line, |
| 82 |
Input: line, |
| 83 |
}) |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
totalBytes := 0 |
| 88 |
for _, tc := range testCases { |
| 89 |
totalBytes += len(tc.Input) |
| 90 |
} |
| 91 |
|
| 92 |
return testCases, int64(totalBytes), nil |
| 93 |
} |
| 94 |
|
| 95 |
var ( |
| 96 |
|
| 97 |
asciiTestStrings = []string{ |
| 98 |
"hello", |
| 99 |
"Hello World", |
| 100 |
"1234567890", |
| 101 |
"!@#$%^&*()", |
| 102 |
"This is a very long string with many characters to test performance of both implementations.", |
| 103 |
} |
| 104 |
|
| 105 |
emojiTestStrings = []string{ |
| 106 |
"๐ ๐ ๐ ๐คฃ ๐ ๐ ๐
๐ ๐ ๐", |
| 107 |
"๐ ๐ ๐ ๐ ๐ ๐ ๐ ๐ ๐ ๐", |
| 108 |
"๐จโ๐ฉโ๐งโ๐ฆ ๐จโ๐ป ๐ฉโ๐ฌ ๐จโ๐จ ๐ฉโ๐", |
| 109 |
"๐บ๐ธ ๐ฌ๐ง ๐ซ๐ท ๐ฉ๐ช ๐ฏ๐ต ๐ฐ๐ท ๐จ๐ณ", |
| 110 |
"Hello ไธ็! ๐", |
| 111 |
"๐จโ๐ป working on ๐", |
| 112 |
"๐๐๐๐คฃ๐๐๐
๐๐๐๐๐๐๐๐ฅฐ๐๐๐โบ๏ธ๐๐ค๐คฉ๐ค๐คจ๐๐๐ถ๐๐๐ฃ๐ฅ๐ฎ๐ค๐ฏ๐ช๐ซ๐ฅฑ๐ด๐๐๐๐๐คค๐๐๐๐๐๐ค๐ฒโน๏ธ๐๐๐๐๐ค๐ข๐ญ๐ฆ๐ง๐จ๐ฉ๐คฏ๐ฌ๐ฐ๐ฑ๐ฅต๐ฅถ๐ณ๐คช๐ต๐ก๐ ๐คฌ๐ท๐ค๐ค๐คข๐คฎ๐คง๐๐ค ๐คก๐ฅณ๐ฅด๐ฅบ๐คฅ๐คซ๐คญ๐ง๐ค๐๐ฟ๐โ ๏ธ๐น๐บ๐ค๐ฝ๐พ๐ฉ๐บ๐ธ๐น๐ป๐ผ๐ฝ๐๐ฟ๐พ", |
| 113 |
} |
| 114 |
) |
| 115 |
|
| 116 |
|
| 117 |
func BenchmarkString_Mixed(b *testing.B) { |
| 118 |
b.Run("clipperhouse/displaywidth", func(b *testing.B) { |
| 119 |
testCases, n, err := loadTestCases() |
| 120 |
if err != nil { |
| 121 |
b.Fatalf("Failed to load test cases: %v", err) |
| 122 |
} |
| 123 |
b.SetBytes(n) |
| 124 |
b.ReportAllocs() |
| 125 |
b.ResetTimer() |
| 126 |
|
| 127 |
for i := 0; i < b.N; i++ { |
| 128 |
for _, tc := range testCases { |
| 129 |
|
| 130 |
_ = displaywidth.String(tc.Input) |
| 131 |
} |
| 132 |
} |
| 133 |
}) |
| 134 |
|
| 135 |
b.Run("mattn/go-runewidth", func(b *testing.B) { |
| 136 |
testCases, n, err := loadTestCases() |
| 137 |
if err != nil { |
| 138 |
b.Fatalf("Failed to load test cases: %v", err) |
| 139 |
} |
| 140 |
b.SetBytes(n) |
| 141 |
b.ReportAllocs() |
| 142 |
b.ResetTimer() |
| 143 |
|
| 144 |
for i := 0; i < b.N; i++ { |
| 145 |
for _, tc := range testCases { |
| 146 |
_ = runewidth.StringWidth(tc.Input) |
| 147 |
} |
| 148 |
} |
| 149 |
}) |
| 150 |
|
| 151 |
b.Run("rivo/uniseg", func(b *testing.B) { |
| 152 |
testCases, n, err := loadTestCases() |
| 153 |
if err != nil { |
| 154 |
b.Fatalf("Failed to load test cases: %v", err) |
| 155 |
} |
| 156 |
b.SetBytes(n) |
| 157 |
b.ReportAllocs() |
| 158 |
b.ResetTimer() |
| 159 |
|
| 160 |
for i := 0; i < b.N; i++ { |
| 161 |
for _, tc := range testCases { |
| 162 |
_ = uniseg.StringWidth(tc.Input) |
| 163 |
} |
| 164 |
} |
| 165 |
}) |
| 166 |
} |
| 167 |
|
| 168 |
func BenchmarkString_EastAsian(b *testing.B) { |
| 169 |
options := displaywidth.Options{ |
| 170 |
EastAsianWidth: true, |
| 171 |
} |
| 172 |
|
| 173 |
condition := &runewidth.Condition{ |
| 174 |
EastAsianWidth: true, |
| 175 |
} |
| 176 |
|
| 177 |
|
| 178 |
originalEAAWidth := uniseg.EastAsianAmbiguousWidth |
| 179 |
defer func() { |
| 180 |
uniseg.EastAsianAmbiguousWidth = originalEAAWidth |
| 181 |
}() |
| 182 |
|
| 183 |
b.Run("clipperhouse/displaywidth", func(b *testing.B) { |
| 184 |
testCases, n, err := loadTestCases() |
| 185 |
if err != nil { |
| 186 |
b.Fatalf("Failed to load test cases: %v", err) |
| 187 |
} |
| 188 |
b.SetBytes(n) |
| 189 |
b.ReportAllocs() |
| 190 |
b.ResetTimer() |
| 191 |
|
| 192 |
for i := 0; i < b.N; i++ { |
| 193 |
for _, tc := range testCases { |
| 194 |
|
| 195 |
_ = options.String(tc.Input) |
| 196 |
} |
| 197 |
} |
| 198 |
}) |
| 199 |
|
| 200 |
b.Run("mattn/go-runewidth", func(b *testing.B) { |
| 201 |
testCases, n, err := loadTestCases() |
| 202 |
if err != nil { |
| 203 |
b.Fatalf("Failed to load test cases: %v", err) |
| 204 |
} |
| 205 |
b.SetBytes(n) |
| 206 |
b.ReportAllocs() |
| 207 |
b.ResetTimer() |
| 208 |
|
| 209 |
for i := 0; i < b.N; i++ { |
| 210 |
for _, tc := range testCases { |
| 211 |
_ = condition.StringWidth(tc.Input) |
| 212 |
} |
| 213 |
} |
| 214 |
}) |
| 215 |
|
| 216 |
b.Run("rivo/uniseg", func(b *testing.B) { |
| 217 |
|
| 218 |
uniseg.EastAsianAmbiguousWidth = 2 |
| 219 |
defer func() { |
| 220 |
uniseg.EastAsianAmbiguousWidth = 1 |
| 221 |
}() |
| 222 |
|
| 223 |
testCases, n, err := loadTestCases() |
| 224 |
if err != nil { |
| 225 |
b.Fatalf("Failed to load test cases: %v", err) |
| 226 |
} |
| 227 |
b.SetBytes(n) |
| 228 |
b.ReportAllocs() |
| 229 |
b.ResetTimer() |
| 230 |
|
| 231 |
for i := 0; i < b.N; i++ { |
| 232 |
for _, tc := range testCases { |
| 233 |
_ = uniseg.StringWidth(tc.Input) |
| 234 |
} |
| 235 |
} |
| 236 |
}) |
| 237 |
} |
| 238 |
|
| 239 |
|
| 240 |
func BenchmarkString_ASCII(b *testing.B) { |
| 241 |
n := 0 |
| 242 |
for _, s := range asciiTestStrings { |
| 243 |
n += len(s) |
| 244 |
} |
| 245 |
|
| 246 |
b.Run("clipperhouse/displaywidth", func(b *testing.B) { |
| 247 |
b.SetBytes(int64(n)) |
| 248 |
b.ReportAllocs() |
| 249 |
b.ResetTimer() |
| 250 |
|
| 251 |
for i := 0; i < b.N; i++ { |
| 252 |
for _, s := range asciiTestStrings { |
| 253 |
_ = displaywidth.String(s) |
| 254 |
} |
| 255 |
} |
| 256 |
}) |
| 257 |
|
| 258 |
b.Run("mattn/go-runewidth", func(b *testing.B) { |
| 259 |
b.SetBytes(int64(n)) |
| 260 |
b.ReportAllocs() |
| 261 |
b.ResetTimer() |
| 262 |
|
| 263 |
for i := 0; i < b.N; i++ { |
| 264 |
for _, s := range asciiTestStrings { |
| 265 |
_ = runewidth.StringWidth(s) |
| 266 |
} |
| 267 |
} |
| 268 |
}) |
| 269 |
|
| 270 |
b.Run("rivo/uniseg", func(b *testing.B) { |
| 271 |
b.SetBytes(int64(n)) |
| 272 |
b.ReportAllocs() |
| 273 |
b.ResetTimer() |
| 274 |
|
| 275 |
for i := 0; i < b.N; i++ { |
| 276 |
for _, s := range asciiTestStrings { |
| 277 |
_ = uniseg.StringWidth(s) |
| 278 |
} |
| 279 |
} |
| 280 |
}) |
| 281 |
} |
| 282 |
|
| 283 |
|
| 284 |
func BenchmarkString_Emoji(b *testing.B) { |
| 285 |
n := 0 |
| 286 |
for _, s := range emojiTestStrings { |
| 287 |
n += len(s) |
| 288 |
} |
| 289 |
|
| 290 |
b.Run("clipperhouse/displaywidth", func(b *testing.B) { |
| 291 |
b.SetBytes(int64(n)) |
| 292 |
b.ReportAllocs() |
| 293 |
b.ResetTimer() |
| 294 |
for i := 0; i < b.N; i++ { |
| 295 |
for _, s := range emojiTestStrings { |
| 296 |
_ = displaywidth.String(s) |
| 297 |
} |
| 298 |
} |
| 299 |
}) |
| 300 |
|
| 301 |
b.Run("mattn/go-runewidth", func(b *testing.B) { |
| 302 |
b.SetBytes(int64(n)) |
| 303 |
b.ReportAllocs() |
| 304 |
b.ResetTimer() |
| 305 |
for i := 0; i < b.N; i++ { |
| 306 |
for _, s := range emojiTestStrings { |
| 307 |
_ = runewidth.StringWidth(s) |
| 308 |
} |
| 309 |
} |
| 310 |
}) |
| 311 |
|
| 312 |
b.Run("rivo/uniseg", func(b *testing.B) { |
| 313 |
b.SetBytes(int64(n)) |
| 314 |
b.ReportAllocs() |
| 315 |
b.ResetTimer() |
| 316 |
for i := 0; i < b.N; i++ { |
| 317 |
for _, s := range emojiTestStrings { |
| 318 |
_ = uniseg.StringWidth(s) |
| 319 |
} |
| 320 |
} |
| 321 |
}) |
| 322 |
} |
| 323 |
|
| 324 |
|
| 325 |
func BenchmarkRune_Mixed(b *testing.B) { |
| 326 |
testCases, _, err := loadTestCases() |
| 327 |
if err != nil { |
| 328 |
b.Fatalf("Failed to load test cases: %v", err) |
| 329 |
} |
| 330 |
|
| 331 |
|
| 332 |
var testRunes []rune |
| 333 |
n := 0 |
| 334 |
for _, tc := range testCases { |
| 335 |
runes := []rune(tc.Input) |
| 336 |
testRunes = append(testRunes, runes...) |
| 337 |
n += len(tc.Input) |
| 338 |
} |
| 339 |
|
| 340 |
b.Run("clipperhouse/displaywidth", func(b *testing.B) { |
| 341 |
b.SetBytes(int64(n)) |
| 342 |
b.ReportAllocs() |
| 343 |
b.ResetTimer() |
| 344 |
for i := 0; i < b.N; i++ { |
| 345 |
for _, r := range testRunes { |
| 346 |
_ = displaywidth.Rune(r) |
| 347 |
} |
| 348 |
} |
| 349 |
}) |
| 350 |
|
| 351 |
b.Run("mattn/go-runewidth", func(b *testing.B) { |
| 352 |
b.SetBytes(int64(n)) |
| 353 |
b.ReportAllocs() |
| 354 |
b.ResetTimer() |
| 355 |
for i := 0; i < b.N; i++ { |
| 356 |
for _, r := range testRunes { |
| 357 |
_ = runewidth.RuneWidth(r) |
| 358 |
} |
| 359 |
} |
| 360 |
}) |
| 361 |
} |
| 362 |
|
| 363 |
|
| 364 |
func BenchmarkRune_EastAsian(b *testing.B) { |
| 365 |
options := displaywidth.Options{ |
| 366 |
EastAsianWidth: true, |
| 367 |
} |
| 368 |
|
| 369 |
condition := &runewidth.Condition{ |
| 370 |
EastAsianWidth: true, |
| 371 |
} |
| 372 |
|
| 373 |
testCases, _, err := loadTestCases() |
| 374 |
if err != nil { |
| 375 |
b.Fatalf("Failed to load test cases: %v", err) |
| 376 |
} |
| 377 |
|
| 378 |
|
| 379 |
var testRunes []rune |
| 380 |
n := 0 |
| 381 |
for _, tc := range testCases { |
| 382 |
runes := []rune(tc.Input) |
| 383 |
testRunes = append(testRunes, runes...) |
| 384 |
n += len(tc.Input) |
| 385 |
} |
| 386 |
|
| 387 |
b.Run("clipperhouse/displaywidth", func(b *testing.B) { |
| 388 |
b.SetBytes(int64(n)) |
| 389 |
b.ReportAllocs() |
| 390 |
b.ResetTimer() |
| 391 |
for i := 0; i < b.N; i++ { |
| 392 |
for _, r := range testRunes { |
| 393 |
_ = options.Rune(r) |
| 394 |
} |
| 395 |
} |
| 396 |
}) |
| 397 |
|
| 398 |
b.Run("mattn/go-runewidth", func(b *testing.B) { |
| 399 |
b.SetBytes(int64(n)) |
| 400 |
b.ReportAllocs() |
| 401 |
b.ResetTimer() |
| 402 |
for i := 0; i < b.N; i++ { |
| 403 |
for _, r := range testRunes { |
| 404 |
_ = condition.RuneWidth(r) |
| 405 |
} |
| 406 |
} |
| 407 |
}) |
| 408 |
} |
| 409 |
|
| 410 |
|
| 411 |
func BenchmarkRune_ASCII(b *testing.B) { |
| 412 |
|
| 413 |
var asciiRunes []rune |
| 414 |
n := 0 |
| 415 |
for _, s := range asciiTestStrings { |
| 416 |
runes := []rune(s) |
| 417 |
asciiRunes = append(asciiRunes, runes...) |
| 418 |
n += len(s) |
| 419 |
} |
| 420 |
|
| 421 |
b.Run("clipperhouse/displaywidth", func(b *testing.B) { |
| 422 |
b.SetBytes(int64(n)) |
| 423 |
b.ReportAllocs() |
| 424 |
b.ResetTimer() |
| 425 |
for i := 0; i < b.N; i++ { |
| 426 |
for _, r := range asciiRunes { |
| 427 |
_ = displaywidth.Rune(r) |
| 428 |
} |
| 429 |
} |
| 430 |
}) |
| 431 |
|
| 432 |
b.Run("mattn/go-runewidth", func(b *testing.B) { |
| 433 |
b.SetBytes(int64(n)) |
| 434 |
b.ReportAllocs() |
| 435 |
b.ResetTimer() |
| 436 |
for i := 0; i < b.N; i++ { |
| 437 |
for _, r := range asciiRunes { |
| 438 |
_ = runewidth.RuneWidth(r) |
| 439 |
} |
| 440 |
} |
| 441 |
}) |
| 442 |
} |
| 443 |
|
| 444 |
|
| 445 |
func BenchmarkRune_Emoji(b *testing.B) { |
| 446 |
|
| 447 |
var emojiRunes []rune |
| 448 |
n := 0 |
| 449 |
for _, s := range emojiTestStrings { |
| 450 |
runes := []rune(s) |
| 451 |
emojiRunes = append(emojiRunes, runes...) |
| 452 |
n += len(s) |
| 453 |
} |
| 454 |
|
| 455 |
b.Run("clipperhouse/displaywidth", func(b *testing.B) { |
| 456 |
b.SetBytes(int64(n)) |
| 457 |
b.ReportAllocs() |
| 458 |
b.ResetTimer() |
| 459 |
for i := 0; i < b.N; i++ { |
| 460 |
for _, r := range emojiRunes { |
| 461 |
_ = displaywidth.Rune(r) |
| 462 |
} |
| 463 |
} |
| 464 |
}) |
| 465 |
|
| 466 |
b.Run("mattn/go-runewidth", func(b *testing.B) { |
| 467 |
b.SetBytes(int64(n)) |
| 468 |
b.ReportAllocs() |
| 469 |
b.ResetTimer() |
| 470 |
for i := 0; i < b.N; i++ { |
| 471 |
for _, r := range emojiRunes { |
| 472 |
_ = runewidth.RuneWidth(r) |
| 473 |
} |
| 474 |
} |
| 475 |
}) |
| 476 |
} |
| 477 |
|
| 478 |
func BenchmarkTruncateWithTail(b *testing.B) { |
| 479 |
testStrings := []string{ |
| 480 |
"hello world", |
| 481 |
"This is a very long string that will definitely be truncated", |
| 482 |
"Hello ไธ็! ๐", |
| 483 |
"๐จโ๐ป working on ๐", |
| 484 |
"ไธญๆๅญ็ฌฆไธฒๆต่ฏ", |
| 485 |
"๐๐๐๐คฃ๐๐๐
๐๐๐", |
| 486 |
} |
| 487 |
|
| 488 |
n := int64(0) |
| 489 |
for _, s := range testStrings { |
| 490 |
n += int64(len(s)) |
| 491 |
} |
| 492 |
|
| 493 |
maxWidths := []int{5, 10, 20, 30} |
| 494 |
tail := "..." |
| 495 |
|
| 496 |
b.Run("clipperhouse/displaywidth", func(b *testing.B) { |
| 497 |
options := displaywidth.Options{} |
| 498 |
b.SetBytes(n) |
| 499 |
b.ReportAllocs() |
| 500 |
b.ResetTimer() |
| 501 |
|
| 502 |
for i := 0; i < b.N; i++ { |
| 503 |
for _, s := range testStrings { |
| 504 |
for _, w := range maxWidths { |
| 505 |
_ = options.TruncateString(s, w, tail) |
| 506 |
} |
| 507 |
} |
| 508 |
} |
| 509 |
}) |
| 510 |
|
| 511 |
b.Run("mattn/go-runewidth", func(b *testing.B) { |
| 512 |
b.SetBytes(n) |
| 513 |
b.ReportAllocs() |
| 514 |
b.ResetTimer() |
| 515 |
|
| 516 |
for i := 0; i < b.N; i++ { |
| 517 |
for _, s := range testStrings { |
| 518 |
for _, w := range maxWidths { |
| 519 |
_ = runewidth.Truncate(s, w, tail) |
| 520 |
} |
| 521 |
} |
| 522 |
} |
| 523 |
}) |
| 524 |
} |
| 525 |
|
| 526 |
func BenchmarkTruncateWithoutTail(b *testing.B) { |
| 527 |
testStrings := []string{ |
| 528 |
"hello world", |
| 529 |
"This is a very long string that will definitely be truncated", |
| 530 |
"Hello ไธ็! ๐", |
| 531 |
"๐จโ๐ป working on ๐", |
| 532 |
"a very long string that will definitely be truncated", |
| 533 |
"ไธญๆๅญ็ฌฆไธฒๆต่ฏ", |
| 534 |
"๐๐๐๐คฃ๐๐๐
๐๐๐", |
| 535 |
} |
| 536 |
|
| 537 |
n := int64(0) |
| 538 |
for _, s := range testStrings { |
| 539 |
n += int64(len(s)) |
| 540 |
} |
| 541 |
|
| 542 |
maxWidths := []int{5, 10, 20, 30} |
| 543 |
|
| 544 |
b.Run("clipperhouse/displaywidth", func(b *testing.B) { |
| 545 |
options := displaywidth.Options{} |
| 546 |
b.SetBytes(n) |
| 547 |
b.ReportAllocs() |
| 548 |
b.ResetTimer() |
| 549 |
|
| 550 |
for i := 0; i < b.N; i++ { |
| 551 |
for _, s := range testStrings { |
| 552 |
for _, w := range maxWidths { |
| 553 |
_ = options.TruncateString(s, w, "") |
| 554 |
} |
| 555 |
} |
| 556 |
} |
| 557 |
}) |
| 558 |
|
| 559 |
b.Run("mattn/go-runewidth", func(b *testing.B) { |
| 560 |
b.SetBytes(n) |
| 561 |
b.ReportAllocs() |
| 562 |
b.ResetTimer() |
| 563 |
|
| 564 |
for i := 0; i < b.N; i++ { |
| 565 |
for _, s := range testStrings { |
| 566 |
for _, w := range maxWidths { |
| 567 |
_ = runewidth.Truncate(s, w, "") |
| 568 |
} |
| 569 |
} |
| 570 |
} |
| 571 |
}) |
| 572 |
} |