-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbench_construct_validate_test.go
228 lines (213 loc) · 6.08 KB
/
bench_construct_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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
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"
"github.com/invopop/validation"
)
// validateResponseLazy is a benchmark for the Lazy Evaluator.
func validateResponseLazy(resp *Response) error {
return validator.NewLazyValidator().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()
}
// validateResponseParallelLazy is a benchmark for the Parallel Lazy Evaluator.
func validateResponseParallelLazy(resp *Response) error {
return validator.NewParallelLazyValidator().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()
}
// validateIfImplementation is a benchmark for just pure if statements.
func validateIfImplementation(resp *Response) error {
if resp.Code == 0 {
return errs.IsEmptyError
}
if len(resp.Message) == 0 {
return errs.IsEmptyError
}
if resp.Extras == nil {
return errs.IsEmptyError
}
if len(resp.Optional) > 0 && len(resp.SetIfOptSet) == 0 {
return errs.IsEmptyError
}
if len(resp.Optional) == 0 && len(resp.SetIfOptSet) > 0 {
return errs.IsEmptyError
}
return nil
}
// validateResponseInvopop is a benchmark for the Invopop Validation Library.
func validateResponseInvopop(resp *Response) error {
return validation.ValidateStruct(
resp,
validation.Field(&resp.Code, validation.Required),
validation.Field(&resp.Message, validation.Required),
validation.Field(&resp.Extras, validation.NotNil),
validation.Field(
&resp.SetIfOptSet,
validation.Required.When(
len(resp.Optional) > 0,
),
validation.Empty.When(
len(resp.Optional) == 0,
),
),
)
}
// validateResponseValidator is a benchmark for the normal Validator.
func validateResponseValidator(resp *Response) error {
return validator.NewValidator().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 validateResponseValueWrapperLong(resp *Response) error {
return 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),
).Validate(resp)
}
func validateResponseValueWrapperShort(resp *Response) error {
intValidator := wrapper.NewValueWrapper[int]().WithOptions(options.VIsNotDefault[int]())
return 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
},
).Validate(resp)
}
// BenchmarkConstructAndValidateData benchmarks the different validators.
func BenchmarkConstructAndValidateData(b *testing.B) {
algorithms := map[string]ttypes.ValTest[*Response]{
"TestLazyValidator": validateResponseLazy,
"TestInvopop": validateResponseInvopop,
"TestParallelLazy": validateResponseParallelLazy,
"TestValidator": validateResponseValidator,
"TestIfStmts": validateIfImplementation,
"TestValueWrapperLong": validateResponseValueWrapperLong,
"TestValueWrapperShort": validateResponseValueWrapperShort,
}
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)
})
}
}
}