forked from aliuygur/is
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbenchmarks_test.go
164 lines (158 loc) · 5.43 KB
/
benchmarks_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
// +build go1.7
package is
import (
"path/filepath"
"reflect"
"runtime"
"testing"
)
type benchmarkCase struct {
function func(string) bool
caseName string
param string
}
var (
benchmarkFuncs = []benchmarkCase{
// Alpha
benchmarkCase{Alpha, "Empty", ""},
benchmarkCase{Alpha, "Space", " "},
benchmarkCase{Alpha, "False", "abc!!!"},
benchmarkCase{Alpha, "True", "abc"},
//UTFLetter
benchmarkCase{UTFLetter, "Empty", ""},
benchmarkCase{UTFLetter, "Space", " "},
benchmarkCase{UTFLetter, "False", "\u0026"}, //UTF-8(ASCII): &
benchmarkCase{UTFLetter, "True", "abc"},
// Alphanumeric
benchmarkCase{Alphanumeric, "Empty", ""},
benchmarkCase{Alphanumeric, "Space", " "},
benchmarkCase{Alphanumeric, "False", "\u0026"}, //UTF-8(ASCII): &
benchmarkCase{Alphanumeric, "True", "abc123"},
// UTFLetterNumeric
benchmarkCase{UTFLetterNumeric, "Empty", ""},
benchmarkCase{UTFLetterNumeric, "Space", " "},
benchmarkCase{UTFLetterNumeric, "False", "abc!!!"},
benchmarkCase{UTFLetterNumeric, "True", "abc1"},
// Numeric
benchmarkCase{Numeric, "Empty", ""},
benchmarkCase{Numeric, "Space", " "},
benchmarkCase{Numeric, "False", "abc!!!"},
benchmarkCase{Numeric, "True", "0123"},
// UTFNumeric
benchmarkCase{UTFNumeric, "Empty", ""},
benchmarkCase{UTFNumeric, "Space", " "},
benchmarkCase{UTFNumeric, "False", "abc!!!"},
benchmarkCase{UTFNumeric, "True", "\u0030"}, //UTF-8(ASCII): 0
// UTFDigit
benchmarkCase{UTFDigit, "Empty", ""},
benchmarkCase{UTFDigit, "Space", " "},
benchmarkCase{UTFDigit, "False", "abc!!!"},
benchmarkCase{UTFDigit, "True", "\u0030"}, //UTF-8(ASCII): 0
// LowerCase
benchmarkCase{LowerCase, "Empty", ""},
benchmarkCase{LowerCase, "Space", " "},
benchmarkCase{LowerCase, "False", "ABC"},
benchmarkCase{LowerCase, "True", "abc"},
// UpperCase
benchmarkCase{UpperCase, "Empty", ""},
benchmarkCase{UpperCase, "Space", " "},
benchmarkCase{UpperCase, "False", "abc"},
benchmarkCase{UpperCase, "True", "ABC"},
// Int
benchmarkCase{Int, "Empty", ""},
benchmarkCase{Int, "Space", " "},
benchmarkCase{Int, "False", "abc"},
benchmarkCase{Int, "True", "000"},
// Email
benchmarkCase{Email, "Empty", ""},
benchmarkCase{Email, "Space", " "},
benchmarkCase{Email, "False", "@invalid.com"},
benchmarkCase{Email, "True", "[email protected]"},
// URL
benchmarkCase{URL, "Empty", ""},
benchmarkCase{URL, "Space", " "},
benchmarkCase{URL, "False", "./rel/test/dir"},
benchmarkCase{URL, "True", "http://foobar.org/"},
// RequestURL
benchmarkCase{RequestURL, "Empty", ""},
benchmarkCase{RequestURL, "Space", " "},
benchmarkCase{RequestURL, "False", "invalid."},
benchmarkCase{RequestURL, "True", "http://foobar.org/"},
// RequestURI
benchmarkCase{RequestURI, "Empty", ""},
benchmarkCase{RequestURI, "Space", " "},
benchmarkCase{RequestURI, "False", "invalid."},
benchmarkCase{RequestURI, "True", "http://foobar.org/"},
// Float
benchmarkCase{Float, "Empty", ""},
benchmarkCase{Float, "Space", " "},
benchmarkCase{Float, "False", "+1f"},
benchmarkCase{Float, "True", "123.123"},
// Hexadecimal
benchmarkCase{Hexadecimal, "Empty", ""},
benchmarkCase{Hexadecimal, "Space", " "},
benchmarkCase{Hexadecimal, "False", ".."},
benchmarkCase{Hexadecimal, "True", "deadBEEF"},
// Hexcolor
benchmarkCase{Hexcolor, "Empty", ""},
benchmarkCase{Hexcolor, "Space", " "},
benchmarkCase{Hexcolor, "False", "#ff12FG"},
benchmarkCase{Hexcolor, "True", "#f00"},
// RGBcolor
benchmarkCase{RGBcolor, "Empty", ""},
benchmarkCase{RGBcolor, "Space", " "},
benchmarkCase{RGBcolor, "False", "rgba(0,31,255)"},
benchmarkCase{RGBcolor, "True", "rgb(0,31,255)"},
// JSON
benchmarkCase{JSON, "Empty", ""},
benchmarkCase{JSON, "Space", " "},
benchmarkCase{JSON, "False", "123:f00"},
benchmarkCase{JSON, "True", "{\"Name\":\"Alice\",\"Body\":\"Hello\",\"Time\":1294706395881547000}"},
// Multibyte
benchmarkCase{Multibyte, "Empty", ""},
benchmarkCase{Multibyte, "Space", " "},
benchmarkCase{Multibyte, "False", "abc"},
benchmarkCase{Multibyte, "True", "test@example.com"},
// ASCII
benchmarkCase{ASCII, "Empty", ""},
benchmarkCase{ASCII, "Space", " "},
benchmarkCase{ASCII, "False", "カタカナ"},
benchmarkCase{ASCII, "True", "foobar"},
// PrintableASCII
benchmarkCase{PrintableASCII, "Empty", ""},
benchmarkCase{PrintableASCII, "Space", " "},
benchmarkCase{PrintableASCII, "False", "newline\n"},
benchmarkCase{PrintableASCII, "True", "foobar"},
// FullWidth
benchmarkCase{FullWidth, "Empty", ""},
benchmarkCase{FullWidth, "Space", " "},
benchmarkCase{FullWidth, "False", "abc123"},
benchmarkCase{FullWidth, "True", "Good=Parts"},
// HalfWidth
benchmarkCase{HalfWidth, "Empty", ""},
benchmarkCase{HalfWidth, "Space", " "},
benchmarkCase{HalfWidth, "False", "あいうえお"},
benchmarkCase{HalfWidth, "True", "l-btn_02--active"},
// Semver
benchmarkCase{Semver, "Empty", ""},
benchmarkCase{Semver, "Space", " "},
benchmarkCase{Semver, "False", "1.0.0-b+-9+eta"},
benchmarkCase{Semver, "True", "1.0.0-alpha"},
}
)
func getFuncName(f interface{}) string {
n := runtime.FuncForPC(reflect.ValueOf(f).Pointer()).Name()
return filepath.Base(n)
}
func Benchmark(b *testing.B) {
for _, bencCase := range benchmarkFuncs {
name := getFuncName(bencCase.function) + "." + bencCase.caseName
b.Run(name, func(b *testing.B) {
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
bencCase.function(bencCase.param)
}
})
}
}