github.com/grafana/pyroscope@v1.18.0/pkg/querier/timeline/timeline_test.go (about) 1 package timeline_test 2 3 import ( 4 "testing" 5 "time" 6 7 "github.com/stretchr/testify/assert" 8 9 typesv1 "github.com/grafana/pyroscope/api/gen/proto/go/types/v1" 10 "github.com/grafana/pyroscope/pkg/querier/timeline" 11 ) 12 13 const timelineStepSec = 10 14 15 func Test_No_Backfill(t *testing.T) { 16 const startMs = int64(1692395965) 17 const endMs = startMs + (timelineStepSec * 1000) 18 const stepSec = 10 19 20 points := &typesv1.Series{ 21 Points: []*typesv1.Point{ 22 {Timestamp: startMs, Value: 99}, 23 }, 24 } 25 26 timeline := timeline.New(points, startMs, endMs, stepSec) 27 28 const snappedStartMs = int64(1692390000) 29 assert.Equal(t, snappedStartMs/1000, timeline.StartTime) 30 assert.Equal(t, []uint64{99}, timeline.Samples) 31 } 32 33 func Test_Backfill_Data_Start_End(t *testing.T) { 34 const startMs = int64(1692397017190) 35 const endMs = int64(1692397137190) 36 const stepSec = 10 37 38 points := &typesv1.Series{ 39 Points: []*typesv1.Point{ 40 // 1692397017190 ms 41 // 1692397027190 ms 42 // 1692397037190 ms 43 // 1692397047190 ms 44 // 1692397057190 ms 45 // 1692397067190 ms 46 {Timestamp: 1692397077190, Value: 99}, 47 // 1692397087190 ms 48 // 1692397097190 ms 49 // 1692397107190 ms 50 // 1692397117190 ms 51 // 1692397127190 ms 52 }, 53 } 54 55 timeline := timeline.New(points, startMs, endMs, stepSec) 56 57 const snappedStartMs = int64(1692397010000) 58 assert.Equal(t, snappedStartMs/1000, timeline.StartTime) 59 assert.Equal(t, []uint64{ 60 0, // 1692397010000 ms (1692397017190 ms) 61 0, // 1692397020000 ms (1692397027190 ms) 62 0, // 1692397030000 ms (1692397037190 ms) 63 0, // 1692397040000 ms (1692397047190 ms) 64 0, // 1692397050000 ms (1692397057190 ms) 65 0, // 1692397060000 ms (1692397067190 ms) 66 99, // 1692397070000 ms (1692397077190 ms) 67 0, // 1692397080000 ms (1692397087190 ms) 68 0, // 1692397090000 ms (1692397097190 ms) 69 0, // 1692397100000 ms (1692397107190 ms) 70 0, // 1692397110000 ms (1692397117190 ms) 71 0, // 1692397120000 ms (1692397127190 ms) 72 }, timeline.Samples) 73 } 74 75 func Test_Backfill_Data_Middle(t *testing.T) { 76 const startMs = int64(1692397658567) 77 const endMs = int64(1692397778567) 78 const stepSec = 10 79 80 points := &typesv1.Series{ 81 Points: []*typesv1.Point{ 82 // 1692397658567 ms 83 // 1692397668567 ms 84 // 1692397678567 ms 85 // 1692397688567 ms 86 // 1692397698567 ms 87 // 1692397708567 ms 88 {Timestamp: 1692397718567, Value: 99}, 89 // 1692397728567 ms 90 {Timestamp: 1692397738567, Value: 98}, 91 // 1692397748567 ms 92 // 1692397758567 ms 93 // 1692397768567 ms 94 }, 95 } 96 97 timeline := timeline.New(points, startMs, endMs, stepSec) 98 99 const snappedStartMs = int64(1692397650000) 100 assert.Equal(t, snappedStartMs/1000, timeline.StartTime) 101 assert.Equal(t, []uint64{ 102 0, // 1692397650000 ms (1692397658567 ms) 103 0, // 1692397660000 ms (1692397668567 ms) 104 0, // 1692397670000 ms (1692397678567 ms) 105 0, // 1692397680000 ms (1692397688567 ms) 106 0, // 1692397690000 ms (1692397698567 ms) 107 0, // 1692397700000 ms (1692397708567 ms) 108 99, // 1692397710000 ms (1692397718567 ms) 109 0, // 1692397720000 ms (1692397728567 ms) 110 98, // 1692397730000 ms (1692397738567 ms) 111 0, // 1692397740000 ms (1692397748567 ms) 112 0, // 1692397750000 ms (1692397758567 ms) 113 0, // 1692397760000 ms (1692397768567 ms) 114 }, timeline.Samples) 115 } 116 117 func Test_Backfill_All(t *testing.T) { 118 const startMs = int64(1692398026941) 119 const endMs = int64(1692398146941) 120 const stepSec = 10 121 122 points := &typesv1.Series{ 123 Points: []*typesv1.Point{}, 124 } 125 126 timeline := timeline.New(points, startMs, endMs, stepSec) 127 128 const snappedStartMs = int64(1692398020000) 129 assert.Equal(t, snappedStartMs/1000, timeline.StartTime) 130 assert.Equal(t, []uint64{ 131 0, // 1692398020000 ms (1692398026941 ms) 132 0, // 1692398030000 ms (1692398036941 ms) 133 0, // 1692398040000 ms (1692398046941 ms) 134 0, // 1692398050000 ms (1692398056941 ms) 135 0, // 1692398060000 ms (1692398066941 ms) 136 0, // 1692398070000 ms (1692398076941 ms) 137 0, // 1692398080000 ms (1692398086941 ms) 138 0, // 1692398090000 ms (1692398096941 ms) 139 0, // 1692398100000 ms (1692398106941 ms) 140 0, // 1692398110000 ms (1692398116941 ms) 141 0, // 1692398120000 ms (1692398126941 ms) 142 0, // 1692398130000 ms (1692398136941 ms) 143 }, timeline.Samples) 144 } 145 146 func Test_Backfill_Arbitrary(t *testing.T) { 147 startMs := int64(0) 148 endMs := int64(10 * time.Second / time.Millisecond) 149 step := int64(1) 150 series := &typesv1.Series{ 151 Points: []*typesv1.Point{ 152 // 0 ms 153 // 1000 ms 154 {Timestamp: 2000, Value: 69}, 155 {Timestamp: 3000, Value: 83}, 156 // 4000 ms 157 // 5000 ms 158 {Timestamp: 6000, Value: 85}, 159 // 7000 ms 160 {Timestamp: 8000, Value: 91}, 161 // 9000 ms 162 }, 163 } 164 165 tl := timeline.New(series, startMs, endMs, step) 166 assert.Equal(t, startMs/1000, tl.StartTime) 167 168 assert.Equal(t, []uint64{ 169 0, // 0 ms 170 0, // 1000 ms 171 69, // 2000 ms 172 83, // 3000 ms 173 0, // 4000 ms 174 0, // 5000 ms 175 85, // 6000 ms 176 0, // 7000 ms 177 91, // 8000 ms 178 0, // 9000 ms 179 }, tl.Samples) 180 } 181 182 func Test_Backfill_LastSample(t *testing.T) { 183 const startMs = int64(3000) 184 const stepSec = int64(10) 185 const snappedStartMs = int64(0) 186 187 series := &typesv1.Series{ 188 Points: []*typesv1.Point{ 189 {Timestamp: 23000, Value: 69}, 190 {Timestamp: 53000, Value: 91}, 191 }, 192 } 193 194 t.Run("series value in last step bucket is not included", func(t *testing.T) { 195 const endMs = int64(53000) 196 tl := timeline.New(series, startMs, endMs, stepSec) 197 198 assert.Equal(t, snappedStartMs/1000, tl.StartTime) 199 assert.Equal(t, []uint64{ 200 0, // 3000 ms ( 0 ms) 201 0, // 13000 ms (10000 ms) 202 69, // 23000 ms (20000 ms) 203 0, // 33000 ms (30000 ms) 204 0, // 43000 ms (40000 ms) 205 }, tl.Samples) 206 }) 207 208 t.Run("last bucket does not get backfilled", func(t *testing.T) { 209 const endMs = int64(63000) 210 tl := timeline.New(series, startMs, endMs, stepSec) 211 212 assert.Equal(t, snappedStartMs/1000, tl.StartTime) 213 assert.Equal(t, []uint64{ 214 0, // 3000 ( 0 ms) 215 0, // 13000 (10000 ms) 216 69, // 23000 (20000 ms) 217 0, // 33000 (30000 ms) 218 0, // 43000 (40000 ms) 219 91, // 53000 (50000 ms) 220 }, tl.Samples) 221 }) 222 } 223 224 func Test_Timeline_Bounds(t *testing.T) { 225 const stepSec = int64(1) 226 series := &typesv1.Series{ 227 Points: []*typesv1.Point{ 228 // 0 ms 229 // 1000 ms 230 {Timestamp: 2000, Value: 69}, 231 {Timestamp: 3000, Value: 83}, 232 // 4000 ms 233 // 5000 ms 234 {Timestamp: 6000, Value: 85}, 235 // 7000 ms 236 {Timestamp: 8000, Value: 91}, 237 // 9000 ms 238 }, 239 } 240 241 t.Run("start bounded", func(t *testing.T) { 242 const startMs = int64(1000) 243 const endMs = int64(10_000) 244 245 tl := timeline.New(series, startMs, endMs, stepSec) 246 assert.Equal(t, startMs/1000, tl.StartTime) 247 248 assert.Equal(t, []uint64{ 249 0, // 1000 ms 250 69, // 2000 ms 251 83, // 3000 ms 252 0, // 4000 ms 253 0, // 5000 ms 254 85, // 6000 ms 255 0, // 7000 ms 256 91, // 8000 ms 257 0, // 9000 ms 258 }, tl.Samples) 259 }) 260 261 t.Run("end bounded", func(t *testing.T) { 262 const startMs = int64(0) 263 const endMs = int64(9000) 264 265 tl := timeline.New(series, startMs, endMs, stepSec) 266 assert.Equal(t, startMs/1000, tl.StartTime) 267 268 assert.Equal(t, []uint64{ 269 0, // 0 ms 270 0, // 1000 ms 271 69, // 2000 ms 272 83, // 3000 ms 273 0, // 4000 ms 274 0, // 5000 ms 275 85, // 6000 ms 276 0, // 7000 ms 277 91, // 8000 ms 278 }, tl.Samples) 279 }) 280 281 t.Run("start and end bounded", func(t *testing.T) { 282 const startMs = int64(1000) 283 const endMs = int64(9000) 284 285 tl := timeline.New(series, startMs, endMs, stepSec) 286 assert.Equal(t, startMs/1000, tl.StartTime) 287 288 assert.Equal(t, []uint64{ 289 0, // 1000 ms 290 69, // 2000 ms 291 83, // 3000 ms 292 0, // 4000 ms 293 0, // 5000 ms 294 85, // 6000 ms 295 0, // 7000 ms 296 91, // 8000 ms 297 }, tl.Samples) 298 }) 299 300 t.Run("start == end", func(t *testing.T) { 301 const startMs = int64(1000) 302 const endMs = startMs 303 304 tl := timeline.New(series, startMs, endMs, stepSec) 305 assert.Equal(t, startMs/1000, tl.StartTime) 306 307 assert.Equal(t, []uint64{}, tl.Samples) 308 }) 309 310 t.Run("start > end", func(t *testing.T) { 311 const startMs = int64(9000) 312 const endMs = int64(1000) 313 314 tl := timeline.New(series, startMs, endMs, stepSec) 315 assert.Equal(t, startMs/1000, tl.StartTime) 316 317 assert.Equal(t, []uint64{}, tl.Samples) 318 }) 319 320 t.Run("points are not contained within start and end", func(t *testing.T) { 321 const startMs = int64(10_000) 322 const endMs = int64(20_000) 323 324 tl := timeline.New(series, startMs, endMs, stepSec) 325 assert.Equal(t, startMs/1000, tl.StartTime) 326 327 assert.Equal(t, []uint64{ 328 0, // 10000 ms 329 0, // 11000 ms 330 0, // 12000 ms 331 0, // 13000 ms 332 0, // 14000 ms 333 0, // 15000 ms 334 0, // 16000 ms 335 0, // 17000 ms 336 0, // 18000 ms 337 0, // 19000 ms 338 }, tl.Samples) 339 }) 340 341 t.Run("start is halfway through a bucket window", func(t *testing.T) { 342 const startMs = int64(500) 343 const endMs = int64(9000) 344 345 tl := timeline.New(series, startMs, endMs, stepSec) 346 assert.Equal(t, startMs/1000, tl.StartTime) 347 348 assert.Equal(t, []uint64{ 349 0, // 0 ms 350 0, // 1000 ms 351 69, // 2000 ms 352 83, // 3000 ms 353 0, // 4000 ms 354 0, // 5000 ms 355 85, // 6000 ms 356 0, // 7000 ms 357 91, // 8000 ms 358 }, tl.Samples) 359 }) 360 361 t.Run("end is halfway through a bucket window", func(t *testing.T) { 362 const startMs = int64(0) 363 const endMs = int64(8500) 364 365 tl := timeline.New(series, startMs, endMs, stepSec) 366 assert.Equal(t, startMs/1000, tl.StartTime) 367 368 assert.Equal(t, []uint64{ 369 0, // 0 ms 370 0, // 1000 ms 371 69, // 2000 ms 372 83, // 3000 ms 373 0, // 4000 ms 374 0, // 5000 ms 375 85, // 6000 ms 376 0, // 7000 ms 377 }, tl.Samples) 378 }) 379 }