github.com/vkolev/form@v1.2.1/benchmarks/formam_test.go (about)

     1  package benchmarks
     2  
     3  import (
     4  	"net/url"
     5  	"testing"
     6  
     7  	"github.com/monoculum/formam"
     8  )
     9  
    10  // Simple Benchmarks
    11  
    12  func BenchmarkSimpleUserStructFormam(b *testing.B) {
    13  
    14  	values := getUserStructValues()
    15  	var err error
    16  	b.ReportAllocs()
    17  	for n := 0; n < b.N; n++ {
    18  		var test User
    19  		if err = formam.Decode(values, &test); err != nil {
    20  			b.Error(err)
    21  		}
    22  	}
    23  }
    24  
    25  func BenchmarkSimpleUserStructFormamParallel(b *testing.B) {
    26  
    27  	values := getUserStructValues()
    28  	var err error
    29  
    30  	b.ReportAllocs()
    31  	b.RunParallel(func(pb *testing.PB) {
    32  		for pb.Next() {
    33  			var test User
    34  			if err = formam.Decode(values, &test); err != nil {
    35  				b.Error(err)
    36  			}
    37  		}
    38  	})
    39  }
    40  
    41  // Primitives ALL types
    42  
    43  func BenchmarkPrimitivesStructAllPrimitivesFormamTypes(b *testing.B) {
    44  	values := getPrimitivesStructValues()
    45  	var err error
    46  
    47  	b.ReportAllocs()
    48  	for n := 0; n < b.N; n++ {
    49  		var test PrimitivesStruct
    50  		if err = formam.Decode(values, &test); err != nil {
    51  			b.Error(err)
    52  		}
    53  	}
    54  }
    55  
    56  func BenchmarkPrimitivesStructAllPrimitivesTypesFormamParallel(b *testing.B) {
    57  	values := getPrimitivesStructValues()
    58  	var err error
    59  
    60  	b.ReportAllocs()
    61  	b.RunParallel(func(pb *testing.PB) {
    62  		for pb.Next() {
    63  			var test PrimitivesStruct
    64  			if err = formam.Decode(values, &test); err != nil {
    65  				b.Error(err)
    66  			}
    67  		}
    68  	})
    69  }
    70  
    71  // Complex Array ALL types
    72  
    73  func BenchmarkComplexArrayStructAllTypesFormam(b *testing.B) {
    74  	values := getComplexArrayStructValues()
    75  	var err error
    76  
    77  	b.ReportAllocs()
    78  	for n := 0; n < b.N; n++ {
    79  		var test ComplexArrayStruct
    80  		if err = formam.Decode(values, &test); err != nil {
    81  			b.Error(err)
    82  		}
    83  	}
    84  }
    85  
    86  func BenchmarkComplexArrayStructAllTypesFormamParallel(b *testing.B) {
    87  	values := getComplexArrayStructValues()
    88  	var err error
    89  
    90  	b.ReportAllocs()
    91  	b.RunParallel(func(pb *testing.PB) {
    92  		for pb.Next() {
    93  			var test ComplexArrayStruct
    94  			if err = formam.Decode(values, &test); err != nil {
    95  				b.Error(err)
    96  			}
    97  		}
    98  	})
    99  }
   100  
   101  // Complex Map ALL types
   102  
   103  func getComplexMapStructValuesFormam() url.Values {
   104  	return url.Values{
   105  		"String.key":       []string{"value"},
   106  		"StringPtr.key":    []string{"value"},
   107  		"Int.0":            []string{"1"},
   108  		"IntPtr.0":         []string{"1"},
   109  		"Int8.0":           []string{"1"},
   110  		"Int8Ptr.0":        []string{"1"},
   111  		"Int16.0":          []string{"1"},
   112  		"Int16Ptr.0":       []string{"1"},
   113  		"Int32.0":          []string{"1"},
   114  		"Int32Ptr.0":       []string{"1"},
   115  		"Int64.0":          []string{"1"},
   116  		"Int64Ptr.0":       []string{"1"},
   117  		"Uint.0":           []string{"1"},
   118  		"UintPtr.0":        []string{"1"},
   119  		"Uint8.0":          []string{"1"},
   120  		"Uint8Ptr.0":       []string{"1"},
   121  		"Uint16.0":         []string{"1"},
   122  		"Uint16Ptr.0":      []string{"1"},
   123  		"Uint32.0":         []string{"1"},
   124  		"Uint32Ptr.0":      []string{"1"},
   125  		"Uint64.0":         []string{"1"},
   126  		"Uint64Ptr.0":      []string{"1"},
   127  		"NestedInt.1.2":    []string{"3"},
   128  		"NestedIntPtr.1.2": []string{"3"},
   129  	}
   130  }
   131  
   132  func BenchmarkComplexMapStructAllTypesFormam(b *testing.B) {
   133  	// b.Log("Formam only supports map key of string at this time")
   134  	// b.SkipNow()
   135  	values := getComplexMapStructValuesFormam()
   136  	var err error
   137  
   138  	b.ReportAllocs()
   139  	for n := 0; n < b.N; n++ {
   140  		var test ComplexMapStruct
   141  		if err = formam.Decode(values, &test); err != nil {
   142  			b.Error(err)
   143  		}
   144  	}
   145  }
   146  
   147  func BenchmarkComplexMapStructAllTypesFormamParallel(b *testing.B) {
   148  	// b.Log("Formam only supports map key of string at this time")
   149  	// b.SkipNow()
   150  	values := getComplexMapStructValuesFormam()
   151  	var err error
   152  
   153  	b.ReportAllocs()
   154  	b.RunParallel(func(pb *testing.PB) {
   155  		for pb.Next() {
   156  			var test ComplexMapStruct
   157  			if err = formam.Decode(values, &test); err != nil {
   158  				b.Error(err)
   159  			}
   160  		}
   161  	})
   162  }
   163  
   164  // NestedStruct Benchmarks
   165  
   166  func BenchmarkArrayMapNestedStructFormam(b *testing.B) {
   167  
   168  	values := getNestedStructValues()
   169  	var err error
   170  	b.ReportAllocs()
   171  	for n := 0; n < b.N; n++ {
   172  		var test NestedStruct
   173  		if err = formam.Decode(values, &test); err != nil {
   174  			b.Error(err)
   175  		}
   176  	}
   177  }
   178  
   179  func BenchmarkArrayMapNestedStructFormamParallel(b *testing.B) {
   180  
   181  	values := getNestedStructValues()
   182  	var err error
   183  
   184  	b.ReportAllocs()
   185  	b.RunParallel(func(pb *testing.PB) {
   186  		for pb.Next() {
   187  			var test NestedStruct
   188  			if err = formam.Decode(values, &test); err != nil {
   189  				b.Error(err)
   190  			}
   191  		}
   192  	})
   193  }