-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbench_validate_test.go
195 lines (186 loc) · 5.59 KB
/
bench_validate_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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
package main
import (
"fmt"
"testing"
"github.com/Jh123x/go-validate/errs"
"github.com/Jh123x/go-validate/options"
"github.com/Jh123x/go-validate/ttypes"
"github.com/Jh123x/go-validate/validator"
"github.com/Jh123x/go-validate/wrapper"
)
// validateOnlyResponseLazy is a benchmark for the Lazy Evaluator.
func validateOnlyResponseLazy() ttypes.ValTest[*Response] {
validator := validator.NewLazyValidator()
return func(resp *Response) error {
return validator.WithOptions(
options.IsNotEmpty(resp.Code),
options.IsNotEmpty(resp.Message),
options.WithRequire(func() bool { return resp.Extras != nil }, errTest),
options.Or(
options.And(
options.IsEmpty(resp.Optional),
options.IsEmpty(resp.SetIfOptSet),
),
options.And(
options.IsNotEmpty(resp.Optional),
options.IsNotEmpty(resp.SetIfOptSet),
),
),
).Validate()
}
}
// validateOnlyResponseParallelLazy is a benchmark for the Parallel Lazy Evaluator.
func validateOnlyResponseParallelLazy() ttypes.ValTest[*Response] {
parallelValidator := validator.NewParallelLazyValidator()
return func(resp *Response) error {
return parallelValidator.WithOptions(
options.IsNotEmpty(resp.Code),
options.IsNotEmpty(resp.Message),
options.WithRequire(func() bool { return resp.Extras != nil }, errTest),
options.Or(
options.And(
options.IsEmpty(resp.Optional),
options.IsEmpty(resp.SetIfOptSet),
),
options.And(
options.IsNotEmpty(resp.Optional),
options.IsNotEmpty(resp.SetIfOptSet),
),
),
).Validate()
}
}
// validateResponseValidator is a benchmark for the normal Validator.
func validateOnlyResponseValidator() ttypes.ValTest[*Response] {
validator := validator.NewValidator()
return func(resp *Response) error {
return validator.WithOptions(
options.IsNotEmpty(resp.Code),
options.IsNotEmpty(resp.Message),
options.WithRequire(func() bool { return resp.Extras != nil }, errTest),
options.Or(
options.And(
options.IsEmpty(resp.Optional),
options.IsEmpty(resp.SetIfOptSet),
),
options.And(
options.IsNotEmpty(resp.Optional),
options.IsNotEmpty(resp.SetIfOptSet),
),
),
).Validate()
}
}
func validateOnlyResponseValueWrapperLong() ttypes.ValTest[*Response] {
validator := wrapper.NewValueWrapper[*Response]().WithOptions(
options.VWithRequire(func(r *Response) bool { return r.Code != 0 }, errs.IsEmptyError),
options.VWithRequire(func(r *Response) bool { return len(r.Message) > 0 }, errs.IsEmptyError),
options.VWithRequire(func(r *Response) bool { return r.Extras != nil }, errs.IsEmptyError),
options.VWithRequire(func(r *Response) bool {
return len(r.Optional) > 0 && len(r.SetIfOptSet) > 0 || len(r.Optional) == 0 && len(r.SetIfOptSet) == 0
}, errs.IsEmptyError),
)
return func(resp *Response) error { return validator.Validate(resp) }
}
func validateOnlyResponseValueWrapperShort() ttypes.ValTest[*Response] {
intValidator := wrapper.NewValueWrapper[int]().WithOptions(options.VIsNotDefault[int]())
validator := wrapper.NewValueWrapper[*Response]().WithOptions(
func(r *Response) error { return intValidator.Validate(r.Code) },
func(r *Response) error { return intValidator.Validate(len(r.Message)) },
options.VWithRequire(func(r *Response) bool { return r.Extras != nil }, errs.IsNotEmptyErr),
func(r *Response) error {
if len(r.Optional) > 0 && len(r.SetIfOptSet) > 0 || len(r.Optional) == 0 && len(r.SetIfOptSet) == 0 {
return nil
}
return errs.IsEmptyError
},
)
return func(resp *Response) error { return validator.Validate(resp) }
}
// BenchmarkOnlyValidate Data benchmarks the different validators only for their validation cost.
func BenchmarkOnlyValidateData(b *testing.B) {
algorithms := map[string]ttypes.ValTest[*Response]{
"TestLazyValidator": validateOnlyResponseLazy(),
"TestInvopop": validateResponseInvopop, // No Option to create a validator first.
"TestParallelLazy": validateOnlyResponseParallelLazy(),
"TestValidator": validateOnlyResponseValidator(),
"TestIfStmts": validateIfImplementation,
"TestValueWrapperLong": validateOnlyResponseValueWrapperLong(),
"TestValueWrapperShort": validateOnlyResponseValueWrapperShort(),
}
tests := map[string]struct {
resp Response
hasErr bool
}{
"no err": {
resp: Response{
Code: 200,
Message: "OK",
Extras: map[string]any{},
},
hasErr: false,
},
"err in code": {
resp: Response{
Code: 0,
Message: "OK",
Extras: map[string]any{},
},
hasErr: true,
},
"err in message": {
resp: Response{
Code: 200,
Message: "",
Extras: map[string]any{},
},
hasErr: true,
},
"err in extras": {
resp: Response{
Code: 200,
Message: "OK",
Extras: nil,
},
hasErr: true,
},
"err in optional": {
resp: Response{
Code: 200,
Message: "OK",
Extras: map[string]any{},
Optional: "optional",
SetIfOptSet: "",
},
hasErr: true,
},
"no err in optional": {
resp: Response{
Code: 200,
Message: "OK",
Extras: map[string]any{},
Optional: "optional",
SetIfOptSet: "set",
},
hasErr: false,
},
"err in setIfOptSet": {
resp: Response{
Code: 200,
Message: "OK",
Extras: map[string]any{},
Optional: "",
SetIfOptSet: "set",
},
hasErr: true,
},
}
for testName, testCase := range tests {
for name, algo := range algorithms {
tcName := fmt.Sprintf("%s for %s", testName, name)
b.Run(tcName, func(b *testing.B) {
benchmarkValidator(b, &testCase.resp, algo, testCase.hasErr)
})
}
}
}