forked from facebookarchive/flashback
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stats_analyser_test.go
135 lines (118 loc) · 4.22 KB
/
stats_analyser_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
package flashback
import (
"fmt"
"math"
"testing"
"time"
"github.com/facebookgo/ensure"
)
func floatEquals(a float64, b float64, t *testing.T) {
if !(math.Abs(a-b)/b < 5e-2) {
fmt.Println(a, b)
}
ensure.True(t, math.Abs(a-b)/b < 5e-2)
}
func TestBasics(t *testing.T) {
statsChan := make(chan OpStat)
analyser := NewStatsAnalyzer(statsChan)
for i := 0; i < 10; i += 1 {
for _, opType := range AllOpTypes {
statsChan <- OpStat{opType, time.Duration(i) * time.Millisecond, false}
}
}
time.Sleep(100 * time.Millisecond)
status := analyser.GetStatus()
ensure.DeepEqual(t, status.OpsExecuted, int64(10*len(AllOpTypes)))
ensure.DeepEqual(t, status.IntervalOpsExecuted, int64(10*len(AllOpTypes)))
ensure.DeepEqual(t, status.OpsErrors, int64(0))
ensure.DeepEqual(t, status.IntervalOpsErrors, int64(0))
floatEquals(status.OpsPerSec, 689.0, t)
floatEquals(status.IntervalOpsPerSec, 689.0, t)
for _, opType := range AllOpTypes {
ensure.DeepEqual(t, status.Latencies[opType][P50], float64(4))
ensure.DeepEqual(t, status.Latencies[opType][P70], float64(6))
ensure.DeepEqual(t, status.Latencies[opType][P95], float64(8))
ensure.DeepEqual(t, status.Latencies[opType][P99], float64(8))
ensure.DeepEqual(t, status.MaxLatency[opType], float64(9))
ensure.DeepEqual(t, status.IntervalLatencies[opType][P50], float64(4))
ensure.DeepEqual(t, status.IntervalLatencies[opType][P70], float64(6))
ensure.DeepEqual(t, status.IntervalLatencies[opType][P95], float64(8))
ensure.DeepEqual(t, status.IntervalLatencies[opType][P99], float64(8))
ensure.DeepEqual(t, status.IntervalMaxLatency[opType], float64(9))
ensure.DeepEqual(t, status.Counts[opType], int64(10))
ensure.DeepEqual(t, status.IntervalCounts[opType], int64(10))
floatEquals(status.TypeOpsSec[opType], 100.0, t)
floatEquals(status.IntervalTypeOpsSec[opType], 100.0, t)
}
// second interval
for i := 0; i < 10; i += 1 {
for _, opType := range AllOpTypes {
statsChan <- OpStat{opType, time.Duration(i) * time.Millisecond, false}
}
}
statsChan <- OpStat{Insert, 0, true}
time.Sleep(200 * time.Millisecond)
status = analyser.GetStatus()
ensure.DeepEqual(t, status.OpsExecuted, int64(20*len(AllOpTypes))+1)
ensure.DeepEqual(t, status.IntervalOpsExecuted, int64(10*len(AllOpTypes))+1)
ensure.DeepEqual(t, status.OpsErrors, int64(1))
ensure.DeepEqual(t, status.IntervalOpsErrors, int64(1))
floatEquals(status.OpsPerSec, 458.0, t)
floatEquals(status.IntervalOpsPerSec, 352.0, t)
for _, opType := range AllOpTypes {
if opType == Insert {
ensure.DeepEqual(t, status.Counts[opType], int64(21))
ensure.DeepEqual(t, status.IntervalCounts[opType], int64(11))
} else {
ensure.DeepEqual(t, status.Counts[opType], int64(20))
ensure.DeepEqual(t, status.IntervalCounts[opType], int64(10))
floatEquals(status.TypeOpsSec[opType], 66.6, t)
floatEquals(status.IntervalTypeOpsSec[opType], 50.0, t)
}
}
}
func TestLatencies(t *testing.T) {
statsChan := make(chan OpStat)
analyser := NewStatsAnalyzer(statsChan)
start := 1000
for _, opType := range AllOpTypes {
for i := 100; i >= 0; i-- {
statsChan <- OpStat{opType, time.Duration(start+i) * time.Millisecond, false}
}
start += 2000
}
time.Sleep(10)
status := analyser.GetStatus()
// Check results
start = 1000
for _, opType := range AllOpTypes {
latencies := status.Latencies[opType]
intervalLatencies := status.IntervalLatencies[opType]
for i, perc := range latencyPercentiles {
floatEquals(latencies[i], float64(perc*100.0+float64(start)), t)
floatEquals(intervalLatencies[i], float64(perc*100.0+float64(start)), t)
}
start += 2000
}
// -- second round
start = 2000
for _, opType := range AllOpTypes {
for i := 100; i >= 0; i-- {
statsChan <- OpStat{opType, time.Duration(start+i) * time.Millisecond, false}
}
start += 2000
}
time.Sleep(10)
status = analyser.GetStatus()
start = 2000
for _, opType := range AllOpTypes {
latencies := status.Latencies[opType]
intervalLatencies := status.IntervalLatencies[opType]
for i, perc := range latencyPercentiles {
floatEquals(intervalLatencies[i], float64(perc*100.0+float64(start)), t)
}
floatEquals(latencies[len(latencies)-1], float64(start+100), t)
floatEquals(latencies[0], float64(start-1000+100), t)
start += 2000
}
}