Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Benchmarks and Unit tests for parseRequestBody and improve it #714

Merged
merged 6 commits into from Oct 2, 2023

Conversation

SVilgelm
Copy link
Contributor

Improve the parseRequestBody function and all nested functions.

Original benchmarks:

% 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       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
Benchmark_parseRequestBody_FormData-16      954213     1266 ns/op     304 B/op     14 allocs/op
Benchmark_parseRequestBody_MultiPart-16      94276    12538 ns/op    8746 B/op    131 allocs/op

After the improvements:

Benchmark_parseRequestBody_string-16                              7635237    155.7 ns/op      16 B/op      1 allocs/op
Benchmark_parseRequestBody_byte-16                                8453085    140.4 ns/op      16 B/op      1 allocs/op
Benchmark_parseRequestBody_reader_with_SetContentLength-16       17494558    67.62 ns/op      16 B/op      1 allocs/op
Benchmark_parseRequestBody_reader_without_SetContentLength-16    26582679    45.11 ns/op       0 B/op      0 allocs/op
Benchmark_parseRequestBody_struct-16                              1264702    943.9 ns/op      40 B/op      2 allocs/op
Benchmark_parseRequestBody_struct_xml-16                           479361     2496 ns/op    4645 B/op     10 allocs/op
Benchmark_parseRequestBody_map-16                                  685204     1770 ns/op     454 B/op     12 allocs/op
Benchmark_parseRequestBody_slice-16                               1318749    910.2 ns/op      32 B/op      2 allocs/op
Benchmark_parseRequestBody_FormData-16                            1000000     1116 ns/op     272 B/op      9 allocs/op
Benchmark_parseRequestBody_MultiPart-16                             92143    12532 ns/op    8738 B/op    130 allocs/op

@codecov
Copy link

codecov bot commented Sep 29, 2023

Codecov Report

Attention: 2 lines in your changes are missing coverage. Please review.

Comparison is base (3face0d) 96.34% compared to head (3408a15) 96.57%.

Additional details and impacted files
@@            Coverage Diff             @@
##           master     #714      +/-   ##
==========================================
+ Coverage   96.34%   96.57%   +0.22%     
==========================================
  Files          12       12              
  Lines        1616     1607       -9     
==========================================
- Hits         1557     1552       -5     
+ Misses         37       35       -2     
+ Partials       22       20       -2     
Files Coverage Δ
middleware.go 94.03% <96.00%> (+1.17%) ⬆️

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@moorereason
Copy link
Contributor

Tip: use benchstat.

@SVilgelm

This comment was marked as outdated.

@SVilgelm
Copy link
Contributor Author

Tip: use benchstat.

thank you, didn't know

@moorereason
Copy link
Contributor

Also, use something like -count=10 to gather more samples. That should get rid of the benchstat footnotes.

@SVilgelm

This comment was marked as outdated.

@jeevatkm jeevatkm added this to the v2.10.0 milestone Oct 1, 2023
middleware.go Show resolved Hide resolved
middleware.go Outdated Show resolved Hide resolved
```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
```
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
```
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
```
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
```
@SVilgelm
Copy link
Contributor Author

SVilgelm commented Oct 2, 2023

New benchmarks, also no significant changes:

goos: darwin
goarch: amd64
pkg: github.com/go-resty/resty/v2
cpu: Intel(R) Core(TM) i9-9880H CPU @ 2.30GHz
                                                     │    old.txt    │               new.txt               │
                                                     │    sec/op     │   sec/op     vs base                │
_parseRequestHeader-16                                  171.2n ±  3%   173.7n ± 6%   +1.46% (p=0.009 n=10)
_parseRequestBody_string-16                             542.7n ±  2%   183.3n ± 1%  -66.22% (p=0.000 n=10)
_parseRequestBody_byte-16                               536.8n ±  1%   176.3n ± 1%  -67.15% (p=0.000 n=10)
_parseRequestBody_reader_with_SetContentLength-16      161.15n ±  0%   87.97n ± 1%  -45.41% (p=0.000 n=10)
_parseRequestBody_reader_without_SetContentLength-16    89.63n ±  1%   50.53n ± 0%  -43.62% (p=0.000 n=10)
_parseRequestBody_struct-16                            1250.0n ± 10%   863.2n ± 0%  -30.94% (p=0.000 n=10)
_parseRequestBody_struct_xml-16                         2.854µ ±  3%   2.534µ ± 3%  -11.23% (p=0.000 n=10)
_parseRequestBody_map-16                                1.842µ ±  2%   1.451µ ± 0%  -21.21% (p=0.000 n=10)
_parseRequestBody_slice-16                             1166.5n ±  1%   834.8n ± 0%  -28.44% (p=0.000 n=10)
_parseRequestBody_FormData-16                           1.288µ ±  1%   1.003µ ± 1%  -22.17% (p=0.000 n=10)
_parseRequestBody_MultiPart-16                          14.65µ ±  1%   14.62µ ± 1%        ~ (p=0.183 n=10)
geomean                                                 814.1n         532.1n       -34.64%

                                                     │    old.txt     │                new.txt                 │
                                                     │      B/op      │     B/op      vs base                  │
_parseRequestHeader-16                                   0.000 ± 0%       0.000 ± 0%        ~ (p=1.000 n=10) ¹
_parseRequestBody_string-16                             128.00 ± 0%       16.00 ± 0%  -87.50% (p=0.000 n=10)
_parseRequestBody_byte-16                               128.00 ± 0%       16.00 ± 0%  -87.50% (p=0.000 n=10)
_parseRequestBody_reader_with_SetContentLength-16        16.00 ± 0%       16.00 ± 0%        ~ (p=1.000 n=10) ¹
_parseRequestBody_reader_without_SetContentLength-16     0.000 ± 0%       0.000 ± 0%        ~ (p=1.000 n=10) ¹
_parseRequestBody_struct-16                             155.00 ± 0%       40.00 ± 0%  -74.19% (p=0.000 n=10)
_parseRequestBody_struct_xml-16                        4.649Ki ± 0%     4.534Ki ± 0%   -2.48% (p=0.000 n=10)
_parseRequestBody_map-16                                 487.0 ± 0%       372.0 ± 0%  -23.61% (p=0.000 n=10)
_parseRequestBody_slice-16                              145.00 ± 0%       32.00 ± 0%  -77.93% (p=0.000 n=10)
_parseRequestBody_FormData-16                            304.0 ± 0%       272.0 ± 0%  -10.53% (p=0.000 n=10)
_parseRequestBody_MultiPart-16                         9.919Ki ± 0%     9.911Ki ± 0%   -0.08% (p=0.000 n=10)
geomean                                                             ²                 -49.11%                ²
¹ all samples are equal
² summaries must be >0 to compute geomean

                                                     │    old.txt    │               new.txt                │
                                                     │   allocs/op   │ allocs/op   vs base                  │
_parseRequestHeader-16                                  0.000 ± 0%     0.000 ± 0%        ~ (p=1.000 n=10) ¹
_parseRequestBody_string-16                             3.000 ± 0%     1.000 ± 0%  -66.67% (p=0.000 n=10)
_parseRequestBody_byte-16                               3.000 ± 0%     1.000 ± 0%  -66.67% (p=0.000 n=10)
_parseRequestBody_reader_with_SetContentLength-16       1.000 ± 0%     1.000 ± 0%        ~ (p=1.000 n=10) ¹
_parseRequestBody_reader_without_SetContentLength-16    0.000 ± 0%     0.000 ± 0%        ~ (p=1.000 n=10) ¹
_parseRequestBody_struct-16                             5.000 ± 0%     2.000 ± 0%  -60.00% (p=0.000 n=10)
_parseRequestBody_struct_xml-16                         13.00 ± 0%     10.00 ± 0%  -23.08% (p=0.000 n=10)
_parseRequestBody_map-16                                13.00 ± 0%     10.00 ± 0%  -23.08% (p=0.000 n=10)
_parseRequestBody_slice-16                              4.000 ± 0%     2.000 ± 0%  -50.00% (p=0.000 n=10)
_parseRequestBody_FormData-16                          14.000 ± 0%     9.000 ± 0%  -35.71% (p=0.000 n=10)
_parseRequestBody_MultiPart-16                          133.0 ± 0%     132.0 ± 0%   -0.75% (p=0.000 n=10)
geomean                                                            ²               -35.25%                ²
¹ all samples are equal
² summaries must be >0 to compute geomean

Copy link
Member

@jeevatkm jeevatkm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@SVilgelm Thank you!

@jeevatkm jeevatkm merged commit 4604150 into go-resty:master Oct 2, 2023
3 checks passed
@SVilgelm SVilgelm deleted the parseRequestBody branch October 2, 2023 03:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

Successfully merging this pull request may close these issues.

None yet

3 participants