Skip to content
This repository was archived by the owner on Aug 23, 2023. It is now read-only.

Commit 1668180

Browse files
committed
add test for adding many chunks
1 parent 3b6ab0c commit 1668180

File tree

1 file changed

+43
-10
lines changed

1 file changed

+43
-10
lines changed

mdata/cache/ccache_metric_test.go

+43-10
Original file line numberDiff line numberDiff line change
@@ -4,32 +4,34 @@ import (
44
"testing"
55

66
"github.com/grafana/metrictank/mdata/chunk"
7+
"github.com/grafana/metrictank/test"
78
"gopkg.in/raintank/schema.v1"
89
)
910

10-
func generateChunks(b *testing.B) []chunk.IterGen {
11-
res := make([]chunk.IterGen, 0, b.N)
11+
func generateChunks(b testing.TB, startAt, count, step int) []chunk.IterGen {
12+
res := make([]chunk.IterGen, 0, count)
1213

14+
values := make([]uint32, step)
1315
// the metric is initialized with the first chunk at ts 1,
1416
// so the generated slice should start at ts 2
15-
for i := uint32(2); i < uint32(b.N+2); i++ {
16-
c := getItgen(b, []uint32{1}, i, true)
17+
for i := startAt; i < (step*count)+startAt; i += step {
18+
c := getItgen(b, values, uint32(i), true)
1719
res = append(res, c)
1820
}
1921
return res
2022
}
2123

22-
func initMetric(b *testing.B) *CCacheMetric {
24+
func initMetric(b testing.TB) (schema.MKey, *CCacheMetric) {
2325
ccm := NewCCacheMetric()
2426
mkey, _ := schema.MKeyFromString("1.12345678901234567890123456789012")
2527
c := getItgen(b, []uint32{1}, uint32(1), true)
2628
ccm.Init(mkey, 0, c)
27-
return ccm
29+
return mkey, ccm
2830
}
2931

3032
func BenchmarkAddingManyChunksOneByOne(b *testing.B) {
31-
ccm := initMetric(b)
32-
chunks := generateChunks(b)
33+
_, ccm := initMetric(b)
34+
chunks := generateChunks(b, 2, b.N, 1)
3335
prev := uint32(1)
3436
b.ResetTimer()
3537
for _, chunk := range chunks {
@@ -39,9 +41,40 @@ func BenchmarkAddingManyChunksOneByOne(b *testing.B) {
3941
}
4042

4143
func BenchmarkAddingManyChunksAtOnce(b *testing.B) {
42-
ccm := initMetric(b)
43-
chunks := generateChunks(b)
44+
_, ccm := initMetric(b)
45+
chunks := generateChunks(b, 2, b.N, 1)
4446
prev := uint32(1)
4547
b.ResetTimer()
4648
ccm.AddSlice(prev, chunks)
4749
}
50+
51+
func TestAddingChunksOneByOneAndQueryingThem(t *testing.T) {
52+
mkey, ccm := initMetric(t)
53+
amkey := schema.AMKey{MKey: mkey, Archive: 0}
54+
chunks := generateChunks(t, 10, 100, 10)
55+
prev := uint32(10)
56+
for _, chunk := range chunks {
57+
ccm.Add(prev, chunk)
58+
prev = chunk.Ts
59+
}
60+
61+
res := CCSearchResult{}
62+
ccm.Search(test.NewContext(), amkey, &res, 25, 45)
63+
if res.Complete != true {
64+
t.Fatalf("Expected result to be complete, but it was not")
65+
}
66+
}
67+
68+
func TestAddingChunksAtOnceAndQueryingThem(t *testing.T) {
69+
mkey, ccm := initMetric(t)
70+
amkey := schema.AMKey{MKey: mkey, Archive: 0}
71+
chunks := generateChunks(t, 10, 100, 10)
72+
prev := uint32(10)
73+
ccm.AddSlice(prev, chunks)
74+
75+
res := CCSearchResult{}
76+
ccm.Search(test.NewContext(), amkey, &res, 25, 45)
77+
if res.Complete != true {
78+
t.Fatalf("Expected result to be complete, but it was not")
79+
}
80+
}

0 commit comments

Comments
 (0)