From beaa9adf3144e6718f8534defe124c74d897d80c Mon Sep 17 00:00:00 2001 From: joeybloggs Date: Sat, 30 May 2015 20:19:05 -0400 Subject: [PATCH] minor performance updates add benchmarks --- .gitignore | 2 ++ validator.go | 9 +++++---- validator_test.go | 30 ++++++++++++++++++++++++++++++ 3 files changed, 37 insertions(+), 4 deletions(-) diff --git a/.gitignore b/.gitignore index daf913b..4159020 100644 --- a/.gitignore +++ b/.gitignore @@ -22,3 +22,5 @@ _testmain.go *.exe *.test *.prof +*.test +*.out \ No newline at end of file diff --git a/validator.go b/validator.go index a37f1fe..0ae7faf 100644 --- a/validator.go +++ b/validator.go @@ -166,8 +166,6 @@ func (v *Validate) Struct(s interface{}) *StructErrors { func (v *Validate) structRecursive(top interface{}, current interface{}, s interface{}) *StructErrors { structValue := reflect.ValueOf(s) - structType := reflect.TypeOf(s) - structName := structType.Name() if structValue.Kind() == reflect.Ptr && !structValue.IsNil() { return v.structRecursive(top, current, structValue.Elem().Interface()) @@ -177,6 +175,9 @@ func (v *Validate) structRecursive(top interface{}, current interface{}, s inter panic("interface passed for validation is not a struct") } + structType := reflect.TypeOf(s) + structName := structType.Name() + validationErrors := &StructErrors{ Struct: structName, Errors: map[string]*FieldError{}, @@ -339,7 +340,7 @@ func (v *Validate) fieldWithNameAndValue(val interface{}, current interface{}, f func (v *Validate) fieldWithNameAndSingleTag(val interface{}, current interface{}, f interface{}, name string, valTag string) (*FieldError, error) { vals := strings.Split(valTag, tagKeySeparator) - key := strings.Trim(vals[0], " ") + key := strings.TrimSpace(vals[0]) if len(key) == 0 { panic(fmt.Sprintf("Invalid validation tag on field %s", name)) @@ -364,7 +365,7 @@ func (v *Validate) fieldWithNameAndSingleTag(val interface{}, current interface{ param := "" if len(vals) > 1 { - param = strings.Trim(vals[1], " ") + param = strings.TrimSpace(vals[1]) } if err := valFunc(val, current, f, param); !err { diff --git a/validator_test.go b/validator_test.go index ae00dc1..517d720 100644 --- a/validator_test.go +++ b/validator_test.go @@ -13,6 +13,14 @@ import ( // - Run "go test" to run tests // - Run "gocov test | gocov report" to report on test converage by file // - Run "gocov test | gocov annotate -" to report on all code and functions, those ,marked with "MISS" were never called +// +// +// go test -cpuprofile cpu.out +// ./validator.test -test.bench=. -test.cpuprofile=cpu.prof +// go tool pprof validator.test cpu.prof +// +// +// go test -memprofile mem.out type I interface { Foo() string @@ -2310,3 +2318,25 @@ func TestInvalidValidatorFunction(t *testing.T) { PanicMatches(t, func() { validate.Field(s.Test, "zzxxBadFunction") }, fmt.Sprintf("Undefined validation function on field %s", "")) } + +func BenchmarkValidateField(b *testing.B) { + for n := 0; n < b.N; n++ { + validate.Field("1", "len=1") + } +} + +func BenchmarkValidateStruct(b *testing.B) { + type Test struct { + StringVal string `bson:"required,lt=10"` + Int64Val int64 `bson:"gt=0,lt=10"` + } + + t := &Test{ + StringVal: "test", + Int64Val: 5, + } + + for n := 0; n < b.N; n++ { + validate.Struct(t) + } +}