2016-11-15 01:33:34 -08:00
|
|
|
package chunks
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"math/rand"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
2016-11-29 13:02:58 -08:00
|
|
|
type pair struct {
|
|
|
|
t int64
|
|
|
|
v float64
|
2016-11-15 01:33:34 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
func benchmarkIterator(b *testing.B, newChunk func(int) Chunk) {
|
|
|
|
var (
|
2016-11-29 13:02:58 -08:00
|
|
|
t = int64(1234123324)
|
|
|
|
v = 1243535.123
|
2016-11-15 01:33:34 -08:00
|
|
|
)
|
2016-11-29 13:02:58 -08:00
|
|
|
var exp []pair
|
2016-11-15 01:33:34 -08:00
|
|
|
for i := 0; i < b.N; i++ {
|
2016-11-29 13:02:58 -08:00
|
|
|
t += int64(rand.Intn(10000) + 1)
|
|
|
|
v = rand.Float64()
|
|
|
|
exp = append(exp, pair{t: t, v: v})
|
2016-11-15 01:33:34 -08:00
|
|
|
}
|
2016-11-29 13:02:58 -08:00
|
|
|
|
2016-11-15 01:33:34 -08:00
|
|
|
var chunks []Chunk
|
|
|
|
for i := 0; i < b.N; {
|
|
|
|
c := newChunk(1024)
|
2016-11-29 13:02:58 -08:00
|
|
|
|
|
|
|
a, err := c.Appender()
|
|
|
|
if err != nil {
|
|
|
|
b.Fatalf("get appender: %s", err)
|
|
|
|
}
|
|
|
|
for _, p := range exp {
|
|
|
|
if err := a.Append(p.t, p.v); err == ErrChunkFull {
|
2016-11-15 01:33:34 -08:00
|
|
|
break
|
|
|
|
} else if err != nil {
|
|
|
|
b.Fatal(err)
|
|
|
|
}
|
|
|
|
i++
|
|
|
|
}
|
|
|
|
chunks = append(chunks, c)
|
|
|
|
}
|
|
|
|
|
|
|
|
b.ReportAllocs()
|
|
|
|
b.ResetTimer()
|
|
|
|
|
2016-11-29 13:02:58 -08:00
|
|
|
fmt.Println("num", b.N, "created chunks", len(chunks))
|
|
|
|
|
|
|
|
res := make([]float64, 0, 1024)
|
|
|
|
|
2016-11-15 01:33:34 -08:00
|
|
|
for i := 0; i < len(chunks); i++ {
|
|
|
|
c := chunks[i]
|
|
|
|
it := c.Iterator()
|
|
|
|
|
2016-11-29 13:02:58 -08:00
|
|
|
for it.Next() {
|
|
|
|
_, v := it.Values()
|
|
|
|
res = append(res, v)
|
2016-11-15 01:33:34 -08:00
|
|
|
}
|
|
|
|
if it.Err() != io.EOF {
|
2016-11-20 05:33:00 -08:00
|
|
|
require.NoError(b, it.Err())
|
2016-11-15 01:33:34 -08:00
|
|
|
}
|
|
|
|
res = res[:0]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-20 05:33:00 -08:00
|
|
|
func BenchmarkXORIterator(b *testing.B) {
|
|
|
|
benchmarkIterator(b, func(sz int) Chunk {
|
|
|
|
return NewXORChunk(sz)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkXORAppender(b *testing.B) {
|
|
|
|
benchmarkAppender(b, func(sz int) Chunk {
|
|
|
|
return NewXORChunk(sz)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-11-15 01:33:34 -08:00
|
|
|
func benchmarkAppender(b *testing.B, newChunk func(int) Chunk) {
|
|
|
|
var (
|
2016-11-29 13:02:58 -08:00
|
|
|
t = int64(1234123324)
|
|
|
|
v = 1243535.123
|
2016-11-15 01:33:34 -08:00
|
|
|
)
|
2016-11-29 13:02:58 -08:00
|
|
|
var exp []pair
|
2016-11-15 01:33:34 -08:00
|
|
|
for i := 0; i < b.N; i++ {
|
2016-11-29 13:02:58 -08:00
|
|
|
t += int64(rand.Intn(10000) + 1)
|
|
|
|
v = rand.Float64()
|
|
|
|
exp = append(exp, pair{t: t, v: v})
|
2016-11-15 01:33:34 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
b.ReportAllocs()
|
|
|
|
b.ResetTimer()
|
|
|
|
|
|
|
|
var chunks []Chunk
|
|
|
|
for i := 0; i < b.N; {
|
|
|
|
c := newChunk(1024)
|
2016-11-29 13:02:58 -08:00
|
|
|
|
|
|
|
a, err := c.Appender()
|
|
|
|
if err != nil {
|
|
|
|
b.Fatalf("get appender: %s", err)
|
|
|
|
}
|
|
|
|
for _, p := range exp {
|
|
|
|
if err := a.Append(p.t, p.v); err == ErrChunkFull {
|
2016-11-15 01:33:34 -08:00
|
|
|
break
|
|
|
|
} else if err != nil {
|
|
|
|
b.Fatal(err)
|
|
|
|
}
|
|
|
|
i++
|
|
|
|
}
|
|
|
|
chunks = append(chunks, c)
|
|
|
|
}
|
|
|
|
|
2016-11-29 13:02:58 -08:00
|
|
|
fmt.Println("num", b.N, "created chunks", len(chunks))
|
2016-11-15 01:33:34 -08:00
|
|
|
}
|