github.com/thanos-io/thanos@v0.32.5/internal/cortex/querier/queryrange/marshaling_test.go (about)

     1  // Copyright (c) The Cortex Authors.
     2  // Licensed under the Apache License 2.0.
     3  
     4  package queryrange
     5  
     6  import (
     7  	"bytes"
     8  	"context"
     9  	"io/ioutil"
    10  	"math/rand"
    11  	"net/http"
    12  	"testing"
    13  
    14  	"github.com/stretchr/testify/require"
    15  
    16  	"github.com/thanos-io/thanos/internal/cortex/cortexpb"
    17  )
    18  
    19  func BenchmarkPrometheusCodec_DecodeResponse(b *testing.B) {
    20  	const (
    21  		numSeries           = 1000
    22  		numSamplesPerSeries = 1000
    23  	)
    24  
    25  	// Generate a mocked response and marshal it.
    26  	res := mockPrometheusResponse(numSeries, numSamplesPerSeries)
    27  	encodedRes, err := json.Marshal(res)
    28  	require.NoError(b, err)
    29  	b.Log("test prometheus response size:", len(encodedRes))
    30  
    31  	b.ResetTimer()
    32  	b.ReportAllocs()
    33  
    34  	for n := 0; n < b.N; n++ {
    35  		_, err := PrometheusCodec.DecodeResponse(context.Background(), &http.Response{
    36  			StatusCode:    200,
    37  			Body:          ioutil.NopCloser(bytes.NewReader(encodedRes)),
    38  			ContentLength: int64(len(encodedRes)),
    39  		}, nil)
    40  		require.NoError(b, err)
    41  	}
    42  }
    43  
    44  func BenchmarkPrometheusCodec_EncodeResponse(b *testing.B) {
    45  	const (
    46  		numSeries           = 1000
    47  		numSamplesPerSeries = 1000
    48  	)
    49  
    50  	// Generate a mocked response and marshal it.
    51  	res := mockPrometheusResponse(numSeries, numSamplesPerSeries)
    52  
    53  	b.ResetTimer()
    54  	b.ReportAllocs()
    55  
    56  	for n := 0; n < b.N; n++ {
    57  		_, err := PrometheusCodec.EncodeResponse(context.Background(), res)
    58  		require.NoError(b, err)
    59  	}
    60  }
    61  
    62  func mockPrometheusResponse(numSeries, numSamplesPerSeries int) *PrometheusResponse {
    63  	stream := make([]SampleStream, numSeries)
    64  	for s := 0; s < numSeries; s++ {
    65  		// Generate random samples.
    66  		samples := make([]cortexpb.Sample, numSamplesPerSeries)
    67  		for i := 0; i < numSamplesPerSeries; i++ {
    68  			samples[i] = cortexpb.Sample{
    69  				Value:       rand.Float64(),
    70  				TimestampMs: int64(i),
    71  			}
    72  		}
    73  
    74  		// Generate random labels.
    75  		lbls := make([]cortexpb.LabelAdapter, 10)
    76  		for i := range lbls {
    77  			lbls[i].Name = "a_medium_size_label_name"
    78  			lbls[i].Value = "a_medium_size_label_value_that_is_used_to_benchmark_marshalling"
    79  		}
    80  
    81  		stream[s] = SampleStream{
    82  			Labels:  lbls,
    83  			Samples: samples,
    84  		}
    85  	}
    86  
    87  	return &PrometheusResponse{
    88  		Status: "success",
    89  		Data: PrometheusData{
    90  			ResultType: "vector",
    91  			Result:     stream,
    92  		},
    93  	}
    94  }