2017-04-10 18:59:45 +00:00
|
|
|
// Copyright 2017 The Prometheus Authors
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2017-11-30 14:34:49 +00:00
|
|
|
package chunkenc
|
2016-11-15 09:33:34 +00:00
|
|
|
|
|
|
|
import (
|
2023-11-16 18:54:41 +00:00
|
|
|
"errors"
|
2016-11-15 09:33:34 +00:00
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"math/rand"
|
|
|
|
"testing"
|
|
|
|
|
2020-10-29 09:43:23 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2016-11-15 09:33:34 +00:00
|
|
|
)
|
|
|
|
|
2016-11-29 21:02:58 +00:00
|
|
|
type pair struct {
|
|
|
|
t int64
|
|
|
|
v float64
|
2016-11-15 09:33:34 +00:00
|
|
|
}
|
|
|
|
|
2016-11-29 21:43:24 +00:00
|
|
|
func TestChunk(t *testing.T) {
|
2016-12-31 09:10:27 +00:00
|
|
|
for enc, nc := range map[Encoding]func() Chunk{
|
|
|
|
EncXOR: func() Chunk { return NewXORChunk() },
|
2016-11-29 21:43:24 +00:00
|
|
|
} {
|
2019-01-07 11:39:11 +00:00
|
|
|
t.Run(fmt.Sprintf("%v", enc), func(t *testing.T) {
|
2016-11-30 22:01:01 +00:00
|
|
|
for range make([]struct{}, 1) {
|
2016-12-31 09:10:27 +00:00
|
|
|
c := nc()
|
2020-02-06 15:58:38 +00:00
|
|
|
testChunk(t, c)
|
2016-11-29 21:43:24 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-06 15:58:38 +00:00
|
|
|
func testChunk(t *testing.T, c Chunk) {
|
2016-11-29 21:43:24 +00:00
|
|
|
app, err := c.Appender()
|
2020-10-29 09:43:23 +00:00
|
|
|
require.NoError(t, err)
|
2016-11-29 21:43:24 +00:00
|
|
|
|
|
|
|
var exp []pair
|
|
|
|
var (
|
|
|
|
ts = int64(1234123324)
|
|
|
|
v = 1243535.123
|
|
|
|
)
|
2016-12-08 09:04:24 +00:00
|
|
|
for i := 0; i < 300; i++ {
|
2016-11-29 21:43:24 +00:00
|
|
|
ts += int64(rand.Intn(10000) + 1)
|
2016-11-30 22:01:01 +00:00
|
|
|
if i%2 == 0 {
|
|
|
|
v += float64(rand.Intn(1000000))
|
|
|
|
} else {
|
|
|
|
v -= float64(rand.Intn(1000000))
|
|
|
|
}
|
2016-11-30 18:08:28 +00:00
|
|
|
|
|
|
|
// Start with a new appender every 10th sample. This emulates starting
|
|
|
|
// appending to a partially filled chunk.
|
|
|
|
if i%10 == 0 {
|
|
|
|
app, err = c.Appender()
|
2020-10-29 09:43:23 +00:00
|
|
|
require.NoError(t, err)
|
2016-11-30 18:08:28 +00:00
|
|
|
}
|
2016-11-29 21:43:24 +00:00
|
|
|
|
2016-12-31 09:10:27 +00:00
|
|
|
app.Append(ts, v)
|
2016-11-29 21:43:24 +00:00
|
|
|
exp = append(exp, pair{t: ts, v: v})
|
|
|
|
}
|
|
|
|
|
2020-02-06 15:58:38 +00:00
|
|
|
// 1. Expand iterator in simple case.
|
|
|
|
it1 := c.Iterator(nil)
|
|
|
|
var res1 []pair
|
2021-11-29 07:54:23 +00:00
|
|
|
for it1.Next() == ValFloat {
|
2020-02-06 15:58:38 +00:00
|
|
|
ts, v := it1.At()
|
|
|
|
res1 = append(res1, pair{t: ts, v: v})
|
2016-11-29 21:43:24 +00:00
|
|
|
}
|
2020-10-29 09:43:23 +00:00
|
|
|
require.NoError(t, it1.Err())
|
|
|
|
require.Equal(t, exp, res1)
|
2020-02-06 15:58:38 +00:00
|
|
|
|
|
|
|
// 2. Expand second iterator while reusing first one.
|
|
|
|
it2 := c.Iterator(it1)
|
|
|
|
var res2 []pair
|
2021-11-29 07:54:23 +00:00
|
|
|
for it2.Next() == ValFloat {
|
2020-02-06 15:58:38 +00:00
|
|
|
ts, v := it2.At()
|
|
|
|
res2 = append(res2, pair{t: ts, v: v})
|
2016-11-29 21:43:24 +00:00
|
|
|
}
|
2020-10-29 09:43:23 +00:00
|
|
|
require.NoError(t, it2.Err())
|
|
|
|
require.Equal(t, exp, res2)
|
2020-02-06 15:58:38 +00:00
|
|
|
|
2020-02-11 16:34:09 +00:00
|
|
|
// 3. Test iterator Seek.
|
2020-02-06 15:58:38 +00:00
|
|
|
mid := len(exp) / 2
|
|
|
|
|
|
|
|
it3 := c.Iterator(nil)
|
|
|
|
var res3 []pair
|
2021-11-29 07:54:23 +00:00
|
|
|
require.Equal(t, ValFloat, it3.Seek(exp[mid].t))
|
2020-02-06 15:58:38 +00:00
|
|
|
// Below ones should not matter.
|
2021-11-29 07:54:23 +00:00
|
|
|
require.Equal(t, ValFloat, it3.Seek(exp[mid].t))
|
|
|
|
require.Equal(t, ValFloat, it3.Seek(exp[mid].t))
|
2020-02-06 15:58:38 +00:00
|
|
|
ts, v = it3.At()
|
|
|
|
res3 = append(res3, pair{t: ts, v: v})
|
|
|
|
|
2021-11-29 07:54:23 +00:00
|
|
|
for it3.Next() == ValFloat {
|
2020-02-06 15:58:38 +00:00
|
|
|
ts, v := it3.At()
|
|
|
|
res3 = append(res3, pair{t: ts, v: v})
|
2016-11-29 21:43:24 +00:00
|
|
|
}
|
2020-10-29 09:43:23 +00:00
|
|
|
require.NoError(t, it3.Err())
|
|
|
|
require.Equal(t, exp[mid:], res3)
|
2021-11-29 07:54:23 +00:00
|
|
|
require.Equal(t, ValNone, it3.Seek(exp[len(exp)-1].t+1))
|
2016-11-29 21:43:24 +00:00
|
|
|
}
|
|
|
|
|
2024-01-17 17:28:06 +00:00
|
|
|
func TestPool(t *testing.T) {
|
|
|
|
p := NewPool()
|
|
|
|
for _, tc := range []struct {
|
|
|
|
name string
|
|
|
|
encoding Encoding
|
|
|
|
expErr error
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "xor",
|
|
|
|
encoding: EncXOR,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "histogram",
|
|
|
|
encoding: EncHistogram,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "float histogram",
|
|
|
|
encoding: EncFloatHistogram,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "invalid encoding",
|
|
|
|
encoding: EncNone,
|
2024-11-03 12:15:51 +00:00
|
|
|
expErr: errors.New(`invalid chunk encoding "none"`),
|
2024-01-17 17:28:06 +00:00
|
|
|
},
|
|
|
|
} {
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
|
|
c, err := p.Get(tc.encoding, []byte("test"))
|
|
|
|
if tc.expErr != nil {
|
|
|
|
require.EqualError(t, err, tc.expErr.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
var b *bstream
|
|
|
|
switch tc.encoding {
|
|
|
|
case EncHistogram:
|
|
|
|
b = &c.(*HistogramChunk).b
|
|
|
|
case EncFloatHistogram:
|
|
|
|
b = &c.(*FloatHistogramChunk).b
|
|
|
|
default:
|
|
|
|
b = &c.(*XORChunk).b
|
|
|
|
}
|
|
|
|
|
|
|
|
require.Equal(t, &bstream{
|
|
|
|
stream: []byte("test"),
|
|
|
|
count: 0,
|
|
|
|
}, b)
|
|
|
|
|
|
|
|
b.count = 1
|
|
|
|
require.NoError(t, p.Put(c))
|
|
|
|
require.Equal(t, &bstream{
|
|
|
|
stream: nil,
|
|
|
|
count: 0,
|
|
|
|
}, b)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Run("put bad chunk wrapper", func(t *testing.T) {
|
|
|
|
// When a wrapping chunk poses as an encoding it can't be converted to, Put should skip it.
|
|
|
|
c := fakeChunk{
|
|
|
|
encoding: EncXOR,
|
|
|
|
t: t,
|
|
|
|
}
|
|
|
|
require.NoError(t, p.Put(c))
|
|
|
|
})
|
|
|
|
t.Run("put invalid encoding", func(t *testing.T) {
|
|
|
|
c := fakeChunk{
|
|
|
|
encoding: EncNone,
|
|
|
|
t: t,
|
|
|
|
}
|
|
|
|
require.EqualError(t, p.Put(c), `invalid chunk encoding "none"`)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
type fakeChunk struct {
|
|
|
|
Chunk
|
|
|
|
|
|
|
|
encoding Encoding
|
|
|
|
t *testing.T
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c fakeChunk) Encoding() Encoding {
|
|
|
|
return c.encoding
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c fakeChunk) Reset([]byte) {
|
|
|
|
c.t.Fatal("Reset should not be called")
|
|
|
|
}
|
|
|
|
|
2016-12-31 09:10:27 +00:00
|
|
|
func benchmarkIterator(b *testing.B, newChunk func() Chunk) {
|
2021-07-26 14:06:54 +00:00
|
|
|
const samplesPerChunk = 250
|
2016-11-15 09:33:34 +00:00
|
|
|
var (
|
2020-03-24 20:15:47 +00:00
|
|
|
t = int64(1234123324)
|
|
|
|
v = 1243535.123
|
|
|
|
exp []pair
|
2016-11-15 09:33:34 +00:00
|
|
|
)
|
2021-07-26 14:06:54 +00:00
|
|
|
for i := 0; i < samplesPerChunk; i++ {
|
2017-01-02 21:24:35 +00:00
|
|
|
// t += int64(rand.Intn(10000) + 1)
|
|
|
|
t += int64(1000)
|
2016-11-30 18:08:28 +00:00
|
|
|
// v = rand.Float64()
|
2016-12-07 14:37:37 +00:00
|
|
|
v += float64(100)
|
2016-11-29 21:02:58 +00:00
|
|
|
exp = append(exp, pair{t: t, v: v})
|
2016-11-15 09:33:34 +00:00
|
|
|
}
|
2016-11-29 21:02:58 +00:00
|
|
|
|
2021-07-26 14:06:54 +00:00
|
|
|
chunk := newChunk()
|
|
|
|
{
|
|
|
|
a, err := chunk.Appender()
|
2016-11-29 21:02:58 +00:00
|
|
|
if err != nil {
|
|
|
|
b.Fatalf("get appender: %s", err)
|
|
|
|
}
|
2016-12-31 09:10:27 +00:00
|
|
|
j := 0
|
2016-11-29 21:02:58 +00:00
|
|
|
for _, p := range exp {
|
2016-12-31 09:10:27 +00:00
|
|
|
if j > 250 {
|
2016-11-15 09:33:34 +00:00
|
|
|
break
|
|
|
|
}
|
2016-12-31 09:10:27 +00:00
|
|
|
a.Append(p.t, p.v)
|
|
|
|
j++
|
2016-11-15 09:33:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
b.ReportAllocs()
|
|
|
|
b.ResetTimer()
|
|
|
|
|
2021-07-26 14:06:54 +00:00
|
|
|
var res float64
|
2019-07-09 09:49:34 +00:00
|
|
|
var it Iterator
|
2021-07-26 14:06:54 +00:00
|
|
|
for i := 0; i < b.N; {
|
|
|
|
it := chunk.Iterator(it)
|
2016-11-15 09:33:34 +00:00
|
|
|
|
2021-11-29 07:54:23 +00:00
|
|
|
for it.Next() == ValFloat {
|
2017-01-02 12:27:52 +00:00
|
|
|
_, v := it.At()
|
2021-07-26 14:06:54 +00:00
|
|
|
res = v
|
|
|
|
i++
|
2016-11-15 09:33:34 +00:00
|
|
|
}
|
2023-11-16 18:54:41 +00:00
|
|
|
if err := it.Err(); err != nil && !errors.Is(err, io.EOF) {
|
|
|
|
require.NoError(b, err)
|
2016-11-15 09:33:34 +00:00
|
|
|
}
|
2021-07-26 14:06:54 +00:00
|
|
|
_ = res
|
2016-11-15 09:33:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-16 07:48:49 +00:00
|
|
|
func newXORChunk() Chunk {
|
|
|
|
return NewXORChunk()
|
|
|
|
}
|
|
|
|
|
2016-11-20 13:33:00 +00:00
|
|
|
func BenchmarkXORIterator(b *testing.B) {
|
2024-09-16 07:48:49 +00:00
|
|
|
benchmarkIterator(b, newXORChunk)
|
2016-11-20 13:33:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkXORAppender(b *testing.B) {
|
2024-09-16 07:48:49 +00:00
|
|
|
r := rand.New(rand.NewSource(1))
|
|
|
|
b.Run("constant", func(b *testing.B) {
|
|
|
|
benchmarkAppender(b, func() (int64, float64) {
|
|
|
|
return 1000, 0
|
|
|
|
}, newXORChunk)
|
|
|
|
})
|
|
|
|
b.Run("random steps", func(b *testing.B) {
|
|
|
|
benchmarkAppender(b, func() (int64, float64) {
|
|
|
|
return int64(r.Intn(100) - 50 + 15000), // 15 seconds +- up to 100ms of jitter.
|
|
|
|
float64(r.Intn(100) - 50) // Varying from -50 to +50 in 100 discrete steps.
|
|
|
|
}, newXORChunk)
|
|
|
|
})
|
|
|
|
b.Run("random 0-1", func(b *testing.B) {
|
|
|
|
benchmarkAppender(b, func() (int64, float64) {
|
|
|
|
return int64(r.Intn(100) - 50 + 15000), // 15 seconds +- up to 100ms of jitter.
|
|
|
|
r.Float64() // Random between 0 and 1.0.
|
|
|
|
}, newXORChunk)
|
2016-11-20 13:33:00 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2024-09-16 07:48:49 +00:00
|
|
|
func benchmarkAppender(b *testing.B, deltas func() (int64, float64), newChunk func() Chunk) {
|
2016-11-15 09:33:34 +00:00
|
|
|
var (
|
2016-11-29 21:02:58 +00:00
|
|
|
t = int64(1234123324)
|
|
|
|
v = 1243535.123
|
2016-11-15 09:33:34 +00:00
|
|
|
)
|
2024-09-16 07:48:49 +00:00
|
|
|
const nSamples = 120 // Same as tsdb.DefaultSamplesPerChunk.
|
2016-11-29 21:02:58 +00:00
|
|
|
var exp []pair
|
2024-09-16 07:48:49 +00:00
|
|
|
for i := 0; i < nSamples; i++ {
|
|
|
|
dt, dv := deltas()
|
|
|
|
t += dt
|
|
|
|
v += dv
|
2016-11-29 21:02:58 +00:00
|
|
|
exp = append(exp, pair{t: t, v: v})
|
2016-11-15 09:33:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
b.ReportAllocs()
|
|
|
|
b.ResetTimer()
|
|
|
|
|
2024-09-16 07:48:49 +00:00
|
|
|
for i := 0; i < b.N; i++ {
|
2016-12-31 09:10:27 +00:00
|
|
|
c := newChunk()
|
2016-11-29 21:02:58 +00:00
|
|
|
|
|
|
|
a, err := c.Appender()
|
|
|
|
if err != nil {
|
|
|
|
b.Fatalf("get appender: %s", err)
|
|
|
|
}
|
|
|
|
for _, p := range exp {
|
2016-12-31 09:10:27 +00:00
|
|
|
a.Append(p.t, p.v)
|
2016-11-15 09:33:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|