move top level functions as method on the Client struct
zaibon opened this issue · 3 comments
Is your feature request related to a problem? Please describe.
The way the main-level functions are organized prevents the reuse of an HTTP client and thus forces the creation of a new TCP connection for every function call.
Here is the list of the functions I talking about:
func GetInfo
func GetVersion
func GetDependencies
func GetProject
func GetAdvisory
func GetQuery
Mainly all the exported functions in this file https://github.com/edoardottt/depsdev/blob/main/pkg/depsdev/api.go
Describe the solution you'd like
These functions should be converted to methods on a struct that encapsulates the Client struct
This way, the HTTP client would be kept between requests, which in turn would allow to re-use the TCP connection.
Additional context
I've run a very simple benchmark to highlight the potential gain of this change.
I've benchmarked the GetInfo
and GetVersion
API calls.
Here are the results:
goos: darwin
goarch: arm64
pkg: github.com/edoardottt/depsdev/pkg/depsdev
│ functions.perf │ methods.perf │
│ sec/op │ sec/op vs base │
GetInfo-8 143.66m ± 6% 78.47m ± 15% -45.38% (p=0.000 n=10)
GetVersion-8 119.86m ± 16% 53.91m ± 6% -55.02% (p=0.000 n=10)
geomean 131.2m 65.04m -50.43%
The code of the benchmark can be found at https://github.com/zaibon/depsdev/blob/reuse-http-client/pkg/depsdev/api_test.go
Amazing @zaibon !
Cool suggestion. Waiting for the PR :)
Great, I'll start to work on that then 👍
Done! #31