...
1 package swag
2
3 import (
4 "fmt"
5 "io"
6 "testing"
7 )
8
9 func BenchmarkToXXXName(b *testing.B) {
10 samples := []string{
11 "sample text",
12 "sample-text",
13 "sample_text",
14 "sampleText",
15 "sample 2 Text",
16 "findThingById",
17 "日本語sample 2 Text",
18 "日本語findThingById",
19 "findTHINGSbyID",
20 }
21
22 b.Run("ToGoName", benchmarkFunc(ToGoName, samples))
23 b.Run("ToVarName", benchmarkFunc(ToVarName, samples))
24 b.Run("ToFileName", benchmarkFunc(ToFileName, samples))
25 b.Run("ToCommandName", benchmarkFunc(ToCommandName, samples))
26 b.Run("ToHumanNameLower", benchmarkFunc(ToHumanNameLower, samples))
27 b.Run("ToHumanNameTitle", benchmarkFunc(ToHumanNameTitle, samples))
28 }
29
30 func benchmarkFunc(fn func(string) string, samples []string) func(*testing.B) {
31 return func(b *testing.B) {
32 b.ResetTimer()
33 b.ReportAllocs()
34
35 var res string
36 for i := 0; i < b.N; i++ {
37 res = fn(samples[i%len(samples)])
38 }
39
40 fmt.Fprintln(io.Discard, res)
41 }
42 }
43
View as plain text