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

Benchmarks and Improvements for parseRequestURL function #711

Merged
merged 8 commits into from
Oct 2, 2023

Conversation

SVilgelm
Copy link
Contributor

@SVilgelm SVilgelm commented Sep 25, 2023

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
Copy link

codecov bot commented Sep 25, 2023

Codecov Report

All modified lines are covered by tests ✅

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

Additional details and impacted files
@@            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 Coverage Δ
middleware.go 94.51% <100.00%> (+0.48%) ⬆️

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

@SVilgelm
Copy link
Contributor Author

@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

@SVilgelm SVilgelm force-pushed the bench-parseRequestURL branch 3 times, most recently from 2b10e62 to 6674c2f Compare September 25, 2023 17:51
@jeevatkm
Copy link
Member

@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
Copy link
Contributor Author

rebased

@SVilgelm

This comment was marked as outdated.

@SVilgelm SVilgelm force-pushed the bench-parseRequestURL branch 2 times, most recently from a57888a to f92bf9f Compare September 29, 2023 13:49
@jeevatkm jeevatkm added this to the v2.10.0 milestone Oct 1, 2023
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 Thanks for your PR, appreciated.
Can you please check the couple of comments?

middleware.go Outdated Show resolved Hide resolved
middleware.go Outdated Show resolved Hide resolved
@SVilgelm
Copy link
Contributor Author

SVilgelm commented Oct 1, 2023

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

@jeevatkm
Copy link
Member

jeevatkm commented Oct 1, 2023

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

Thanks, take your time 👍 safe biking!

@SVilgelm

This comment was marked as outdated.

@SVilgelm SVilgelm requested a review from jeevatkm October 2, 2023 02:29
```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_parseRequestURL_PathParams-16           524658              2260 ns/op             448 B/op          9 allocs/op
PASS
ok      github.com/go-resty/resty/v2    2.327s
```
```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_parseRequestURL_QueryParams-16          865923              1371 ns/op             416 B/op         13 allocs/op
PASS
ok      github.com/go-resty/resty/v2    2.491s
```
* Use the map to collect all replacements and use replace all path parameters using O(1) logic
* Add additional unit tests to cover empty `{}` and not closed `{bar` path parameters

```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_parseRequestURL_PathParams-16           785971              1410 ns/op             320 B/op          6 allocs/op
PASS
ok      github.com/go-resty/resty/v2    1.445s
```
* improve the loging by adding the query parameters from the request first, then adding the parameters from the client and skip if already exists
* additional unit tests for the query parameters

```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_parseRequestURL_QueryParams-16         1000000              1158 ns/op             352 B/op          9 allocs/op
PASS
ok      github.com/go-resty/resty/v2    2.473s
```
reusing a buffer from the pool decreases the allocs and memory usage

```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_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
PASS
ok      github.com/go-resty/resty/v2    2.373s
```
@SVilgelm
Copy link
Contributor Author

SVilgelm commented Oct 2, 2023

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%

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.

Thanks, @SVilgelm, for updating the PR. Looks good!

@jeevatkm jeevatkm merged commit 1f11e18 into go-resty:master Oct 2, 2023
3 checks passed
@SVilgelm SVilgelm deleted the bench-parseRequestURL branch October 2, 2023 13:18
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.

2 participants