-
Notifications
You must be signed in to change notification settings - Fork 5
/
sample_test.go
99 lines (81 loc) · 2.6 KB
/
sample_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
package wzprof
import (
"context"
"testing"
"github.com/tetratelabs/wazero/api"
"github.com/tetratelabs/wazero/experimental"
"github.com/tetratelabs/wazero/experimental/wazerotest"
)
func TestFlaggedFunctionListener(t *testing.T) {
module := wazerotest.NewModule(nil,
wazerotest.NewFunction(func(ctx context.Context, mod api.Module) {}),
)
n := 0
f := func(context.Context, api.Module, api.FunctionDefinition, []uint64, experimental.StackIterator) { n++ }
flag := false
factory := Flag(&flag, experimental.FunctionListenerFactoryFunc(
func(def api.FunctionDefinition) experimental.FunctionListener {
return experimental.FunctionListenerFunc(f)
},
))
function := module.Function(0).Definition()
listener := factory.NewFunctionListener(function)
ctx := context.Background()
for i := 0; i < 20; i++ {
listener.Before(ctx, module, function, nil, nil)
}
if n != 0 {
t.Error("function listener called while the flag was set to false")
}
flag = true
for i := 0; i < 2; i++ {
listener.Before(ctx, module, function, nil, nil)
}
if n != 2 {
t.Errorf("wrong number of called to sampled listener: want=2 got=%d", n)
}
flag = false
for i := 0; i < 2; i++ {
listener.Before(ctx, module, function, nil, nil)
}
if n != 2 {
t.Errorf("wrong number of called to sampled listener: want=2 got=%d", n)
}
for i := 0; i < 24; i++ {
listener.After(ctx, module, function, nil)
}
}
func TestSampledFunctionListener(t *testing.T) {
module := wazerotest.NewModule(nil,
wazerotest.NewFunction(func(ctx context.Context, mod api.Module) {}),
)
n := 0
f := func(context.Context, api.Module, api.FunctionDefinition, []uint64, experimental.StackIterator) { n++ }
factory := Sample(0.1, experimental.FunctionListenerFactoryFunc(
func(def api.FunctionDefinition) experimental.FunctionListener {
return experimental.FunctionListenerFunc(f)
},
))
function := module.Function(0).Definition()
listener := factory.NewFunctionListener(function)
ctx := context.Background()
for i := 0; i < 20; i++ {
listener.Before(ctx, module, function, nil, nil)
listener.After(ctx, module, function, nil)
}
if n != 2 {
t.Errorf("wrong number of called to sampled listener: want=2 got=%d", n)
}
}
func BenchmarkSampledFunctionListener(b *testing.B) {
benchmarkFunctionListener(b,
Sample(0.1, experimental.FunctionListenerFactoryFunc(
func(def api.FunctionDefinition) experimental.FunctionListener {
return experimental.FunctionListenerFunc(
func(ctx context.Context, mod api.Module, def api.FunctionDefinition, paramValues []uint64, stackIterator experimental.StackIterator) {
},
)
},
)),
)
}