| 1 |
package memmetrics |
| 2 |
|
| 3 |
import ( |
| 4 |
"strconv" |
| 5 |
"testing" |
| 6 |
"time" |
| 7 |
|
| 8 |
"github.com/stretchr/testify/assert" |
| 9 |
"github.com/vulcand/oxy/v2/internal/holsterv4/clock" |
| 10 |
) |
| 11 |
|
| 12 |
func TestMedian(t *testing.T) { |
| 13 |
testCases := []struct { |
| 14 |
desc string |
| 15 |
values []float64 |
| 16 |
expected float64 |
| 17 |
}{ |
| 18 |
{ |
| 19 |
desc: "2 values", |
| 20 |
values: []float64{0.1, 0.2}, |
| 21 |
expected: (float64(0.1) + float64(0.2)) / 2.0, |
| 22 |
}, |
| 23 |
{ |
| 24 |
desc: "3 values", |
| 25 |
values: []float64{0.3, 0.2, 0.5}, |
| 26 |
expected: 0.3, |
| 27 |
}, |
| 28 |
} |
| 29 |
|
| 30 |
for _, test := range testCases { |
| 31 |
test := test |
| 32 |
t.Run(test.desc, func(t *testing.T) { |
| 33 |
t.Parallel() |
| 34 |
|
| 35 |
actual := median(test.values) |
| 36 |
assert.Equal(t, test.expected, actual) |
| 37 |
}) |
| 38 |
} |
| 39 |
} |
| 40 |
|
| 41 |
func TestSplitRatios(t *testing.T) { |
| 42 |
testCases := []struct { |
| 43 |
values []float64 |
| 44 |
good []float64 |
| 45 |
bad []float64 |
| 46 |
}{ |
| 47 |
{ |
| 48 |
values: []float64{0, 0}, |
| 49 |
good: []float64{0}, |
| 50 |
bad: []float64{}, |
| 51 |
}, |
| 52 |
|
| 53 |
{ |
| 54 |
values: []float64{0, 1}, |
| 55 |
good: []float64{0}, |
| 56 |
bad: []float64{1}, |
| 57 |
}, |
| 58 |
{ |
| 59 |
values: []float64{0.1, 0.1}, |
| 60 |
good: []float64{0.1}, |
| 61 |
bad: []float64{}, |
| 62 |
}, |
| 63 |
|
| 64 |
{ |
| 65 |
values: []float64{0.15, 0.1}, |
| 66 |
good: []float64{0.15, 0.1}, |
| 67 |
bad: []float64{}, |
| 68 |
}, |
| 69 |
{ |
| 70 |
values: []float64{0.01, 0.01}, |
| 71 |
good: []float64{0.01}, |
| 72 |
bad: []float64{}, |
| 73 |
}, |
| 74 |
{ |
| 75 |
values: []float64{0.012, 0.01, 1}, |
| 76 |
good: []float64{0.012, 0.01}, |
| 77 |
bad: []float64{1}, |
| 78 |
}, |
| 79 |
{ |
| 80 |
values: []float64{0, 0, 1, 1}, |
| 81 |
good: []float64{0}, |
| 82 |
bad: []float64{1}, |
| 83 |
}, |
| 84 |
{ |
| 85 |
values: []float64{0, 0.1, 0.1, 0}, |
| 86 |
good: []float64{0}, |
| 87 |
bad: []float64{0.1}, |
| 88 |
}, |
| 89 |
{ |
| 90 |
values: []float64{0, 0.01, 0.1, 0}, |
| 91 |
good: []float64{0}, |
| 92 |
bad: []float64{0.01, 0.1}, |
| 93 |
}, |
| 94 |
{ |
| 95 |
values: []float64{0, 0.01, 0.02, 1}, |
| 96 |
good: []float64{0, 0.01, 0.02}, |
| 97 |
bad: []float64{1}, |
| 98 |
}, |
| 99 |
{ |
| 100 |
values: []float64{0, 0, 0, 0, 0, 0.01, 0.02, 1}, |
| 101 |
good: []float64{0}, |
| 102 |
bad: []float64{0.01, 0.02, 1}, |
| 103 |
}, |
| 104 |
} |
| 105 |
|
| 106 |
for ind, test := range testCases { |
| 107 |
test := test |
| 108 |
t.Run(strconv.Itoa(ind), func(t *testing.T) { |
| 109 |
t.Parallel() |
| 110 |
|
| 111 |
good, bad := SplitRatios(test.values) |
| 112 |
|
| 113 |
vgood := make(map[float64]bool, len(test.good)) |
| 114 |
for _, v := range test.good { |
| 115 |
vgood[v] = true |
| 116 |
} |
| 117 |
|
| 118 |
vbad := make(map[float64]bool, len(test.bad)) |
| 119 |
for _, v := range test.bad { |
| 120 |
vbad[v] = true |
| 121 |
} |
| 122 |
|
| 123 |
assert.Equal(t, vgood, good) |
| 124 |
assert.Equal(t, vbad, bad) |
| 125 |
}) |
| 126 |
} |
| 127 |
} |
| 128 |
|
| 129 |
func TestSplitLatencies(t *testing.T) { |
| 130 |
testCases := []struct { |
| 131 |
values []int |
| 132 |
good []int |
| 133 |
bad []int |
| 134 |
}{ |
| 135 |
{ |
| 136 |
values: []int{0, 0}, |
| 137 |
good: []int{0}, |
| 138 |
bad: []int{}, |
| 139 |
}, |
| 140 |
{ |
| 141 |
values: []int{1, 2}, |
| 142 |
good: []int{1, 2}, |
| 143 |
bad: []int{}, |
| 144 |
}, |
| 145 |
{ |
| 146 |
values: []int{1, 2, 4}, |
| 147 |
good: []int{1, 2, 4}, |
| 148 |
bad: []int{}, |
| 149 |
}, |
| 150 |
{ |
| 151 |
values: []int{8, 8, 18}, |
| 152 |
good: []int{8}, |
| 153 |
bad: []int{18}, |
| 154 |
}, |
| 155 |
{ |
| 156 |
values: []int{32, 28, 11, 26, 19, 51, 25, 39, 28, 26, 8, 97}, |
| 157 |
good: []int{32, 28, 11, 26, 19, 51, 25, 39, 28, 26, 8}, |
| 158 |
bad: []int{97}, |
| 159 |
}, |
| 160 |
{ |
| 161 |
values: []int{1, 2, 4, 40}, |
| 162 |
good: []int{1, 2, 4}, |
| 163 |
bad: []int{40}, |
| 164 |
}, |
| 165 |
{ |
| 166 |
values: []int{40, 60, 1000}, |
| 167 |
good: []int{40, 60}, |
| 168 |
bad: []int{1000}, |
| 169 |
}, |
| 170 |
} |
| 171 |
|
| 172 |
for ind, test := range testCases { |
| 173 |
test := test |
| 174 |
t.Run(strconv.Itoa(ind), func(t *testing.T) { |
| 175 |
t.Parallel() |
| 176 |
|
| 177 |
values := make([]time.Duration, len(test.values)) |
| 178 |
for i, d := range test.values { |
| 179 |
values[i] = clock.Millisecond * time.Duration(d) |
| 180 |
} |
| 181 |
|
| 182 |
good, bad := SplitLatencies(values, clock.Millisecond) |
| 183 |
|
| 184 |
vgood := make(map[time.Duration]bool, len(test.good)) |
| 185 |
for _, v := range test.good { |
| 186 |
vgood[time.Duration(v)*clock.Millisecond] = true |
| 187 |
} |
| 188 |
assert.Equal(t, vgood, good) |
| 189 |
|
| 190 |
vbad := make(map[time.Duration]bool, len(test.bad)) |
| 191 |
for _, v := range test.bad { |
| 192 |
vbad[time.Duration(v)*clock.Millisecond] = true |
| 193 |
} |
| 194 |
assert.Equal(t, vbad, bad) |
| 195 |
}) |
| 196 |
} |
| 197 |
} |