Skip to content

Commit beaa9ad

Browse files
joeybloggsjoeybloggs
authored andcommitted
minor performance updates
add benchmarks
1 parent 756455c commit beaa9ad

File tree

3 files changed

+37
-4
lines changed

3 files changed

+37
-4
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,5 @@ _testmain.go
2222
*.exe
2323
*.test
2424
*.prof
25+
*.test
26+
*.out

validator.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -166,8 +166,6 @@ func (v *Validate) Struct(s interface{}) *StructErrors {
166166
func (v *Validate) structRecursive(top interface{}, current interface{}, s interface{}) *StructErrors {
167167

168168
structValue := reflect.ValueOf(s)
169-
structType := reflect.TypeOf(s)
170-
structName := structType.Name()
171169

172170
if structValue.Kind() == reflect.Ptr && !structValue.IsNil() {
173171
return v.structRecursive(top, current, structValue.Elem().Interface())
@@ -177,6 +175,9 @@ func (v *Validate) structRecursive(top interface{}, current interface{}, s inter
177175
panic("interface passed for validation is not a struct")
178176
}
179177

178+
structType := reflect.TypeOf(s)
179+
structName := structType.Name()
180+
180181
validationErrors := &StructErrors{
181182
Struct: structName,
182183
Errors: map[string]*FieldError{},
@@ -339,7 +340,7 @@ func (v *Validate) fieldWithNameAndValue(val interface{}, current interface{}, f
339340
func (v *Validate) fieldWithNameAndSingleTag(val interface{}, current interface{}, f interface{}, name string, valTag string) (*FieldError, error) {
340341

341342
vals := strings.Split(valTag, tagKeySeparator)
342-
key := strings.Trim(vals[0], " ")
343+
key := strings.TrimSpace(vals[0])
343344

344345
if len(key) == 0 {
345346
panic(fmt.Sprintf("Invalid validation tag on field %s", name))
@@ -364,7 +365,7 @@ func (v *Validate) fieldWithNameAndSingleTag(val interface{}, current interface{
364365

365366
param := ""
366367
if len(vals) > 1 {
367-
param = strings.Trim(vals[1], " ")
368+
param = strings.TrimSpace(vals[1])
368369
}
369370

370371
if err := valFunc(val, current, f, param); !err {

validator_test.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,14 @@ import (
1313
// - Run "go test" to run tests
1414
// - Run "gocov test | gocov report" to report on test converage by file
1515
// - Run "gocov test | gocov annotate -" to report on all code and functions, those ,marked with "MISS" were never called
16+
//
17+
//
18+
// go test -cpuprofile cpu.out
19+
// ./validator.test -test.bench=. -test.cpuprofile=cpu.prof
20+
// go tool pprof validator.test cpu.prof
21+
//
22+
//
23+
// go test -memprofile mem.out
1624

1725
type I interface {
1826
Foo() string
@@ -2310,3 +2318,25 @@ func TestInvalidValidatorFunction(t *testing.T) {
23102318

23112319
PanicMatches(t, func() { validate.Field(s.Test, "zzxxBadFunction") }, fmt.Sprintf("Undefined validation function on field %s", ""))
23122320
}
2321+
2322+
func BenchmarkValidateField(b *testing.B) {
2323+
for n := 0; n < b.N; n++ {
2324+
validate.Field("1", "len=1")
2325+
}
2326+
}
2327+
2328+
func BenchmarkValidateStruct(b *testing.B) {
2329+
type Test struct {
2330+
StringVal string `bson:"required,lt=10"`
2331+
Int64Val int64 `bson:"gt=0,lt=10"`
2332+
}
2333+
2334+
t := &Test{
2335+
StringVal: "test",
2336+
Int64Val: 5,
2337+
}
2338+
2339+
for n := 0; n < b.N; n++ {
2340+
validate.Struct(t)
2341+
}
2342+
}

0 commit comments

Comments
 (0)