-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paththreadMeta_test.go
66 lines (54 loc) · 1.24 KB
/
threadMeta_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
package threadMeta
import (
"fmt"
"github.com/stretchr/testify/assert"
"runtime"
"sync"
"testing"
)
func TestSetMeta(t *testing.T) {
printData := func() {
_, data := GetMeta()
fmt.Println(getg().goid, data)
}
SetMeta(nil, "test_data")
printData()
exitChan := make(chan struct{})
go func() {
printData()
SetMeta(nil, "test_data2")
printData()
exitChan <- struct{}{}
}()
<-exitChan
printData()
}
func Test_GetMeta(t *testing.T) {
const tCnt = 100000
memStats := new(runtime.MemStats)
var wg sync.WaitGroup
wg.Add(tCnt)
for i := 0; i < tCnt; i++ {
go func(d int) {
defer wg.Done()
want := d
SetMeta(nil, want)
_, got := GetMeta()
assert.Equal(t, want, got, "want", want, "got", got)
//AddLabel("uid", want)
//uid := GetLabel("uid")
//assert.Equal(t, want, uid, "want", want, "got", uid)
//
//_, got = GetMeta()
//assert.Equal(t, want, got, "want", want, "got", got)
}(i)
}
wg.Wait()
runtime.ReadMemStats(memStats)
println("Mallocs:", memStats.Mallocs, "HeapObjects:", memStats.HeapObjects)
runtime.GC()
runtime.ReadMemStats(memStats)
println("Frees:", memStats.Frees, "HeapObjects:", memStats.HeapObjects)
//Mallocs: 627573 HeapObjects: 152111
//Frees: 579544 HeapObjects: 48029
}