summaryrefslogtreecommitdiffstats
path: root/vendor/github.com/armon/go-metrics/inmem_test.go
blob: ed3b521da7bab2992b489b74b6e649dc10d4b510 (plain)
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
173
174
175
176
177
178
package metrics

import (
	"math"
	"net/url"
	"strings"
	"testing"
	"time"
)

func TestInmemSink(t *testing.T) {
	inm := NewInmemSink(10*time.Millisecond, 50*time.Millisecond)

	data := inm.Data()
	if len(data) != 1 {
		t.Fatalf("bad: %v", data)
	}

	// Add data points
	inm.SetGauge([]string{"foo", "bar"}, 42)
	inm.EmitKey([]string{"foo", "bar"}, 42)
	inm.IncrCounter([]string{"foo", "bar"}, 20)
	inm.IncrCounter([]string{"foo", "bar"}, 22)
	inm.AddSample([]string{"foo", "bar"}, 20)
	inm.AddSample([]string{"foo", "bar"}, 22)

	data = inm.Data()
	if len(data) != 1 {
		t.Fatalf("bad: %v", data)
	}

	intvM := data[0]
	intvM.RLock()

	if time.Now().Sub(intvM.Interval) > 10*time.Millisecond {
		t.Fatalf("interval too old")
	}
	if intvM.Gauges["foo.bar"] != 42 {
		t.Fatalf("bad val: %v", intvM.Gauges)
	}
	if intvM.Points["foo.bar"][0] != 42 {
		t.Fatalf("bad val: %v", intvM.Points)
	}

	agg := intvM.Counters["foo.bar"]
	if agg.Count != 2 {
		t.Fatalf("bad val: %v", agg)
	}
	if agg.Rate != 200 {
		t.Fatalf("bad val: %v", agg.Rate)
	}
	if agg.Sum != 42 {
		t.Fatalf("bad val: %v", agg)
	}
	if agg.SumSq != 884 {
		t.Fatalf("bad val: %v", agg)
	}
	if agg.Min != 20 {
		t.Fatalf("bad val: %v", agg)
	}
	if agg.Max != 22 {
		t.Fatalf("bad val: %v", agg)
	}
	if agg.Mean() != 21 {
		t.Fatalf("bad val: %v", agg)
	}
	if agg.Stddev() != math.Sqrt(2) {
		t.Fatalf("bad val: %v", agg)
	}

	if agg.LastUpdated.IsZero() {
		t.Fatalf("agg.LastUpdated is not set: %v", agg)
	}

	diff := time.Now().Sub(agg.LastUpdated).Seconds()
	if diff > 1 {
		t.Fatalf("time diff too great: %f", diff)
	}

	if agg = intvM.Samples["foo.bar"]; agg == nil {
		t.Fatalf("missing sample")
	}

	intvM.RUnlock()

	for i := 1; i < 10; i++ {
		time.Sleep(10 * time.Millisecond)
		inm.SetGauge([]string{"foo", "bar"}, 42)
		data = inm.Data()
		if len(data) != min(i+1, 5) {
			t.Fatalf("bad: %v", data)
		}
	}

	// Should not exceed 5 intervals!
	time.Sleep(10 * time.Millisecond)
	inm.SetGauge([]string{"foo", "bar"}, 42)
	data = inm.Data()
	if len(data) != 5 {
		t.Fatalf("bad: %v", data)
	}
}

func TestNewInmemSinkFromURL(t *testing.T) {
	for _, tc := range []struct {
		desc           string
		input          string
		expectErr      string
		expectInterval time.Duration
		expectRetain   time.Duration
	}{
		{
			desc:           "interval and duration are set via query params",
			input:          "inmem://?interval=11s&retain=22s",
			expectInterval: duration(t, "11s"),
			expectRetain:   duration(t, "22s"),
		},
		{
			desc:      "interval is required",
			input:     "inmem://?retain=22s",
			expectErr: "Bad 'interval' param",
		},
		{
			desc:      "interval must be a duration",
			input:     "inmem://?retain=30s&interval=HIYA",
			expectErr: "Bad 'interval' param",
		},
		{
			desc:      "retain is required",
			input:     "inmem://?interval=30s",
			expectErr: "Bad 'retain' param",
		},
		{
			desc:      "retain must be a valid duration",
			input:     "inmem://?interval=30s&retain=HELLO",
			expectErr: "Bad 'retain' param",
		},
	} {
		t.Run(tc.desc, func(t *testing.T) {
			u, err := url.Parse(tc.input)
			if err != nil {
				t.Fatalf("error parsing URL: %s", err)
			}
			ms, err := NewInmemSinkFromURL(u)
			if tc.expectErr != "" {
				if !strings.Contains(err.Error(), tc.expectErr) {
					t.Fatalf("expected err: %q, to contain: %q", err, tc.expectErr)
				}
			} else {
				if err != nil {
					t.Fatalf("unexpected err: %s", err)
				}
				is := ms.(*InmemSink)
				if is.interval != tc.expectInterval {
					t.Fatalf("expected interval %s, got: %s", tc.expectInterval, is.interval)
				}
				if is.retain != tc.expectRetain {
					t.Fatalf("expected retain %s, got: %s", tc.expectRetain, is.retain)
				}
			}
		})
	}
}

func min(a, b int) int {
	if a < b {
		return a
	}
	return b
}

func duration(t *testing.T, s string) time.Duration {
	dur, err := time.ParseDuration(s)
	if err != nil {
		t.Fatalf("error parsing duration: %s", err)
	}
	return dur
}