forked from yireyun/go-queue
/
pprofTest.go
84 lines (77 loc) · 1.61 KB
/
pprofTest.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
// esQueue_test
package main
import (
"fmt"
"os"
"runtime"
"runtime/pprof"
"sync"
"time"
sq "github.com/yireyun/go-queue"
)
var (
value = 1
)
func testQueueHigh(grp, cnt int) int {
var wg sync.WaitGroup
wg.Add(grp)
q := sq.NewQueue(1024 * 1024)
for i := 0; i < grp; i++ {
go func(g int) {
ok := false
for j := 0; j < cnt; j++ {
ok, _ = q.Put(&value)
for !ok {
time.Sleep(time.Microsecond)
ok, _ = q.Put(&value)
}
}
wg.Done()
}(i)
}
wg.Add(grp)
for i := 0; i < grp; i++ {
go func(g int) {
ok := false
for j := 0; j < cnt; j++ {
_, ok, _ = q.Get() //该语句注释掉将导致运行结果不正确
for !ok {
time.Sleep(time.Microsecond * 100)
_, ok, _ = q.Get()
}
}
wg.Done()
}(i)
}
wg.Wait()
return 0
}
func TestQueueHigh() {
pproF, _ := os.Create("pprof") // 创建记录文件
pprof.StartCPUProfile(pproF) // 开始cpu profile,结果写到文件f中
defer pprof.StopCPUProfile()
var miss, Sum int
var Use time.Duration
for i := 1; i <= runtime.NumCPU()*4; i++ {
cnt := 10000 * 1000
if i > 9 {
cnt = 10000 * 100
}
sum := i * cnt
start := time.Now()
miss = testQueueHigh(i, cnt)
end := time.Now()
use := end.Sub(start)
op := use / time.Duration(sum)
fmt.Printf("%v %v, Grp: %3d, Times: %10d, miss:%6v, use: %12v, %8v/op\n",
runtime.Version(), runtime.GOARCH, i, sum, miss, use, op)
Use += use
Sum += sum
}
op := Use / time.Duration(Sum)
fmt.Printf("%v %v, Grp: %3v, Times: %10d, miss:%6v, use: %12v, %8v/op\n",
runtime.Version(), runtime.GOARCH, "Sum", Sum, 0, Use, op)
}
func main() {
TestQueueHigh()
}