Skip to content

Commit aef21d4

Browse files
joeybloggsjoeybloggs
authored andcommitted
Update Benchmarks
Split out benchmarks into Success and Failure for more realistic numbers. I was validating 2 structs within the benchmarks, both a Successful and Failing struct which was artificially inflating the numbers.
1 parent a5d0ef3 commit aef21d4

File tree

2 files changed

+96
-38
lines changed

2 files changed

+96
-38
lines changed

README.md

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -120,12 +120,18 @@ hurt parallel performance too much.
120120
```go
121121
$ go test -cpu=4 -bench=. -benchmem=true
122122
PASS
123-
BenchmarkField-4 5000000 314 ns/op 16 B/op 1 allocs/op
124-
BenchmarkFieldOrTag-4 500000 2425 ns/op 20 B/op 2 allocs/op
125-
BenchmarkStructSimple-4 500000 3117 ns/op 553 B/op 14 allocs/op
126-
BenchmarkStructSimpleParallel-4 1000000 1149 ns/op 553 B/op 14 allocs/op
127-
BenchmarkStructComplex-4 100000 19580 ns/op 3230 B/op 102 allocs/op
128-
BenchmarkStructComplexParallel-4 200000 6686 ns/op 3232 B/op 102 allocs/op
123+
BenchmarkFieldSuccess-4 5000000 326 ns/op 16 B/op 1 allocs/op
124+
BenchmarkFieldFailure-4 5000000 327 ns/op 16 B/op 1 allocs/op
125+
BenchmarkFieldOrTagSuccess-4 500000 2738 ns/op 20 B/op 2 allocs/op
126+
BenchmarkFieldOrTagFailure-4 1000000 1341 ns/op 384 B/op 6 allocs/op
127+
BenchmarkStructSimpleSuccess-4 1000000 1282 ns/op 24 B/op 3 allocs/op
128+
BenchmarkStructSimpleFailure-4 1000000 1870 ns/op 529 B/op 11 allocs/op
129+
BenchmarkStructSimpleSuccessParallel-4 5000000 348 ns/op 24 B/op 3 allocs/op
130+
BenchmarkStructSimpleFailureParallel-4 2000000 807 ns/op 529 B/op 11 allocs/op
131+
BenchmarkStructComplexSuccess-4 200000 8081 ns/op 368 B/op 30 allocs/op
132+
BenchmarkStructComplexFailure-4 100000 12418 ns/op 2861 B/op 72 allocs/op
133+
BenchmarkStructComplexSuccessParallel-4 500000 2249 ns/op 369 B/op 30 allocs/op
134+
BenchmarkStructComplexFailureParallel-4 300000 5183 ns/op 2863 B/op 72 allocs/op
129135
```
130136

131137
How to Contribute

benchmarks_test.go

Lines changed: 84 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -2,78 +2,92 @@ package validator
22

33
import "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

Comments
 (0)