-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
68 lines (59 loc) · 1.24 KB
/
main.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
package main
import (
"flag"
"fmt"
"math"
"net/http"
_ "net/http/pprof"
"time"
)
var tickerLeakCount int = 0
var tickerLeakThread int = 0
var busyLoopThread int = 0
func main() {
flag.IntVar(&tickerLeakCount, "ticker-leak-count", 30000, "单线程泄露的Ticker数量")
flag.IntVar(&tickerLeakThread, "ticker-leak-thread", 2, "ticker泄漏的线程数")
flag.IntVar(&busyLoopThread, "busy-loop-thread", 1, "空忙线程数")
flag.Parse()
// 启动pprof接口
go func() {
fmt.Println("PPROF server started on :6060")
http.ListenAndServe(":6060", nil)
}()
for i := 0; i < busyLoopThread; i++ {
go busyLoop(i)
}
for i := 0; i < tickerLeakThread; i++ {
go tickerLeak(i)
}
select {}
}
func busyLoop(id int) {
fmt.Printf("Starting busy loop thread id: %d\n", id)
for {
// 模拟计算密集型任务
for i := 0; i < 2000000; i++ {
_ = math.Sqrt(float64(i))
}
time.Sleep(10 * time.Millisecond)
}
}
func tickerLeak(id int) {
fmt.Printf("Starting ticker leak thread id: %d\n", id)
i := 0
var ticker *time.Ticker
for {
i++
if i > tickerLeakCount {
time.Sleep(time.Second)
continue
}
if ticker == nil {
ticker = time.NewTicker(3 * time.Millisecond)
}
for range ticker.C {
// do something
break
}
}
}