github.com/m3db/m3@v1.5.1-0.20231129193456-75a402aa583b/src/query/functions/temporal/rate_test.go (about) 1 // Copyright (c) 2018 Uber Technologies, Inc. 2 // 3 // Permission is hereby granted, free of charge, to any person obtaining a copy 4 // of this software and associated documentation files (the "Software"), to deal 5 // in the Software without restriction, including without limitation the rights 6 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 // copies of the Software, and to permit persons to whom the Software is 8 // furnished to do so, subject to the following conditions: 9 // 10 // The above copyright notice and this permission notice shall be included in 11 // all copies or substantial portions of the Software. 12 // 13 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 19 // THE SOFTWARE. 20 21 package temporal 22 23 import ( 24 "testing" 25 "time" 26 27 "github.com/m3db/m3/src/query/executor/transform" 28 29 "github.com/stretchr/testify/require" 30 ) 31 32 var rateTestCases = []testCase{ 33 { 34 name: "irate", 35 opType: IRateType, 36 vals: [][]float64{ 37 {nan, 680986, 683214, 685442, 687670, 38 678758, 680986, 683214, 685442, 687670}, 39 {1987036, 1988988, 1990940, 1992892, 1994844, 40 1987036, 1988988, 1990940, 1992892, 1994844}, 41 }, 42 expected: [][]float64{ 43 {nan, nan, 37.1333, 37.1333, 37.1333, 44 11312.6333, 37.1333, 37.1333, 37.1333, 37.1333}, 45 {nan, 32.5333, 32.5333, 32.5333, 32.5333, 46 33117.2666, 32.5333, 32.5333, 32.5333, 32.5333}, 47 }, 48 }, 49 { 50 name: "irate with some NaNs", 51 opType: IRateType, 52 vals: [][]float64{ 53 {nan, 1988988, 1990940, nan, 1994844, 54 1987036, 1988988, 1990940, nan, 1994844}, 55 {1987036, 1988988, 1990940, nan, nan, 56 1987036, 1988988, 1990940, nan, nan}, 57 }, 58 expected: [][]float64{ 59 {nan, nan, 32.5333, 32.5333, 32.5333, 60 33117.2666, 32.5333, 32.5333, 32.5333, 32.5333}, 61 {nan, 32.5333, 32.5333, 32.5333, 32.5333, 62 11039.0888, 32.5333, 32.5333, 32.5333, 32.5333}, 63 }, 64 }, 65 { 66 name: "irate with all NaNs", 67 opType: IRateType, 68 vals: [][]float64{ 69 {nan, nan, nan, nan, nan, nan, nan, nan, nan, nan}, 70 {nan, nan, nan, nan, nan, nan, nan, nan, nan, nan}, 71 }, 72 expected: [][]float64{ 73 {nan, nan, nan, nan, nan, nan, nan, nan, nan, nan}, 74 {nan, nan, nan, nan, nan, nan, nan, nan, nan, nan}, 75 }, 76 }, 77 { 78 name: "rate", 79 opType: RateType, 80 vals: [][]float64{ 81 {nan, 61943808, 61943808, 61943808, 62205952, 82 61108224, 61943808, 61943808, 61943808, 62205952}, 83 {1987036, 1988988, 1990940, 1992892, 1994844, 84 1987036, 1988988, 1990940, 1992892, 1994844}, 85 }, 86 expected: [][]float64{ 87 {nan, nan, 0, 0, 1019.44889, 88 255709.8666, 259191.4666, 259191.4666, 258099.2, 4573.8666}, 89 {nan, 9.760000, 16.26666, 22.77333, 32.53333, 90 8303.7166, 8303.7166, 8303.7166, 8303.7166, 32.53333}, 91 }, 92 }, 93 { 94 name: "rate with some NaNs", 95 opType: RateType, 96 vals: [][]float64{ 97 {nan, 61943808, 61943808, 62205952, nan, 98 61108224, 61943808, 61943808, 62205952, nan}, 99 {1987036, 1988988, 1990940, nan, 1994844, 100 1987036, 1988988, 1990940, nan, 1994844}, 101 }, 102 expected: [][]float64{ 103 {nan, nan, 0, 1092.266673, 1529.17334, 104 255709.8666, 259191.4666, 258099.2, 4268.9422, 6098.48888}, 105 {nan, 9.760000, 16.26666, 22.77333, 32.533333, 106 8303.7166, 8303.7166, 7742.5444, 11060.7777, 32.5333}, 107 }, 108 }, 109 { 110 name: "rate with all NaNs", 111 opType: RateType, 112 vals: [][]float64{ 113 {nan, nan, nan, nan, nan, nan, nan, nan, nan, nan}, 114 {nan, nan, nan, nan, nan, nan, nan, nan, nan, nan}, 115 }, 116 expected: [][]float64{ 117 {nan, nan, nan, nan, nan, nan, nan, nan, nan, nan}, 118 {nan, nan, nan, nan, nan, nan, nan, nan, nan, nan}, 119 }, 120 }, 121 } 122 123 func TestRate(t *testing.T) { 124 testRate(t, rateTestCases) 125 } 126 127 var deltaTestCases = []testCase{ 128 { 129 name: "idelta", 130 opType: IDeltaType, 131 vals: [][]float64{ 132 {nan, 865910, 868138, 870366, 872594, 133 863682, 865910, 868138, 870366, 872594}, 134 {1987036, 1988988, 1990940, 1992892, 1994844, 135 1987036, 1988988, 1990940, 1992892, 1994844}, 136 }, 137 expected: [][]float64{ 138 {nan, nan, 2228, 2228, 2228, 139 -8912, 2228, 2228, 2228, 2228}, 140 {nan, 1952, 1952, 1952, 1952, 141 -7808, 1952, 1952, 1952, 1952}, 142 }, 143 }, 144 { 145 name: "idelta with some NaNs", 146 opType: IDeltaType, 147 vals: [][]float64{ 148 {nan, 1988988, 1990940, nan, 1994844, 149 1987036, 1988988, 1990940, nan, 1994844}, 150 {1987036, 1988988, 1990940, nan, nan, 151 1987036, 1988988, 1990940, nan, nan}, 152 }, 153 expected: [][]float64{ 154 {nan, nan, 1952, 1952, 3904, 155 -7808, 1952, 1952, 1952, 3904}, 156 {nan, 1952, 1952, 1952, 1952, 157 -3904, 1952, 1952, 1952, 1952}, 158 }, 159 }, 160 { 161 name: "idelta with all NaNs", 162 opType: IDeltaType, 163 vals: [][]float64{ 164 {nan, nan, nan, nan, nan, nan, nan, nan, nan, nan}, 165 {nan, nan, nan, nan, nan, nan, nan, nan, nan, nan}, 166 }, 167 expected: [][]float64{ 168 {nan, nan, nan, nan, nan, nan, nan, nan, nan, nan}, 169 {nan, nan, nan, nan, nan, nan, nan, nan, nan, nan}, 170 }, 171 }, 172 { 173 name: "delta", 174 opType: DeltaType, 175 vals: [][]float64{ 176 {nan, 680986, 683214, 685442, 687670, 177 678758, 680986, 683214, 685442, 687670}, 178 {2299, 2299, 2299, 2787, 2787, 179 2299, 2299, 2299, 2787, 2787}, 180 }, 181 expected: [][]float64{ 182 {nan, nan, 3342.000037, 5570.000037, 7798.00003, 183 -2785, -2785, -2785, -2785, 11140}, 184 {nan, 0, 0, 569.33333, 610, 185 0, 0, -610, 0, 610}, 186 }, 187 }, 188 { 189 name: "delta with some NaNs", 190 opType: DeltaType, 191 vals: [][]float64{ 192 {nan, 680986, 683214, 685442, nan, 193 678758, 680986, 683214, 685442, nan}, 194 {2299, 2299, 2299, nan, 2787, 195 2299, 2299, 2299, nan, 2787}, 196 }, 197 expected: [][]float64{ 198 {nan, nan, 3342.000037, 5570.000037, 7798.00003, 199 -2785, -2785, -2785, 7798.000037, 11140}, 200 {nan, 0, 0, 0, 610, 201 0, 0, -569.33333, -813.33333, 610}, 202 }, 203 }, 204 { 205 name: "delta with all NaNs", 206 opType: DeltaType, 207 vals: [][]float64{ 208 {nan, nan, nan, nan, nan, nan, nan, nan, nan, nan}, 209 {nan, nan, nan, nan, nan, nan, nan, nan, nan, nan}, 210 }, 211 expected: [][]float64{ 212 {nan, nan, nan, nan, nan, nan, nan, nan, nan, nan}, 213 {nan, nan, nan, nan, nan, nan, nan, nan, nan, nan}, 214 }, 215 }, 216 } 217 218 func TestDelta(t *testing.T) { 219 testRate(t, deltaTestCases) 220 } 221 222 var increaseTestCases = []testCase{ 223 { 224 name: "increase", 225 opType: IncreaseType, 226 vals: [][]float64{ 227 {nan, 865910, 868138, 870366, 872594, 228 872594, 865910, 868138, 870366, 872594}, 229 {1987036, 1988988, 1990940, 1992892, 1994844, 230 1987036, 1988988, 1990940, 1992892, 1994844}, 231 }, 232 expected: [][]float64{ 233 {nan, nan, 3342, 5570, 7798, 234 8355, 1087957.5, 1087957.5, 1087957.5, 1090742.5}, 235 {nan, 2928, 4880, 6832, 9760, 236 2491115, 2491115, 2491115, 2491115, 9760}, 237 }, 238 }, 239 { 240 name: "increase with some NaNs", 241 opType: IncreaseType, 242 vals: [][]float64{ 243 {nan, 865910, 868138, 872694, nan, 244 872594, 865910, 868138, 872694, nan}, 245 {1987036, 1988988, 1990940, nan, 1994844, 246 1987036, 1988988, 1990940, nan, 1994844}, 247 }, 248 expected: [][]float64{ 249 {nan, nan, 3342.000037, 8480, 11872, 250 1099222.5, 2178825, 2175915, 1018143.00484, 1454490}, 251 {nan, 2928, 4880, 6832, 9760, 252 2491115, 2491115, 2322763.34439, 3318233.3333, 9760}, 253 }, 254 }, 255 { 256 name: "increase with all NaNs", 257 opType: IncreaseType, 258 vals: [][]float64{ 259 {nan, nan, nan, nan, nan, nan, nan, nan, nan, nan}, 260 {nan, nan, nan, nan, nan, nan, nan, nan, nan, nan}, 261 }, 262 expected: [][]float64{ 263 {nan, nan, nan, nan, nan, nan, nan, nan, nan, nan}, 264 {nan, nan, nan, nan, nan, nan, nan, nan, nan, nan}, 265 }, 266 }, 267 } 268 269 func TestIncrease(t *testing.T) { 270 testRate(t, increaseTestCases) 271 } 272 273 func testRate(t *testing.T, testCases []testCase) { 274 opGen := func(t *testing.T, tc testCase) transform.Params { 275 op, err := NewRateOp([]interface{}{5 * time.Minute}, tc.opType) 276 require.NoError(t, err) 277 return op 278 } 279 280 testTemporalFunc(t, opGen, testCases) 281 } 282 283 func TestUnknownRate(t *testing.T) { 284 _, err := NewRateOp([]interface{}{5 * time.Minute}, "unknown_rate_func") 285 require.Error(t, err) 286 }