From 01880dc50b9f7921dd45465b84ac33e00b88a627 Mon Sep 17 00:00:00 2001 From: Jeanette Tan Date: Fri, 27 Jan 2023 18:38:41 +0800 Subject: [PATCH] Add benchmarks for marshalling Signed-off-by: Jeanette Tan --- model/value_float_test.go | 80 +++--- model/value_histogram_test.go | 468 +++++++++++++++++----------------- 2 files changed, 287 insertions(+), 261 deletions(-) diff --git a/model/value_float_test.go b/model/value_float_test.go index 5ceb56e6..911d0171 100644 --- a/model/value_float_test.go +++ b/model/value_float_test.go @@ -20,6 +20,42 @@ import ( "testing" ) +var ( + samplePairMatrixPlain = `[{"metric":{"__name__":"test_metric"},"values":[[1234.567,"123.1"],[12345.678,"123.12"]]},{"metric":{"foo":"bar"},"values":[[2234.567,"223.1"],[22345.678,"223.12"]]}]` + samplePairMatrixValue = Matrix{ + &SampleStream{ + Metric: Metric{ + MetricNameLabel: "test_metric", + }, + Values: []SamplePair{ + { + Value: 123.1, + Timestamp: 1234567, + }, + { + Value: 123.12, + Timestamp: 12345678, + }, + }, + }, + &SampleStream{ + Metric: Metric{ + "foo": "bar", + }, + Values: []SamplePair{ + { + Value: 223.1, + Timestamp: 2234567, + }, + { + Value: 223.12, + Timestamp: 22345678, + }, + }, + }, + } +) + func TestEqualValues(t *testing.T) { tests := map[string]struct { in1, in2 SampleValue @@ -231,39 +267,8 @@ func TestMatrixJSON(t *testing.T) { value: Matrix{}, }, { - plain: `[{"metric":{"__name__":"test_metric"},"values":[[1234.567,"123.1"],[12345.678,"123.12"]]},{"metric":{"foo":"bar"},"values":[[2234.567,"223.1"],[22345.678,"223.12"]]}]`, - value: Matrix{ - &SampleStream{ - Metric: Metric{ - MetricNameLabel: "test_metric", - }, - Values: []SamplePair{ - { - Value: 123.1, - Timestamp: 1234567, - }, - { - Value: 123.12, - Timestamp: 12345678, - }, - }, - }, - &SampleStream{ - Metric: Metric{ - "foo": "bar", - }, - Values: []SamplePair{ - { - Value: 223.1, - Timestamp: 2234567, - }, - { - Value: 223.12, - Timestamp: 22345678, - }, - }, - }, - }, + plain: samplePairMatrixPlain, + value: samplePairMatrixValue, }, } @@ -291,3 +296,12 @@ func TestMatrixJSON(t *testing.T) { } } } + +func BenchmarkJSONMarshallingSamplePairMatrix(b *testing.B) { + for i := 0; i < b.N; i++ { + _, err := json.Marshal(samplePairMatrixValue) + if err != nil { + b.Fatal("error marshalling") + } + } +} diff --git a/model/value_histogram_test.go b/model/value_histogram_test.go index db75630d..f36306fb 100644 --- a/model/value_histogram_test.go +++ b/model/value_histogram_test.go @@ -22,6 +22,235 @@ import ( var ( noWhitespace = regexp.MustCompile(`\s`) + + sampleHistogramPairMatrixPlain = `[ + { + "metric":{ + "__name__":"test_metric" + }, + "histograms":[ + [ + 1234.567, + { + "count":"6", + "sum":"3897", + "buckets":[ + [ + 1, + "-4870.992343051145", + "-4466.7196729968955", + "1" + ], + [ + 1, + "-861.0779292198035", + "-789.6119426088657", + "1" + ], + [ + 1, + "-558.3399591246119", + "-512", + "1" + ], + [ + 0, + "2048", + "2233.3598364984477", + "1" + ], + [ + 0, + "2896.3093757400984", + "3158.4477704354626", + "1" + ], + [ + 0, + "4466.7196729968955", + "4870.992343051145", + "1" + ] + ] + } + ], + [ + 12345.678, + { + "count":"6", + "sum":"3897", + "buckets":[ + [ + 1, + "-4870.992343051145", + "-4466.7196729968955", + "1" + ], + [ + 1, + "-861.0779292198035", + "-789.6119426088657", + "1" + ], + [ + 1, + "-558.3399591246119", + "-512", + "1" + ], + [ + 0, + "2048", + "2233.3598364984477", + "1" + ], + [ + 0, + "2896.3093757400984", + "3158.4477704354626", + "1" + ], + [ + 0, + "4466.7196729968955", + "4870.992343051145", + "1" + ] + ] + } + ] + ] + }, + { + "metric":{ + "foo":"bar" + }, + "histograms":[ + [ + 2234.567, + { + "count":"6", + "sum":"3897", + "buckets":[ + [ + 1, + "-4870.992343051145", + "-4466.7196729968955", + "1" + ], + [ + 1, + "-861.0779292198035", + "-789.6119426088657", + "1" + ], + [ + 1, + "-558.3399591246119", + "-512", + "1" + ], + [ + 0, + "2048", + "2233.3598364984477", + "1" + ], + [ + 0, + "2896.3093757400984", + "3158.4477704354626", + "1" + ], + [ + 0, + "4466.7196729968955", + "4870.992343051145", + "1" + ] + ] + } + ], + [ + 22345.678, + { + "count":"6", + "sum":"3897", + "buckets":[ + [ + 1, + "-4870.992343051145", + "-4466.7196729968955", + "1" + ], + [ + 1, + "-861.0779292198035", + "-789.6119426088657", + "1" + ], + [ + 1, + "-558.3399591246119", + "-512", + "1" + ], + [ + 0, + "2048", + "2233.3598364984477", + "1" + ], + [ + 0, + "2896.3093757400984", + "3158.4477704354626", + "1" + ], + [ + 0, + "4466.7196729968955", + "4870.992343051145", + "1" + ] + ] + } + ] + ] + } + ]` + sampleHistogramPairMatrixValue = Matrix{ + &SampleStream{ + Metric: Metric{ + MetricNameLabel: "test_metric", + }, + Histograms: []SampleHistogramPair{ + { + Histogram: genSampleHistogram(), + Timestamp: 1234567, + }, + { + Histogram: genSampleHistogram(), + Timestamp: 12345678, + }, + }, + }, + &SampleStream{ + Metric: Metric{ + "foo": "bar", + }, + Histograms: []SampleHistogramPair{ + { + Histogram: genSampleHistogram(), + Timestamp: 2234567, + }, + { + Histogram: genSampleHistogram(), + Timestamp: 22345678, + }, + }, + }, + } ) func genSampleHistogram() *SampleHistogram { @@ -487,234 +716,8 @@ func TestMatrixHistogramJSON(t *testing.T) { value: Matrix{}, }, { - plain: `[ - { - "metric":{ - "__name__":"test_metric" - }, - "histograms":[ - [ - 1234.567, - { - "count":"6", - "sum":"3897", - "buckets":[ - [ - 1, - "-4870.992343051145", - "-4466.7196729968955", - "1" - ], - [ - 1, - "-861.0779292198035", - "-789.6119426088657", - "1" - ], - [ - 1, - "-558.3399591246119", - "-512", - "1" - ], - [ - 0, - "2048", - "2233.3598364984477", - "1" - ], - [ - 0, - "2896.3093757400984", - "3158.4477704354626", - "1" - ], - [ - 0, - "4466.7196729968955", - "4870.992343051145", - "1" - ] - ] - } - ], - [ - 12345.678, - { - "count":"6", - "sum":"3897", - "buckets":[ - [ - 1, - "-4870.992343051145", - "-4466.7196729968955", - "1" - ], - [ - 1, - "-861.0779292198035", - "-789.6119426088657", - "1" - ], - [ - 1, - "-558.3399591246119", - "-512", - "1" - ], - [ - 0, - "2048", - "2233.3598364984477", - "1" - ], - [ - 0, - "2896.3093757400984", - "3158.4477704354626", - "1" - ], - [ - 0, - "4466.7196729968955", - "4870.992343051145", - "1" - ] - ] - } - ] - ] - }, - { - "metric":{ - "foo":"bar" - }, - "histograms":[ - [ - 2234.567, - { - "count":"6", - "sum":"3897", - "buckets":[ - [ - 1, - "-4870.992343051145", - "-4466.7196729968955", - "1" - ], - [ - 1, - "-861.0779292198035", - "-789.6119426088657", - "1" - ], - [ - 1, - "-558.3399591246119", - "-512", - "1" - ], - [ - 0, - "2048", - "2233.3598364984477", - "1" - ], - [ - 0, - "2896.3093757400984", - "3158.4477704354626", - "1" - ], - [ - 0, - "4466.7196729968955", - "4870.992343051145", - "1" - ] - ] - } - ], - [ - 22345.678, - { - "count":"6", - "sum":"3897", - "buckets":[ - [ - 1, - "-4870.992343051145", - "-4466.7196729968955", - "1" - ], - [ - 1, - "-861.0779292198035", - "-789.6119426088657", - "1" - ], - [ - 1, - "-558.3399591246119", - "-512", - "1" - ], - [ - 0, - "2048", - "2233.3598364984477", - "1" - ], - [ - 0, - "2896.3093757400984", - "3158.4477704354626", - "1" - ], - [ - 0, - "4466.7196729968955", - "4870.992343051145", - "1" - ] - ] - } - ] - ] - } - ]`, - value: Matrix{ - &SampleStream{ - Metric: Metric{ - MetricNameLabel: "test_metric", - }, - Histograms: []SampleHistogramPair{ - { - Histogram: genSampleHistogram(), - Timestamp: 1234567, - }, - { - Histogram: genSampleHistogram(), - Timestamp: 12345678, - }, - }, - }, - &SampleStream{ - Metric: Metric{ - "foo": "bar", - }, - Histograms: []SampleHistogramPair{ - { - Histogram: genSampleHistogram(), - Timestamp: 2234567, - }, - { - Histogram: genSampleHistogram(), - Timestamp: 22345678, - }, - }, - }, - }, + plain: sampleHistogramPairMatrixPlain, + value: sampleHistogramPairMatrixValue, }, } @@ -743,3 +746,12 @@ func TestMatrixHistogramJSON(t *testing.T) { } } } + +func BenchmarkJSONMarshallingSampleHistogramPairMatrix(b *testing.B) { + for i := 0; i < b.N; i++ { + _, err := json.Marshal(sampleHistogramPairMatrixValue) + if err != nil { + b.Fatal("error marshalling") + } + } +}