/
pool_test.go
72 lines (58 loc) · 1.35 KB
/
pool_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
package gopool_test
import (
"bytes"
"sync"
"testing"
"github.com/karrick/gopool"
)
const defaultBufSize = 1024
const defaultMaxKeep = 1024 * 128
const lowConcurrency = 16
const medConcurrency = 128
const highConcurrency = 1024
const lowCap = 100
const medCap = 1000
const largeCap = 10000
////////////////////////////////////////
func makeBuffer() (interface{}, error) {
return bytes.NewBuffer(make([]byte, defaultBufSize)), nil
}
func resetBuffer(item interface{}) {
item.(*bytes.Buffer).Reset()
}
func closeBuffer(item interface{}) error {
item.(*bytes.Buffer).Reset()
return nil
}
////////////////////////////////////////
func testC(bp gopool.Pool, concurrency, loops int) {
const byteCount = defaultBufSize / 2
var wg sync.WaitGroup
wg.Add(concurrency)
for c := 0; c < concurrency; c++ {
go func() {
for j := 0; j < loops; j++ {
bb := bp.Get().(*bytes.Buffer)
max := byteCount
if j%8 == 0 {
max = 2 * defaultMaxKeep
}
for k := 0; k < max; k++ {
bb.WriteByte(byte(k % 256))
}
bp.Put(bb)
}
wg.Done()
}()
}
wg.Wait()
}
func test(t *testing.T, bp gopool.Pool) {
const concurrency = 128
const loops = 128
testC(bp, concurrency, loops)
}
func bench(b *testing.B, bp gopool.Pool, concurrency int) {
b.ResetTimer() // do not include initialization time in benchmarks
testC(bp, concurrency, b.N)
}