Skip to content

Commit

Permalink
break out benchmarks into separate file
Browse files Browse the repository at this point in the history
create examples file for better godocs
  • Loading branch information
joeybloggs authored and joeybloggs committed Jun 11, 2015
1 parent df95f9d commit 9ff11ae
Show file tree
Hide file tree
Showing 4 changed files with 259 additions and 161 deletions.
163 changes: 163 additions & 0 deletions benchmarks_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
package validator

import "testing"

func BenchmarkValidateField(b *testing.B) {
for n := 0; n < b.N; n++ {
validate.Field("1", "len=1")
}
}

func BenchmarkValidateStructSimple(b *testing.B) {

type Foo struct {
StringValue string `validate:"min=5,max=10"`
IntValue int `validate:"min=5,max=10"`
}

validFoo := &Foo{StringValue: "Foobar", IntValue: 7}
invalidFoo := &Foo{StringValue: "Fo", IntValue: 3}

for n := 0; n < b.N; n++ {
validate.Struct(validFoo)
validate.Struct(invalidFoo)
}
}

// func BenchmarkTemplateParallelSimple(b *testing.B) {

// type Foo struct {
// StringValue string `validate:"min=5,max=10"`
// IntValue int `validate:"min=5,max=10"`
// }

// 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 BenchmarkValidateStructLarge(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",
},
}

tSuccess := &TestString{
Required: "Required",
Len: "length==10",
Min: "min=1",
Max: "1234567890",
MinMax: "12345",
Lt: "012345678",
Lte: "0123456789",
Gt: "01234567890",
Gte: "0123456789",
OmitEmpty: "",
Sub: &SubTest{
Test: "1",
},
SubIgnore: &SubTest{
Test: "",
},
Anonymous: struct {
A string `validate:"required"`
}{
A: "1",
},
Iface: &Impl{
F: "123",
},
}

for n := 0; n < b.N; n++ {
validate.Struct(tSuccess)
validate.Struct(tFail)
}
}

// func BenchmarkTemplateParallelLarge(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",
// },
// }

// tSuccess := &TestString{
// Required: "Required",
// Len: "length==10",
// Min: "min=1",
// Max: "1234567890",
// MinMax: "12345",
// Lt: "012345678",
// Lte: "0123456789",
// Gt: "01234567890",
// Gte: "0123456789",
// OmitEmpty: "",
// Sub: &SubTest{
// Test: "1",
// },
// SubIgnore: &SubTest{
// Test: "",
// },
// Anonymous: struct {
// A string `validate:"required"`
// }{
// A: "1",
// },
// Iface: &Impl{
// F: "123",
// },
// }

// b.RunParallel(func(pb *testing.PB) {
// for pb.Next() {
// validate.Struct(tSuccess)
// validate.Struct(tFail)
// }
// })
// }
95 changes: 95 additions & 0 deletions examples_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package validator_test

import (
"fmt"

"../validator"
)

func ExampleValidate_new() {
validator.New("validate", validator.BakedInValidators)
}

func ExampleValidate_addFunction() {
// This should be stored somewhere globally
var validate *validator.Validate

validate = validator.New("validate", validator.BakedInValidators)

fn := func(top interface{}, current interface{}, field interface{}, param string) bool {
return field.(string) == "hello"
}

validate.AddFunction("valueishello", fn)

message := "hello"
err := validate.Field(message, "valueishello")
fmt.Println(err)
//Output:
//<nil>
}

func ExampleValidate_field() {
// This should be stored somewhere globally
var validate *validator.Validate

validate = validator.New("validate", validator.BakedInValidators)

i := 0
err := validate.Field(i, "gt=1,lte=10")
fmt.Println(err.Field)
fmt.Println(err.Tag)
fmt.Println(err.Kind) // NOTE: Kind and Type can be different i.e. time Kind=struct and Type=time.Time
fmt.Println(err.Type)
fmt.Println(err.Param)
fmt.Println(err.Value)
//Output:
//
//gt
//int
//int
//1
//0
}

func ExampleValidate_struct() {
// This should be stored somewhere globally
var validate *validator.Validate

validate = validator.New("validate", validator.BakedInValidators)

type ContactInformation struct {
Phone string `validate:"required"`
Street string `validate:"required"`
City string `validate:"required"`
}

type User struct {
Name string `validate:"required,excludesall=!@#$%^&*()_+-=:;?/0x2C"` // 0x2C = comma (,)
Age int8 `validate:"required,gt=0,lt=150"`
Email string `validate:"email"`
ContactInformation []*ContactInformation
}

contactInfo := &ContactInformation{
Street: "26 Here Blvd.",
City: "Paradeso",
}

user := &User{
Name: "Joey Bloggs",
Age: 31,
Email: "joeybloggs@gmail.com",
ContactInformation: []*ContactInformation{contactInfo},
}

structError := validate.Struct(user)
for _, fieldError := range structError.Errors {
fmt.Println(fieldError.Field) // Phone
fmt.Println(fieldError.Tag) // required
//... and so forth
//Output:
//Phone
//required
}
}
2 changes: 1 addition & 1 deletion validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ func (v *Validate) SetTag(tagName string) {
v.tagName = tagName
}

// SetStructPoolMax sets the struct pools max size. this may be usefull for fine grained
// SetMaxStructPoolSize sets the struct pools max size. this may be usefull for fine grained
// performance tuning towards your application, however, the default should be fine for
// nearly all cases. only increase if you have a deeply nested struct structure.
// NOTE: this method is not thread-safe
Expand Down
Loading

0 comments on commit 9ff11ae

Please sign in to comment.