Skip to content

Commit 5fb95c5

Browse files
committed
branchmark
1 parent 72bb2a0 commit 5fb95c5

File tree

1 file changed

+55
-0
lines changed

1 file changed

+55
-0
lines changed

pkg/cortexpb/timeseries_test.go

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
package cortexpb
22

33
import (
4+
"fmt"
45
"testing"
56

7+
"github.com/gogo/protobuf/proto"
68
"github.com/stretchr/testify/assert"
79
"github.com/stretchr/testify/require"
810
)
@@ -64,3 +66,56 @@ func TestTimeseriesFromPool(t *testing.T) {
6466
assert.Len(t, reused.Samples, 0)
6567
})
6668
}
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: "pool",
99+
writeRequestFactory: func() proto.Marshaler {
100+
w := PreallocWriteRequestFromPool()
101+
w.Timeseries = ts
102+
return w
103+
},
104+
clean: func(in interface{}) {
105+
ReuseWriteRequest(in.(*PreallocWriteRequest))
106+
},
107+
},
108+
}
109+
110+
for _, tc := range tests {
111+
b.Run(tc.name, func(b *testing.B) {
112+
for i := 0; i < b.N; i++ {
113+
w := tc.writeRequestFactory()
114+
_, err := w.Marshal()
115+
require.NoError(b, err)
116+
tc.clean(w)
117+
}
118+
b.ReportAllocs()
119+
})
120+
}
121+
}

0 commit comments

Comments
 (0)