-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbenchmark_native_alloc_test.go
More file actions
45 lines (38 loc) · 1.44 KB
/
benchmark_native_alloc_test.go
File metadata and controls
45 lines (38 loc) · 1.44 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
package purejson
import "testing"
const (
benchmarkMetricNativeBytesPerOp = "native-bytes/op"
benchmarkMetricNativeAllocsPerOp = "native-allocs/op"
benchmarkMetricNativeLiveBytes = "native-live-bytes"
)
func benchmarkRunWithNativeAllocMetrics(b *testing.B, requireNativeAllocs bool, run func()) {
b.Helper()
library, err := activeLibrary()
if err != nil {
b.Fatalf("activeLibrary(): %v", err)
}
if err := wrapStatus(library.bindings.NativeAllocStatsReset()); err != nil {
b.Fatalf("NativeAllocStatsReset(): %v", err)
}
// Why: benchmark bodies using this helper are single-threaded, so reset/run/snapshot forms
// a closed native allocation window for the comparator under measurement.
b.ResetTimer()
run()
b.StopTimer()
stats, rc := library.bindings.NativeAllocStatsSnapshot()
if err := wrapStatus(rc); err != nil {
b.Fatalf("NativeAllocStatsSnapshot(): %v", err)
}
if requireNativeAllocs && b.N > 0 && stats.AllocCount == 0 {
b.Fatalf("NativeAllocStatsSnapshot(): alloc_count = 0, want native allocation telemetry for this path")
}
if b.N > 0 {
perOp := float64(b.N)
b.ReportMetric(float64(stats.TotalAllocBytes)/perOp, benchmarkMetricNativeBytesPerOp)
b.ReportMetric(float64(stats.AllocCount)/perOp, benchmarkMetricNativeAllocsPerOp)
} else {
b.ReportMetric(0, benchmarkMetricNativeBytesPerOp)
b.ReportMetric(0, benchmarkMetricNativeAllocsPerOp)
}
b.ReportMetric(float64(stats.LiveBytes), benchmarkMetricNativeLiveBytes)
}