-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathgraphite_test.go
172 lines (139 loc) · 4.04 KB
/
graphite_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
164
165
166
167
168
169
170
171
172
package report
import (
"bufio"
"net"
"strconv"
"strings"
"sync"
"testing"
"time"
"github.com/rcrowley/go-metrics"
)
func floatEquals(a, b float64) bool {
return (a-b) < 0.000001 && (b-a) < 0.000001
}
func NewTestServer(t *testing.T, prefix string) (map[string]float64, net.Listener, *Recorder, *sync.WaitGroup) {
res := make(map[string]float64)
ln, err := net.Listen("tcp", ":0")
if err != nil {
t.Fatal("could not start dummy server:", err)
}
var wg sync.WaitGroup
t.Log("test")
go func() {
t.Log("test")
for {
conn, err := ln.Accept()
if err != nil {
t.Log("listener failed: ", err)
return
}
r := bufio.NewReader(conn)
line, err := r.ReadString('\n')
for err == nil {
parts := strings.Split(line, " ")
i, _ := strconv.ParseFloat(parts[1], 0)
if testing.Verbose() {
t.Log("recv", parts[0], i)
}
res[parts[0]] = res[parts[0]] + i
line, err = r.ReadString('\n')
}
wg.Done()
conn.Close()
}
}()
r := NewRecorder()
r.Prefix = prefix
r.graphite = ln.Addr().(*net.TCPAddr)
return res, ln, r, &wg
}
type DummyMeter struct {
count int64
rate1 float64
metrics.Meter
}
func (m DummyMeter) Count() int64 { return m.count }
func (m DummyMeter) Rate1() float64 { return m.rate1 }
func (m DummyMeter) Snapshot() metrics.Meter { return m }
var _ (metrics.Meter) = (*DummyMeter)(nil)
func fillMetrics(r *Recorder) {
r.Register("bar", DummyMeter{40, 4.0, metrics.NilMeter{}})
r.Time("baz", time.Second*5)
r.Time("baz", time.Second*4)
r.Time("baz", time.Second*3)
r.Time("baz", time.Second*2)
r.Time("baz", time.Second*1)
}
// TODO: test is producing deadlock
func IgnoreTestGoMetricsWrites(t *testing.T) {
res, l, r, wg := NewTestServer(t, "foobar")
defer l.Close()
metrics.GetOrRegisterCounter("foo", r).Inc(2)
fillMetrics(r)
wg.Add(1)
r.Format = GoMetricsFormats
if testing.Verbose() {
t.Log("Sening go-metrics format to graphite..")
}
r.sendToGraphite()
wg.Wait()
if expected, found := 2.0, res["foobar.foo.count"]; !floatEquals(found, expected) {
t.Fatal("bad value:", expected, found)
}
if expected, found := 40.0, res["foobar.bar.count"]; !floatEquals(found, expected) {
t.Fatal("bad value:", expected, found)
}
if expected, found := 4.0, res["foobar.bar.one-minute"]; !floatEquals(found, expected) {
t.Fatal("bad value:", expected, found)
}
if expected, found := 5.0, res["foobar.baz.count"]; !floatEquals(found, expected) {
t.Fatal("bad value:", expected, found)
}
if expected, found := 5000.0, res["foobar.baz.99-percentile"]; !floatEquals(found, expected) {
t.Fatal("bad value:", expected, found)
}
if expected, found := 3000.0, res["foobar.baz.50-percentile"]; !floatEquals(found, expected) {
t.Fatal("bad value:", expected, found)
}
}
// TODO: test is producing deadlock
func IgnoreTestOstrichWrites(t *testing.T) {
res, l, r, wg := NewTestServer(t, "foobar")
defer l.Close()
fillMetrics(r)
wg.Add(1)
r.Format = OstrichFormats
if testing.Verbose() {
t.Log("Sending ostrich format to graphite..")
}
r.sendToGraphite()
wg.Wait()
if expected, found := 0.0, res["foobar.baz.99-percentile"]; !floatEquals(found, expected) {
t.Fatal("bad value:", expected, found)
}
if expected, found := 0.0, res["foobar.baz.50-percentile"]; !floatEquals(found, expected) {
t.Fatal("bad value:", expected, found)
}
if expected, found := 5000.0, res["foobar.baz.percentiles.p99"]; !floatEquals(found, expected) {
t.Fatal("bad value:", expected, found)
}
if expected, found := 3000.0, res["foobar.baz.percentiles.p50"]; !floatEquals(found, expected) {
t.Fatal("bad value:", expected, found)
}
for k, _ := range res {
delete(res, k)
}
wg.Add(1)
if testing.Verbose() {
t.Log("Sending recently cleared metrics to graphite...")
}
r.sendToGraphite()
wg.Wait()
if expected, found := 0.0, res["foobar.baz.percentiles.p99"]; !floatEquals(found, expected) {
t.Fatal("bad value:", expected, found)
}
if expected, found := 0.0, res["foobar.baz.percentiles.p50"]; !floatEquals(found, expected) {
t.Fatal("bad value:", expected, found)
}
}