-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
221 lines (189 loc) · 4.88 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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
package main
import (
"flag"
"log"
"sync"
"time"
"github.com/cbroglie/fluent-bench/batcher"
"github.com/rcrowley/go-metrics"
)
type stats struct {
published metrics.Counter
processed metrics.Timer
succeeded metrics.Counter
failed metrics.Counter
}
const (
timeout = 1 * time.Hour
delay = 1 * time.Hour
batchSize = 1
maxEffectiveRate = 100 // anything higher than this the ticker is too coarse
)
var (
rate = 0
workers = 10
count = 0
useScribe = false
debug = false
s = &stats{}
b *batcher.B
)
func setThrottle(rate int, ticker *time.Ticker) (int, *time.Ticker) {
if ticker != nil {
ticker.Stop()
}
if rate == 0 {
if debug {
log.Printf("Throttling is off rate=%d", rate)
}
return 0, nil
}
if debug {
log.Printf("Setting rate=%d", rate)
}
if rate <= maxEffectiveRate {
batchSize := 1
return batchSize, time.NewTicker(1e9 / time.Duration(rate))
}
// Timer resolution is about 10 ms. Rate higher than 100 rps will
// require batching. It also introduces burstiness.
batchSize := rate / maxEffectiveRate
return batchSize, time.NewTicker(1e9 / time.Duration(maxEffectiveRate))
}
type workerContext struct {
id int
f *fluent
}
func initWorker(id int) interface{} {
if debug {
log.Printf("[worker %d] Init\n", id)
}
f := &fluent{}
if useScribe {
if err := f.connectScribe(); err != nil {
log.Fatal(err)
}
} else {
if err := f.connectTCP(); err != nil {
log.Fatal(err)
}
}
return &workerContext{
id: id,
f: f,
}
}
func shutdownWorker(context interface{}) {
wc := context.(*workerContext)
if debug {
log.Printf("[worker %d] Shutdown\n", wc.id)
}
wc.f.close()
}
func worker(stats *stats, debug bool) func(context interface{}, b batcher.Batch) {
return func(context interface{}, b batcher.Batch) {
wc := context.(*workerContext)
for _, task := range b.Tasks {
ts := time.Now()
if debug {
log.Printf("[worker %d] %s\n", wc.id, task)
}
m := task.(*message)
m.Time = ts.Unix()
err := wc.f.sendMessage(m)
if err != nil {
stats.failed.Inc(1)
} else {
stats.succeeded.Inc(1)
}
stats.processed.UpdateSince(ts)
}
}
}
func main() {
// Parse command line arguments.
flag.IntVar(&rate, "rate", rate, "max requests per second (0 is unlimited)")
flag.IntVar(&workers, "workers", workers, "number of worker processes")
flag.IntVar(&count, "count", count, "number of events to send to fluentd")
flag.BoolVar(&debug, "debug", debug, "debug mode")
flag.BoolVar(&useScribe, "scribe", useScribe, "use scribe protocol")
flag.Parse()
if count <= 0 {
log.Fatal("count must be a positive integer")
}
// Push items onto the input channel as fast as we can.
if debug {
log.Printf("starting work loop rate=%d worker=%d", rate, workers)
}
// Hook in go-metrics
s.published = metrics.NewCounter()
s.processed = metrics.NewTimer()
s.succeeded = metrics.NewCounter()
s.failed = metrics.NewCounter()
b = batcher.New(workers, timeout, batchSize, delay, worker(s, debug), initWorker, shutdownWorker)
b.Start()
var metricsWG sync.WaitGroup
metricsWG.Add(1)
metricsChan := make(chan struct{})
go func() {
defer metricsWG.Done()
for {
select {
case _, ok := <-metricsChan:
if !ok {
return
}
case <-time.After(1 * time.Second):
log.Printf("published=%-10d succeeded=%-10d failed=%-10d inflight=%-10d\n",
s.published.Count(),
s.succeeded.Count(),
s.failed.Count(),
s.published.Count()-s.processed.Count())
}
}
}()
startTime := time.Now()
batchSize, ticker := setThrottle(rate, nil)
for i := 0; i < count; i++ {
// Send the next record.
b.AddTask(&message{
Tag: "ztrack.count",
Record: map[string]interface{}{
"id": "391e4cd5a739444cb2e51c6a549d287d",
"e": "dev",
"c": "count",
"v": 5,
"d": "1,63,1,12213656201,Fuel,consumable,1,,0,Harvester,,,,null,100,2016-04-18,05:39:02,0,cash,1,128657335,,,0,,,0,,0,100,Counter,0,0,0,0,,null,null",
},
})
s.published.Inc(1)
// Rate limit before continuing.
if ticker != nil && i%batchSize == 0 {
<-ticker.C
}
}
if debug {
log.Println("completed work loop")
}
// Short delay to make sure the batcher has picked up everything from the
// input channel (it only waits for in flight work to complete, it doesn't
// drain the input channel).
time.Sleep(10 * time.Millisecond)
b.Stop()
// Stop the metrics worker.
close(metricsChan)
metricsWG.Wait()
log.Printf("published=%-10d succeeded=%-10d failed=%-10d inflight=%-10d\n",
s.published.Count(),
s.succeeded.Count(),
s.failed.Count(),
s.published.Count()-s.processed.Count())
log.Printf("Processed %d records in %.2f seconds (p50=%s p99=%s)\n",
s.processed.Count(),
time.Since(startTime).Seconds(),
formatDuration(s.processed.Percentile(0.50)),
formatDuration(s.processed.Percentile(0.99)))
}
func formatDuration(duration float64) string {
return time.Duration(duration).String()
}