Commit 4604150
authored
Add Benchmarks and Unit tests for parseRequestBody and improve it (#714)
* Unit tests and Benchmarks for parseRequestBody function
```shell
% go test -benchmem -bench=. -run=^Benchmark
goos: darwin
goarch: amd64
pkg: github.com/go-resty/resty/v2
cpu: Intel(R) Core(TM) i9-9880H CPU @ 2.30GHz
Benchmark_parseRequestBody_string-16 2194669 550.2 ns/op 128 B/op 3 allocs/op
Benchmark_parseRequestBody_byte-16 2260675 531.5 ns/op 128 B/op 3 allocs/op
Benchmark_parseRequestBody_reader_with_SetContentLength-16 8393974 141.4 ns/op 16 B/op 1 allocs/op
Benchmark_parseRequestBody_reader_without_SetContentLength-16 14253069 84.30 ns/op 0 B/op 0 allocs/op
Benchmark_parseRequestBody_struct-16 880549 1307 ns/op 155 B/op 5 allocs/op
Benchmark_parseRequestBody_struct_xml-16 424707 2886 ns/op 4762 B/op 13 allocs/op
Benchmark_parseRequestBody_map-16 547629 2152 ns/op 569 B/op 15 allocs/op
Benchmark_parseRequestBody_slice-16 959576 1264 ns/op 146 B/op 4 allocs/op
Benchmark_parseRequestBody_FormData-16 973964 1243 ns/op 304 B/op 14 allocs/op
Benchmark_parseRequestBody_MultiPart-16 98246 12320 ns/op 8746 B/op 131 allocs/op
```
* improve handleFormData
Benchmarks:
```
Old: Benchmark_parseRequestBody_FormData-16 954213 1266 ns/op 304 B/op 14 allocs/op
New: Benchmark_parseRequestBody_FormData-16 968466 1248 ns/op 280 B/op 10 allocs/op
```
* improve handleRequestBody
Benchmarks:
```
Old:
Benchmark_parseRequestBody_string-16 2199196 550.3 ns/op 128 B/op 3 allocs/op
Benchmark_parseRequestBody_byte-16 2264421 532.9 ns/op 128 B/op 3 allocs/op
Benchmark_parseRequestBody_reader-16 8307141 141.8 ns/op 16 B/op 1 allocs/op
Benchmark_parseRequestBody_struct-16 931632 1317 ns/op 156 B/op 5 allocs/op
Benchmark_parseRequestBody_struct_xml-16 409074 2921 ns/op 4765 B/op 13 allocs/op
Benchmark_parseRequestBody_map-16 566750 2158 ns/op 570 B/op 15 allocs/op
Benchmark_parseRequestBody_slice-16 957828 1279 ns/op 146 B/op 4 allocs/op
New:
Benchmark_parseRequestBody_string-16 5084247 237.0 ns/op 16 B/op 1 allocs/op
Benchmark_parseRequestBody_byte-16 5298362 218.0 ns/op 16 B/op 1 allocs/op
Benchmark_parseRequestBody_reader-16 8402954 141.3 ns/op 16 B/op 1 allocs/op
Benchmark_parseRequestBody_struct-16 1000000 1066 ns/op 42 B/op 3 allocs/op
Benchmark_parseRequestBody_struct_xml-16 452389 2575 ns/op 4648 B/op 11 allocs/op
Benchmark_parseRequestBody_map-16 620391 1913 ns/op 457 B/op 13 allocs/op
Benchmark_parseRequestBody_slice-16 1207551 1203 ns/op 32 B/op 2 allocs/op
```
* improve parseRequesBody and add additional benchmarks
Final benchmarks:
```shell
% go test -benchmem -bench=. -run=^Benchmark
goos: darwin
goarch: amd64
pkg: github.com/go-resty/resty/v2
cpu: Intel(R) Core(TM) i9-9880H CPU @ 2.30GHz
Benchmark_parseRequestBody_string-16 7623501 155.3 ns/op 16 B/op 1 allocs/op
Benchmark_parseRequestBody_byte-16 8421992 141.6 ns/op 16 B/op 1 allocs/op
Benchmark_parseRequestBody_reader_with_SetContentLength-16 17632350 67.37 ns/op 16 B/op 1 allocs/op
Benchmark_parseRequestBody_reader_without_SetContentLength-16 26575016 45.34 ns/op 0 B/op 0 allocs/op
Benchmark_parseRequestBody_struct-16 1243986 953.8 ns/op 40 B/op 2 allocs/op
Benchmark_parseRequestBody_struct_xml-16 495250 2458 ns/op 4647 B/op 10 allocs/op
Benchmark_parseRequestBody_map-16 694786 1761 ns/op 454 B/op 12 allocs/op
Benchmark_parseRequestBody_slice-16 1304724 913.1 ns/op 32 B/op 2 allocs/op
Benchmark_parseRequestBody_FormData-16 1000000 1128 ns/op 272 B/op 9 allocs/op
Benchmark_parseRequestBody_MultiPart-16 93248 12583 ns/op 8738 B/op 130 allocs/op
```
* add error test cases
* use r.FormData inatead of creating new variable1 parent 6310546 commit 4604150
2 files changed
+681
-83
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
15 | 15 | | |
16 | 16 | | |
17 | 17 | | |
| 18 | + | |
18 | 19 | | |
19 | 20 | | |
20 | 21 | | |
| |||
134 | 135 | | |
135 | 136 | | |
136 | 137 | | |
137 | | - | |
| 138 | + | |
138 | 139 | | |
139 | | - | |
140 | | - | |
141 | | - | |
142 | | - | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
143 | 144 | | |
144 | | - | |
145 | | - | |
146 | | - | |
147 | | - | |
148 | | - | |
149 | | - | |
| 145 | + | |
150 | 146 | | |
151 | | - | |
152 | | - | |
153 | | - | |
154 | | - | |
155 | | - | |
156 | | - | |
| 147 | + | |
157 | 148 | | |
158 | 149 | | |
159 | | - | |
160 | | - | |
| 150 | + | |
| 151 | + | |
161 | 152 | | |
162 | 153 | | |
163 | 154 | | |
164 | 155 | | |
165 | | - | |
166 | 156 | | |
167 | 157 | | |
168 | 158 | | |
169 | 159 | | |
170 | 160 | | |
171 | | - | |
| 161 | + | |
172 | 162 | | |
173 | 163 | | |
174 | 164 | | |
175 | | - | |
| 165 | + | |
176 | 166 | | |
177 | 167 | | |
178 | 168 | | |
| |||
370 | 360 | | |
371 | 361 | | |
372 | 362 | | |
373 | | - | |
| 363 | + | |
374 | 364 | | |
375 | 365 | | |
376 | 366 | | |
377 | 367 | | |
378 | 368 | | |
379 | | - | |
| 369 | + | |
380 | 370 | | |
381 | 371 | | |
382 | 372 | | |
| |||
385 | 375 | | |
386 | 376 | | |
387 | 377 | | |
388 | | - | |
389 | | - | |
390 | | - | |
| 378 | + | |
| 379 | + | |
391 | 380 | | |
392 | 381 | | |
393 | | - | |
| 382 | + | |
394 | 383 | | |
395 | 384 | | |
396 | 385 | | |
397 | 386 | | |
398 | 387 | | |
399 | 388 | | |
400 | 389 | | |
401 | | - | |
402 | | - | |
403 | | - | |
404 | | - | |
405 | | - | |
406 | | - | |
| 390 | + | |
| 391 | + | |
| 392 | + | |
407 | 393 | | |
408 | 394 | | |
409 | 395 | | |
410 | 396 | | |
411 | | - | |
412 | | - | |
413 | | - | |
414 | | - | |
415 | | - | |
| 397 | + | |
| 398 | + | |
| 399 | + | |
416 | 400 | | |
417 | 401 | | |
418 | 402 | | |
419 | 403 | | |
420 | | - | |
421 | | - | |
422 | | - | |
| 404 | + | |
423 | 405 | | |
424 | 406 | | |
425 | 407 | | |
426 | | - | |
427 | | - | |
428 | 408 | | |
429 | | - | |
430 | | - | |
431 | | - | |
432 | | - | |
433 | | - | |
434 | | - | |
435 | | - | |
436 | | - | |
437 | | - | |
438 | | - | |
439 | | - | |
440 | | - | |
| 409 | + | |
| 410 | + | |
441 | 411 | | |
| 412 | + | |
442 | 413 | | |
443 | 414 | | |
444 | | - | |
| 415 | + | |
| 416 | + | |
445 | 417 | | |
446 | 418 | | |
447 | 419 | | |
| |||
454 | 426 | | |
455 | 427 | | |
456 | 428 | | |
457 | | - | |
| 429 | + | |
458 | 430 | | |
459 | | - | |
460 | | - | |
| 431 | + | |
461 | 432 | | |
462 | 433 | | |
463 | | - | |
| 434 | + | |
| 435 | + | |
464 | 436 | | |
465 | 437 | | |
466 | | - | |
| 438 | + | |
| 439 | + | |
| 440 | + | |
467 | 441 | | |
468 | 442 | | |
469 | 443 | | |
470 | | - | |
471 | | - | |
472 | | - | |
473 | | - | |
474 | | - | |
475 | | - | |
476 | | - | |
477 | | - | |
478 | | - | |
479 | | - | |
480 | | - | |
| 444 | + | |
| 445 | + | |
| 446 | + | |
| 447 | + | |
| 448 | + | |
| 449 | + | |
| 450 | + | |
| 451 | + | |
| 452 | + | |
| 453 | + | |
| 454 | + | |
| 455 | + | |
| 456 | + | |
| 457 | + | |
481 | 458 | | |
482 | | - | |
483 | | - | |
484 | 459 | | |
485 | | - | |
| 460 | + | |
486 | 461 | | |
487 | 462 | | |
488 | 463 | | |
489 | 464 | | |
490 | | - | |
491 | | - | |
492 | | - | |
493 | | - | |
494 | | - | |
495 | | - | |
| 465 | + | |
496 | 466 | | |
497 | 467 | | |
498 | 468 | | |
| |||
501 | 471 | | |
502 | 472 | | |
503 | 473 | | |
504 | | - | |
| 474 | + | |
505 | 475 | | |
506 | 476 | | |
507 | 477 | | |
| |||
0 commit comments