From eba07a7d3d43ff9b95998cde072c98e57e17cb13 Mon Sep 17 00:00:00 2001 From: Fabian Reinartz Date: Tue, 12 May 2015 12:21:24 +0200 Subject: [PATCH] Migrate histogram tests to test language. --- promql/promql_test.go | 184 -------------------------------- promql/setup_test.go | 85 --------------- promql/testdata/histograms.test | 141 ++++++++++++++++++++++++ 3 files changed, 141 insertions(+), 269 deletions(-) create mode 100644 promql/testdata/histograms.test diff --git a/promql/promql_test.go b/promql/promql_test.go index 4201dacea..acc2d8bfd 100644 --- a/promql/promql_test.go +++ b/promql/promql_test.go @@ -627,14 +627,6 @@ func TestExpressions(t *testing.T) { `x{y="testvalue"} => 100 @[%v]`, `label_grouping_test{a="a", b="abb"} => 200 @[%v]`, `label_grouping_test{a="aa", b="bb"} => 100 @[%v]`, - `testhistogram_bucket{le="0.1", start="positive"} => 50 @[%v]`, - `testhistogram_bucket{le=".2", start="positive"} => 70 @[%v]`, - `testhistogram_bucket{le="1e0", start="positive"} => 110 @[%v]`, - `testhistogram_bucket{le="+Inf", start="positive"} => 120 @[%v]`, - `testhistogram_bucket{le="-.2", start="negative"} => 10 @[%v]`, - `testhistogram_bucket{le="-0.1", start="negative"} => 20 @[%v]`, - `testhistogram_bucket{le="0.3", start="negative"} => 20 @[%v]`, - `testhistogram_bucket{le="+Inf", start="negative"} => 30 @[%v]`, `request_duration_seconds_bucket{instance="ins1", job="job1", le="0.1"} => 10 @[%v]`, `request_duration_seconds_bucket{instance="ins1", job="job1", le="0.2"} => 30 @[%v]`, `request_duration_seconds_bucket{instance="ins1", job="job1", le="+Inf"} => 40 @[%v]`, @@ -934,182 +926,6 @@ func TestExpressions(t *testing.T) { `{a="aa", b="bb"} => 100 @[%v]`, }, }, - // Quantile too low. - { - expr: `histogram_quantile(-0.1, testhistogram_bucket)`, - output: []string{ - `{start="positive"} => -Inf @[%v]`, - `{start="negative"} => -Inf @[%v]`, - }, - }, - // Quantile too high. - { - expr: `histogram_quantile(1.01, testhistogram_bucket)`, - output: []string{ - `{start="positive"} => +Inf @[%v]`, - `{start="negative"} => +Inf @[%v]`, - }, - }, - // Quantile value in lowest bucket, which is positive. - { - expr: `histogram_quantile(0, testhistogram_bucket{start="positive"})`, - output: []string{ - `{start="positive"} => 0 @[%v]`, - }, - }, - // Quantile value in lowest bucket, which is negative. - { - expr: `histogram_quantile(0, testhistogram_bucket{start="negative"})`, - output: []string{ - `{start="negative"} => -0.2 @[%v]`, - }, - }, - // Quantile value in highest bucket. - { - expr: `histogram_quantile(1, testhistogram_bucket)`, - output: []string{ - `{start="positive"} => 1 @[%v]`, - `{start="negative"} => 0.3 @[%v]`, - }, - }, - // Finally some useful quantiles. - { - expr: `histogram_quantile(0.2, testhistogram_bucket)`, - output: []string{ - `{start="positive"} => 0.048 @[%v]`, - `{start="negative"} => -0.2 @[%v]`, - }, - }, - { - expr: `histogram_quantile(0.5, testhistogram_bucket)`, - output: []string{ - `{start="positive"} => 0.15 @[%v]`, - `{start="negative"} => -0.15 @[%v]`, - }, - }, - { - expr: `histogram_quantile(0.8, testhistogram_bucket)`, - output: []string{ - `{start="positive"} => 0.72 @[%v]`, - `{start="negative"} => 0.3 @[%v]`, - }, - }, - // More realistic with rates. - { - expr: `histogram_quantile(0.2, rate(testhistogram_bucket[5m]))`, - output: []string{ - `{start="positive"} => 0.048 @[%v]`, - `{start="negative"} => -0.2 @[%v]`, - }, - }, - { - expr: `histogram_quantile(0.5, rate(testhistogram_bucket[5m]))`, - output: []string{ - `{start="positive"} => 0.15 @[%v]`, - `{start="negative"} => -0.15 @[%v]`, - }, - }, - { - expr: `histogram_quantile(0.8, rate(testhistogram_bucket[5m]))`, - output: []string{ - `{start="positive"} => 0.72 @[%v]`, - `{start="negative"} => 0.3 @[%v]`, - }, - }, - // Aggregated histogram: Everything in one. - { - expr: `histogram_quantile(0.3, sum(rate(request_duration_seconds_bucket[5m])) by (le))`, - output: []string{ - `{} => 0.075 @[%v]`, - }, - }, - { - expr: `histogram_quantile(0.5, sum(rate(request_duration_seconds_bucket[5m])) by (le))`, - output: []string{ - `{} => 0.1277777777777778 @[%v]`, - }, - }, - // Aggregated histogram: Everything in one. Now with avg, which does not change anything. - { - expr: `histogram_quantile(0.3, avg(rate(request_duration_seconds_bucket[5m])) by (le))`, - output: []string{ - `{} => 0.075 @[%v]`, - }, - }, - { - expr: `histogram_quantile(0.5, avg(rate(request_duration_seconds_bucket[5m])) by (le))`, - output: []string{ - `{} => 0.12777777777777778 @[%v]`, - }, - }, - // Aggregated histogram: By job. - { - expr: `histogram_quantile(0.3, sum(rate(request_duration_seconds_bucket[5m])) by (le, instance))`, - output: []string{ - `{instance="ins1"} => 0.075 @[%v]`, - `{instance="ins2"} => 0.075 @[%v]`, - }, - }, - { - expr: `histogram_quantile(0.5, sum(rate(request_duration_seconds_bucket[5m])) by (le, instance))`, - output: []string{ - `{instance="ins1"} => 0.1333333333 @[%v]`, - `{instance="ins2"} => 0.125 @[%v]`, - }, - }, - // Aggregated histogram: By instance. - { - expr: `histogram_quantile(0.3, sum(rate(request_duration_seconds_bucket[5m])) by (le, job))`, - output: []string{ - `{job="job1"} => 0.1 @[%v]`, - `{job="job2"} => 0.0642857142857143 @[%v]`, - }, - }, - { - expr: `histogram_quantile(0.5, sum(rate(request_duration_seconds_bucket[5m])) by (le, job))`, - output: []string{ - `{job="job1"} => 0.14 @[%v]`, - `{job="job2"} => 0.1125 @[%v]`, - }, - }, - // Aggregated histogram: By job and instance. - { - expr: `histogram_quantile(0.3, sum(rate(request_duration_seconds_bucket[5m])) by (le, job, instance))`, - output: []string{ - `{instance="ins1", job="job1"} => 0.11 @[%v]`, - `{instance="ins2", job="job1"} => 0.09 @[%v]`, - `{instance="ins1", job="job2"} => 0.06 @[%v]`, - `{instance="ins2", job="job2"} => 0.0675 @[%v]`, - }, - }, - { - expr: `histogram_quantile(0.5, sum(rate(request_duration_seconds_bucket[5m])) by (le, job, instance))`, - output: []string{ - `{instance="ins1", job="job1"} => 0.15 @[%v]`, - `{instance="ins2", job="job1"} => 0.1333333333333333 @[%v]`, - `{instance="ins1", job="job2"} => 0.1 @[%v]`, - `{instance="ins2", job="job2"} => 0.1166666666666667 @[%v]`, - }, - }, - // The unaggregated histogram for comparison. Same result as the previous one. - { - expr: `histogram_quantile(0.3, rate(request_duration_seconds_bucket[5m]))`, - output: []string{ - `{instance="ins1", job="job1"} => 0.11 @[%v]`, - `{instance="ins2", job="job1"} => 0.09 @[%v]`, - `{instance="ins1", job="job2"} => 0.06 @[%v]`, - `{instance="ins2", job="job2"} => 0.0675 @[%v]`, - }, - }, - { - expr: `histogram_quantile(0.5, rate(request_duration_seconds_bucket[5m]))`, - output: []string{ - `{instance="ins1", job="job1"} => 0.15 @[%v]`, - `{instance="ins2", job="job1"} => 0.13333333333333333 @[%v]`, - `{instance="ins1", job="job2"} => 0.1 @[%v]`, - `{instance="ins2", job="job2"} => 0.11666666666666667 @[%v]`, - }, - }, { expr: `12.34e6`, output: []string{`scalar: 12340000 @[%v]`}, diff --git a/promql/setup_test.go b/promql/setup_test.go index 796c93896..a02ee5a66 100644 --- a/promql/setup_test.go +++ b/promql/setup_test.go @@ -209,91 +209,6 @@ var testMatrix = Matrix{ }, Values: getTestValueStream(0, 200, 20, testStartTime), }, - // Two histograms with 4 buckets each (*_sum and *_count not included, - // only buckets). Lowest bucket for one histogram < 0, for the other > - // 0. They have the same name, just separated by label. Not useful in - // practice, but can happen (if clients change bucketing), and the - // server has to cope with it. - { - Metric: clientmodel.COWMetric{ - Metric: clientmodel.Metric{ - clientmodel.MetricNameLabel: "testhistogram_bucket", - "le": "0.1", - "start": "positive", - }, - }, - Values: getTestValueStream(0, 50, 5, testStartTime), - }, - { - Metric: clientmodel.COWMetric{ - Metric: clientmodel.Metric{ - clientmodel.MetricNameLabel: "testhistogram_bucket", - "le": ".2", - "start": "positive", - }, - }, - Values: getTestValueStream(0, 70, 7, testStartTime), - }, - { - Metric: clientmodel.COWMetric{ - Metric: clientmodel.Metric{ - clientmodel.MetricNameLabel: "testhistogram_bucket", - "le": "1e0", - "start": "positive", - }, - }, - Values: getTestValueStream(0, 110, 11, testStartTime), - }, - { - Metric: clientmodel.COWMetric{ - Metric: clientmodel.Metric{ - clientmodel.MetricNameLabel: "testhistogram_bucket", - "le": "+Inf", - "start": "positive", - }, - }, - Values: getTestValueStream(0, 120, 12, testStartTime), - }, - { - Metric: clientmodel.COWMetric{ - Metric: clientmodel.Metric{ - clientmodel.MetricNameLabel: "testhistogram_bucket", - "le": "-.2", - "start": "negative", - }, - }, - Values: getTestValueStream(0, 10, 1, testStartTime), - }, - { - Metric: clientmodel.COWMetric{ - Metric: clientmodel.Metric{ - clientmodel.MetricNameLabel: "testhistogram_bucket", - "le": "-0.1", - "start": "negative", - }, - }, - Values: getTestValueStream(0, 20, 2, testStartTime), - }, - { - Metric: clientmodel.COWMetric{ - Metric: clientmodel.Metric{ - clientmodel.MetricNameLabel: "testhistogram_bucket", - "le": "0.3", - "start": "negative", - }, - }, - Values: getTestValueStream(0, 20, 2, testStartTime), - }, - { - Metric: clientmodel.COWMetric{ - Metric: clientmodel.Metric{ - clientmodel.MetricNameLabel: "testhistogram_bucket", - "le": "+Inf", - "start": "negative", - }, - }, - Values: getTestValueStream(0, 30, 3, testStartTime), - }, // Now a more realistic histogram per job and instance to test aggregation. { Metric: clientmodel.COWMetric{ diff --git a/promql/testdata/histograms.test b/promql/testdata/histograms.test new file mode 100644 index 000000000..6d1f16dfa --- /dev/null +++ b/promql/testdata/histograms.test @@ -0,0 +1,141 @@ +# Two histograms with 4 buckets each (x_sum and x_count not included, +# only buckets). Lowest bucket for one histogram < 0, for the other > +# 0. They have the same name, just separated by label. Not useful in +# practice, but can happen (if clients change bucketing), and the +# server has to cope with it. + +# Test histogram. +load 5m + testhistogram_bucket{le="0.1", start="positive"} 0+5x10 + testhistogram_bucket{le=".2", start="positive"} 0+7x10 + testhistogram_bucket{le="1e0", start="positive"} 0+11x10 + testhistogram_bucket{le="+Inf", start="positive"} 0+12x10 + testhistogram_bucket{le="-.2", start="negative"} 0+1x10 + testhistogram_bucket{le="-0.1", start="negative"} 0+2x10 + testhistogram_bucket{le="0.3", start="negative"} 0+2x10 + testhistogram_bucket{le="+Inf", start="negative"} 0+3x10 + + +# Now a more realistic histogram per job and instance to test aggregation. +load 5m + request_duration_seconds_bucket{job="job1", instance="ins1", le="0.1"} 0+1x10 + request_duration_seconds_bucket{job="job1", instance="ins1", le="0.2"} 0+3x10 + request_duration_seconds_bucket{job="job1", instance="ins1", le="+Inf"} 0+4x10 + request_duration_seconds_bucket{job="job1", instance="ins2", le="0.1"} 0+2x10 + request_duration_seconds_bucket{job="job1", instance="ins2", le="0.2"} 0+5x10 + request_duration_seconds_bucket{job="job1", instance="ins2", le="+Inf"} 0+6x10 + request_duration_seconds_bucket{job="job2", instance="ins1", le="0.1"} 0+3x10 + request_duration_seconds_bucket{job="job2", instance="ins1", le="0.2"} 0+4x10 + request_duration_seconds_bucket{job="job2", instance="ins1", le="+Inf"} 0+6x10 + request_duration_seconds_bucket{job="job2", instance="ins2", le="0.1"} 0+4x10 + request_duration_seconds_bucket{job="job2", instance="ins2", le="0.2"} 0+7x10 + request_duration_seconds_bucket{job="job2", instance="ins2", le="+Inf"} 0+9x10 + + +# Quantile too low. +eval instant at 50m histogram_quantile(-0.1, testhistogram_bucket) + {start="positive"} -Inf + {start="negative"} -Inf + +# Quantile too high. +eval instant at 50m histogram_quantile(1.01, testhistogram_bucket) + {start="positive"} +Inf + {start="negative"} +Inf + +# Quantile value in lowest bucket, which is positive. +eval instant at 50m histogram_quantile(0, testhistogram_bucket{start="positive"}) + {start="positive"} 0 + +# Quantile value in lowest bucket, which is negative. +eval instant at 50m histogram_quantile(0, testhistogram_bucket{start="negative"}) + {start="negative"} -0.2 + +# Quantile value in highest bucket. +eval instant at 50m histogram_quantile(1, testhistogram_bucket) + {start="positive"} 1 + {start="negative"} 0.3 + +# Finally some useful quantiles. +eval instant at 50m histogram_quantile(0.2, testhistogram_bucket) + {start="positive"} 0.048 + {start="negative"} -0.2 + + +eval instant at 50m histogram_quantile(0.5, testhistogram_bucket) + {start="positive"} 0.15 + {start="negative"} -0.15 + +eval instant at 50m histogram_quantile(0.8, testhistogram_bucket) + {start="positive"} 0.72 + {start="negative"} 0.3 + +# More realistic with rates. +eval instant at 50m histogram_quantile(0.2, rate(testhistogram_bucket[5m])) + {start="positive"} 0.048 + {start="negative"} -0.2 + +eval instant at 50m histogram_quantile(0.5, rate(testhistogram_bucket[5m])) + {start="positive"} 0.15 + {start="negative"} -0.15 + +eval instant at 50m histogram_quantile(0.8, rate(testhistogram_bucket[5m])) + {start="positive"} 0.72 + {start="negative"} 0.3 + +# Aggregated histogram: Everything in one. +eval instant at 50m histogram_quantile(0.3, sum(rate(request_duration_seconds_bucket[5m])) by (le)) + {} 0.075 + +eval instant at 50m histogram_quantile(0.5, sum(rate(request_duration_seconds_bucket[5m])) by (le)) + {} 0.1277777777777778 + +# Aggregated histogram: Everything in one. Now with avg, which does not change anything. +eval instant at 50m histogram_quantile(0.3, avg(rate(request_duration_seconds_bucket[5m])) by (le)) + {} 0.075 + +eval instant at 50m histogram_quantile(0.5, avg(rate(request_duration_seconds_bucket[5m])) by (le)) + {} 0.12777777777777778 + +# Aggregated histogram: By job. +eval instant at 50m histogram_quantile(0.3, sum(rate(request_duration_seconds_bucket[5m])) by (le, instance)) + {instance="ins1"} 0.075 + {instance="ins2"} 0.075 + +eval instant at 50m histogram_quantile(0.5, sum(rate(request_duration_seconds_bucket[5m])) by (le, instance)) + {instance="ins1"} 0.1333333333 + {instance="ins2"} 0.125 + +# Aggregated histogram: By instance. +eval instant at 50m histogram_quantile(0.3, sum(rate(request_duration_seconds_bucket[5m])) by (le, job)) + {job="job1"} 0.1 + {job="job2"} 0.0642857142857143 + +eval instant at 50m histogram_quantile(0.5, sum(rate(request_duration_seconds_bucket[5m])) by (le, job)) + {job="job1"} 0.14 + {job="job2"} 0.1125 + +# Aggregated histogram: By job and instance. +eval instant at 50m histogram_quantile(0.3, sum(rate(request_duration_seconds_bucket[5m])) by (le, job, instance)) + {instance="ins1", job="job1"} 0.11 + {instance="ins2", job="job1"} 0.09 + {instance="ins1", job="job2"} 0.06 + {instance="ins2", job="job2"} 0.0675 + +eval instant at 50m histogram_quantile(0.5, sum(rate(request_duration_seconds_bucket[5m])) by (le, job, instance)) + {instance="ins1", job="job1"} 0.15 + {instance="ins2", job="job1"} 0.1333333333333333 + {instance="ins1", job="job2"} 0.1 + {instance="ins2", job="job2"} 0.1166666666666667 + +# The unaggregated histogram for comparison. Same result as the previous one. +eval instant at 50m histogram_quantile(0.3, rate(request_duration_seconds_bucket[5m])) + {instance="ins1", job="job1"} 0.11 + {instance="ins2", job="job1"} 0.09 + {instance="ins1", job="job2"} 0.06 + {instance="ins2", job="job2"} 0.0675 + +eval instant at 50m histogram_quantile(0.5, rate(request_duration_seconds_bucket[5m])) + {instance="ins1", job="job1"} 0.15 + {instance="ins2", job="job1"} 0.13333333333333333 + {instance="ins1", job="job2"} 0.1 + {instance="ins2", job="job2"} 0.11666666666666667 \ No newline at end of file