-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
158 lines (140 loc) · 4.04 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package main
import (
"context"
"fmt"
"log"
"math/rand"
"os"
"sync"
"time"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/common/model"
"github.com/prometheus/prometheus/pkg/labels"
"github.com/weaveworks/common/user"
"github.com/cortexproject/cortex/pkg/chunk"
"github.com/cortexproject/cortex/pkg/chunk/local"
"github.com/cortexproject/cortex/pkg/chunk/storage"
"github.com/cortexproject/cortex/pkg/ingester/client"
util_log "github.com/cortexproject/cortex/pkg/util/log"
"github.com/credativ/vali/pkg/chunkenc"
"github.com/credativ/vali/pkg/logproto"
"github.com/credativ/vali/pkg/logql"
lstore "github.com/credativ/vali/pkg/storage"
"github.com/credativ/vali/pkg/util/validation"
)
var (
start = model.Time(1523750400000)
ctx = user.InjectOrgID(context.Background(), "fake")
maxChunks = 1200 // 1200 chunks is 2gib ish of data enough to run benchmark
)
// fill up the local filesystem store with 1gib of data to run benchmark
func main() {
if _, err := os.Stat("/tmp/benchmark/chunks"); os.IsNotExist(err) {
if err := fillStore(); err != nil {
log.Fatal("error filling up storage:", err)
}
}
}
func getStore() (lstore.Store, error) {
storeConfig := lstore.Config{
Config: storage.Config{
BoltDBConfig: local.BoltDBConfig{Directory: "/tmp/benchmark/index"},
FSConfig: local.FSConfig{Directory: "/tmp/benchmark/chunks"},
},
}
schemaCfg := lstore.SchemaConfig{
SchemaConfig: chunk.SchemaConfig{
Configs: []chunk.PeriodConfig{
{
From: chunk.DayTime{Time: start},
IndexType: "boltdb",
ObjectType: "filesystem",
Schema: "v9",
IndexTables: chunk.PeriodicTableConfig{
Prefix: "index_",
Period: time.Hour * 168,
},
},
},
},
}
chunkStore, err := storage.NewStore(
storeConfig.Config,
chunk.StoreConfig{},
schemaCfg.SchemaConfig,
&validation.Overrides{},
prometheus.DefaultRegisterer,
nil,
util_log.Logger,
)
if err != nil {
return nil, err
}
return lstore.NewStore(storeConfig, schemaCfg, chunkStore, prometheus.DefaultRegisterer)
}
func fillStore() error {
store, err := getStore()
if err != nil {
return err
}
defer store.Stop()
var wgPush sync.WaitGroup
var flushCount int
// insert 5 streams with a random logs every nanoseconds
// the string is randomize so chunks are big ~2mb
// take ~1min to build 1gib of data
for i := 0; i < 5; i++ {
wgPush.Add(1)
go func(j int) {
defer wgPush.Done()
lbs, err := logql.ParseLabels(fmt.Sprintf("{foo=\"bar\",level=\"%d\"}", j))
if err != nil {
panic(err)
}
labelsBuilder := labels.NewBuilder(lbs)
labelsBuilder.Set(labels.MetricName, "logs")
metric := labelsBuilder.Labels()
fp := client.Fingerprint(lbs)
chunkEnc := chunkenc.NewMemChunk(chunkenc.EncLZ4_4M, 262144, 1572864)
for ts := start.UnixNano(); ts < start.UnixNano()+time.Hour.Nanoseconds(); ts = ts + time.Millisecond.Nanoseconds() {
entry := &logproto.Entry{
Timestamp: time.Unix(0, ts),
Line: randString(250),
}
if chunkEnc.SpaceFor(entry) {
_ = chunkEnc.Append(entry)
} else {
from, to := chunkEnc.Bounds()
c := chunk.NewChunk("fake", fp, metric, chunkenc.NewFacade(chunkEnc, 0, 0), model.TimeFromUnixNano(from.UnixNano()), model.TimeFromUnixNano(to.UnixNano()))
if err := c.Encode(); err != nil {
panic(err)
}
err := store.Put(ctx, []chunk.Chunk{c})
if err != nil {
panic(err)
}
flushCount++
log.Println("flushed ", flushCount, from.UnixNano(), to.UnixNano(), metric)
if flushCount >= maxChunks {
return
}
chunkEnc = chunkenc.NewMemChunk(chunkenc.EncLZ4_64k, 262144, 1572864)
}
}
}(i)
}
wgPush.Wait()
return nil
}
const charset = "abcdefghijklmnopqrstuvwxyz" +
"ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
func randStringWithCharset(length int, charset string) string {
b := make([]byte, length)
for i := range b {
b[i] = charset[rand.Intn(len(charset)-1)]
}
return string(b)
}
func randString(length int) string {
return randStringWithCharset(length, charset)
}