github.com/thanos-io/thanos@v0.32.5/pkg/queryfrontend/downsampled_test.go (about) 1 // Copyright (c) The Thanos Authors. 2 // Licensed under the Apache License 2.0. 3 4 package queryfrontend 5 6 import ( 7 "testing" 8 9 "github.com/efficientgo/core/testutil" 10 "github.com/thanos-io/thanos/internal/cortex/cortexpb" 11 "github.com/thanos-io/thanos/internal/cortex/querier/queryrange" 12 ) 13 14 func TestDownsampled_MinResponseTime(t *testing.T) { 15 for _, tc := range []struct { 16 desc string 17 sampleStreams []queryrange.SampleStream 18 expected int64 19 }{ 20 { 21 desc: "empty []sampleStream", 22 sampleStreams: []queryrange.SampleStream{}, 23 expected: -1, 24 }, 25 { 26 desc: "one SampleStream with zero samples", 27 sampleStreams: []queryrange.SampleStream{ 28 { 29 Samples: []cortexpb.Sample{}, 30 }, 31 }, 32 expected: -1, 33 }, 34 { 35 desc: "one SampleStream with one sample at zero time", 36 sampleStreams: []queryrange.SampleStream{ 37 { 38 Samples: []cortexpb.Sample{ 39 {TimestampMs: 0}, 40 }, 41 }, 42 }, 43 expected: 0, 44 }, 45 { 46 desc: "one SampleStream with one sample", 47 sampleStreams: []queryrange.SampleStream{ 48 { 49 Samples: []cortexpb.Sample{ 50 {TimestampMs: 1}, 51 }, 52 }, 53 }, 54 expected: 1, 55 }, 56 { 57 desc: "two SampleStreams, first is the earliest", 58 sampleStreams: []queryrange.SampleStream{ 59 { 60 Samples: []cortexpb.Sample{ 61 {TimestampMs: 1}, 62 }, 63 }, 64 { 65 Samples: []cortexpb.Sample{ 66 {TimestampMs: 2}, 67 }, 68 }, 69 }, 70 expected: 1, 71 }, 72 { 73 desc: "three SampleStreams, second is earliest", 74 sampleStreams: []queryrange.SampleStream{ 75 { 76 Samples: []cortexpb.Sample{ 77 {TimestampMs: 2}, 78 {TimestampMs: 3}, 79 }, 80 }, 81 { 82 Samples: []cortexpb.Sample{ 83 {TimestampMs: 1}, 84 }, 85 }, 86 { 87 Samples: []cortexpb.Sample{ 88 {TimestampMs: 2}, 89 }, 90 }, 91 }, 92 expected: 1, 93 }, 94 { 95 desc: "three SampleStreams, last is earliest", 96 sampleStreams: []queryrange.SampleStream{ 97 { 98 Samples: []cortexpb.Sample{ 99 {TimestampMs: 2}, 100 {TimestampMs: 3}, 101 }, 102 }, 103 { 104 Samples: []cortexpb.Sample{ 105 {TimestampMs: 2}, 106 }, 107 }, 108 { 109 Samples: []cortexpb.Sample{ 110 {TimestampMs: 1}, 111 }, 112 }, 113 }, 114 expected: 1, 115 }, 116 { 117 desc: "three histogram SampleStreams, last is earliest", 118 sampleStreams: []queryrange.SampleStream{ 119 { 120 Histograms: []queryrange.SampleHistogramPair{ 121 {Timestamp: 2}, 122 {Timestamp: 3}, 123 }, 124 }, 125 { 126 Histograms: []queryrange.SampleHistogramPair{ 127 {Timestamp: 2}, 128 }, 129 }, 130 { 131 Histograms: []queryrange.SampleHistogramPair{ 132 {Timestamp: 1}, 133 }, 134 }, 135 }, 136 expected: 1, 137 }, 138 { 139 desc: "mixed float and histogram SampleStreams, float is earliest", 140 sampleStreams: []queryrange.SampleStream{ 141 { 142 Samples: []cortexpb.Sample{ 143 {TimestampMs: 1}, 144 }, 145 }, 146 { 147 Histograms: []queryrange.SampleHistogramPair{ 148 {Timestamp: 2}, 149 }, 150 }, 151 }, 152 expected: 1, 153 }, 154 { 155 desc: "mixed float and histogram SampleStreams, float is earliest", 156 sampleStreams: []queryrange.SampleStream{ 157 { 158 Samples: []cortexpb.Sample{ 159 {TimestampMs: 1}, 160 }, 161 Histograms: []queryrange.SampleHistogramPair{ 162 {Timestamp: 2}, 163 }, 164 }, 165 }, 166 expected: 1, 167 }, 168 { 169 desc: "mixed float and histogram SampleStreams, histogram is earliest", 170 sampleStreams: []queryrange.SampleStream{ 171 { 172 Samples: []cortexpb.Sample{ 173 {TimestampMs: 3}, 174 }, 175 }, 176 { 177 Histograms: []queryrange.SampleHistogramPair{ 178 {Timestamp: 2}, 179 }, 180 }, 181 }, 182 expected: 2, 183 }, 184 { 185 desc: "mixed float and histogram SampleStream, histogram is earliest", 186 sampleStreams: []queryrange.SampleStream{ 187 { 188 Samples: []cortexpb.Sample{ 189 {TimestampMs: 3}, 190 }, 191 Histograms: []queryrange.SampleHistogramPair{ 192 {Timestamp: 2}, 193 }, 194 }, 195 }, 196 expected: 2, 197 }, 198 } { 199 t.Run(tc.desc, func(t *testing.T) { 200 pr := queryrange.NewEmptyPrometheusResponse() 201 pr.Data.Result = tc.sampleStreams 202 res := minResponseTime(pr) 203 testutil.Equals(t, tc.expected, res) 204 }) 205 } 206 }