metric_test.go 1.46 KB
Newer Older
“李磊”'s avatar
“李磊” committed
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
package stats

import (
	"testing"
	"time"
)

func TestCalculateRate(t *testing.T) {
	ts1 := time.Now().Unix()
	time.Sleep(1 * time.Second)
	ts2 := time.Now().Unix()

	v1 := metric{value: 1, ts: ts1}
	v2 := metric{value: 0, ts: ts1}
	result := calculateRate(v1, v2)
	t.Log("v1.Value > v2.Value && v1.Timestamp == v2.Timestamp, result:", result)
	if result != 0 {
		t.Fatal("unexpected result")
	}

	v1 = metric{value: 1, ts: ts1}
	v2 = metric{value: 1, ts: ts1}
	result = calculateRate(v1, v2)
	t.Log("v1.Value == v2.Value && v1.Timestamp == v2.Timestamp, result:", result)
	if result != 0 {
		t.Fatal("unexpected result")
	}

	v1 = metric{value: 1, ts: ts1}
	v2 = metric{value: 2, ts: ts1}
	result = calculateRate(v1, v2)
	t.Log("v1.Value < v2.Value && v1.Timestamp == v2.Timestamp, result:", result)
	if result != 0 {
		t.Fatal("unexpected result")
	}

	v1 = metric{value: 1, ts: ts2}
	v2 = metric{value: 2, ts: ts1}
	result = calculateRate(v1, v2)
	t.Log("v1.Value < v2.Value && v1.Timestamp > v2.Timestamp, result:", result)
	if result != 0 {
		t.Fatal("unexpected result")
	}

	v1 = metric{value: 1, ts: ts1}
	v2 = metric{value: 2, ts: ts2}
	result = calculateRate(v1, v2)
	t.Log("v1.Value < v2.Value && v1.Timestamp < v2.Timestamp, result:", result)
	if result == 0 {
		t.Fatal("unexpected result")
	}

	v1 = metric{value: 1024, ts: ts1}
	v2 = metric{value: 2048, ts: ts2}
	result = calculateRate(v1, v2)
	t.Log("rate is", result)
	if result != 1024 {
		t.Fatal("unexpected result")
	}
}