Debian Codegraph @

comparison/benchmark_test.go

raw symbols SCIP
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 // TestCase represents a test case from the test_cases.txt file
14 type TestCase struct {
15 Name string
16 Input string
17 }
18
19 // loadTestCases reads and parses test cases from test_cases.txt
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 // Skip empty lines and comments
31 if line == "" || strings.HasPrefix(line, "#") {
32 continue
33 }
34
35 // Handle special cases with colons (like "newline:", "tab:", etc.)
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 // For other cases, use the name as input
71 input = name
72 }
73
74 testCases = append(testCases, TestCase{
75 Name: name,
76 Input: input,
77 })
78 } else {
79 // Regular test case - use the line as both name and input
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 // Shared test data for benchmarks
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 // BenchmarkString_Mixed benchmarks our displaywidth package
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 // Test with default settings (eastAsianWidth=false)
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 // Save original value and restore after benchmark
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 // Test with East Asian Width enabled
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 // Set EastAsianAmbiguousWidth to 2 to match the other libraries
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 // BenchmarkString_ASCII benchmarks ASCII-only strings
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 // BenchmarkString_Emoji benchmarks emoji strings
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 // BenchmarkRune_Mixed benchmarks rune width calculation using test cases
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 // Convert all strings to []rune
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 // BenchmarkRune_EastAsian benchmarks rune width with East Asian Width option
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 // Convert all strings to []rune
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 // BenchmarkRune_ASCII benchmarks ASCII rune width calculation
411 func BenchmarkRune_ASCII(b *testing.B) {
412 // Convert ASCII strings to []rune
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 // BenchmarkRune_Emoji benchmarks emoji rune width calculation
445 func BenchmarkRune_Emoji(b *testing.B) {
446 // Convert emoji strings to []rune
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 }