-
Notifications
You must be signed in to change notification settings - Fork 36
/
agent_test.go
163 lines (129 loc) · 2.76 KB
/
agent_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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
package stackimpact
import (
"errors"
"fmt"
"io"
"net/http"
"os"
"testing"
"time"
)
func TestMeasureSegment(t *testing.T) {
agent := NewAgent()
done1 := make(chan bool)
var seg1 *Segment
go func() {
seg1 = agent.MeasureSegment("seg1")
defer seg1.Stop()
time.Sleep(50 * time.Millisecond)
done1 <- true
}()
<-done1
time.Sleep(10 * time.Millisecond)
if seg1.Duration < 50 {
t.Errorf("Duration of seg1 is too low: %v", seg1.Duration)
}
}
func TestMeasureHandler(t *testing.T) {
agent := NewAgent()
// start HTTP server
go func() {
http.Handle(agent.MeasureHandler("/test1", http.StripPrefix("/test1", http.FileServer(http.Dir("/tmp")))))
if err := http.ListenAndServe(":5010", nil); err != nil {
t.Error(err)
return
}
}()
waitForServer("http://localhost:5010/test1")
res, err := http.Get("http://localhost:5010/test1")
if err != nil {
t.Error(err)
return
} else if res.StatusCode != 200 {
io.Copy(os.Stdout, res.Body)
t.Error(err)
return
} else {
defer res.Body.Close()
}
}
func TestMeasureHandlerFunc(t *testing.T) {
agent := NewAgent()
// start HTTP server
go func() {
http.HandleFunc(agent.MeasureHandlerFunc("/test2", func(w http.ResponseWriter, r *http.Request) {
time.Sleep(100 * time.Millisecond)
fmt.Fprintf(w, "OK")
}))
if err := http.ListenAndServe(":5011", nil); err != nil {
t.Error(err)
return
}
}()
waitForServer("http://localhost:5011/test2")
res, err := http.Get("http://localhost:5011/test2")
if err != nil {
t.Error(err)
return
} else if res.StatusCode != 200 {
t.Error(err)
return
} else {
defer res.Body.Close()
}
}
func TestRecoverPanic(t *testing.T) {
agent := NewAgent()
done := make(chan bool)
go func() {
defer func() {
if err := recover(); err != nil {
t.Error("panic1 unrecovered")
}
}()
defer agent.RecordAndRecoverPanic()
defer func() {
done <- true
}()
panic("panic1")
}()
<-done
}
func waitForServer(url string) {
for {
if _, err := http.Get(url); err == nil {
time.Sleep(100 * time.Millisecond)
break
}
}
}
func BenchmarkProfile(b *testing.B) {
agent := NewAgent()
agent.Start(Options{
AgentKey: "key1",
AppName: "app1",
})
agent.internalAgent.Enable()
b.ResetTimer()
for i := 0; i < b.N; i++ {
s := agent.Profile()
s.Stop()
}
// go test -v -run=^$ -bench=BenchmarkProfile -cpuprofile=cpu.out
// go tool pprof internal.test cpu.out
}
func BenchmarkRecordError(b *testing.B) {
agent := NewAgent()
agent.Start(Options{
AgentKey: "key1",
AppName: "app1",
})
agent.internalAgent.Enable()
err := errors.New("error1")
b.ResetTimer()
for i := 0; i < b.N; i++ {
agent.RecordError(err)
}
// go test -v -run=^$ -bench=BenchmarkRecordError -cpuprofile=cpu.out
// go tool pprof internal.test cpu.out
}