forked from go-eden/routine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathroutine_goid_test.go
118 lines (107 loc) · 3.26 KB
/
routine_goid_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
112
113
114
115
116
117
118
package routine
import (
"github.com/stretchr/testify/assert"
"runtime"
"testing"
"time"
)
func TestGetGoid(t *testing.T) {
var id int64
for i := 0; i < 100; i++ {
nid, _ := getGoidByNative()
sid := getGoidByStack()
assert.True(t, nid == sid)
if id == 0 {
id = sid
} else {
assert.True(t, id == sid)
}
}
t.Log(getGoidByStack())
t.Log(getGoidByNative())
}
func TestPC(t *testing.T) {
const num = 10
for i := 0; i < num; i++ {
go func() {
time.Sleep(time.Minute)
}()
}
time.Sleep(time.Millisecond)
// can only load pc info, cannot load goroutine context
res := make([]runtime.StackRecord, 1024)
n, ok := runtime.GoroutineProfile(res)
t.Log(n, ok)
// 获取全部协程上下文快照
buf := make([]byte, 1<<20)
n = runtime.Stack(buf, true)
t.Log("all: \n", string(buf[:n]))
}
// BenchmarkGetGoidByNative-12 409483110 2.530 ns/op 0 B/op 0 allocs/op
func BenchmarkGetGoidByNative(b *testing.B) {
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
getGoidByNative()
}
}
// BenchmarkGetGoidByStack-12 391472 2996 ns/op 0 B/op 0 allocs/op
func BenchmarkGetGoidByStack(b *testing.B) {
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
getGoidByStack()
}
}
// When routineNum = 16
// BenchmarkGetAllGoidByNative-12 6226236 242.9 ns/op 896 B/op 1 allocs/op
// When routineNum = 64
// BenchmarkGetAllGoidByNative-12 2064740 740.3 ns/op 3072 B/op 1 allocs/op
// When routineNum = 256
// BenchmarkGetAllGoidByNative-12 402352 3163 ns/op 9472 B/op 1 allocs/op
// When routineNum = 1024
// BenchmarkGetAllGoidByNative-12 84907 18508 ns/op 40960 B/op 1 allocs/op
// When routineNum = 8192
// BenchmarkGetAllGoidByNative-12 7520 253149 ns/op 270336 B/op 1 allocs/op
// When routineNum = 65536
// BenchmarkGetAllGoidByNative-12 457 4195487 ns/op 1581056 B/op 1 allocs/op
func BenchmarkGetAllGoidByNative(b *testing.B) {
const routineNum = 65536
for i := 0; i < routineNum; i++ {
go func() {
time.Sleep(time.Minute)
}()
}
time.Sleep(time.Millisecond * 100)
b.ResetTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
_, _ = getAllGoidByNative()
}
}
// When routineNum = 16
// BenchmarkGetAllGoidByStack-12 7779 147593 ns/op 57793 B/op 2 allocs/op
// When routineNum = 64
// BenchmarkGetAllGoidByStack-12 2754 463350 ns/op 206595 B/op 2 allocs/op
// When routineNum = 256
// BenchmarkGetAllGoidByStack-12 843 1858839 ns/op 801156 B/op 2 allocs/op
// When routineNum = 1024
// BenchmarkGetAllGoidByStack-12 254 6923118 ns/op 3181195 B/op 2 allocs/op
// When routineNum = 8192
// BenchmarkGetAllGoidByStack-12 43 37269949 ns/op 16924678 B/op 2 allocs/op
// When routineNum = 65536
// BenchmarkGetAllGoidByStack-12 6 316648415 ns/op 135282688 B/op 2 allocs/op
func BenchmarkGetAllGoidByStack(b *testing.B) {
const routineNum = 65536
for i := 0; i < routineNum; i++ {
go func() {
time.Sleep(time.Minute)
}()
}
time.Sleep(time.Millisecond * 100)
b.ResetTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
_ = getAllGoidByStack()
}
}