diff --git a/README.md b/README.md index c4da807..13917f7 100644 --- a/README.md +++ b/README.md @@ -120,12 +120,18 @@ hurt parallel performance too much. ```go $ go test -cpu=4 -bench=. -benchmem=true PASS -BenchmarkField-4 5000000 314 ns/op 16 B/op 1 allocs/op -BenchmarkFieldOrTag-4 500000 2425 ns/op 20 B/op 2 allocs/op -BenchmarkStructSimple-4 500000 3117 ns/op 553 B/op 14 allocs/op -BenchmarkStructSimpleParallel-4 1000000 1149 ns/op 553 B/op 14 allocs/op -BenchmarkStructComplex-4 100000 19580 ns/op 3230 B/op 102 allocs/op -BenchmarkStructComplexParallel-4 200000 6686 ns/op 3232 B/op 102 allocs/op +BenchmarkFieldSuccess-4 5000000 326 ns/op 16 B/op 1 allocs/op +BenchmarkFieldFailure-4 5000000 327 ns/op 16 B/op 1 allocs/op +BenchmarkFieldOrTagSuccess-4 500000 2738 ns/op 20 B/op 2 allocs/op +BenchmarkFieldOrTagFailure-4 1000000 1341 ns/op 384 B/op 6 allocs/op +BenchmarkStructSimpleSuccess-4 1000000 1282 ns/op 24 B/op 3 allocs/op +BenchmarkStructSimpleFailure-4 1000000 1870 ns/op 529 B/op 11 allocs/op +BenchmarkStructSimpleSuccessParallel-4 5000000 348 ns/op 24 B/op 3 allocs/op +BenchmarkStructSimpleFailureParallel-4 2000000 807 ns/op 529 B/op 11 allocs/op +BenchmarkStructComplexSuccess-4 200000 8081 ns/op 368 B/op 30 allocs/op +BenchmarkStructComplexFailure-4 100000 12418 ns/op 2861 B/op 72 allocs/op +BenchmarkStructComplexSuccessParallel-4 500000 2249 ns/op 369 B/op 30 allocs/op +BenchmarkStructComplexFailureParallel-4 300000 5183 ns/op 2863 B/op 72 allocs/op ``` How to Contribute diff --git a/benchmarks_test.go b/benchmarks_test.go index 14c5d2b..9fcf85b 100644 --- a/benchmarks_test.go +++ b/benchmarks_test.go @@ -2,19 +2,31 @@ package validator import "testing" -func BenchmarkField(b *testing.B) { +func BenchmarkFieldSuccess(b *testing.B) { for n := 0; n < b.N; n++ { validate.Field("1", "len=1") } } -func BenchmarkFieldOrTag(b *testing.B) { +func BenchmarkFieldFailure(b *testing.B) { + for n := 0; n < b.N; n++ { + validate.Field("2", "len=1") + } +} + +func BenchmarkFieldOrTagSuccess(b *testing.B) { for n := 0; n < b.N; n++ { validate.Field("rgba(0,0,0,1)", "rgb|rgba") } } -func BenchmarkStructSimple(b *testing.B) { +func BenchmarkFieldOrTagFailure(b *testing.B) { + for n := 0; n < b.N; n++ { + validate.Field("#000", "rgb|rgba") + } +} + +func BenchmarkStructSimpleSuccess(b *testing.B) { type Foo struct { StringValue string `validate:"min=5,max=10"` @@ -22,15 +34,27 @@ func BenchmarkStructSimple(b *testing.B) { } validFoo := &Foo{StringValue: "Foobar", IntValue: 7} - invalidFoo := &Foo{StringValue: "Fo", IntValue: 3} for n := 0; n < b.N; n++ { validate.Struct(validFoo) + } +} + +func BenchmarkStructSimpleFailure(b *testing.B) { + + type Foo struct { + StringValue string `validate:"min=5,max=10"` + IntValue int `validate:"min=5,max=10"` + } + + invalidFoo := &Foo{StringValue: "Fo", IntValue: 3} + + for n := 0; n < b.N; n++ { validate.Struct(invalidFoo) } } -func BenchmarkStructSimpleParallel(b *testing.B) { +func BenchmarkStructSimpleSuccessParallel(b *testing.B) { type Foo struct { StringValue string `validate:"min=5,max=10"` @@ -38,42 +62,32 @@ func BenchmarkStructSimpleParallel(b *testing.B) { } validFoo := &Foo{StringValue: "Foobar", IntValue: 7} - invalidFoo := &Foo{StringValue: "Fo", IntValue: 3} b.RunParallel(func(pb *testing.PB) { for pb.Next() { validate.Struct(validFoo) - validate.Struct(invalidFoo) } }) } -func BenchmarkStructComplex(b *testing.B) { +func BenchmarkStructSimpleFailureParallel(b *testing.B) { - tFail := &TestString{ - Required: "", - Len: "", - Min: "", - Max: "12345678901", - MinMax: "", - Lt: "0123456789", - Lte: "01234567890", - Gt: "1", - Gte: "1", - OmitEmpty: "12345678901", - Sub: &SubTest{ - Test: "", - }, - Anonymous: struct { - A string `validate:"required"` - }{ - A: "", - }, - Iface: &Impl{ - F: "12", - }, + type Foo struct { + StringValue string `validate:"min=5,max=10"` + IntValue int `validate:"min=5,max=10"` } + invalidFoo := &Foo{StringValue: "Fo", IntValue: 3} + + b.RunParallel(func(pb *testing.PB) { + for pb.Next() { + validate.Struct(invalidFoo) + } + }) +} + +func BenchmarkStructComplexSuccess(b *testing.B) { + tSuccess := &TestString{ Required: "Required", Len: "length==10", @@ -103,11 +117,10 @@ func BenchmarkStructComplex(b *testing.B) { for n := 0; n < b.N; n++ { validate.Struct(tSuccess) - validate.Struct(tFail) } } -func BenchmarkStructComplexParallel(b *testing.B) { +func BenchmarkStructComplexFailure(b *testing.B) { tFail := &TestString{ Required: "", @@ -133,6 +146,13 @@ func BenchmarkStructComplexParallel(b *testing.B) { }, } + for n := 0; n < b.N; n++ { + validate.Struct(tFail) + } +} + +func BenchmarkStructComplexSuccessParallel(b *testing.B) { + tSuccess := &TestString{ Required: "Required", Len: "length==10", @@ -163,6 +183,38 @@ func BenchmarkStructComplexParallel(b *testing.B) { b.RunParallel(func(pb *testing.PB) { for pb.Next() { validate.Struct(tSuccess) + } + }) +} + +func BenchmarkStructComplexFailureParallel(b *testing.B) { + + tFail := &TestString{ + Required: "", + Len: "", + Min: "", + Max: "12345678901", + MinMax: "", + Lt: "0123456789", + Lte: "01234567890", + Gt: "1", + Gte: "1", + OmitEmpty: "12345678901", + Sub: &SubTest{ + Test: "", + }, + Anonymous: struct { + A string `validate:"required"` + }{ + A: "", + }, + Iface: &Impl{ + F: "12", + }, + } + + b.RunParallel(func(pb *testing.PB) { + for pb.Next() { validate.Struct(tFail) } }) diff --git a/doc.go b/doc.go index df2db9f..6733893 100644 --- a/doc.go +++ b/doc.go @@ -121,11 +121,6 @@ Here is a list of the current built in validators: gt=0 will be applied to [] []string will be spared validation required will be applied to string - NOTE: in Example2 if the required validation failed, but all others passed - the hierarchy of FieldError's in the middle with have their IsPlaceHolder field - set to true. If a FieldError has IsSliceOrMap=true or IsMap=true then the - FieldError is a Slice or Map field and if IsPlaceHolder=true then contains errors - within its SliceOrArrayErrs or MapErrs fields. required This validates that the value is not the data types default value.