-
Notifications
You must be signed in to change notification settings - Fork 0
/
qps_set.go
64 lines (49 loc) · 1.14 KB
/
qps_set.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
package qube
import (
"sort"
)
type QPSSet []float64
func NewQPSSet(dps []DataPoint) QPSSet {
if len(dps) == 0 {
return nil
}
sort.Slice(dps, func(i, j int) bool {
return dps[i].Time < dps[j].Time
})
baseTime := dps[0].Time
countSet := []int{0}
// Calculate number of queries per second
for _, v := range dps {
// baseTime + 1s <= v.Time
if (baseTime + 1) <= v.Time {
baseTime += 1
countSet = append(countSet, 0)
}
countSet[len(countSet)-1]++
}
qpsSet := make([]float64, len(countSet))
// Convert "countSet" to float64 array
for i, v := range countSet {
qpsSet[i] = float64(v)
}
return qpsSet
}
func (qpsSet QPSSet) Stats() (minQPS float64, maxQPS float64, medianQPS float64) {
sort.Slice(qpsSet, func(i, j int) bool {
return qpsSet[i] < qpsSet[j]
})
minQPS = qpsSet[0]
maxQPS = qpsSet[len(qpsSet)-1]
median := len(qpsSet) / 2
medianNext := median + 1
if len(qpsSet) == 1 {
medianQPS = qpsSet[0]
} else if len(qpsSet) == 2 {
medianQPS = (qpsSet[0] + qpsSet[1]) / 2
} else if len(qpsSet)%2 == 0 {
medianQPS = (qpsSet[median] + qpsSet[medianNext]) / 2
} else {
medianQPS = qpsSet[median]
}
return
}