forked from hagen1778/tsbs
-
Notifications
You must be signed in to change notification settings - Fork 5
/
main.go
185 lines (158 loc) · 4.56 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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
// bulk_load_influx loads an InfluxDB daemon with data from stdin.
//
// The caller is responsible for assuring that the database is empty before
// bulk load.
package main
import (
"bufio"
"bytes"
"flag"
"fmt"
"log"
"strings"
"sync"
"time"
"github.com/hagen1778/tsbs/load"
"github.com/valyala/fasthttp"
)
// Program option vars:
var (
daemonURLs []string
replicationFactor int
backoff time.Duration
useGzip bool
doAbortOnExist bool
consistency string
)
// Global vars
var (
loader *load.BenchmarkRunner
bufPool sync.Pool
)
var consistencyChoices = map[string]struct{}{
"any": struct{}{},
"one": struct{}{},
"quorum": struct{}{},
"all": struct{}{},
}
// allows for testing
var fatal = log.Fatalf
// Parse args:
func init() {
loader = load.GetBenchmarkRunner()
var csvDaemonURLs string
flag.StringVar(&csvDaemonURLs, "urls", "http://localhost:8086", "InfluxDB URLs, comma-separated. Will be used in a round-robin fashion.")
flag.IntVar(&replicationFactor, "replication-factor", 1, "Cluster replication factor (only applies to clustered databases).")
flag.StringVar(&consistency, "consistency", "all", "Write consistency. Must be one of: any, one, quorum, all.")
flag.DurationVar(&backoff, "backoff", time.Second, "Time to sleep between requests when server indicates backpressure is needed.")
flag.BoolVar(&useGzip, "gzip", true, "Whether to gzip encode requests (default true).")
flag.Parse()
if _, ok := consistencyChoices[consistency]; !ok {
log.Fatalf("invalid consistency settings")
}
daemonURLs = strings.Split(csvDaemonURLs, ",")
if len(daemonURLs) == 0 {
log.Fatal("missing 'urls' flag")
}
}
type benchmark struct{}
func (b *benchmark) GetPointDecoder(br *bufio.Reader) load.PointDecoder {
return &decoder{scanner: bufio.NewScanner(br)}
}
func (b *benchmark) GetBatchFactory() load.BatchFactory {
return &factory{}
}
func (b *benchmark) GetPointIndexer(_ uint) load.PointIndexer {
return &load.ConstantIndexer{}
}
func (b *benchmark) GetProcessor() load.Processor {
return &processor{}
}
func (b *benchmark) GetDBCreator() load.DBCreator {
return &dbCreator{}
}
func main() {
bufPool = sync.Pool{
New: func() interface{} {
return bytes.NewBuffer(make([]byte, 0, 4*1024*1024))
},
}
loader.RunBenchmark(&benchmark{}, load.SingleQueue)
}
type processor struct {
backingOffChan chan bool
backingOffDone chan struct{}
httpWriter *HTTPWriter
}
func (p *processor) Init(numWorker int, _ bool) {
daemonURL := daemonURLs[numWorker%len(daemonURLs)]
p.backingOffChan = make(chan bool, 100)
p.backingOffDone = make(chan struct{})
cfg := HTTPWriterConfig{
DebugInfo: fmt.Sprintf("worker #%d, dest url: %s", numWorker, daemonURL),
Host: daemonURL,
Database: loader.DatabaseName(),
BackingOffChan: p.backingOffChan,
BackingOffDone: p.backingOffDone,
}
p.httpWriter = NewHTTPWriter(cfg, consistency)
go processBackoffMessages(numWorker, p.backingOffChan, p.backingOffDone)
}
func (p *processor) Close(_ bool) {
close(p.backingOffChan)
<-p.backingOffDone
}
func (p *processor) ProcessBatch(b load.Batch, doLoad bool) (uint64, uint64) {
batch := b.(*batch)
// Write the batch: try until backoff is not needed.
if doLoad {
var err error
for {
if useGzip {
compressedBatch := bufPool.Get().(*bytes.Buffer)
fasthttp.WriteGzip(compressedBatch, batch.buf.Bytes())
_, err = p.httpWriter.WriteLineProtocol(compressedBatch.Bytes(), true)
// Return the compressed batch buffer to the pool.
compressedBatch.Reset()
bufPool.Put(compressedBatch)
} else {
_, err = p.httpWriter.WriteLineProtocol(batch.buf.Bytes(), false)
}
if err == BackoffError {
p.backingOffChan <- true
time.Sleep(backoff)
} else {
p.backingOffChan <- false
break
}
}
if err != nil {
log.Fatalf("Error writing: %s\n", err.Error())
}
}
metricCnt := batch.metrics
rowCnt := batch.rows
// Return the batch buffer to the pool.
batch.buf.Reset()
bufPool.Put(batch.buf)
return metricCnt, rowCnt
}
func processBackoffMessages(workerID int, src chan bool, dst chan struct{}) {
var totalBackoffSecs float64
var start time.Time
last := false
for this := range src {
if this && !last {
start = time.Now()
last = true
} else if !this && last {
took := time.Now().Sub(start)
fmt.Printf("[worker %d] backoff took %.02fsec\n", workerID, took.Seconds())
totalBackoffSecs += took.Seconds()
last = false
start = time.Now()
}
}
fmt.Printf("[worker %d] backoffs took a total of %fsec of runtime\n", workerID, totalBackoffSecs)
dst <- struct{}{}
}