|
| 1 | +// Copyright (c) The Thanos Authors. |
| 2 | +// Licensed under the Apache License 2.0. |
| 3 | + |
| 4 | +package extgrpc |
| 5 | + |
| 6 | +import ( |
| 7 | + "testing" |
| 8 | + |
| 9 | + "github.com/prometheus/prometheus/model/labels" |
| 10 | + "github.com/stretchr/testify/require" |
| 11 | + "github.com/thanos-io/thanos/pkg/info/infopb" |
| 12 | + "github.com/thanos-io/thanos/pkg/store/labelpb" |
| 13 | + "google.golang.org/grpc/encoding" |
| 14 | +) |
| 15 | + |
| 16 | +func BenchmarkVanillaProtoMarshal(b *testing.B) { |
| 17 | + ls := []labels.Labels{} |
| 18 | + |
| 19 | + for i := range 1000 { |
| 20 | + ls = append(ls, labels.FromStrings("label_"+string(rune(i)), "value_"+string(rune(i)))) |
| 21 | + } |
| 22 | + i := &infopb.InfoResponse{ |
| 23 | + LabelSets: labelpb.ZLabelSetsFromPromLabels( |
| 24 | + ls..., |
| 25 | + ), |
| 26 | + ComponentType: "asdsadsadsa", |
| 27 | + Store: &infopb.StoreInfo{ |
| 28 | + MinTime: 123, |
| 29 | + MaxTime: 456, |
| 30 | + SupportsSharding: true, |
| 31 | + TsdbInfos: []infopb.TSDBInfo{ |
| 32 | + { |
| 33 | + Labels: labelpb.ZLabelSet{Labels: labelpb.ZLabelsFromPromLabels(labels.FromStrings("replica", "1"))}, |
| 34 | + MinTime: 100, |
| 35 | + MaxTime: 200, |
| 36 | + }, |
| 37 | + }, |
| 38 | + }, |
| 39 | + Rules: &infopb.RulesInfo{}, |
| 40 | + MetricMetadata: &infopb.MetricMetadataInfo{}, |
| 41 | + Exemplars: &infopb.ExemplarsInfo{ |
| 42 | + MinTime: 100, |
| 43 | + MaxTime: 200, |
| 44 | + }, |
| 45 | + } |
| 46 | + |
| 47 | + c := &nonPoolingCodec{} |
| 48 | + |
| 49 | + ogCodec := encoding.GetCodecV2("proto").(*nonPoolingCodec).CodecV2 |
| 50 | + |
| 51 | + b.Run("vanilla proto marshal", func(b *testing.B) { |
| 52 | + b.ReportAllocs() |
| 53 | + b.ResetTimer() |
| 54 | + |
| 55 | + for b.Loop() { |
| 56 | + _, err := ogCodec.Marshal(i) |
| 57 | + require.NoError(b, err) |
| 58 | + } |
| 59 | + }) |
| 60 | + |
| 61 | + b.Run("non pooling codec marshal", func(b *testing.B) { |
| 62 | + for b.Loop() { |
| 63 | + _, err := c.Marshal(i) |
| 64 | + require.NoError(b, err) |
| 65 | + } |
| 66 | + }) |
| 67 | +} |
| 68 | + |
| 69 | +func TestNonPoolingCodecMarshal(t *testing.T) { |
| 70 | + i := &infopb.InfoResponse{ |
| 71 | + LabelSets: labelpb.ZLabelSetsFromPromLabels( |
| 72 | + labels.FromStrings("foo", "bar"), |
| 73 | + labels.FromStrings("baz", "qux"), |
| 74 | + labels.FromStrings("aaa", "bbb"), |
| 75 | + ), |
| 76 | + ComponentType: "asdsadsadsa", |
| 77 | + Store: &infopb.StoreInfo{ |
| 78 | + MinTime: 123, |
| 79 | + MaxTime: 456, |
| 80 | + SupportsSharding: true, |
| 81 | + TsdbInfos: []infopb.TSDBInfo{ |
| 82 | + { |
| 83 | + Labels: labelpb.ZLabelSet{Labels: labelpb.ZLabelsFromPromLabels(labels.FromStrings("replica", "1"))}, |
| 84 | + MinTime: 100, |
| 85 | + MaxTime: 200, |
| 86 | + }, |
| 87 | + }, |
| 88 | + }, |
| 89 | + Rules: &infopb.RulesInfo{}, |
| 90 | + MetricMetadata: &infopb.MetricMetadataInfo{}, |
| 91 | + Exemplars: &infopb.ExemplarsInfo{ |
| 92 | + MinTime: 100, |
| 93 | + MaxTime: 200, |
| 94 | + }, |
| 95 | + } |
| 96 | + |
| 97 | + c := &nonPoolingCodec{} |
| 98 | + |
| 99 | + _, err := c.Marshal(i) |
| 100 | + require.NoError(t, err) |
| 101 | +} |
0 commit comments