Repository navigation
Expand file tree
/
Copy pathbench_test.go
More file actions
132 lines (122 loc) · 2.77 KB
/
Copy pathbench_test.go
File metadata and controls
132 lines (122 loc) · 2.77 KB
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
package validator
import (
"context"
"fmt"
"testing"
)
type benchUser struct {
Name string `validate:"required && alpha"`
Email string `validate:"required && email"`
Age int `validate:"required && gte:18 && lte:120"`
}
func BenchmarkStructSuccess(b *testing.B) {
v := MustNew()
u := benchUser{Name: "alice", Email: "a@b.com", Age: 30}
ctx := context.Background()
b.ReportAllocs()
b.ResetTimer()
for b.Loop() {
vd := v.MustStruct(u)
_ = vd.Validate(ctx)
if vd.Fails() {
b.Fatal("should pass")
}
}
}
func BenchmarkStructFailure(b *testing.B) {
v := MustNew()
u := benchUser{Name: "1", Email: "nope", Age: 5}
ctx := context.Background()
b.ReportAllocs()
b.ResetTimer()
for b.Loop() {
vd := v.MustStruct(u)
_ = vd.Validate(ctx)
if !vd.Fails() {
b.Fatal("should fail")
}
}
}
func BenchmarkMapSuccess(b *testing.B) {
v := MustNew()
data := map[string]any{"Name": "alice", "Email": "a@b.com", "Age": 30}
sigs := map[string]string{"Name": "required && alpha", "Email": "required && email", "Age": "required && gte:18"}
ctx := context.Background()
b.ReportAllocs()
b.ResetTimer()
for b.Loop() {
vd := v.MustMap(data, sigs)
_ = vd.Validate(ctx)
if vd.Fails() {
b.Fatal("should pass")
}
}
}
func BenchmarkVarSuccess(b *testing.B) {
v := MustNew()
ctx := context.Background()
b.ReportAllocs()
b.ResetTimer()
for b.Loop() {
vd := v.MustValue("a@b.com", "required && email")
_ = vd.Validate(ctx)
if vd.Fails() {
b.Fatal("should pass")
}
}
}
func BenchmarkDiving(b *testing.B) {
v := MustNew()
data := map[string]any{"tags": []any{"foo", "bar", "baz"}}
sigs := map[string]string{"tags": "required && dive && alpha"}
ctx := context.Background()
b.ReportAllocs()
b.ResetTimer()
for b.Loop() {
vd := v.MustMap(data, sigs)
_ = vd.Validate(ctx)
if vd.Fails() {
b.Fatal("should pass")
}
}
}
func BenchmarkCompileCold(b *testing.B) {
ctx := context.Background()
b.ReportAllocs()
b.ResetTimer()
for b.Loop() {
v := MustNew()
vd := v.MustValue("a@b.com", "required && (email || regex:\"^x\")")
_ = vd.Validate(ctx)
}
}
func BenchmarkCompileCached(b *testing.B) {
v := MustNew()
// warm exprCache so the timed loop measures cache hits
_, _ = v.compile("required && (email || regex:\"^x\")")
b.ReportAllocs()
b.ResetTimer()
for b.Loop() {
_, _ = v.compile("required && (email || regex:\"^x\")")
}
}
func BenchmarkParallelStruct(b *testing.B) {
v := MustNew(WithParallel(8))
sigs := map[string]string{}
data := map[string]any{}
for i := 0; i < 20; i++ {
f := fmt.Sprintf("f%02d", i)
sigs[f] = "required && alpha"
data[f] = "abc"
}
ctx := context.Background()
b.ReportAllocs()
b.ResetTimer()
for b.Loop() {
vd := v.MustMap(data, sigs)
_ = vd.Validate(ctx)
if vd.Fails() {
b.Fatal("should pass")
}
}
}