@@ -4,32 +4,34 @@ import (
4
4
"testing"
5
5
6
6
"github.com/grafana/metrictank/mdata/chunk"
7
+ "github.com/grafana/metrictank/test"
7
8
"gopkg.in/raintank/schema.v1"
8
9
)
9
10
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 )
12
13
14
+ values := make ([]uint32 , step )
13
15
// the metric is initialized with the first chunk at ts 1,
14
16
// 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 )
17
19
res = append (res , c )
18
20
}
19
21
return res
20
22
}
21
23
22
- func initMetric (b * testing.B ) * CCacheMetric {
24
+ func initMetric (b testing.TB ) (schema. MKey , * CCacheMetric ) {
23
25
ccm := NewCCacheMetric ()
24
26
mkey , _ := schema .MKeyFromString ("1.12345678901234567890123456789012" )
25
27
c := getItgen (b , []uint32 {1 }, uint32 (1 ), true )
26
28
ccm .Init (mkey , 0 , c )
27
- return ccm
29
+ return mkey , ccm
28
30
}
29
31
30
32
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 )
33
35
prev := uint32 (1 )
34
36
b .ResetTimer ()
35
37
for _ , chunk := range chunks {
@@ -39,9 +41,40 @@ func BenchmarkAddingManyChunksOneByOne(b *testing.B) {
39
41
}
40
42
41
43
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 )
44
46
prev := uint32 (1 )
45
47
b .ResetTimer ()
46
48
ccm .AddSlice (prev , chunks )
47
49
}
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