-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpool_test.go
111 lines (93 loc) · 1.92 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
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
package objpool_test
import (
"runtime"
"testing"
"github.com/youngbloood/objpool"
)
type Struct struct {
Int int
}
func newStruct() interface{} {
return &Struct{
Int: 1000,
}
}
func newInt() interface{} {
return 2000
}
func TestObjPoolWithoutGC(t *testing.T) {
pool := objpool.New()
structName := pool.Set(&Struct{}, newStruct)
pool.Set(8, newInt)
stc := &Struct{
Int: 20,
}
pool.Put(stc)
structVal, ok := pool.Get(structName, false).(*Struct)
if ok {
t.Logf("structVal.ptr.before = %p\n", stc)
t.Logf("structVal = %+v\n", structVal)
t.Logf("structVal.ptr.after = %p\n", structVal)
}
intVal, ok := pool.Get("int", false).(int)
if ok {
t.Logf("intVal = %d\n", intVal)
t.Logf("intVal.ptr = %p\n", &intVal)
}
}
func TestObjPoolWithGC(t *testing.T) {
pool := objpool.New()
structName := pool.Set(&Struct{}, newStruct)
pool.Set(8, newInt)
stc := &Struct{
Int: 20,
}
pool.Put(stc)
runtime.GC()
structVal, ok := pool.Get(structName, false).(*Struct)
if ok {
t.Logf("structVal.ptr.before = %p\n", stc)
t.Logf("structVal = %+v\n", structVal)
t.Logf("structVal.ptr.after = %p\n", structVal)
}
intVal, ok := pool.Get("int", false).(int)
if ok {
t.Logf("intVal = %d\n", intVal)
t.Logf("intVal.ptr = %p\n", &intVal)
}
}
func BenchmarkObjPoolGet(b *testing.B) {
pool := objpool.New()
structName := pool.Set(&Struct{}, newStruct)
pool.Put(&Struct{
Int: 20,
})
for i := 0; i < b.N; i++ {
_ = pool.Get(structName, true)
}
}
func BenchmarkObjPoolSet(b *testing.B) {
pool := objpool.New()
pool.Set(&Struct{}, newStruct)
stc := &Struct{
Int: 20,
}
pool.Put(stc)
for i := 0; i < b.N; i++ {
pool.Put(stc)
}
}
func BenchmarkObjPoolGetAndSet(b *testing.B) {
pool := objpool.New()
structName := pool.Set(&Struct{}, newStruct)
stc := &Struct{
Int: 20,
}
pool.Put(stc)
for i := 0; i < b.N; i += 2 {
pool.Put(stc)
}
for i := 1; i < b.N; i += 2 {
_ = pool.Get(structName, true)
}
}