-
Notifications
You must be signed in to change notification settings - Fork 77
/
tsdb_test.go
147 lines (117 loc) · 3.11 KB
/
tsdb_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
package mandodb
import (
"os"
"strconv"
"testing"
"time"
"github.com/chenjiandongx/logger"
"github.com/stretchr/testify/assert"
)
// 模拟一些监控指标
var metrics = []string{
"cpu.busy", "cpu.load1", "cpu.load5", "cpu.load15", "cpu.iowait",
"disk.write.ops", "disk.read.ops", "disk.used",
"net.in.bytes", "net.out.bytes", "net.in.packages", "net.out.packages",
"mem.used", "mem.idle", "mem.used.bytes", "mem.total.bytes",
}
func genPoints(ts int64, node, dc int) []*Row {
points := make([]*Row, 0)
for _, metric := range metrics {
points = append(points, &Row{
Metric: metric,
Labels: []Label{
{Name: "node", Value: "vm" + strconv.Itoa(node)},
{Name: "dc", Value: strconv.Itoa(dc)},
},
Point: Point{Ts: ts, Value: float64(ts)},
})
}
return points
}
func TestTSDB_QueryRange(t *testing.T) {
tmpdir := "/tmp/tsdb1"
store := OpenTSDB(WithDataPath(tmpdir), WithLoggerConfig(&logger.Options{
Stdout: true,
ConsoleMode: true,
Level: logger.ErrorLevel,
}))
defer store.Close()
defer os.RemoveAll(tmpdir)
var start int64 = 1600000000
var now = start
for i := 0; i < 720; i++ {
for n := 0; n < 3; n++ {
for j := 0; j < 24; j++ {
_ = store.InsertRows(genPoints(now, n, j))
}
}
now += 60 //1min
}
time.Sleep(time.Millisecond * 20)
ret, err := store.QueryRange("cpu.busy", LabelMatcherSet{
{Name: "node", Value: "vm1"},
{Name: "dc", Value: "0"},
}, start, start+120)
assert.NoError(t, err)
ret[0].Labels.Sorted()
labels := LabelSet{
{"__name__", "cpu.busy"},
{"dc", "0"},
{"node", "vm1"},
}
assert.Equal(t, ret[0].Labels, labels)
values := []int64{start, start + 60, start + 120}
for idx, d := range ret[0].Points {
assert.Equal(t, d.Ts, values[idx])
assert.Equal(t, d.Value, float64(values[idx]))
}
ret, err = store.QueryRange("cpu.busy", LabelMatcherSet{
{Name: "node", Value: "vm1"},
{Name: "dc", Value: "0"},
}, now-120, now)
assert.NoError(t, err)
assert.Equal(t, len(ret[0].Points), 2)
}
func TestTSDB_QuerySeries(t *testing.T) {
tmpdir := "/tmp/tsdb2"
store := OpenTSDB(WithDataPath(tmpdir))
defer store.Close()
defer os.RemoveAll(tmpdir)
var start int64 = 1600000000
var now = start
for i := 0; i < 720; i++ {
for n := 0; n < 3; n++ {
for j := 0; j < 24; j++ {
_ = store.InsertRows(genPoints(now, n, j))
}
}
now += 60 //1min
}
time.Sleep(time.Millisecond * 20)
ret, err := store.QuerySeries(LabelMatcherSet{
{Name: "__name__", Value: "disk.*", IsRegx: true},
{Name: "node", Value: "vm1"},
{Name: "dc", Value: "0"},
}, start, start+120)
assert.NoError(t, err)
assert.Equal(t, len(ret), 3)
}
func TestTSDB_QueryLabelValues(t *testing.T) {
tmpdir := "/tmp/tsdb3"
store := OpenTSDB(WithDataPath(tmpdir))
defer store.Close()
defer os.RemoveAll(tmpdir)
var start int64 = 1600000000
var now = start
for i := 0; i < 720; i++ {
for n := 0; n < 3; n++ {
for j := 0; j < 24; j++ {
_ = store.InsertRows(genPoints(now, n, j))
}
}
now += 60 //1min
}
time.Sleep(time.Millisecond * 20)
ret := store.QueryLabelValues("node", start, start+120)
assert.Equal(t, ret, []string{"vm0", "vm1", "vm2"})
}