Skip to content

Commit 47c3cab

Browse files
Dean KarnDean Karn
authored andcommitted
Merge pull request #213 from joeybloggs/v8-development
Update benchmarks + correct benchmark error
2 parents ba623db + d451d57 commit 47c3cab

File tree

4 files changed

+82
-41
lines changed

4 files changed

+82
-41
lines changed

README.md

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -310,34 +310,34 @@ Benchmarks
310310
```go
311311
$ go test -cpu=4 -bench=. -benchmem=true
312312
PASS
313-
BenchmarkFieldSuccess-4 5000000 288 ns/op 16 B/op 1 allocs/op
314-
BenchmarkFieldFailure-4 5000000 292 ns/op 16 B/op 1 allocs/op
315-
BenchmarkFieldDiveSuccess-4 500000 3464 ns/op 528 B/op 28 allocs/op
316-
BenchmarkFieldDiveFailure-4 500000 4031 ns/op 928 B/op 32 allocs/op
317-
BenchmarkFieldCustomTypeSuccess-4 3000000 446 ns/op 32 B/op 2 allocs/op
318-
BenchmarkFieldCustomTypeFailure-4 2000000 755 ns/op 400 B/op 4 allocs/op
319-
BenchmarkFieldOrTagSuccess-4 1000000 1356 ns/op 32 B/op 2 allocs/op
320-
BenchmarkFieldOrTagFailure-4 1000000 1177 ns/op 432 B/op 6 allocs/op
321-
BenchmarkStructLevelValidationSuccess-4 2000000 810 ns/op 160 B/op 6 allocs/op
322-
BenchmarkStructLevelValidationFailure-4 1000000 1424 ns/op 592 B/op 11 allocs/op
323-
BenchmarkStructSimpleCustomTypeSuccess-4 1000000 1231 ns/op 80 B/op 5 allocs/op
324-
BenchmarkStructSimpleCustomTypeFailure-4 1000000 1779 ns/op 624 B/op 11 allocs/op
325-
BenchmarkStructPartialSuccess-4 1000000 1396 ns/op 400 B/op 11 allocs/op
326-
BenchmarkStructPartialFailure-4 1000000 1928 ns/op 816 B/op 16 allocs/op
327-
BenchmarkStructExceptSuccess-4 2000000 946 ns/op 368 B/op 9 allocs/op
328-
BenchmarkStructExceptFailure-4 1000000 1396 ns/op 400 B/op 11 allocs/op
329-
BenchmarkStructSimpleCrossFieldSuccess-4 1000000 1241 ns/op 128 B/op 6 allocs/op
330-
BenchmarkStructSimpleCrossFieldFailure-4 1000000 1837 ns/op 560 B/op 11 allocs/op
331-
BenchmarkStructSimpleCrossStructCrossFieldSuccess-4 1000000 1867 ns/op 176 B/op 9 allocs/op
332-
BenchmarkStructSimpleCrossStructCrossFieldFailure-4 500000 2495 ns/op 608 B/op 14 allocs/op
333-
BenchmarkStructSimpleSuccess-4 1000000 1253 ns/op 48 B/op 3 allocs/op
334-
BenchmarkStructSimpleFailure-4 1000000 1852 ns/op 624 B/op 11 allocs/op
335-
BenchmarkStructSimpleSuccessParallel-4 5000000 372 ns/op 48 B/op 3 allocs/op
336-
BenchmarkStructSimpleFailureParallel-4 2000000 784 ns/op 624 B/op 11 allocs/op
337-
BenchmarkStructComplexSuccess-4 200000 8080 ns/op 512 B/op 30 allocs/op
338-
BenchmarkStructComplexFailure-4 100000 12643 ns/op 3416 B/op 72 allocs/op
339-
BenchmarkStructComplexSuccessParallel-4 1000000 2688 ns/op 512 B/op 30 allocs/op
340-
BenchmarkStructComplexFailureParallel-4 300000 4727 ns/op 3416 B/op 72 allocs/op
313+
BenchmarkFieldSuccess-4 5000000 254 ns/op 0 B/op 0 allocs/op
314+
BenchmarkFieldFailure-4 2000000 779 ns/op 400 B/op 4 allocs/op
315+
BenchmarkFieldDiveSuccess-4 500000 3451 ns/op 480 B/op 27 allocs/op
316+
BenchmarkFieldDiveFailure-4 300000 3954 ns/op 880 B/op 31 allocs/op
317+
BenchmarkFieldCustomTypeSuccess-4 3000000 451 ns/op 32 B/op 2 allocs/op
318+
BenchmarkFieldCustomTypeFailure-4 2000000 751 ns/op 400 B/op 4 allocs/op
319+
BenchmarkFieldOrTagSuccess-4 1000000 1312 ns/op 16 B/op 1 allocs/op
320+
BenchmarkFieldOrTagFailure-4 1000000 1206 ns/op 432 B/op 6 allocs/op
321+
BenchmarkStructLevelValidationSuccess-4 2000000 829 ns/op 160 B/op 6 allocs/op
322+
BenchmarkStructLevelValidationFailure-4 1000000 1403 ns/op 592 B/op 11 allocs/op
323+
BenchmarkStructSimpleCustomTypeSuccess-4 1000000 1238 ns/op 80 B/op 5 allocs/op
324+
BenchmarkStructSimpleCustomTypeFailure-4 1000000 1771 ns/op 624 B/op 11 allocs/op
325+
BenchmarkStructPartialSuccess-4 1000000 1399 ns/op 400 B/op 11 allocs/op
326+
BenchmarkStructPartialFailure-4 1000000 1926 ns/op 816 B/op 16 allocs/op
327+
BenchmarkStructExceptSuccess-4 2000000 930 ns/op 368 B/op 9 allocs/op
328+
BenchmarkStructExceptFailure-4 1000000 1381 ns/op 400 B/op 11 allocs/op
329+
BenchmarkStructSimpleCrossFieldSuccess-4 1000000 1253 ns/op 128 B/op 6 allocs/op
330+
BenchmarkStructSimpleCrossFieldFailure-4 1000000 1849 ns/op 560 B/op 11 allocs/op
331+
BenchmarkStructSimpleCrossStructCrossFieldSuccess-4 1000000 1902 ns/op 176 B/op 9 allocs/op
332+
BenchmarkStructSimpleCrossStructCrossFieldFailure-4 500000 2453 ns/op 608 B/op 14 allocs/op
333+
BenchmarkStructSimpleSuccess-4 1000000 1182 ns/op 48 B/op 3 allocs/op
334+
BenchmarkStructSimpleFailure-4 1000000 1842 ns/op 624 B/op 11 allocs/op
335+
BenchmarkStructSimpleSuccessParallel-4 5000000 342 ns/op 48 B/op 3 allocs/op
336+
BenchmarkStructSimpleFailureParallel-4 2000000 665 ns/op 624 B/op 11 allocs/op
337+
BenchmarkStructComplexSuccess-4 200000 7924 ns/op 512 B/op 30 allocs/op
338+
BenchmarkStructComplexFailure-4 100000 12605 ns/op 3415 B/op 72 allocs/op
339+
BenchmarkStructComplexSuccessParallel-4 1000000 2421 ns/op 512 B/op 30 allocs/op
340+
BenchmarkStructComplexFailureParallel-4 300000 4234 ns/op 3416 B/op 72 allocs/op
341341
```
342342

343343
How to Contribute

benchmarks_test.go

Lines changed: 46 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,26 +7,56 @@ import (
77
)
88

99
func BenchmarkFieldSuccess(b *testing.B) {
10+
11+
var s *string
12+
tmp := "1"
13+
s = &tmp
14+
1015
for n := 0; n < b.N; n++ {
11-
validate.Field("1", "len=1")
16+
validate.Field(s, "len=1")
1217
}
1318
}
1419

1520
func BenchmarkFieldFailure(b *testing.B) {
21+
22+
var s *string
23+
tmp := "12"
24+
s = &tmp
25+
1626
for n := 0; n < b.N; n++ {
17-
validate.Field("2", "len=1")
27+
validate.Field(s, "len=1")
1828
}
1929
}
2030

2131
func BenchmarkFieldDiveSuccess(b *testing.B) {
32+
33+
m := make([]*string, 3)
34+
t1 := "val1"
35+
t2 := "val2"
36+
t3 := "val3"
37+
38+
m[0] = &t1
39+
m[1] = &t2
40+
m[2] = &t3
41+
2242
for n := 0; n < b.N; n++ {
23-
validate.Field([]string{"val1", "val2", "val3"}, "required,dive,required")
43+
validate.Field(m, "required,dive,required")
2444
}
2545
}
2646

2747
func BenchmarkFieldDiveFailure(b *testing.B) {
48+
49+
m := make([]*string, 3)
50+
t1 := "val1"
51+
t2 := ""
52+
t3 := "val3"
53+
54+
m[0] = &t1
55+
m[1] = &t2
56+
m[2] = &t3
57+
2858
for n := 0; n < b.N; n++ {
29-
validate.Field([]string{"val1", "", "val3"}, "required,dive,required")
59+
validate.Field(m, "required,dive,required")
3060
}
3161
}
3262

@@ -45,7 +75,6 @@ func BenchmarkFieldCustomTypeSuccess(b *testing.B) {
4575

4676
func BenchmarkFieldCustomTypeFailure(b *testing.B) {
4777

48-
// validate := New(Config{TagName: "validate"})
4978
validate.RegisterCustomTypeFunc(ValidateValuerType, (*sql.Valuer)(nil), valuer{})
5079

5180
val := valuer{}
@@ -56,14 +85,24 @@ func BenchmarkFieldCustomTypeFailure(b *testing.B) {
5685
}
5786

5887
func BenchmarkFieldOrTagSuccess(b *testing.B) {
88+
89+
var s *string
90+
tmp := "rgba(0,0,0,1)"
91+
s = &tmp
92+
5993
for n := 0; n < b.N; n++ {
60-
validate.Field("rgba(0,0,0,1)", "rgb|rgba")
94+
validate.Field(s, "rgb|rgba")
6195
}
6296
}
6397

6498
func BenchmarkFieldOrTagFailure(b *testing.B) {
99+
100+
var s *string
101+
tmp := "#000"
102+
s = &tmp
103+
65104
for n := 0; n < b.N; n++ {
66-
validate.Field("#000", "rgb|rgba")
105+
validate.Field(s, "rgb|rgba")
67106
}
68107
}
69108

util.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,15 @@ func (v *Validate) ExtractType(current reflect.Value) (reflect.Value, reflect.Ki
5959
default:
6060

6161
if v.hasCustomFuncs {
62+
// fmt.Println("Type", current.Type())
6263
if fn, ok := v.customTypeFuncs[current.Type()]; ok {
64+
65+
// fmt.Println("OK")
66+
6367
return v.ExtractType(reflect.ValueOf(fn(current)))
6468
}
69+
70+
// fmt.Println("NOT OK")
6571
}
6672

6773
return current, current.Kind()

validator_test.go

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,9 @@ type CustomMadeUpStruct struct {
181181
}
182182

183183
func ValidateValuerType(field reflect.Value) interface{} {
184+
184185
if valuer, ok := field.Interface().(driver.Valuer); ok {
186+
185187
val, err := valuer.Value()
186188
if err != nil {
187189
// handle the error how you want
@@ -1496,12 +1498,6 @@ func TestSQLValue2Validation(t *testing.T) {
14961498

14971499
func TestSQLValueValidation(t *testing.T) {
14981500

1499-
// customTypes := map[reflect.Type]CustomTypeFunc{}
1500-
// customTypes[reflect.TypeOf((*driver.Valuer)(nil))] = ValidateValuerType
1501-
// customTypes[reflect.TypeOf(valuer{})] = ValidateValuerType
1502-
// customTypes[reflect.TypeOf(MadeUpCustomType{})] = ValidateCustomType
1503-
// customTypes[reflect.TypeOf(1)] = OverrideIntTypeForSomeReason
1504-
15051501
validate := New(&Config{TagName: "validate"})
15061502
validate.RegisterCustomTypeFunc(ValidateValuerType, (*driver.Valuer)(nil), valuer{})
15071503
validate.RegisterCustomTypeFunc(ValidateCustomType, MadeUpCustomType{})

0 commit comments

Comments
 (0)