-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvalidator_perf_test.go
173 lines (159 loc) · 4.31 KB
/
validator_perf_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
package validator
import (
"encoding/json"
"regexp"
"strconv"
"testing"
)
type TestStructNoValidation struct {
String string `vld:"-"`
Int int `vld:"-"`
Float float64 `vld:"-"`
Array []string `vld:"-"`
}
type TestStructValidationRex struct {
String string `vld:"rex^[a-zA-Z0-9]+$, gr1min3"`
Int int `vld:"rex^(2|3)$, gr1min3"`
Float float64 `vld:"rex^(2.000000|3.000000)$, gr1min3"`
Array []string `vld:"min3, gr1min3"`
}
type TestStructValidation struct {
String string `json:"string" vld:"rex^[a-zA-Z0-9]+$, gr1min3"`
Int int `json:"int" vld:"equ2 || equ3, gr1min3"`
Float float64 `json:"float" vld:"equ2 || equ3, gr1min3"`
Array []string `json:"array" vld:"min3, gr1min3"`
}
func BenchmarkUnmarshalToMap(b *testing.B) {
// unmarshal to map
jsonString := []byte(`{"string": "test", "int": 2, "float": 3.0, "array": ["", "", ""]}`)
unmarshalToMap := &map[string]interface{}{}
err := json.Unmarshal(jsonString, unmarshalToMap)
if err != nil {
b.Logf("error unmarshalling %s: %v", jsonString, err)
}
}
func BenchmarkUnmarshalToStruct(b *testing.B) {
// unmarshal to struct
jsonString := []byte(`{"string": "test", "int": 2, "float": 3.0, "array": ["", "", ""]}`)
unmarshalToStruct := &TestStructNoValidation{}
err := json.Unmarshal(jsonString, unmarshalToStruct)
if err != nil {
b.Logf("error unmarshalling %s: %v", jsonString, err)
}
}
func BenchmarkNoValidation(b *testing.B) {
// no validation in validator
noValidation := &TestStructNoValidation{
String: "test",
Int: 2,
Float: 3.0,
Array: []string{"", "", ""},
}
err := Validate(noValidation)
if err != nil {
b.Logf("error no validation %v", err)
}
}
func BenchmarkValidationRex(b *testing.B) {
// validation with regex in validator
validationRex := &TestStructValidationRex{
String: "test",
Int: 2,
Float: 3.0,
Array: []string{"", "", ""},
}
err := Validate(validationRex)
if err != nil {
b.Logf("error validation rex %v", err)
}
}
func BenchmarkManualValidationRex(b *testing.B) {
// manual validation with regex
var errors int
manualValidationRex := &TestStructValidationRex{
String: "test",
Int: 2,
Float: 3.0,
Array: []string{"", "", ""},
}
match, err := regexp.MatchString("^[a-zA-Z0-9]+$", manualValidationRex.String)
if err != nil || !match {
errors++
}
match, err = regexp.MatchString("^(2|3)$", strconv.Itoa(manualValidationRex.Int))
if err != nil || !match {
errors++
}
match, err = regexp.MatchString("^(2.000000|3.000000)$", strconv.FormatFloat(manualValidationRex.Float, 'f', 0, 64))
if err != nil || !match {
errors++
}
if len(manualValidationRex.Array) < 3 {
errors++
}
if errors > 1 {
b.Logf("error manual validation rex %v", err)
}
}
func BenchmarkValidatationMinimalRex(b *testing.B) {
// validation with minimal regex (only string) in validator
validation := &TestStructValidation{
String: "test",
Int: 2,
Float: 3.0,
Array: []string{"", "", ""},
}
err := Validate(validation)
if err != nil {
b.Logf("error validation %v", err)
}
}
func BenchmarkManualValidationMinimalRex(b *testing.B) {
// manual validation with minimal regex (only string)
var errors int
manualValidation := &TestStructValidation{
String: "test",
Int: 2,
Float: 3.0,
Array: []string{"", "", ""},
}
match, err := regexp.MatchString("^[a-zA-Z0-9]+$", manualValidation.String)
if err != nil || !match {
errors++
}
if manualValidation.Int != 2 && manualValidation.Int != 3 {
errors++
}
if manualValidation.Float != 2 && manualValidation.Float != 3 {
errors++
}
if len(manualValidation.Array) < 3 {
errors++
}
if errors > 1 {
b.Logf("error manual validation %v", err)
}
}
func BenchmarkValidationWithNoValidation(b *testing.B) {
// no validation in validator
noValidation := &TestStructNoValidation{
String: "test",
Int: 2,
Float: 3.0,
Array: []string{"", "", ""},
}
err := Validate(noValidation)
if err != nil {
b.Logf("error no validation %v", err)
}
}
func BenchmarkUnmarshalAndValidate(b *testing.B) {
// unmarshal and validate
jsonString := []byte(`{"string": "test", "int": 2, "float": 3.0, "array": ["", "", ""]}`)
unmarshalAndValidate := &TestStructValidation{}
err := UnmarshalAndValidate(jsonString, unmarshalAndValidate)
if err != nil {
b.Logf("error unmarshal and validate %v", err)
}
b.Error("test ended")
}