|
| 1 | +package expr |
| 2 | + |
| 3 | +import ( |
| 4 | + "math" |
| 5 | + "strconv" |
| 6 | + "testing" |
| 7 | + |
| 8 | + "github.com/grafana/metrictank/api/models" |
| 9 | + "github.com/grafana/metrictank/test" |
| 10 | + "gopkg.in/raintank/schema.v1" |
| 11 | +) |
| 12 | + |
| 13 | +func TestScaleToSecondsSingle(t *testing.T) { |
| 14 | + testScaleToSeconds( |
| 15 | + "identity", |
| 16 | + []models.Series{ |
| 17 | + { |
| 18 | + Interval: 10, |
| 19 | + QueryPatt: "a", |
| 20 | + Target: "a", |
| 21 | + Datapoints: getCopy(a), |
| 22 | + }, |
| 23 | + }, |
| 24 | + []models.Series{ |
| 25 | + { |
| 26 | + Interval: 10, |
| 27 | + QueryPatt: "scaleToSeconds(a,10)", |
| 28 | + Datapoints: getCopy(a), |
| 29 | + }, |
| 30 | + }, |
| 31 | + t, |
| 32 | + 10, |
| 33 | + ) |
| 34 | +} |
| 35 | + |
| 36 | +func TestScaleToSecondsSingleAllNonNull(t *testing.T) { |
| 37 | + out := []schema.Point{ |
| 38 | + {Val: 0, Ts: 10}, |
| 39 | + {Val: 3.0437127721620759e+19, Ts: 20}, |
| 40 | + {Val: 1.8354510353341003e+20, Ts: 30}, |
| 41 | + {Val: 2.674777890687885e+19, Ts: 40}, |
| 42 | + {Val: 7.3786976294838198e+19, Ts: 50}, |
| 43 | + {Val: 2.3058430092136936e+20, Ts: 60}, |
| 44 | + } |
| 45 | + |
| 46 | + testScaleToSeconds( |
| 47 | + "identity-largeseconds", |
| 48 | + []models.Series{ |
| 49 | + { |
| 50 | + Interval: 10, |
| 51 | + QueryPatt: "d", |
| 52 | + Target: "d", |
| 53 | + Datapoints: getCopy(d), |
| 54 | + }, |
| 55 | + }, |
| 56 | + []models.Series{ |
| 57 | + { |
| 58 | + Interval: 10, |
| 59 | + QueryPatt: "scaleToSeconds(d,9223372036854774784)", |
| 60 | + Datapoints: out, |
| 61 | + }, |
| 62 | + }, |
| 63 | + t, |
| 64 | + 9223372036854774784, |
| 65 | + ) |
| 66 | +} |
| 67 | + |
| 68 | +func TestScaleToSecondsMulti(t *testing.T) { |
| 69 | + out1 := []schema.Point{ |
| 70 | + {Val: 0, Ts: 10}, |
| 71 | + {Val: math.Inf(0), Ts: 20}, |
| 72 | + {Val: math.Inf(0), Ts: 30}, |
| 73 | + {Val: math.NaN(), Ts: 40}, |
| 74 | + {Val: 123456.7890, Ts: 50}, |
| 75 | + {Val: math.NaN(), Ts: 60}, |
| 76 | + } |
| 77 | + out2 := []schema.Point{ |
| 78 | + {Val: 0, Ts: 10}, |
| 79 | + {Val: 0, Ts: 20}, |
| 80 | + {Val: 0.0001, Ts: 30}, |
| 81 | + {Val: 0.0002, Ts: 40}, |
| 82 | + {Val: 0.0003, Ts: 50}, |
| 83 | + {Val: 0.0004, Ts: 60}, |
| 84 | + } |
| 85 | + testScaleToSeconds( |
| 86 | + "multiple-series-subseconds", |
| 87 | + []models.Series{ |
| 88 | + { |
| 89 | + Interval: 10, |
| 90 | + QueryPatt: "b.*", |
| 91 | + Target: "b.*", |
| 92 | + Datapoints: getCopy(b), |
| 93 | + }, |
| 94 | + { |
| 95 | + Interval: 10, |
| 96 | + QueryPatt: "c.foo{bar,baz}", |
| 97 | + Target: "c.foo{bar,baz}", |
| 98 | + Datapoints: getCopy(c), |
| 99 | + }, |
| 100 | + }, |
| 101 | + []models.Series{ |
| 102 | + { |
| 103 | + QueryPatt: "scaleToSeconds(b.*,0)", |
| 104 | + Datapoints: out1, |
| 105 | + }, |
| 106 | + { |
| 107 | + QueryPatt: "scaleToSeconds(c.foo{bar,baz},0)", |
| 108 | + Datapoints: out2, |
| 109 | + }, |
| 110 | + }, |
| 111 | + t, |
| 112 | + 0.001, |
| 113 | + ) |
| 114 | +} |
| 115 | + |
| 116 | +func testScaleToSeconds(name string, in []models.Series, out []models.Series, t *testing.T, seconds float64) { |
| 117 | + f := NewScaleToSeconds() |
| 118 | + f.(*FuncScaleToSeconds).in = NewMock(in) |
| 119 | + f.(*FuncScaleToSeconds).seconds = seconds |
| 120 | + gots, err := f.Exec(make(map[Req][]models.Series)) |
| 121 | + if err != nil { |
| 122 | + t.Fatalf("case %q (%f): err should be nil. got %q", name, seconds, err) |
| 123 | + } |
| 124 | + if len(gots) != len(out) { |
| 125 | + t.Fatalf("case %q (%f): isNonNull len output expected %d, got %d", name, seconds, len(out), len(gots)) |
| 126 | + } |
| 127 | + for i, g := range gots { |
| 128 | + exp := out[i] |
| 129 | + if g.QueryPatt != exp.QueryPatt { |
| 130 | + t.Fatalf("case %q (%f): expected target %q, got %q", name, seconds, exp.QueryPatt, g.QueryPatt) |
| 131 | + } |
| 132 | + if len(g.Datapoints) != len(exp.Datapoints) { |
| 133 | + t.Fatalf("case %q (%f) len output expected %d, got %d", name, seconds, len(exp.Datapoints), len(g.Datapoints)) |
| 134 | + } |
| 135 | + for j, p := range g.Datapoints { |
| 136 | + bothNaN := math.IsNaN(p.Val) && math.IsNaN(exp.Datapoints[j].Val) |
| 137 | + if (bothNaN || p.Val == exp.Datapoints[j].Val) && p.Ts == exp.Datapoints[j].Ts { |
| 138 | + continue |
| 139 | + } |
| 140 | + t.Fatalf("case %q (%f): output point %d - expected %v got %v", name, seconds, j, exp.Datapoints[j], p) |
| 141 | + } |
| 142 | + } |
| 143 | +} |
| 144 | + |
| 145 | +func BenchmarkScaleToSeconds10k_1NoNulls(b *testing.B) { |
| 146 | + benchmarkScaleToSeconds(b, 1, test.RandFloats10k, test.RandFloats10k) |
| 147 | +} |
| 148 | +func BenchmarkScaleToSeconds10k_10NoNulls(b *testing.B) { |
| 149 | + benchmarkScaleToSeconds(b, 10, test.RandFloats10k, test.RandFloats10k) |
| 150 | +} |
| 151 | +func BenchmarkScaleToSeconds10k_100NoNulls(b *testing.B) { |
| 152 | + benchmarkScaleToSeconds(b, 100, test.RandFloats10k, test.RandFloats10k) |
| 153 | +} |
| 154 | +func BenchmarkScaleToSeconds10k_1000NoNulls(b *testing.B) { |
| 155 | + benchmarkScaleToSeconds(b, 1000, test.RandFloats10k, test.RandFloats10k) |
| 156 | +} |
| 157 | + |
| 158 | +func BenchmarkScaleToSeconds10k_1SomeSeriesHalfNulls(b *testing.B) { |
| 159 | + benchmarkScaleToSeconds(b, 1, test.RandFloats10k, test.RandFloatsWithNulls10k) |
| 160 | +} |
| 161 | +func BenchmarkScaleToSeconds10k_10SomeSeriesHalfNulls(b *testing.B) { |
| 162 | + benchmarkScaleToSeconds(b, 10, test.RandFloats10k, test.RandFloatsWithNulls10k) |
| 163 | +} |
| 164 | +func BenchmarkScaleToSeconds10k_100SomeSeriesHalfNulls(b *testing.B) { |
| 165 | + benchmarkScaleToSeconds(b, 100, test.RandFloats10k, test.RandFloatsWithNulls10k) |
| 166 | +} |
| 167 | +func BenchmarkScaleToSeconds10k_1000SomeSeriesHalfNulls(b *testing.B) { |
| 168 | + benchmarkScaleToSeconds(b, 1000, test.RandFloats10k, test.RandFloatsWithNulls10k) |
| 169 | +} |
| 170 | + |
| 171 | +func BenchmarkScaleToSeconds10k_1AllSeriesHalfNulls(b *testing.B) { |
| 172 | + benchmarkScaleToSeconds(b, 1, test.RandFloatsWithNulls10k, test.RandFloatsWithNulls10k) |
| 173 | +} |
| 174 | +func BenchmarkScaleToSeconds10k_10AllSeriesHalfNulls(b *testing.B) { |
| 175 | + benchmarkScaleToSeconds(b, 10, test.RandFloatsWithNulls10k, test.RandFloatsWithNulls10k) |
| 176 | +} |
| 177 | +func BenchmarkScaleToSeconds10k_100AllSeriesHalfNulls(b *testing.B) { |
| 178 | + benchmarkScaleToSeconds(b, 100, test.RandFloatsWithNulls10k, test.RandFloatsWithNulls10k) |
| 179 | +} |
| 180 | +func BenchmarkScaleToSeconds10k_1000AllSeriesHalfNulls(b *testing.B) { |
| 181 | + benchmarkScaleToSeconds(b, 1000, test.RandFloatsWithNulls10k, test.RandFloatsWithNulls10k) |
| 182 | +} |
| 183 | + |
| 184 | +func benchmarkScaleToSeconds(b *testing.B, numSeries int, fn0, fn1 func() []schema.Point) { |
| 185 | + var input []models.Series |
| 186 | + for i := 0; i < numSeries; i++ { |
| 187 | + series := models.Series{ |
| 188 | + QueryPatt: strconv.Itoa(i), |
| 189 | + } |
| 190 | + if i%2 == 0 { |
| 191 | + series.Datapoints = fn0() |
| 192 | + } else { |
| 193 | + series.Datapoints = fn1() |
| 194 | + } |
| 195 | + input = append(input, series) |
| 196 | + } |
| 197 | + b.ResetTimer() |
| 198 | + for i := 0; i < b.N; i++ { |
| 199 | + f := NewScaleToSeconds() |
| 200 | + f.(*FuncScaleToSeconds).in = NewMock(input) |
| 201 | + got, err := f.Exec(make(map[Req][]models.Series)) |
| 202 | + if err != nil { |
| 203 | + b.Fatalf("%s", err) |
| 204 | + } |
| 205 | + results = got |
| 206 | + } |
| 207 | +} |
0 commit comments