-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbunch_allocator_test.go
109 lines (98 loc) · 2.05 KB
/
bunch_allocator_test.go
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
package xsync
import (
"fmt"
"runtime"
"testing"
)
type testType1 [1]byte
type testType8 [8]byte
type testType32 [32]byte
type testType256 [256]byte
type testType1024 [1024]byte
type testType65536 [65536]byte
var (
v1 *testType1
v8 *testType8
v32 *testType32
v256 *testType256
v1024 *testType1024
v65536 *testType65536
)
func BenchmarkBunchAllocator(b *testing.B) {
for _, structSize := range []uint{1, 8, 32, 256, 1024, 65536} {
b.Run(fmt.Sprintf("structSize%d", structSize), func(b *testing.B) {
var (
noBunchGetter func()
bunchGetter func()
)
switch structSize {
case 1:
noBunchGetter = func() {
v1 = &testType1{}
}
allocator := NewBunchAllocator[testType1]()
bunchGetter = func() {
v1 = allocator.Get()
}
case 8:
noBunchGetter = func() {
v8 = &testType8{}
}
allocator := NewBunchAllocator[testType8]()
bunchGetter = func() {
v8 = allocator.Get()
}
case 32:
noBunchGetter = func() {
v32 = &testType32{}
}
allocator := NewBunchAllocator[testType32]()
bunchGetter = func() {
v32 = allocator.Get()
}
case 256:
noBunchGetter = func() {
v256 = &testType256{}
}
allocator := NewBunchAllocator[testType256]()
bunchGetter = func() {
v256 = allocator.Get()
}
case 1024:
noBunchGetter = func() {
v1024 = &testType1024{}
}
allocator := NewBunchAllocator[testType1024]()
bunchGetter = func() {
v1024 = allocator.Get()
}
case 65536:
noBunchGetter = func() {
v65536 = &testType65536{}
}
allocator := NewBunchAllocator[testType65536]()
bunchGetter = func() {
v65536 = allocator.Get()
}
}
b.Run("no_bunch", func(b *testing.B) {
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
noBunchGetter()
}
runtime.GC()
runtime.GC()
})
b.Run("bunch", func(b *testing.B) {
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
bunchGetter()
}
runtime.GC()
runtime.GC()
})
})
}
}