-
Notifications
You must be signed in to change notification settings - Fork 11
/
profiler_test.go
92 lines (78 loc) · 1.74 KB
/
profiler_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
package gotaskflow
import (
"bytes"
"testing"
"time"
"github.com/noneback/go-taskflow/utils"
)
func TestProfilerAddSpan(t *testing.T) {
profiler := newProfiler()
mark := attr{
typ: nodeStatic,
name: "test-span",
}
span := &span{
extra: mark,
begin: time.Now(),
cost: 5 * time.Millisecond,
}
profiler.AddSpan(span)
if len(profiler.spans) != 1 {
t.Errorf("expected 1 span, got %d", len(profiler.spans))
}
if profiler.spans[mark] != span {
t.Errorf("expected span to be added correctly, got %v", profiler.spans[mark])
}
}
func TestSpanString(t *testing.T) {
now := time.Now()
span := &span{
extra: attr{
typ: nodeStatic,
name: "test-span",
},
begin: now,
cost: 10 * time.Millisecond,
}
expected := "static,test-span,cost " + utils.NormalizeDuration(10*time.Millisecond)
actual := span.String()
if actual != expected {
t.Errorf("expected %s, got %s", expected, actual)
}
}
func TestProfilerDraw(t *testing.T) {
profiler := newProfiler()
now := time.Now()
parentSpan := &span{
extra: attr{
typ: nodeStatic,
name: "parent",
},
begin: now,
cost: 10 * time.Millisecond,
}
childSpan := &span{
extra: attr{
typ: nodeStatic,
name: "child",
},
begin: now,
cost: 5 * time.Millisecond,
parent: parentSpan,
}
profiler.AddSpan(parentSpan)
profiler.AddSpan(childSpan)
var buf bytes.Buffer
err := profiler.draw(&buf)
if err != nil {
t.Errorf("unexpected error: %v", err)
}
output := buf.String()
if len(output) == 0 {
t.Errorf("expected output, got empty string")
}
expectedOutput := "static,parent,cost 10ms 10000\nstatic,parent,cost 10ms;static,child,cost 5ms 5000\n"
if output != expectedOutput {
t.Errorf("expected output: %v\ngot: %v", expectedOutput, output)
}
}