|
| 1 | +package benchmarks |
| 2 | + |
| 3 | +import ( |
| 4 | + "net/url" |
| 5 | + "testing" |
| 6 | + |
| 7 | + ajg "github.com/ajg/form" |
| 8 | +) |
| 9 | + |
| 10 | +// Simple Benchmarks |
| 11 | + |
| 12 | +func BenchmarkSimpleUserDecodeStructAGJForm(b *testing.B) { |
| 13 | + |
| 14 | + values := getUserStructValues() |
| 15 | + |
| 16 | + b.ReportAllocs() |
| 17 | + for n := 0; n < b.N; n++ { |
| 18 | + var test User |
| 19 | + if err := ajg.DecodeValues(&test, values); err != nil { |
| 20 | + b.Error(err) |
| 21 | + } |
| 22 | + } |
| 23 | +} |
| 24 | + |
| 25 | +func BenchmarkSimpleUserDecodeStructParallelAGJFrom(b *testing.B) { |
| 26 | + |
| 27 | + values := getUserStructValues() |
| 28 | + |
| 29 | + b.ReportAllocs() |
| 30 | + b.RunParallel(func(pb *testing.PB) { |
| 31 | + for pb.Next() { |
| 32 | + var test User |
| 33 | + if err := ajg.DecodeValues(&test, values); err != nil { |
| 34 | + b.Error(err) |
| 35 | + } |
| 36 | + } |
| 37 | + }) |
| 38 | +} |
| 39 | + |
| 40 | +func BenchmarkSimpleUserEncodeStructAGJForm(b *testing.B) { |
| 41 | + |
| 42 | + test := getUserStruct() |
| 43 | + |
| 44 | + b.ReportAllocs() |
| 45 | + for n := 0; n < b.N; n++ { |
| 46 | + if _, err := ajg.EncodeToValues(&test); err != nil { |
| 47 | + b.Error(err) |
| 48 | + } |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +func BenchmarkSimpleUserEncodeStructParallelAGJForm(b *testing.B) { |
| 53 | + |
| 54 | + test := getUserStruct() |
| 55 | + |
| 56 | + b.ReportAllocs() |
| 57 | + b.RunParallel(func(pb *testing.PB) { |
| 58 | + for pb.Next() { |
| 59 | + if _, err := ajg.EncodeToValues(&test); err != nil { |
| 60 | + b.Error(err) |
| 61 | + } |
| 62 | + } |
| 63 | + }) |
| 64 | +} |
| 65 | + |
| 66 | +// Primitives ALL types |
| 67 | + |
| 68 | +func BenchmarkPrimitivesDecodeStructAllPrimitivesTypesAGJForm(b *testing.B) { |
| 69 | + values := getPrimitivesStructValues() |
| 70 | + |
| 71 | + b.ReportAllocs() |
| 72 | + for n := 0; n < b.N; n++ { |
| 73 | + var test PrimitivesStruct |
| 74 | + if err := ajg.DecodeValues(&test, values); err != nil { |
| 75 | + b.Error(err) |
| 76 | + } |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +func BenchmarkPrimitivesDecodeStructAllPrimitivesTypesParallelAGJForm(b *testing.B) { |
| 81 | + values := getPrimitivesStructValues() |
| 82 | + |
| 83 | + b.ReportAllocs() |
| 84 | + b.RunParallel(func(pb *testing.PB) { |
| 85 | + for pb.Next() { |
| 86 | + var test PrimitivesStruct |
| 87 | + if err := ajg.DecodeValues(&test, values); err != nil { |
| 88 | + b.Error(err) |
| 89 | + } |
| 90 | + } |
| 91 | + }) |
| 92 | +} |
| 93 | + |
| 94 | +func BenchmarkPrimitivesEncodeStructAllPrimitivesTypesAGJForm(b *testing.B) { |
| 95 | + test := getPrimitivesStruct() |
| 96 | + |
| 97 | + b.ReportAllocs() |
| 98 | + for n := 0; n < b.N; n++ { |
| 99 | + if _, err := ajg.EncodeToValues(&test); err != nil { |
| 100 | + b.Error(err) |
| 101 | + } |
| 102 | + } |
| 103 | +} |
| 104 | + |
| 105 | +func BenchmarkPrimitivesEncodeStructAllPrimitivesTypesParallelAGJForm(b *testing.B) { |
| 106 | + test := getPrimitivesStruct() |
| 107 | + |
| 108 | + b.ReportAllocs() |
| 109 | + b.RunParallel(func(pb *testing.PB) { |
| 110 | + for pb.Next() { |
| 111 | + if _, err := ajg.EncodeToValues(&test); err != nil { |
| 112 | + b.Error(err) |
| 113 | + } |
| 114 | + } |
| 115 | + }) |
| 116 | +} |
| 117 | + |
| 118 | +// Complex Array ALL types |
| 119 | + |
| 120 | +func BenchmarkComplexArrayDecodeStructAllTypesAGJForm(b *testing.B) { |
| 121 | + values := getComplexArrayStructValues() |
| 122 | + |
| 123 | + b.ReportAllocs() |
| 124 | + for n := 0; n < b.N; n++ { |
| 125 | + var test ComplexArrayStruct |
| 126 | + if err := ajg.DecodeValues(&test, values); err != nil { |
| 127 | + b.Error(err) |
| 128 | + } |
| 129 | + } |
| 130 | +} |
| 131 | + |
| 132 | +func BenchmarkComplexArrayDecodeStructAllTypesParallelAGJForm(b *testing.B) { |
| 133 | + values := getComplexArrayStructValues() |
| 134 | + |
| 135 | + b.ReportAllocs() |
| 136 | + b.RunParallel(func(pb *testing.PB) { |
| 137 | + for pb.Next() { |
| 138 | + var test ComplexArrayStruct |
| 139 | + if err := ajg.DecodeValues(&test, values); err != nil { |
| 140 | + b.Error(err) |
| 141 | + } |
| 142 | + } |
| 143 | + }) |
| 144 | +} |
| 145 | + |
| 146 | +func BenchmarkComplexArrayEncodeStructAllTypesAGJForm(b *testing.B) { |
| 147 | + test := getComplexArrayStruct() |
| 148 | + |
| 149 | + b.ReportAllocs() |
| 150 | + for n := 0; n < b.N; n++ { |
| 151 | + if _, err := ajg.EncodeToValues(&test); err != nil { |
| 152 | + b.Error(err) |
| 153 | + } |
| 154 | + } |
| 155 | +} |
| 156 | + |
| 157 | +func BenchmarkComplexArrayEncodeStructAllTypesParallelAGJForm(b *testing.B) { |
| 158 | + test := getComplexArrayStruct() |
| 159 | + |
| 160 | + b.ReportAllocs() |
| 161 | + b.RunParallel(func(pb *testing.PB) { |
| 162 | + for pb.Next() { |
| 163 | + if _, err := ajg.EncodeToValues(&test); err != nil { |
| 164 | + b.Error(err) |
| 165 | + } |
| 166 | + } |
| 167 | + }) |
| 168 | +} |
| 169 | + |
| 170 | +// Complex Map ALL types |
| 171 | + |
| 172 | +func getComplexMapStructValuesAGJForm() url.Values { |
| 173 | + return url.Values{ |
| 174 | + "String.key": []string{"value"}, |
| 175 | + "StringPtr.key": []string{"value"}, |
| 176 | + "Int.0": []string{"1"}, |
| 177 | + "IntPtr.0": []string{"1"}, |
| 178 | + "Int8.0": []string{"1"}, |
| 179 | + "Int8Ptr.0": []string{"1"}, |
| 180 | + "Int16.0": []string{"1"}, |
| 181 | + "Int16Ptr.0": []string{"1"}, |
| 182 | + "Int32.0": []string{"1"}, |
| 183 | + "Int32Ptr.0": []string{"1"}, |
| 184 | + "Int64.0": []string{"1"}, |
| 185 | + "Int64Ptr.0": []string{"1"}, |
| 186 | + "Uint.0": []string{"1"}, |
| 187 | + "UintPtr.0": []string{"1"}, |
| 188 | + "Uint8.0": []string{"1"}, |
| 189 | + "Uint8Ptr.0": []string{"1"}, |
| 190 | + "Uint16.0": []string{"1"}, |
| 191 | + "Uint16Ptr.0": []string{"1"}, |
| 192 | + "Uint32.0": []string{"1"}, |
| 193 | + "Uint32Ptr.0": []string{"1"}, |
| 194 | + "Uint64.0": []string{"1"}, |
| 195 | + "Uint64Ptr.0": []string{"1"}, |
| 196 | + "NestedInt.1.2": []string{"3"}, |
| 197 | + "NestedIntPtr.1.2": []string{"3"}, |
| 198 | + } |
| 199 | +} |
| 200 | + |
| 201 | +func BenchmarkComplexMapDecodeStructAllTypesAGJForm(b *testing.B) { |
| 202 | + values := getComplexMapStructValuesAGJForm() |
| 203 | + |
| 204 | + b.ReportAllocs() |
| 205 | + for n := 0; n < b.N; n++ { |
| 206 | + var test ComplexMapStruct |
| 207 | + if err := ajg.DecodeValues(&test, values); err != nil { |
| 208 | + b.Error(err) |
| 209 | + } |
| 210 | + } |
| 211 | +} |
| 212 | + |
| 213 | +func BenchmarkComplexMapDecodeStructAllTypesParallelAGJForm(b *testing.B) { |
| 214 | + values := getComplexMapStructValuesAGJForm() |
| 215 | + |
| 216 | + b.ReportAllocs() |
| 217 | + b.RunParallel(func(pb *testing.PB) { |
| 218 | + for pb.Next() { |
| 219 | + var test ComplexMapStruct |
| 220 | + if err := ajg.DecodeValues(&test, values); err != nil { |
| 221 | + b.Error(err) |
| 222 | + } |
| 223 | + } |
| 224 | + }) |
| 225 | +} |
| 226 | + |
| 227 | +func BenchmarkComplexMapEncodeStructAllTypesAGJForm(b *testing.B) { |
| 228 | + test := getComplexMapStructValuesAGJForm() |
| 229 | + |
| 230 | + b.ReportAllocs() |
| 231 | + for n := 0; n < b.N; n++ { |
| 232 | + if _, err := ajg.EncodeToValues(&test); err != nil { |
| 233 | + b.Error(err) |
| 234 | + } |
| 235 | + } |
| 236 | +} |
| 237 | + |
| 238 | +func BenchmarkComplexMapEncodeStructAllTypesParallelAGJForm(b *testing.B) { |
| 239 | + test := getComplexMapStructValuesAGJForm() |
| 240 | + |
| 241 | + b.ReportAllocs() |
| 242 | + b.RunParallel(func(pb *testing.PB) { |
| 243 | + for pb.Next() { |
| 244 | + if _, err := ajg.EncodeToValues(&test); err != nil { |
| 245 | + b.Error(err) |
| 246 | + } |
| 247 | + } |
| 248 | + }) |
| 249 | +} |
| 250 | + |
| 251 | +// NestedStruct Benchmarks |
| 252 | + |
| 253 | +func BenchmarkDecodeNestedStructAGJForm(b *testing.B) { |
| 254 | + |
| 255 | + values := getNestedStructValues() |
| 256 | + |
| 257 | + b.ReportAllocs() |
| 258 | + for n := 0; n < b.N; n++ { |
| 259 | + var test NestedStruct |
| 260 | + if err := ajg.DecodeValues(&test, values); err != nil { |
| 261 | + b.Error(err) |
| 262 | + } |
| 263 | + } |
| 264 | +} |
| 265 | + |
| 266 | +func BenchmarkDecodeNestedStructParallelAGJForm(b *testing.B) { |
| 267 | + |
| 268 | + values := getNestedStructValues() |
| 269 | + |
| 270 | + b.ReportAllocs() |
| 271 | + b.RunParallel(func(pb *testing.PB) { |
| 272 | + for pb.Next() { |
| 273 | + var test NestedStruct |
| 274 | + if err := ajg.DecodeValues(&test, values); err != nil { |
| 275 | + b.Error(err) |
| 276 | + } |
| 277 | + } |
| 278 | + }) |
| 279 | +} |
| 280 | + |
| 281 | +func BenchmarkEncodeNestedStructAGJForm(b *testing.B) { |
| 282 | + |
| 283 | + test := getNestedStruct() |
| 284 | + |
| 285 | + b.ReportAllocs() |
| 286 | + for n := 0; n < b.N; n++ { |
| 287 | + if _, err := ajg.EncodeToValues(&test); err != nil { |
| 288 | + b.Error(err) |
| 289 | + } |
| 290 | + } |
| 291 | +} |
| 292 | + |
| 293 | +func BenchmarkEncodeNestedStructParallelAGJForm(b *testing.B) { |
| 294 | + |
| 295 | + test := getNestedStruct() |
| 296 | + |
| 297 | + b.ReportAllocs() |
| 298 | + b.RunParallel(func(pb *testing.PB) { |
| 299 | + for pb.Next() { |
| 300 | + if _, err := ajg.EncodeToValues(&test); err != nil { |
| 301 | + b.Error(err) |
| 302 | + } |
| 303 | + } |
| 304 | + }) |
| 305 | +} |
0 commit comments