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

Commits on Oct 2, 2023

  1. 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
    ```
    SVilgelm committed Oct 2, 2023
    Configuration menu
    Copy the full SHA
    8c50c67 View commit details
    Browse the repository at this point in the history
  2. 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
    ```
    SVilgelm committed Oct 2, 2023
    Configuration menu
    Copy the full SHA
    c7def10 View commit details
    Browse the repository at this point in the history
  3. 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
    ```
    SVilgelm committed Oct 2, 2023
    Configuration menu
    Copy the full SHA
    980bf46 View commit details
    Browse the repository at this point in the history
  4. 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
    ```
    SVilgelm committed Oct 2, 2023
    Configuration menu
    Copy the full SHA
    537b205 View commit details
    Browse the repository at this point in the history
  5. add error test cases

    SVilgelm committed Oct 2, 2023
    Configuration menu
    Copy the full SHA
    3a60dae View commit details
    Browse the repository at this point in the history
  6. Configuration menu
    Copy the full SHA
    3408a15 View commit details
    Browse the repository at this point in the history