-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathbenchmarks_test.go
129 lines (123 loc) · 3.13 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
// +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
{Alpha, "Empty", ""},
{Alpha, "Space", " "},
{Alpha, "False", "abc!!!"},
{Alpha, "True", "abc"},
//UTFLetter
{UTFLetter, "Empty", ""},
{UTFLetter, "Space", " "},
{UTFLetter, "False", "\u0026"}, //UTF-8(ASCII): &
{UTFLetter, "True", "abc"},
// Alphanumeric
{Alphanumeric, "Empty", ""},
{Alphanumeric, "Space", " "},
{Alphanumeric, "False", "\u0026"}, //UTF-8(ASCII): &
{Alphanumeric, "True", "abc123"},
// UTFLetterNumeric
{UTFLetterNumeric, "Empty", ""},
{UTFLetterNumeric, "Space", " "},
{UTFLetterNumeric, "False", "abc!!!"},
{UTFLetterNumeric, "True", "abc1"},
// Numeric
{Numeric, "Empty", ""},
{Numeric, "Space", " "},
{Numeric, "False", "abc!!!"},
{Numeric, "True", "0123"},
// UTFNumeric
{UTFNumeric, "Empty", ""},
{UTFNumeric, "Space", " "},
{UTFNumeric, "False", "abc!!!"},
{UTFNumeric, "True", "\u0030"}, //UTF-8(ASCII): 0
// UTFDigit
{UTFDigit, "Empty", ""},
{UTFDigit, "Space", " "},
{UTFDigit, "False", "abc!!!"},
{UTFDigit, "True", "\u0030"}, //UTF-8(ASCII): 0
// LowerCase
{LowerCase, "Empty", ""},
{LowerCase, "Space", " "},
{LowerCase, "False", "ABC"},
{LowerCase, "True", "abc"},
// UpperCase
{UpperCase, "Empty", ""},
{UpperCase, "Space", " "},
{UpperCase, "False", "abc"},
{UpperCase, "True", "ABC"},
// Int
{Int, "Empty", ""},
{Int, "Space", " "},
{Int, "False", "abc"},
{Int, "True", "000"},
// Email
{Email, "Empty", ""},
{Email, "Space", " "},
{Email, "False", "@invalid.com"},
{Email, "True", "[email protected]"},
// URL
{URL, "Empty", ""},
{URL, "Space", " "},
{URL, "False", "./rel/test/dir"},
{URL, "True", "http://foobar.org/"},
// RequestURL
{RequestURL, "Empty", ""},
{RequestURL, "Space", " "},
{RequestURL, "False", "invalid."},
{RequestURL, "True", "http://foobar.org/"},
// RequestURI
{RequestURI, "Empty", ""},
{RequestURI, "Space", " "},
{RequestURI, "False", "invalid."},
{RequestURI, "True", "http://foobar.org/"},
// Float
{Float, "Empty", ""},
{Float, "Space", " "},
{Float, "False", "+1f"},
{Float, "True", "123.123"},
// Hexadecimal
{Hexadecimal, "Empty", ""},
{Hexadecimal, "Space", " "},
{Hexadecimal, "False", ".."},
{Hexadecimal, "True", "deadBEEF"},
// Hexcolor
{Hexcolor, "Empty", ""},
{Hexcolor, "Space", " "},
{Hexcolor, "False", "#ff12FG"},
{Hexcolor, "True", "#f00"},
// RGBcolor
{RGBcolor, "Empty", ""},
{RGBcolor, "Space", " "},
{RGBcolor, "False", "rgba(0,31,255)"},
{RGBcolor, "True", "rgb(0,31,255)"},
}
)
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)
}
})
}
}