forked from gofiber/fiber
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrequest_bench_test.go
More file actions
58 lines (50 loc) · 1.49 KB
/
Copy pathrequest_bench_test.go
File metadata and controls
58 lines (50 loc) · 1.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
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++ {
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")
}
}