github.com/System-Glitch/goyave/v2@v2.10.3-0.20200819142921-51011e75d504/validation/validation_bench_test.go (about)

     1  package validation
     2  
     3  import (
     4  	"runtime"
     5  	"testing"
     6  )
     7  
     8  func setupValidationBench(b *testing.B) {
     9  	b.ReportAllocs()
    10  	runtime.GC()
    11  	b.ResetTimer()
    12  }
    13  
    14  func BenchmarkValidateWithParsing(b *testing.B) {
    15  	set := RuleSet{
    16  		"email":    {"required", "string", "between:3,125", "email"},
    17  		"password": {"required", "string", "between:6,64", "confirmed"},
    18  		"info":     {"nullable", "array:string", ">min:2"},
    19  	}
    20  	data := map[string]interface{}{
    21  		"email":                 "pedro@example.org",
    22  		"password":              "this is a strong password",
    23  		"password_confirmation": "this is a strong password",
    24  		"info":                  []string{"smart", "reliable"},
    25  	}
    26  	setupValidationBench(b)
    27  	for n := 0; n < b.N; n++ {
    28  		Validate(data, set, true, "en-US")
    29  	}
    30  }
    31  
    32  func BenchmarkValidatePreParsed(b *testing.B) {
    33  	rules := &Rules{
    34  		Fields: FieldMap{
    35  			"email": {
    36  				Rules: []*Rule{
    37  					{Name: "required"},
    38  					{Name: "string"},
    39  					{Name: "between", Params: []string{"3", "125"}},
    40  					{Name: "email"},
    41  				},
    42  			},
    43  			"password": {
    44  				Rules: []*Rule{
    45  					{Name: "required"},
    46  					{Name: "string"},
    47  					{Name: "between", Params: []string{"6", "64"}},
    48  					{Name: "confirmed"},
    49  				},
    50  			},
    51  			"info": {
    52  				Rules: []*Rule{
    53  					{Name: "nullable"},
    54  					{Name: "array", Params: []string{"string"}},
    55  					{Name: "min", Params: []string{"2"}, ArrayDimension: 1},
    56  				},
    57  			},
    58  		},
    59  	}
    60  	rules.check()
    61  	data := map[string]interface{}{
    62  		"email":                 "pedro@example.org",
    63  		"password":              "this is a strong password",
    64  		"password_confirmation": "this is a strong password",
    65  		"info":                  []string{"smart", "reliable"},
    66  	}
    67  
    68  	setupValidationBench(b)
    69  	for n := 0; n < b.N; n++ {
    70  		Validate(data, rules, true, "en-US")
    71  	}
    72  }
    73  
    74  func BenchmarkParseAndCheck(b *testing.B) {
    75  	set := RuleSet{
    76  		"email":    {"required", "string", "between:3,125", "email"},
    77  		"password": {"required", "string", "between:6,64", "confirmed"},
    78  		"info":     {"nullable", "array:string", ">min:2"},
    79  	}
    80  	setupValidationBench(b)
    81  	for n := 0; n < b.N; n++ {
    82  		set.parse()
    83  	}
    84  }
    85  
    86  func BenchmarkCheck(b *testing.B) {
    87  	rules := &Rules{
    88  		Fields: FieldMap{
    89  			"email": {
    90  				Rules: []*Rule{
    91  					{Name: "required"},
    92  					{Name: "string"},
    93  					{Name: "between", Params: []string{"3", "125"}},
    94  					{Name: "email"},
    95  				},
    96  			},
    97  			"password": {
    98  				Rules: []*Rule{
    99  					{Name: "required"},
   100  					{Name: "string"},
   101  					{Name: "between", Params: []string{"6", "64"}},
   102  					{Name: "confirmed"},
   103  				},
   104  			},
   105  			"info": {
   106  				Rules: []*Rule{
   107  					{Name: "nullable"},
   108  					{Name: "array", Params: []string{"string"}},
   109  					{Name: "min", Params: []string{"2"}, ArrayDimension: 1},
   110  				},
   111  			},
   112  		},
   113  	}
   114  	setupValidationBench(b)
   115  	for n := 0; n < b.N; n++ {
   116  		rules.check()
   117  		rules.checked = false
   118  	}
   119  }