Repository navigation
Expand file tree
/
Copy pathbufferpool_test.go
More file actions
419 lines (394 loc) · 11.6 KB
/
Copy pathbufferpool_test.go
File metadata and controls
419 lines (394 loc) · 11.6 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
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
package bufferpool
import (
"bytes"
"math/bits"
"runtime"
"strconv"
"sync"
"testing"
"time"
"unsafe"
)
func TestAlignRoundsUpToAPowerOfTwo(t *testing.T) {
cases := []struct{ size, want int }{
{-1, 0},
{0, 0},
{1, MinSize},
{MinSize - 1, MinSize},
{MinSize, MinSize},
{MinSize + 1, 2 * MinSize},
{1000, 1024},
{1024, 1024},
{1025, 2048},
{MaxSize, MaxSize},
{MaxSize + 1, MaxSize + 1},
}
for _, c := range cases {
if got := Align(c.size); got != c.want {
t.Fatalf("Align(%d) = %d, want %d", c.size, got, c.want)
}
}
for size := 1; size <= MaxSize; size *= 3 {
got := Align(size)
if got < size {
t.Fatalf("Align(%d) = %d, want at least the size asked for", size, got)
}
if bits.OnesCount(uint(got)) != 1 {
t.Fatalf("Align(%d) = %d, want a power of two", size, got)
}
if got > MinSize && got/2 >= size {
t.Fatalf("Align(%d) = %d, want the smallest class that fits", size, got)
}
}
}
func TestGetHasTheRequestedLengthAndAlignedCapacity(t *testing.T) {
if got := Get(0); got != nil {
t.Fatalf("Get(0) = %v, want nil", got)
}
for _, size := range []int{1, 7, MinSize, MinSize + 1, 4097, 64 << 10, MaxSize} {
buf := Get(size)
if len(buf) != size {
t.Fatalf("len(Get(%d)) = %d, want %d", size, len(buf), size)
}
if cap(buf) != Align(size) {
t.Fatalf("cap(Get(%d)) = %d, want %d", size, cap(buf), Align(size))
}
Put(buf)
}
}
// A buffer larger than the largest class is served, but never pooled: holding
// one for a rare request would cost more than making it again.
func TestOversizedBuffersAreServedButNotPooled(t *testing.T) {
size := MaxSize + 1
buf := Get(size)
if len(buf) != size || cap(buf) != size {
t.Fatalf("Get(%d) has len %d cap %d, want both %d", size, len(buf), cap(buf), size)
}
address := unsafe.SliceData(buf)
Put(buf)
// A class holds addresses and works the length out from the class, so an
// oversized buffer kept by one could not be told apart from an ordinary
// one afterwards. What has to hold is that Put turned it away.
for i := range classes {
if p, _ := classes[i].pool.Get().(*byte); p != nil {
if p == address {
t.Fatalf("class %d pooled a buffer larger than MaxSize", i)
}
classes[i].pool.Put(p)
}
for _, held := range classes[i].reserved() {
if held == address {
t.Fatalf("class %d reserved a buffer larger than MaxSize", i)
}
}
}
}
// reused reports whether one of the buffers Put just took came back out of the
// pool. A collection between the two empties the pool, so the check is given a
// few attempts before it is believed.
//
// The buffers are all taken before any is returned, so the pool ends up
// holding several of them rather than one passed back and forth: under the
// race detector sync.Pool drops a quarter of what it is given, and a buffer
// left in another P's private slot cannot be taken from this one, so a single
// buffer is lost often enough to fail the test.
func reused(t *testing.T, size int) bool {
t.Helper()
const attempts = 5
for range attempts {
const buffers = 8
want := map[uintptr]bool{}
taken := make([][]byte, buffers)
for i := range taken {
taken[i] = Get(size)
want[uintptr(unsafe.Pointer(unsafe.SliceData(taken[i])))] = true
}
for _, buf := range taken {
Put(buf)
}
for range buffers {
if want[uintptr(unsafe.Pointer(unsafe.SliceData(Get(size))))] {
return true
}
}
}
return false
}
func TestPutMakesTheBufferAvailableAgain(t *testing.T) {
for _, size := range []int{MinSize, 1500, 16 << 10} {
if !reused(t, size) {
t.Fatalf("a buffer of %d bytes was never handed back out", size)
}
}
}
// A request is served by any buffer in its class, so one taken at a size that
// rounds up to the same class serves the next size too.
func TestAClassServesEverySizeThatRoundsUpToIt(t *testing.T) {
Put(Get(1024))
buf := Get(600)
if cap(buf) < 1024 {
t.Fatalf("cap = %d, want a buffer from the 1024-byte class", cap(buf))
}
}
// A buffer the pool did not make — grown by append, or made by the caller —
// still belongs to the class below its capacity, and must never be handed out
// as if it held a whole class more than it does. It is trimmed to its class on
// the way in, so a buffer's capacity always says exactly which class it is in.
func TestUnalignedBuffersAreKeptAtTheClassBelow(t *testing.T) {
Put(make([]byte, 0, 100))
for range 16 {
if buf := Get(MinSize); cap(buf) != MinSize {
t.Fatalf("cap = %d, want exactly %d", cap(buf), MinSize)
}
}
// Smaller than the smallest class, so there is no class it could be kept
// at without over-promising. It is dropped instead.
Put(make([]byte, 0, MinSize-1))
for range 16 {
if buf := Get(1); cap(buf) != MinSize {
t.Fatalf("cap = %d, want exactly %d", cap(buf), MinSize)
}
}
}
func TestGrowKeepsTheBytesAndAddsRoom(t *testing.T) {
buf := append(Get(4)[:0], "abcd"...)
grown := Grow(buf, 5000)
if !bytes.Equal(grown, []byte("abcd")) {
t.Fatalf("grown = %q, want %q", grown, "abcd")
}
if cap(grown) != Align(5000) {
t.Fatalf("cap = %d, want %d", cap(grown), Align(5000))
}
same := Grow(grown, cap(grown))
if unsafe.SliceData(same) != unsafe.SliceData(grown) {
t.Fatal("Grow replaced a buffer that already had the room")
}
Put(same)
}
func TestJoinPutsPartsIntoOneBuffer(t *testing.T) {
buf := Join(nil, []byte("hello, "), []byte("world"))
if !bytes.Equal(buf, []byte("hello, world")) {
t.Fatalf("buf = %q, want %q", buf, "hello, world")
}
buf = Append(buf, []byte("!"))
if !bytes.Equal(buf, []byte("hello, world!")) {
t.Fatalf("buf = %q, want %q", buf, "hello, world!")
}
if got := Join(nil); got != nil {
t.Fatalf("Join(nil) = %v, want nil", got)
}
if got := Append(nil, nil); got != nil {
t.Fatalf("Append(nil, nil) = %v, want nil", got)
}
Put(buf)
}
// Join must take a buffer that fits every part at once, not grow once per
// part and leave the earlier ones behind.
func TestJoinGrowsOnceForEveryPart(t *testing.T) {
part := make([]byte, 700)
if got := cap(Join(nil, part, part)); got != 2048 {
t.Fatalf("cap = %d, want 2048", got)
}
}
// Growing piece by piece must move up the classes, not stay at the size of the
// first piece and copy on every append.
func TestAppendGrowsToTheClassThatFits(t *testing.T) {
part := make([]byte, 700)
buf := Append(nil, part)
if cap(buf) != 1024 {
t.Fatalf("cap = %d, want 1024", cap(buf))
}
buf = Append(buf, part)
if cap(buf) != 2048 {
t.Fatalf("cap = %d, want 2048", cap(buf))
}
Put(buf)
}
// Every buffer handed out is the caller's alone: two in flight at once must
// never share an array.
func TestConcurrentUseHandsOutDistinctBuffers(t *testing.T) {
var wg sync.WaitGroup
for worker := range runtime.GOMAXPROCS(0) * 4 {
wg.Add(1)
go func() {
defer wg.Done()
mark := byte(worker)
for range 2000 {
buf := Get(1500)
for i := range buf {
buf[i] = mark
}
runtime.Gosched()
for i := range buf {
if buf[i] != mark {
t.Errorf("byte %d is %d, want %d: the buffer is shared", i, buf[i], mark)
return
}
}
Put(buf)
}
}()
}
wg.Wait()
}
func BenchmarkGetPut(b *testing.B) {
for _, size := range []int{64, 1500, 16 << 10} {
b.Run(sizeName(size), func(b *testing.B) {
b.ReportAllocs()
for range b.N {
Put(Get(size))
}
})
}
}
func BenchmarkGetPutParallel(b *testing.B) {
b.ReportAllocs()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
Put(Get(1500))
}
})
}
func BenchmarkMake(b *testing.B) {
for _, size := range []int{64, 1500, 16 << 10} {
b.Run(sizeName(size), func(b *testing.B) {
b.ReportAllocs()
for range b.N {
sink = make([]byte, size)
}
})
}
}
func BenchmarkJoinParts(b *testing.B) {
head := make([]byte, 120)
body := make([]byte, 900)
b.Run("pooled", func(b *testing.B) {
b.ReportAllocs()
for range b.N {
Put(Join(nil, head, body))
}
})
b.Run("append", func(b *testing.B) {
b.ReportAllocs()
for range b.N {
sink = append(append(make([]byte, 0, len(head)+len(body)), head...), body...)
}
})
}
var sink []byte
func sizeName(size int) string { return strconv.Itoa(size) + "B" }
// collect runs a collection and waits for the cleanup that counts it, since a
// cleanup runs on its own goroutine once the collection has swept the tick.
func collect(t *testing.T) {
t.Helper()
before := gcEpoch.Load()
deadline := time.Now().Add(5 * time.Second)
for gcEpoch.Load() == before {
runtime.GC()
if time.Now().After(deadline) {
t.Fatal("no collection was ever counted: the pool cannot tell its sync.Pool has been emptied")
}
time.Sleep(time.Millisecond)
}
}
// A collection empties every sync.Pool, so without a reserve behind them the
// whole working set is made again afterwards. This is the measurement the
// reserve exists for: once it has settled, a collection costs nothing.
func TestTheWorkingSetSurvivesACollection(t *testing.T) {
const (
size = 16 << 10
working = 256
)
// Start from a clean pool: the classes are shared, and what the tests
// before this one left reserved is budget this one cannot have.
SetRetainedBytes(0)
restoreBudget()
held := make([][]byte, working)
remade := func() uint64 {
var before, after runtime.MemStats
runtime.ReadMemStats(&before)
for i := range held {
held[i] = Get(size)
}
runtime.ReadMemStats(&after)
for _, buf := range held {
Put(buf)
}
return after.Mallocs - before.Mallocs
}
// The target is learned from what a class has had to make, and a reserve
// is filled by the returns that follow a collection, so it takes a round
// or two of both to settle.
var last uint64
for range 8 {
collect(t)
last = remade()
if last <= working/8 {
return
}
}
t.Fatalf("a collection still costs %d of a working set of %d", last, working)
}
// The reserve is bounded, and the bound is what the caller says it is.
func TestRetentionStaysWithinItsBudget(t *testing.T) {
t.Cleanup(restoreBudget)
const size = 64 << 10
SetRetainedBytes(8 * size)
held := make([][]byte, 512)
for range 4 {
collect(t)
for i := range held {
held[i] = Get(size)
}
for _, buf := range held {
Put(buf)
}
if got := RetainedBytes(); got > 8*size {
t.Fatalf("retention reached %d bytes, want at most %d", got, 8*size)
}
}
// Lowering the budget gives back what is already reserved, which is what
// makes it a way to release the pool's memory rather than only a limit on
// what it takes next.
SetRetainedBytes(0)
if got := RetainedBytes(); got != 0 {
t.Fatalf("retention is %d bytes after being set to zero", got)
}
for i := range classes {
if free := len(classes[i].reserved()); free != 0 {
t.Fatalf("class %d still holds %d buffers", i, free)
}
}
}
// With the reserves off the pool is the sync.Pool alone, which still has to
// hand out buffers of the right shape.
func TestTheReservesCanBeTurnedOff(t *testing.T) {
t.Cleanup(restoreBudget)
SetRetainedBytes(0)
collect(t)
for range 64 {
buf := Get(1500)
if len(buf) != 1500 || cap(buf) != 2048 {
t.Fatalf("Get(1500) has len %d cap %d", len(buf), cap(buf))
}
Put(buf)
}
if got := RetainedBytes(); got != 0 {
t.Fatalf("retention grew to %d bytes with the reserves off", got)
}
}
// Get and Put keep their own copies of the pool's body, and Builtin's go
// through getPooled and putPooled: the two must agree.
func TestBuiltinMatchesGetAndPut(t *testing.T) {
for _, size := range []int{1, 64, 65, 1500, 16 << 10, MaxSize, MaxSize + 1} {
a, b := Get(size), Builtin.Get(size)
if len(a) != len(b) || cap(a) != cap(b) {
t.Fatalf("size %d: Get len %d cap %d, Builtin.Get len %d cap %d", size, len(a), cap(a), len(b), cap(b))
}
Put(a)
Builtin.Put(b)
}
if Builtin.Get(0) != nil || Get(0) != nil {
t.Fatal("a zero size returned a buffer")
}
}