@@ -25,14 +25,15 @@ import (
25
25
26
26
// Program option vars:
27
27
var (
28
- csvDaemonUrls string
29
- daemonUrls []string
28
+ csvDaemonUrls string
29
+ daemonUrls []string
30
30
dbName string
31
31
replicationFactor int
32
32
workers int
33
33
lineLimit int64
34
34
batchSize int
35
35
backoff time.Duration
36
+ timeLimit time.Duration
36
37
doLoad bool
37
38
doDBCreate bool
38
39
useGzip bool
@@ -42,10 +43,10 @@ var (
42
43
43
44
// Global vars
44
45
var (
45
- bufPool sync.Pool
46
- batchChan chan * bytes.Buffer
47
- inputDone chan struct {}
48
- workersGroup sync.WaitGroup
46
+ bufPool sync.Pool
47
+ batchChan chan * bytes.Buffer
48
+ inputDone chan struct {}
49
+ workersGroup sync.WaitGroup
49
50
backingOffChans []chan bool
50
51
backingOffDones []chan struct {}
51
52
)
@@ -59,6 +60,7 @@ func init() {
59
60
flag .IntVar (& workers , "workers" , 1 , "Number of parallel requests to make." )
60
61
flag .Int64Var (& lineLimit , "line-limit" , - 1 , "Number of lines to read from stdin before quitting." )
61
62
flag .DurationVar (& backoff , "backoff" , time .Second , "Time to sleep between requests when server indicates backpressure is needed." )
63
+ flag .DurationVar (& timeLimit , "time-limit" , - 1 , "Maximum duration to run (-1 is the default: no limit)." )
62
64
flag .BoolVar (& useGzip , "gzip" , false , "Whether to gzip encode requests." )
63
65
flag .BoolVar (& doLoad , "do-load" , true , "Whether to write data. Set this flag to false to check input read speed." )
64
66
flag .BoolVar (& doDBCreate , "do-db-create" , true , "Whether to create the database." )
@@ -99,7 +101,7 @@ func main() {
99
101
if err != nil {
100
102
log .Fatal (err )
101
103
}
102
- time .Sleep (2000 * time .Millisecond )
104
+ time .Sleep (1000 * time .Millisecond )
103
105
}
104
106
}
105
107
@@ -121,9 +123,9 @@ func main() {
121
123
backingOffDones [i ] = make (chan struct {})
122
124
workersGroup .Add (1 )
123
125
cfg := HTTPWriterConfig {
124
- DebugInfo : fmt .Sprintf ("worker #%d, dest url: %s" , i , daemonUrl ),
125
- Host : daemonUrl ,
126
- Database : dbName ,
126
+ DebugInfo : fmt .Sprintf ("worker #%d, dest url: %s" , i , daemonUrl ),
127
+ Host : daemonUrl ,
128
+ Database : dbName ,
127
129
BackingOffChan : backingOffChans [i ],
128
130
BackingOffDone : backingOffDones [i ],
129
131
}
@@ -159,8 +161,13 @@ func scan(linesPerBatch int) int64 {
159
161
var n int
160
162
var itemsRead int64
161
163
newline := []byte ("\n " )
164
+ var deadline time.Time
165
+ if timeLimit >= 0 {
166
+ deadline = time .Now ().Add (timeLimit )
167
+ }
162
168
163
169
scanner := bufio .NewScanner (bufio .NewReaderSize (os .Stdin , 4 * 1024 * 1024 ))
170
+ outer:
164
171
for scanner .Scan () {
165
172
if itemsRead == lineLimit {
166
173
break
@@ -173,6 +180,9 @@ func scan(linesPerBatch int) int64 {
173
180
174
181
n ++
175
182
if n >= linesPerBatch {
183
+ if timeLimit >= 0 && time .Now ().After (deadline ) {
184
+ break outer
185
+ }
176
186
batchChan <- buf
177
187
buf = bufPool .Get ().(* bytes.Buffer )
178
188
n = 0
@@ -212,7 +222,6 @@ func processBatches(w *HTTPWriter, backoffSrc chan bool, backoffDst chan struct{
212
222
_ , err = w .WriteLineProtocol (batch .Bytes (), false )
213
223
}
214
224
215
-
216
225
if err == BackoffError {
217
226
backoffSrc <- true
218
227
time .Sleep (backoff )
0 commit comments