This repository has been archived by the owner on Sep 21, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgoid_meta_map_test.go
88 lines (72 loc) · 1.56 KB
/
goid_meta_map_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
package goid_test
import (
"context"
"fmt"
"math/rand"
"testing"
"time"
"github.com/go-courier/goid"
"github.com/go-courier/metax"
)
func ExampleLogIDMap() {
for i := 0; i < 20; i++ {
go func() {
// set logid at begin of goroutine
goid.Default.Set(metax.Meta{
"id": {fmt.Sprintf("%d", rand.Int())},
})
// clear at end of goroutine
defer goid.Default.Clear()
// do something with the cached logid
_ = goid.Default.Get()
time.Sleep(10 * time.Millisecond)
}()
}
time.Sleep(5 * time.Millisecond)
fmt.Println(len(goid.Default.All()))
time.Sleep(50 * time.Millisecond)
fmt.Println(len(goid.Default.All()))
// missing
fmt.Println(goid.Default.Get())
// Output:
//20
//0
//
}
func TestLogIDMapGo(t *testing.T) {
for i := 0; i < 3; i++ {
do := goid.Default.With(func() {
meta := goid.Default.Get()
fmt.Println("in goroutinue", meta)
for i := 0; i < 3; i++ {
doInGoroutine := goid.Default.With(func() {
id := goid.Default.Get()
fmt.Println("goroutinue in goroutinue", id)
})
go doInGoroutine()
}
}, metax.Meta{
"id": {fmt.Sprintf("%d", rand.Int())},
})
go do()
}
time.Sleep(500 * time.Millisecond)
}
func BenchmarkLogIDMap(b *testing.B) {
b.Run("log map", func(b *testing.B) {
goid.Default.Set(metax.Meta{
"id": {"1"},
})
for i := 0; i < b.N; i++ {
goid.Default.Get()
}
})
b.Run("meta context", func(b *testing.B) {
ctx := metax.ContextWithMeta(context.Background(), metax.Meta{
"id": {"1"},
})
for i := 0; i < b.N; i++ {
metax.MetaFromContext(ctx)
}
})
}