-
Notifications
You must be signed in to change notification settings - Fork 0
/
bytepool_test.go
179 lines (166 loc) · 4.5 KB
/
bytepool_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
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
package cgobytepool
import (
"testing"
"unsafe"
)
func TestCMallocPool(t *testing.T) {
t.Run("AllocBytes", func(tt *testing.T) {
poolSize := 10
p := newCMallocPool(poolSize, 100)
defer p.Close()
ptr1 := p.Get()
ptr2 := p.Get()
ptr3 := p.Get()
if p.AllocBytes() != 300 {
tt.Errorf("3 item(100*3) get actual=%d", p.AllocBytes())
}
p.Put(ptr1, 100)
p.Put(ptr2, 100)
p.Put(ptr3, 100)
if p.AllocBytes() != 300 {
tt.Errorf("put pool all items actual=%d", p.AllocBytes())
}
ptrs := make([]unsafe.Pointer, poolSize+1)
for i := 0; i < poolSize; i += 1 {
ptrs[i] = p.Get()
}
if p.AllocBytes() != 1000 {
tt.Errorf("reuse 300 + new 700, actual=%d", p.AllocBytes())
}
ptrs[10] = p.Get()
if p.AllocBytes() != 1100 {
tt.Errorf("new 1 item, actual=%d", p.AllocBytes())
}
for i := 0; i < poolSize; i += 1 {
p.Put(ptrs[i], 100)
}
if p.AllocBytes() != 1100 {
tt.Errorf("put all item but 1 item used, actual=%d", p.AllocBytes())
}
p.Put(ptrs[10], 100)
if p.AllocBytes() != 1000 {
tt.Errorf("1 item freed, actual=%d", p.AllocBytes())
}
})
t.Run("Get/Put", func(tt *testing.T) {
p := newCMallocPool(1, 100)
defer p.Close()
ptr1 := p.Get()
if ptr1 == nil {
tt.Fatalf("must alloc")
}
data1 := unsafe.Slice((*byte)(ptr1), 100)
tt.Logf("----- %T", data1)
for i := 0; i < len(data1); i += 1 {
data1[i] = 123
}
p.Put(ptr1, 100)
ptr2 := p.Get() // reuse
data2 := unsafe.Slice((*byte)(ptr2), 100)
for i := 0; i < len(data2); i += 1 {
if data2[i] != 123 {
tt.Errorf("reuse data data2[%d]=%d", i, data2[i])
}
}
p.Put(ptr2, 100)
})
}
func TestCgoBytePool(t *testing.T) {
t.Run("MemoryAlignmentFunc", func(tt *testing.T) {
if DefaultMemoryAlignmentFunc(500) != 752 {
tt.Errorf("alignmentsize = %d, 500 + align = %d", defaultMemoryAlignmentSize, DefaultMemoryAlignmentFunc(500))
}
a8 := func(n int) int {
return ((n + 8) >> 3) << 3
}
if a8(500) != 504 {
tt.Errorf("alignmentsize = %d, 500 + align = %d", 8, a8(500))
}
a16 := func(n int) int {
return ((n + 16) >> 3) << 3
}
if a16(500) != 512 {
tt.Errorf("alignmentsize = %d, 500 + align = %d", 16, a16(500))
}
})
t.Run("fallback", func(tt *testing.T) {
p := NewPool(DefaultMemoryAlignmentFunc, WithPoolSize(2, 100))
ptr1 := p.Get(500)
if p.AllocBytes() != 752 {
tt.Errorf("fallback alloc actual=%d", p.AllocBytes())
}
ptr2 := p.Get(100)
if p.TotalAllocBytes() != 1104 {
tt.Errorf("pools alloc actual=%d", p.TotalAllocBytes())
}
p.Put(ptr1, 500)
if p.AllocBytes() != 0 {
tt.Errorf("fallback put actual=%d", p.AllocBytes())
}
if p.TotalAllocBytes() != 352 {
tt.Errorf("ptr2 is active actual=%d", p.TotalAllocBytes())
}
p.Put(ptr2, 100)
p.Close()
if p.TotalAllocBytes() != 0 {
tt.Errorf("all item put actual=%d", p.TotalAllocBytes())
}
})
t.Run("multipool", func(tt *testing.T) {
p := NewPool(
DefaultMemoryAlignmentFunc,
WithPoolSize(1, 100),
WithPoolSize(1, 200),
WithPoolSize(1, 300),
)
defer p.Close()
ptr1 := p.Get(100)
if p.pools[0].AllocBytes() != 352 {
tt.Errorf("in pool alloc actual=%d", p.pools[0].AllocBytes())
}
ptr2 := p.Get(200)
if p.pools[1].AllocBytes() != 456 {
tt.Errorf("in pool alloc actual=%d", p.pools[1].AllocBytes())
}
ptr3 := p.Get(300)
if p.pools[2].AllocBytes() != 552 {
tt.Errorf("in pool alloc actual=%d", p.pools[2].AllocBytes())
}
ptr11 := p.Get(100)
if p.pools[0].AllocBytes() != 704 {
tt.Errorf("in pool alloc actual=%d", p.pools[0].AllocBytes())
}
ptr12 := p.Get(200)
if p.pools[1].AllocBytes() != 912 {
tt.Errorf("in pool alloc actual=%d", p.pools[1].AllocBytes())
}
ptr13 := p.Get(300)
if p.pools[2].AllocBytes() != 1104 {
tt.Errorf("in pool alloc actual=%d", p.pools[2].AllocBytes())
}
p.Put(ptr1, 100)
if p.pools[0].AllocBytes() != 704 {
tt.Errorf("in pool alloc actual=%d", p.pools[0].AllocBytes())
}
p.Put(ptr2, 200)
if p.pools[1].AllocBytes() != 912 {
tt.Errorf("in pool alloc actual=%d", p.pools[1].AllocBytes())
}
p.Put(ptr3, 300)
if p.pools[2].AllocBytes() != 1104 {
tt.Errorf("in pool alloc actual=%d", p.pools[2].AllocBytes())
}
p.Put(ptr11, 100)
if p.pools[0].AllocBytes() != 352 {
tt.Errorf("in pool alloc actual=%d", p.pools[0].AllocBytes())
}
p.Put(ptr12, 200)
if p.pools[1].AllocBytes() != 456 {
tt.Errorf("in pool alloc actual=%d", p.pools[1].AllocBytes())
}
p.Put(ptr13, 300)
if p.pools[2].AllocBytes() != 552 {
tt.Errorf("in pool alloc actual=%d", p.pools[2].AllocBytes())
}
})
}