-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathcpu_test.go
118 lines (90 loc) · 2.88 KB
/
cpu_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 wzprof
import (
"context"
"testing"
"github.com/tetratelabs/wazero/api"
"github.com/tetratelabs/wazero/experimental"
"github.com/tetratelabs/wazero/experimental/wazerotest"
)
func BenchmarkCPUProfilerOn(b *testing.B) {
p := ProfilingFor(nil).CPUProfiler()
p.StartProfile()
benchmarkFunctionListener(b, p)
}
func BenchmarkCPUProfilerOff(b *testing.B) {
p := ProfilingFor(nil).CPUProfiler()
benchmarkFunctionListener(b, p)
}
func TestCPUProfilerTime(t *testing.T) {
currentTime := int64(0)
p := ProfilingFor(nil).CPUProfiler(
TimeFunc(func() int64 { return currentTime }),
)
module := wazerotest.NewModule(nil,
wazerotest.NewFunction(func(context.Context, api.Module) {}),
wazerotest.NewFunction(func(context.Context, api.Module) {}),
wazerotest.NewFunction(func(context.Context, api.Module) {}),
)
f0 := p.NewFunctionListener(module.Function(0).Definition())
f1 := p.NewFunctionListener(module.Function(1).Definition())
f2 := p.NewFunctionListener(module.Function(2).Definition())
stack0 := []experimental.StackFrame{
{Function: module.Function(0)},
}
stack1 := []experimental.StackFrame{
{Function: module.Function(0)},
{Function: module.Function(1)},
}
stack2 := []experimental.StackFrame{
{Function: module.Function(0)},
{Function: module.Function(1)},
{Function: module.Function(2)},
}
def0 := stack0[0].Function.Definition()
def1 := stack1[1].Function.Definition()
def2 := stack2[2].Function.Definition()
ctx := context.Background()
const (
t0 int64 = 1
t1 int64 = 10
t2 int64 = 42
t3 int64 = 100
t4 int64 = 101
t5 int64 = 102
)
p.StartProfile()
currentTime = t0
f0.Before(ctx, module, def0, nil, experimental.NewStackIterator(stack0...))
currentTime = t1
f1.Before(ctx, module, def1, nil, experimental.NewStackIterator(stack1...))
currentTime = t2
f2.Before(ctx, module, def2, nil, experimental.NewStackIterator(stack2...))
currentTime = t3
f2.After(ctx, module, def2, nil)
currentTime = t4
f1.After(ctx, module, def1, nil)
currentTime = t5
f0.After(ctx, module, def0, nil)
trace0 := makeStackTraceFromFrames(stack0)
trace1 := makeStackTraceFromFrames(stack1)
trace2 := makeStackTraceFromFrames(stack2)
d2 := t3 - t2
d1 := t4 - (t1 + d2)
d0 := t5 - (t0 + d1 + d2)
assertStackCount(t, p.counts, trace0, 1, d0)
assertStackCount(t, p.counts, trace1, 1, d1)
assertStackCount(t, p.counts, trace2, 1, d2)
}
func assertStackCount(t *testing.T, counts stackCounterMap, trace stackTrace, count, total int64) {
t.Helper()
c := counts.lookup(trace)
if c.count() != count {
t.Errorf("%sstack count mismatch: want=%d got=%d", trace, count, c.count())
}
if c.total() != total {
t.Errorf("%sstack total mismatch: want=%d got=%d", trace, total, c.total())
}
}
func makeStackTraceFromFrames(stackFrames []experimental.StackFrame) stackTrace {
return makeStackTrace(stackTrace{}, experimental.NewStackIterator(stackFrames...))
}