-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbench_test.go
More file actions
142 lines (125 loc) · 3.13 KB
/
Copy pathbench_test.go
File metadata and controls
142 lines (125 loc) · 3.13 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
// Copyright (c) 2025 Andrey Kolkov and GoGPU Contributors
// SPDX-License-Identifier: MIT
package galloc
import "testing"
func BenchmarkAllocate(b *testing.B) {
// Measure single Allocate call. Reset each iteration to avoid exhaustion.
a := New(1024*1024*256, 1024)
b.ResetTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
alloc := a.Allocate(256)
a.Free(alloc)
}
// Note: alloc+free cycle to keep the allocator usable across iterations.
// The alloc cost dominates since Free is measured separately.
}
func BenchmarkFree(b *testing.B) {
// Measure single Free (with coalescing against one neighbor).
// Avoid StopTimer/StartTimer in inner loop — their syscall overhead
// dominates and produces misleading results (~16µs instead of ~25ns).
a := New(1024*1024*256, 2048)
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
alloc := a.Allocate(64)
a.Free(alloc)
}
}
func BenchmarkFreeBatch(b *testing.B) {
// Measure batch Free with full coalescing (512 blocks → 1 merged region).
// Setup cost is amortized by running the entire batch as one b.N iteration.
const batchSize = 512
const maxAllocs = 2048
a := New(1024*1024, maxAllocs)
allocs := make([]Allocation, batchSize)
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
a.Reset()
for j := range allocs {
allocs[j] = a.Allocate(64)
}
for j := range allocs {
a.Free(allocs[j])
}
}
}
func BenchmarkAllocFree(b *testing.B) {
// Alloc+Free cycle — measures steady-state overhead.
a := New(1024*1024*256, 1024)
b.ResetTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
alloc := a.Allocate(256)
a.Free(alloc)
}
}
func BenchmarkAllocMany(b *testing.B) {
// Fill an allocator to near capacity, then free everything.
const maxAllocs = 4096
a := New(1024*1024, maxAllocs)
allocs := make([]Allocation, 0, 256)
b.ResetTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
allocs = allocs[:0]
a.Reset()
for {
alloc := a.Allocate(64)
if alloc.Failed() {
break
}
allocs = append(allocs, alloc)
}
for _, alloc := range allocs {
a.Free(alloc)
}
}
}
func BenchmarkAllocateAligned256(b *testing.B) {
a := New(1024*1024*256, 1024)
b.ResetTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
alloc := a.AllocateAligned(4096, 256)
a.Free(alloc)
}
}
func BenchmarkAllocateAligned1(b *testing.B) {
// Fast path: alignment=1 should be identical to Allocate.
a := New(1024*1024*256, 1024)
b.ResetTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
alloc := a.AllocateAligned(256, 1)
a.Free(alloc)
}
}
func BenchmarkFragmented(b *testing.B) {
// Interleaved alloc/free pattern to stress coalescing.
const maxAllocs = 4096
a := New(1024*1024, maxAllocs)
b.ResetTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
a.Reset()
// Allocate 256 blocks.
var allocs [256]Allocation
for j := range allocs {
allocs[j] = a.Allocate(128)
}
// Free every other block (creates fragmentation).
for j := 0; j < 256; j += 2 {
a.Free(allocs[j])
}
// Re-allocate in the gaps.
for j := 0; j < 256; j += 2 {
allocs[j] = a.Allocate(128)
}
// Free all.
for j := range allocs {
a.Free(allocs[j])
}
}
}