Use a fake SeriesIterator (that generates samples on the fly instead of using a slice) for BufferedSeriesIterator, to reduce the variance of benchmark results due to memory pressure.
@@ -163,21 +163,8 @@ func TestBufferedSeriesIteratorNoBadAt(t *testing.T) {
}
func BenchmarkBufferedSeriesIterator(b *testing.B) {
- var (
- samples []sample
- lastT int64
- )
- for i := 0; i < b.N; i++ {
- lastT += 30
-
- samples = append(samples, sample{
- t: lastT,
- v: 123, // doesn't matter
- })
- }
-
// Simulate a 5 minute rate.
- it := NewBufferIterator(newListSeriesIterator(samples), 5*60)
+ it := NewBufferIterator(newFakeSeriesIterator(int64(b.N), 30), 5*60)
b.SetBytes(int64(b.N * 16))
b.ReportAllocs()
@@ -255,3 +242,31 @@ func (it *listSeriesIterator) Seek(t int64) bool {
func (it *listSeriesIterator) Err() error {
return nil
}
+
+type fakeSeriesIterator struct {
+ nsamples int64
+ step int64
+ idx int64
+}
+
+func newFakeSeriesIterator(nsamples, step int64) *fakeSeriesIterator {
+ return &fakeSeriesIterator{nsamples: nsamples, step: step, idx: -1}
+}
+
+func (it *fakeSeriesIterator) At() (int64, float64) {
+ return it.idx * it.step, 123 // value doesn't matter
+}
+
+func (it *fakeSeriesIterator) Next() bool {
+ it.idx++
+ return it.idx < it.nsamples
+}
+
+func (it *fakeSeriesIterator) Seek(t int64) bool {
+ it.idx = t / it.step
+ return it.idx < it.nsamples
+}
+
+func (it *fakeSeriesIterator) Err() error {
+ return nil
+}
