Repository navigation
Expand file tree
/
Copy pathallocator_bench_test.go
More file actions
96 lines (82 loc) · 2.27 KB
/
Copy pathallocator_bench_test.go
File metadata and controls
96 lines (82 loc) · 2.27 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
package gomem
import (
"testing"
)
// BenchmarkAllocatorAllocate benchmarks memory allocation
func BenchmarkAllocatorAllocate(b *testing.B) {
allocator := NewAllocator(1024 * 64) // 64KB pool
b.ResetTimer()
for i := 0; i < b.N; i++ {
offset := allocator.Allocate(1024)
if offset == -1 {
b.Fatal("Failed to allocate memory")
}
allocator.Free(offset, 1024)
}
}
// BenchmarkAllocatorAllocateSmall benchmarks small memory allocation
func BenchmarkAllocatorAllocateSmall(b *testing.B) {
allocator := NewAllocator(1024 * 64) // 64KB pool
b.ResetTimer()
for i := 0; i < b.N; i++ {
offset := allocator.Allocate(64)
if offset == -1 {
b.Fatal("Failed to allocate memory")
}
allocator.Free(offset, 64)
}
}
// BenchmarkAllocatorAllocateLarge benchmarks large memory allocation
func BenchmarkAllocatorAllocateLarge(b *testing.B) {
allocator := NewAllocator(1024 * 64) // 64KB pool
b.ResetTimer()
for i := 0; i < b.N; i++ {
offset := allocator.Allocate(8192)
if offset == -1 {
b.Fatal("Failed to allocate memory")
}
allocator.Free(offset, 8192)
}
}
// BenchmarkAllocatorSequentialAlloc benchmarks sequential allocation and deallocation
func BenchmarkAllocatorSequentialAlloc(b *testing.B) {
allocator := NewAllocator(1024 * 64) // 64KB pool
allocations := make([]int, 50)
b.ResetTimer()
for i := 0; i < b.N; i++ {
// Allocate 50 blocks
for j := 0; j < 50; j++ {
offset := allocator.Allocate(1024)
if offset == -1 {
b.Fatal("Failed to allocate memory")
}
allocations[j] = offset
}
// Free all blocks
for j := 0; j < 50; j++ {
allocator.Free(allocations[j], 1024)
}
}
}
// BenchmarkAllocatorRandomAlloc benchmarks random allocation pattern
func BenchmarkAllocatorRandomAlloc(b *testing.B) {
allocator := NewAllocator(1024 * 64) // 64KB pool
sizes := []int{64, 128, 256, 512, 1024, 2048, 4096}
b.ResetTimer()
for i := 0; i < b.N; i++ {
size := sizes[i%len(sizes)]
offset := allocator.Allocate(size)
if offset == -1 {
b.Fatal("Failed to allocate memory")
}
allocator.Free(offset, size)
}
}
// BenchmarkAllocatorGetFreeSize benchmarks getting free size
func BenchmarkAllocatorGetFreeSize(b *testing.B) {
allocator := NewAllocator(1024 * 64) // 64KB pool
b.ResetTimer()
for i := 0; i < b.N; i++ {
_ = allocator.GetFreeSize()
}
}