-
Notifications
You must be signed in to change notification settings - Fork 2
/
pool_test.go
94 lines (80 loc) · 1.8 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
package pool
import (
"runtime"
"sync"
"testing"
"time"
)
var (
n int32 = 100
curMem uint64
)
const (
_ = 1 << (10 * iota)
KiB // 1024
MiB // 1048576
GiB // 1073741824
TiB // 1099511627776 (超过了int32的范围)
PiB // 1125899906842624
EiB // 1152921504606846976
ZiB // 1180591620717411303424 (超过了int64的范围)
YiB // 1208925819614629174706176
)
func TestNoPool(t *testing.T) {
var wg sync.WaitGroup
for i := int32(0); i < n; i++ {
wg.Add(1)
go func() {
demoFunc()
wg.Done()
}()
}
wg.Wait()
mem := runtime.MemStats{}
runtime.ReadMemStats(&mem)
curMem = mem.TotalAlloc/MiB - curMem
t.Logf("memory usage:%d MB", curMem)
}
func TestTryGoPool(t *testing.T) {
goPool := NewPool(0, 0, 0) // use default config
defer goPool.Release()
var wg sync.WaitGroup
for i := int32(0); i < n; i++ {
wg.Add(1)
goPool.TryGo(func() {
demoFunc()
wg.Done()
})
}
wg.Wait()
t.Logf("pool, capacity:%d", goPool.Cap())
t.Logf("pool, running workers number:%d", goPool.Running())
t.Logf("pool, free workers number:%d", goPool.Free())
mem := runtime.MemStats{}
runtime.ReadMemStats(&mem)
curMem = mem.TotalAlloc/MiB - curMem
t.Logf("memory usage:%d MB", curMem)
}
func TestAnywayGoPool(t *testing.T) {
goPool := NewPool(0, 0, 0) // use default config
defer goPool.Release()
var wg sync.WaitGroup
for i := int32(0); i < n; i++ {
wg.Add(1)
goPool.AnywayGo(func() {
demoFunc()
wg.Done()
})
}
wg.Wait()
t.Logf("pool, capacity:%d", goPool.Cap())
t.Logf("pool, running workers number:%d", goPool.Running())
t.Logf("pool, free workers number:%d", goPool.Free())
mem := runtime.MemStats{}
runtime.ReadMemStats(&mem)
curMem = mem.TotalAlloc/MiB - curMem
t.Logf("memory usage:%d MB", curMem)
}
func demoFunc() {
time.Sleep(1 * time.Millisecond)
}