forked from influxdata/influxdb-comparisons
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
165 lines (138 loc) · 4.03 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
// query_benchmarker_opentsdb speed tests OpenTSDB using requests from stdin.
//
// It reads encoded Query objects from stdin, and makes concurrent requests
// to the provided HTTP endpoint. This program has no knowledge of the
// internals of the endpoint.
//
// TODO(rw): On my machine, this only decodes 700k/sec messages from stdin.
package main
import (
"encoding/gob"
"flag"
"fmt"
"github.com/gdchaochao/influxdb-comparisons/bulk_query"
"github.com/gdchaochao/influxdb-comparisons/util/report"
"io"
"log"
"strings"
"sync"
)
type OpenTsdbQueryBenchmarker struct {
// Program option vars:
csvDaemonUrls string
daemonUrls []string
// Global vars:
queryPool sync.Pool
queryChan chan *Query
scanFinished bool
}
var querier = &OpenTsdbQueryBenchmarker{}
// Parse args:
func init() {
bulk_query.Benchmarker.Init()
querier.Init()
flag.Parse()
bulk_query.Benchmarker.Validate()
querier.Validate()
}
func (b *OpenTsdbQueryBenchmarker) Init() {
flag.StringVar(&b.csvDaemonUrls, "urls", "http://localhost:4242", "OpenTSDB URLs, comma-separated. Will be used in a round-robin fashion.")
}
func (b *OpenTsdbQueryBenchmarker) Validate() {
b.daemonUrls = strings.Split(b.csvDaemonUrls, ",")
if len(b.daemonUrls) == 0 {
log.Fatal("missing 'urls' flag")
}
fmt.Printf("daemon URLs: %v\n", b.daemonUrls)
}
func (b *OpenTsdbQueryBenchmarker) Prepare() {
// Make pools to minimize heap usage:
b.queryPool = sync.Pool{
New: func() interface{} {
return &Query{
HumanLabel: make([]byte, 0, 1024),
HumanDescription: make([]byte, 0, 1024),
Method: make([]byte, 0, 1024),
Path: make([]byte, 0, 1024),
Body: make([]byte, 0, 1024),
}
},
}
}
func (b *OpenTsdbQueryBenchmarker) GetProcessor() bulk_query.Processor {
return b
}
func (b *OpenTsdbQueryBenchmarker) GetScanner() bulk_query.Scanner {
return b
}
func (b *OpenTsdbQueryBenchmarker) PrepareProcess(i int) {
}
func (b *OpenTsdbQueryBenchmarker) RunProcess(i int, workersGroup *sync.WaitGroup, statPool sync.Pool, statChan chan *bulk_query.Stat) {
daemonUrl := b.daemonUrls[i%len(b.daemonUrls)]
w := NewHTTPClient(daemonUrl, bulk_query.Benchmarker.Debug())
b.processQueries(w, workersGroup, statPool, statChan)
}
func (b *OpenTsdbQueryBenchmarker) IsScanFinished() bool {
return b.scanFinished
}
func (b *OpenTsdbQueryBenchmarker) CleanUp() {
close(b.queryChan)
}
func (b *OpenTsdbQueryBenchmarker) UpdateReport(params *report.QueryReportParams, reportTags [][2]string, extraVals []report.ExtraVal) (updatedTags [][2]string, updatedExtraVals []report.ExtraVal) {
params.DBType = "OpenTSDB"
params.DestinationUrl = b.csvDaemonUrls
updatedTags = reportTags
updatedExtraVals = extraVals
return
}
func main() {
bulk_query.Benchmarker.RunBenchmark(querier)
}
// scan reads encoded Queries and places them onto the workqueue.
func (b *OpenTsdbQueryBenchmarker) RunScan(r io.Reader, closeChan chan int) {
dec := gob.NewDecoder(r)
n := int64(0)
loop:
for {
if bulk_query.Benchmarker.Limit() >= 0 && n >= bulk_query.Benchmarker.Limit() {
break
}
q := b.queryPool.Get().(*Query)
err := dec.Decode(q)
if err == io.EOF {
break
}
if err != nil {
log.Fatal(err)
}
q.ID = n
b.queryChan <- q
n++
select {
case <-closeChan:
fmt.Printf("Received finish request\n")
break loop
default:
}
}
b.scanFinished = true
}
// processQueries reads byte buffers from queryChan and writes them to the
// target server, while tracking latency.
func (b *OpenTsdbQueryBenchmarker) processQueries(w *HTTPClient, workersGroup *sync.WaitGroup, statPool sync.Pool, statChan chan *bulk_query.Stat) {
opts := &HTTPClientDoOptions{
Debug: bulk_query.Benchmarker.Debug(),
PrettyPrintResponses: bulk_query.Benchmarker.PrettyPrintResponses(),
}
for q := range b.queryChan {
lag, err := w.Do(q, opts)
stat := statPool.Get().(*bulk_query.Stat)
stat.Init(q.HumanLabel, lag)
statChan <- stat
b.queryPool.Put(q)
if err != nil {
log.Fatalf("Error during request: %s\n", err.Error())
}
}
workersGroup.Done()
}