go-resty / resty

Simple HTTP and REST client library for Go
MIT License
9.89k stars 696 forks source link

Benchmarks and Improvements for parseRequestURL function #711

Closed SVilgelm closed 11 months ago

SVilgelm commented 11 months ago

The benchmarks for the applying PathParams and adding QueryParams.

Original results:

% 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_parseRequestURL_PathParams-16    524658    2260 ns/op    448 B/op     9 allocs/op
Benchmark_parseRequestURL_QueryParams-16   865923    1371 ns/op    416 B/op    13 allocs/op

After the improvements:

% 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_parseRequestURL_PathParams-16     753834    1367 ns/op    256 B/op    5 allocs/op
Benchmark_parseRequestURL_QueryParams-16   1000000    1167 ns/op    352 B/op    9 allocs/op

The applying of PathParams has been improved by using O(1) logic instead of O(N). (N the number of PathParams + RawPathParams in both client and request). Instead of calling strings.Replace for each path parameter, the current logic collects all needed parameters in a map then replaces all parameters in URL by searching for the curly brackets and replacing. It scans the whole URL just once, from first to last positions.

The improvements of adding the QueryParams have been done by swapping the processing order. First - process the request's QueryParams, only then process the client's QueryParams and skip already existed instead of deleting.

codecov[bot] commented 11 months ago

Codecov Report

All modified lines are covered by tests :white_check_mark:

Comparison is base (4604150) 96.57% compared to head (402986b) 96.62%.

Additional details and impacted files ```diff @@ Coverage Diff @@ ## master #711 +/- ## ========================================== + Coverage 96.57% 96.62% +0.05% ========================================== Files 12 12 Lines 1607 1632 +25 ========================================== + Hits 1552 1577 +25 Misses 35 35 Partials 20 20 ``` | [Files](https://app.codecov.io/gh/go-resty/resty/pull/711?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=go-resty) | Coverage Δ | | |---|---|---| | [middleware.go](https://app.codecov.io/gh/go-resty/resty/pull/711?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=go-resty#diff-bWlkZGxld2FyZS5nbw==) | `94.51% <100.00%> (+0.48%)` | :arrow_up: |

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.

SVilgelm commented 11 months ago

@jeevatkm I hope this improvement will help the users. The change is relatively huge, it would be great if you can run some additional tests, or I can add additional tests

jeevatkm commented 11 months ago

@jeevatkm I hope this improvement will help the users. The change is relatively huge, it would be great if you can run some additional tests, or I can add additional tests

Thanks, @SVilgelm; I agree this improvement needs good testing. I will let you know!

SVilgelm commented 11 months ago

rebased

SVilgelm commented 11 months ago
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            190.2n ± 3%   191.3n ± 2%        ~ (p=0.306 n=10)
_parseRequestURL_PathParams-16    1.570µ ± 1%   1.290µ ± 1%  -17.83% (p=0.000 n=10)
_parseRequestURL_QueryParams-16   1.347µ ± 1%   1.109µ ± 1%  -17.68% (p=0.000 n=10)
geomean                           738.1n        649.2n       -12.04%

                                │   old.txt    │               new.txt                │
                                │     B/op     │    B/op     vs base                  │
_parseRequestHeader-16            0.000 ± 0%     0.000 ± 0%        ~ (p=1.000 n=10) ¹
_parseRequestURL_PathParams-16    448.0 ± 0%     256.0 ± 0%  -42.86% (p=0.000 n=10)
_parseRequestURL_QueryParams-16   416.0 ± 0%     352.0 ± 0%  -15.38% (p=0.000 n=10)
geomean                                      ²               -21.51%                ²
¹ 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) ¹
_parseRequestURL_PathParams-16     9.000 ± 0%     5.000 ± 0%  -44.44% (p=0.000 n=10)
_parseRequestURL_QueryParams-16   13.000 ± 0%     9.000 ± 0%  -30.77% (p=0.000 n=10)
geomean                                       ²               -27.28%                ²
¹ all samples are equal
² summaries must be >0 to compute geomean
SVilgelm commented 11 months ago

sure, I'll check later today, biking 🚴 around

jeevatkm commented 11 months ago

sure, I'll check later today, biking 🚴 around

Thanks, take your time 👍 safe biking!

SVilgelm commented 11 months ago

New benchmarks, almost no 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             189.1n ± 3%   188.7n ± 2%        ~ (p=0.361 n=10)
_parseRequestURL_PathParams-16     1.564µ ± 0%   1.304µ ± 1%  -16.62% (p=0.000 n=10)
_parseRequestURL_QueryParams-16   1347.5n ± 1%   971.0n ± 2%  -27.94% (p=0.000 n=10)
geomean                            735.8n        620.5n       -15.68%

                                │   old.txt    │               new.txt                │
                                │     B/op     │    B/op     vs base                  │
_parseRequestHeader-16            0.000 ± 0%     0.000 ± 0%        ~ (p=1.000 n=10) ¹
_parseRequestURL_PathParams-16    448.0 ± 0%     256.0 ± 0%  -42.86% (p=0.000 n=10)
_parseRequestURL_QueryParams-16   416.0 ± 0%     352.0 ± 0%  -15.38% (p=0.000 n=10)
geomean                                      ²               -21.51%                ²
¹ 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) ¹
_parseRequestURL_PathParams-16     9.000 ± 0%     5.000 ± 0%  -44.44% (p=0.000 n=10)
_parseRequestURL_QueryParams-16   13.000 ± 0%     9.000 ± 0%  -30.77% (p=0.000 n=10)
geomean                                       ²               -27.28%                ²
¹ all samples are equal
² summaries must be >0 to compute geomean
SVilgelm commented 11 months ago
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                │
_parseRequestURL_PathParams-16     1.570µ ± 1%   1.269µ ± 1%  -19.15% (p=0.000 n=10)
_parseRequestURL_QueryParams-16   1343.5n ± 2%   999.3n ± 4%  -25.62% (p=0.000 n=10)
geomean                            1.452µ        1.126µ       -22.45%

                                │  old.txt   │              new.txt               │
                                │    B/op    │    B/op     vs base                │
_parseRequestURL_PathParams-16    448.0 ± 0%   256.0 ± 0%  -42.86% (p=0.000 n=10)
_parseRequestURL_QueryParams-16   416.0 ± 0%   352.0 ± 0%  -15.38% (p=0.000 n=10)
geomean                           431.7        300.2       -30.46%

                                │   old.txt   │              new.txt               │
                                │  allocs/op  │ allocs/op   vs base                │
_parseRequestURL_PathParams-16     9.000 ± 0%   5.000 ± 0%  -44.44% (p=0.000 n=10)
_parseRequestURL_QueryParams-16   13.000 ± 0%   9.000 ± 0%  -30.77% (p=0.000 n=10)
geomean                            10.82        6.708       -37.98%