@@ -2,78 +2,92 @@ package validator
22
33import "testing"
44
5- func BenchmarkField (b * testing.B ) {
5+ func BenchmarkFieldSuccess (b * testing.B ) {
66 for n := 0 ; n < b .N ; n ++ {
77 validate .Field ("1" , "len=1" )
88 }
99}
1010
11- func BenchmarkFieldOrTag (b * testing.B ) {
11+ func BenchmarkFieldFailure (b * testing.B ) {
12+ for n := 0 ; n < b .N ; n ++ {
13+ validate .Field ("2" , "len=1" )
14+ }
15+ }
16+
17+ func BenchmarkFieldOrTagSuccess (b * testing.B ) {
1218 for n := 0 ; n < b .N ; n ++ {
1319 validate .Field ("rgba(0,0,0,1)" , "rgb|rgba" )
1420 }
1521}
1622
17- func BenchmarkStructSimple (b * testing.B ) {
23+ func BenchmarkFieldOrTagFailure (b * testing.B ) {
24+ for n := 0 ; n < b .N ; n ++ {
25+ validate .Field ("#000" , "rgb|rgba" )
26+ }
27+ }
28+
29+ func BenchmarkStructSimpleSuccess (b * testing.B ) {
1830
1931 type Foo struct {
2032 StringValue string `validate:"min=5,max=10"`
2133 IntValue int `validate:"min=5,max=10"`
2234 }
2335
2436 validFoo := & Foo {StringValue : "Foobar" , IntValue : 7 }
25- invalidFoo := & Foo {StringValue : "Fo" , IntValue : 3 }
2637
2738 for n := 0 ; n < b .N ; n ++ {
2839 validate .Struct (validFoo )
40+ }
41+ }
42+
43+ func BenchmarkStructSimpleFailure (b * testing.B ) {
44+
45+ type Foo struct {
46+ StringValue string `validate:"min=5,max=10"`
47+ IntValue int `validate:"min=5,max=10"`
48+ }
49+
50+ invalidFoo := & Foo {StringValue : "Fo" , IntValue : 3 }
51+
52+ for n := 0 ; n < b .N ; n ++ {
2953 validate .Struct (invalidFoo )
3054 }
3155}
3256
33- func BenchmarkStructSimpleParallel (b * testing.B ) {
57+ func BenchmarkStructSimpleSuccessParallel (b * testing.B ) {
3458
3559 type Foo struct {
3660 StringValue string `validate:"min=5,max=10"`
3761 IntValue int `validate:"min=5,max=10"`
3862 }
3963
4064 validFoo := & Foo {StringValue : "Foobar" , IntValue : 7 }
41- invalidFoo := & Foo {StringValue : "Fo" , IntValue : 3 }
4265
4366 b .RunParallel (func (pb * testing.PB ) {
4467 for pb .Next () {
4568 validate .Struct (validFoo )
46- validate .Struct (invalidFoo )
4769 }
4870 })
4971}
5072
51- func BenchmarkStructComplex (b * testing.B ) {
73+ func BenchmarkStructSimpleFailureParallel (b * testing.B ) {
5274
53- tFail := & TestString {
54- Required : "" ,
55- Len : "" ,
56- Min : "" ,
57- Max : "12345678901" ,
58- MinMax : "" ,
59- Lt : "0123456789" ,
60- Lte : "01234567890" ,
61- Gt : "1" ,
62- Gte : "1" ,
63- OmitEmpty : "12345678901" ,
64- Sub : & SubTest {
65- Test : "" ,
66- },
67- Anonymous : struct {
68- A string `validate:"required"`
69- }{
70- A : "" ,
71- },
72- Iface : & Impl {
73- F : "12" ,
74- },
75+ type Foo struct {
76+ StringValue string `validate:"min=5,max=10"`
77+ IntValue int `validate:"min=5,max=10"`
7578 }
7679
80+ invalidFoo := & Foo {StringValue : "Fo" , IntValue : 3 }
81+
82+ b .RunParallel (func (pb * testing.PB ) {
83+ for pb .Next () {
84+ validate .Struct (invalidFoo )
85+ }
86+ })
87+ }
88+
89+ func BenchmarkStructComplexSuccess (b * testing.B ) {
90+
7791 tSuccess := & TestString {
7892 Required : "Required" ,
7993 Len : "length==10" ,
@@ -103,11 +117,10 @@ func BenchmarkStructComplex(b *testing.B) {
103117
104118 for n := 0 ; n < b .N ; n ++ {
105119 validate .Struct (tSuccess )
106- validate .Struct (tFail )
107120 }
108121}
109122
110- func BenchmarkStructComplexParallel (b * testing.B ) {
123+ func BenchmarkStructComplexFailure (b * testing.B ) {
111124
112125 tFail := & TestString {
113126 Required : "" ,
@@ -133,6 +146,13 @@ func BenchmarkStructComplexParallel(b *testing.B) {
133146 },
134147 }
135148
149+ for n := 0 ; n < b .N ; n ++ {
150+ validate .Struct (tFail )
151+ }
152+ }
153+
154+ func BenchmarkStructComplexSuccessParallel (b * testing.B ) {
155+
136156 tSuccess := & TestString {
137157 Required : "Required" ,
138158 Len : "length==10" ,
@@ -163,6 +183,38 @@ func BenchmarkStructComplexParallel(b *testing.B) {
163183 b .RunParallel (func (pb * testing.PB ) {
164184 for pb .Next () {
165185 validate .Struct (tSuccess )
186+ }
187+ })
188+ }
189+
190+ func BenchmarkStructComplexFailureParallel (b * testing.B ) {
191+
192+ tFail := & TestString {
193+ Required : "" ,
194+ Len : "" ,
195+ Min : "" ,
196+ Max : "12345678901" ,
197+ MinMax : "" ,
198+ Lt : "0123456789" ,
199+ Lte : "01234567890" ,
200+ Gt : "1" ,
201+ Gte : "1" ,
202+ OmitEmpty : "12345678901" ,
203+ Sub : & SubTest {
204+ Test : "" ,
205+ },
206+ Anonymous : struct {
207+ A string `validate:"required"`
208+ }{
209+ A : "" ,
210+ },
211+ Iface : & Impl {
212+ F : "12" ,
213+ },
214+ }
215+
216+ b .RunParallel (func (pb * testing.PB ) {
217+ for pb .Next () {
166218 validate .Struct (tFail )
167219 }
168220 })
0 commit comments