forked from open-falcon/rrdlite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rrd_test.go
87 lines (79 loc) · 1.81 KB
/
rrd_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
package rrdlite
import (
"fmt"
"testing"
"time"
)
func TestAll(t *testing.T) {
// Create
const (
dbfile = "/tmp/test.rrd"
step = 1
heartbeat = 2 * step
)
c := NewCreator(dbfile, time.Now(), step)
c.RRA("AVERAGE", 0.5, 1, 100)
c.RRA("AVERAGE", 0.5, 5, 100)
c.DS("cnt", "COUNTER", heartbeat, 0, 100)
c.DS("g", "GAUGE", heartbeat, 0, 60)
err := c.Create(true)
if err != nil {
t.Fatal(err)
}
// Update
u := NewUpdater(dbfile)
for i := 0; i < 10; i++ {
time.Sleep(step * time.Second)
err := u.Update(time.Now(), i, 1.5*float64(i))
if err != nil {
t.Fatal(err)
}
}
// Update with cache
for i := 10; i < 20; i++ {
time.Sleep(step * time.Second)
u.Cache(time.Now(), i, 2*float64(i))
}
err = u.Update()
if err != nil {
t.Fatal(err)
}
// Info
inf, err := Info(dbfile)
if err != nil {
t.Fatal(err)
}
for k, v := range inf {
fmt.Printf("%s (%T): %v\n", k, v, v)
}
// Fetch
end := time.Unix(int64(inf["last_update"].(uint)), 0)
start := end.Add(-20 * step * time.Second)
fmt.Printf("Fetch Params:\n")
fmt.Printf("Start: %s\n", start)
fmt.Printf("End: %s\n", end)
fmt.Printf("Step: %s\n", step*time.Second)
fetchRes, err := Fetch(dbfile, "AVERAGE", start, end, step*time.Second)
if err != nil {
t.Fatal(err)
}
defer fetchRes.FreeValues()
fmt.Printf("FetchResult:\n")
fmt.Printf("Start: %s\n", fetchRes.Start)
fmt.Printf("End: %s\n", fetchRes.End)
fmt.Printf("Step: %s\n", fetchRes.Step)
for _, dsName := range fetchRes.DsNames {
fmt.Printf("\t%s", dsName)
}
fmt.Printf("\n")
row := 0
for ti := fetchRes.Start.Add(fetchRes.Step); ti.Before(end) || ti.Equal(end); ti = ti.Add(fetchRes.Step) {
fmt.Printf("%s / %d", ti, ti.Unix())
for i := 0; i < len(fetchRes.DsNames); i++ {
v := fetchRes.ValueAt(i, row)
fmt.Printf("\t%e", v)
}
fmt.Printf("\n")
row++
}
}