|
1 | 1 | package cortexpb
|
2 | 2 |
|
3 | 3 | import (
|
| 4 | + "fmt" |
4 | 5 | "testing"
|
5 | 6 |
|
| 7 | + "github.com/gogo/protobuf/proto" |
6 | 8 | "github.com/stretchr/testify/assert"
|
7 | 9 | "github.com/stretchr/testify/require"
|
8 | 10 | )
|
@@ -64,3 +66,67 @@ func TestTimeseriesFromPool(t *testing.T) {
|
64 | 66 | assert.Len(t, reused.Samples, 0)
|
65 | 67 | })
|
66 | 68 | }
|
| 69 | + |
| 70 | +func BenchmarkMarshallWriteRequest(b *testing.B) { |
| 71 | + ts := PreallocTimeseriesSliceFromPool() |
| 72 | + |
| 73 | + for i := 0; i < 100; i++ { |
| 74 | + ts = append(ts, PreallocTimeseries{TimeSeries: TimeseriesFromPool()}) |
| 75 | + ts[i].Labels = []LabelAdapter{ |
| 76 | + {Name: "foo", Value: "bar"}, |
| 77 | + {Name: "very long label name", Value: "very long label value"}, |
| 78 | + {Name: "very long label name 2", Value: "very long label value 2"}, |
| 79 | + {Name: "very long label name 3", Value: "very long label value 3"}, |
| 80 | + {Name: "int", Value: fmt.Sprint(i)}, |
| 81 | + } |
| 82 | + ts[i].Samples = []Sample{{Value: 1, TimestampMs: 2}} |
| 83 | + } |
| 84 | + |
| 85 | + tests := []struct { |
| 86 | + name string |
| 87 | + writeRequestFactory func() proto.Marshaler |
| 88 | + clean func(in interface{}) |
| 89 | + }{ |
| 90 | + { |
| 91 | + name: "no-pool", |
| 92 | + writeRequestFactory: func() proto.Marshaler { |
| 93 | + return &WriteRequest{Timeseries: ts} |
| 94 | + }, |
| 95 | + clean: func(in interface{}) {}, |
| 96 | + }, |
| 97 | + { |
| 98 | + name: "byte pool", |
| 99 | + writeRequestFactory: func() proto.Marshaler { |
| 100 | + w := &PreallocWriteRequest{} |
| 101 | + w.Timeseries = ts |
| 102 | + return w |
| 103 | + }, |
| 104 | + clean: func(in interface{}) { |
| 105 | + ReuseWriteRequest(in.(*PreallocWriteRequest)) |
| 106 | + }, |
| 107 | + }, |
| 108 | + { |
| 109 | + name: "byte and write pool", |
| 110 | + writeRequestFactory: func() proto.Marshaler { |
| 111 | + w := PreallocWriteRequestFromPool() |
| 112 | + w.Timeseries = ts |
| 113 | + return w |
| 114 | + }, |
| 115 | + clean: func(in interface{}) { |
| 116 | + ReuseWriteRequest(in.(*PreallocWriteRequest)) |
| 117 | + }, |
| 118 | + }, |
| 119 | + } |
| 120 | + |
| 121 | + for _, tc := range tests { |
| 122 | + b.Run(tc.name, func(b *testing.B) { |
| 123 | + for i := 0; i < b.N; i++ { |
| 124 | + w := tc.writeRequestFactory() |
| 125 | + _, err := w.Marshal() |
| 126 | + require.NoError(b, err) |
| 127 | + tc.clean(w) |
| 128 | + } |
| 129 | + b.ReportAllocs() |
| 130 | + }) |
| 131 | + } |
| 132 | +} |
0 commit comments