/
competitor_bench.go
100 lines (80 loc) · 2.36 KB
/
competitor_bench.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
package bench
import (
"errors"
asaskevichGoValidator "github.com/asaskevich/govalidator"
validator "github.com/typerandom/validator"
goValidator "gopkg.in/validator.v2"
"strconv"
"testing"
)
var nativeError error
func BenchmarkNativeMin(b *testing.B) {
type Foo struct {
StringValue string
IntValue int
}
validFoo := &Foo{StringValue: "Foobar", IntValue: 7}
invalidFoo := &Foo{StringValue: "Fo", IntValue: 3}
foos := []*Foo{validFoo, invalidFoo}
for i := 0; i < b.N; i++ {
for j := 0; j < 2; j++ {
nativeError = nil
foo := foos[j%2]
if len(foo.StringValue) < 5 || len(foo.StringValue) > 10 {
nativeError = errors.New("StringValue must be between 5 and 10 characters.")
}
if foo.IntValue < 5 || foo.IntValue > 10 {
nativeError = errors.New("IntValue must be between 5 and 10.")
}
}
}
}
func BenchmarkValidatorMin(b *testing.B) {
type Foo struct {
StringValue string `validate:"min(5),max(10)"`
IntValue int `validate:"min(5),max(10)"`
}
validFoo := &Foo{StringValue: "Foobar", IntValue: 7}
invalidFoo := &Foo{StringValue: "Fo", IntValue: 3}
for i := 0; i < b.N; i++ {
validator.Validate(validFoo)
validator.Validate(invalidFoo)
}
}
func BenchmarkCompetitorGoValidatorMin(b *testing.B) {
type Foo struct {
StringValue string `validate:"min=5,max=10"`
IntValue int `validate:"min=5,max=10"`
}
validFoo := &Foo{StringValue: "Foobar", IntValue: 7}
invalidFoo := &Foo{StringValue: "Fo", IntValue: 3}
for i := 0; i < b.N; i++ {
goValidator.Validate(validFoo)
goValidator.Validate(invalidFoo)
}
}
func BenchmarkCompetitorAsaskevichGoValidatorMin(b *testing.B) {
type Foo struct {
StringValue string `validate:"min_str"`
IntValue int `validate:"min_int"`
}
asaskevichGoValidator.TagMap["min_str"] = asaskevichGoValidator.Validator(func(value string) bool {
if len(value) < 5 || len(value) > 10 {
return false
}
return true
})
asaskevichGoValidator.TagMap["min_int"] = asaskevichGoValidator.Validator(func(value string) bool {
intValue, _ := strconv.ParseInt(value, 10, 32)
if intValue < 5 || intValue > 10 {
return false
}
return true
})
validFoo := &Foo{StringValue: "Foobar", IntValue: 7}
invalidFoo := &Foo{StringValue: "Fo", IntValue: 3}
for i := 0; i < b.N; i++ {
asaskevichGoValidator.ValidateStruct(validFoo)
asaskevichGoValidator.ValidateStruct(invalidFoo)
}
}