Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion client/hooks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -793,7 +793,7 @@ func Benchmark_Parser_Request_Body_File(b *testing.B) {
func newBenchmarkRequest(formValues map[string]string, fileContents [][]byte) *Request {
req := &Request{
boundary: "FiberBenchmarkBoundary",
formData: &FormData{Args: fasthttp.AcquireArgs()},
formData: FormData{Args: fasthttp.AcquireArgs()},
RawRequest: fasthttp.AcquireRequest(),
files: make([]*File, len(fileContents)),
}
Expand Down
24 changes: 12 additions & 12 deletions client/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,14 @@ type Request struct {
ctx context.Context //nolint:containedctx // Context is needed to be stored in the request.

body any
header *Header
params *QueryParam
cookies *Cookie
path *PathParam
header Header
params QueryParam
cookies Cookie
path PathParam

client *Client

formData *FormData
formData FormData

RawRequest *fasthttp.Request
url string
Expand Down Expand Up @@ -324,7 +324,7 @@ func (r *Request) SetReferer(referer string) *Request {
// Cookie returns the value of a named cookie.
// If the cookie does not exist, an empty string is returned.
func (r *Request) Cookie(key string) string {
if val, ok := (*r.cookies)[key]; ok {
if val, ok := r.cookies[key]; ok {
return val
}
return ""
Expand Down Expand Up @@ -363,7 +363,7 @@ func (r *Request) DelCookies(key ...string) *Request {
// PathParam returns the value of a named path parameter.
// If the parameter does not exist, an empty string is returned.
func (r *Request) PathParam(key string) string {
if val, ok := (*r.path)[key]; ok {
if val, ok := r.path[key]; ok {
return val
}
return ""
Expand Down Expand Up @@ -967,12 +967,12 @@ func (f *File) Reset() {
var requestPool = &sync.Pool{
New: func() any {
return &Request{
header: &Header{RequestHeader: &fasthttp.RequestHeader{}},
params: &QueryParam{Args: fasthttp.AcquireArgs()},
cookies: &Cookie{},
path: &PathParam{},
header: Header{RequestHeader: &fasthttp.RequestHeader{}},
params: QueryParam{Args: fasthttp.AcquireArgs()},
cookies: Cookie{},
path: PathParam{},
boundary: boundary,
formData: &FormData{Args: fasthttp.AcquireArgs()},
formData: FormData{Args: fasthttp.AcquireArgs()},
files: make([]*File, 0),
RawRequest: fasthttp.AcquireRequest(),
}
Expand Down
58 changes: 58 additions & 0 deletions client/request_bench_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package client

import (
"runtime"
"runtime/metrics"
"strconv"
"testing"
)

// BenchmarkRequestHeapScan measures how much heap memory the GC needs to scan
// when a batch of requests is created and released.
func BenchmarkRequestHeapScan(b *testing.B) {
samples := []metrics.Sample{
{Name: "/gc/scan/heap:bytes"},
{Name: "/gc/scan/total:bytes"},
}

b.ReportAllocs()
b.StopTimer()
b.ResetTimer()

const batchSize = 512
var totalScanHeap, totalScanAll uint64
for i := 0; i < b.N; i++ {
Comment thread
ReneWerner87 marked this conversation as resolved.
reqs := make([]*Request, batchSize)
// revive:disable-next-line:call-to-gc // Ensure consistent heap state before measuring scan metrics
runtime.GC()
metrics.Read(samples)
startScanHeap := samples[0].Value.Uint64()
startScanAll := samples[1].Value.Uint64()

b.StartTimer()
for j := range reqs {
req := AcquireRequest()
req.SetHeader("X-Benchmark", "value")
req.SetCookie("session", strconv.Itoa(j))
req.SetPathParam("id", strconv.Itoa(j))
req.SetParam("page", strconv.Itoa(j))
reqs[j] = req
}
b.StopTimer()

// revive:disable-next-line:call-to-gc // Force GC to capture post-benchmark scan metrics
runtime.GC()
metrics.Read(samples)
totalScanHeap += samples[0].Value.Uint64() - startScanHeap
totalScanAll += samples[1].Value.Uint64() - startScanAll

for _, req := range reqs {
ReleaseRequest(req)
}
}

if b.N > 0 {
b.ReportMetric(float64(totalScanHeap)/float64(b.N), "scan-bytes-heap/op")
b.ReportMetric(float64(totalScanAll)/float64(b.N), "scan-bytes-total/op")
}
}
Loading