forked from influxdata/influxdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ponyExpress.go
198 lines (162 loc) · 4.29 KB
/
ponyExpress.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
package ponyExpress
import (
"strings"
"sync"
)
// Type refers to the different Package types
type Type int
// There are two package types, Write and Query
const (
Write Type = iota
Query
)
func startPonyExpress(packageCh <-chan Package, directiveCh <-chan Directive, responseCh chan<- Response, testID string) {
c := &ponyExpress{
testID: testID,
addresses: []string{"localhost:8086"},
ssl: false,
username: "",
password: "",
precision: "ns",
database: "stress",
startDate: "2016-01-01",
qdelay: "0s",
wdelay: "0s",
wconc: 10,
qconc: 5,
packageChan: packageCh,
directiveChan: directiveCh,
responseChan: responseCh,
}
// start listening for writes and queries
go c.listen()
// start listening for state changes
go c.directiveListen()
}
type ponyExpress struct {
testID string
// State for the Stress Test
addresses []string
precision string
startDate string
database string
wdelay string
qdelay string
username string
password string
ssl bool
// Channels from statements
packageChan <-chan Package
directiveChan <-chan Directive
// Response channel
responseChan chan<- Response
// Concurrency utilities
sync.WaitGroup
sync.Mutex
// Concurrency Limit for Writes and Reads
wconc int
qconc int
// Manage Read and Write concurrency seperately
wc *ConcurrencyLimiter
rc *ConcurrencyLimiter
}
// NewTestPonyExpress returns a blank ponyExpress for testing
func newTestPonyExpress(url string) (*ponyExpress, chan Directive, chan Package) {
pkgChan := make(chan Package)
dirChan := make(chan Directive)
pe := &ponyExpress{
testID: "foo_id",
addresses: []string{url},
precision: "s",
startDate: "2016-01-01",
database: "fooDatabase",
wdelay: "50ms",
qdelay: "50ms",
ssl: false,
username: "",
password: "",
wconc: 5,
qconc: 5,
packageChan: pkgChan,
directiveChan: dirChan,
wc: NewConcurrencyLimiter(1),
rc: NewConcurrencyLimiter(1),
}
return pe, dirChan, pkgChan
}
// client starts listening for Packages on the main channel
func (pe *ponyExpress) listen() {
defer pe.Wait()
// Keep track of number of concurrent readers and writers seperately
pe.wc = NewConcurrencyLimiter(pe.wconc)
pe.rc = NewConcurrencyLimiter(pe.qconc)
// Manage overall number of goroutines and keep at 2 x (wconc + qconc)
l := NewConcurrencyLimiter((pe.wconc + pe.qconc) * 2)
// Concume incoming packages
counter := 0
for p := range pe.packageChan {
serv := counter % len(pe.addresses)
l.Increment()
go func(p Package) {
defer l.Decrement()
switch p.T {
case Write:
pe.spinOffWritePackage(p, serv)
case Query:
pe.spinOffQueryPackage(p, serv)
}
}(p)
counter++
}
}
// Set handles all SET requests for test state
func (pe *ponyExpress) directiveListen() {
for d := range pe.directiveChan {
pe.Lock()
switch d.Property {
// addresses is a []string of target InfluxDB instance(s) for the test
// comes in as a "|" seperated array of addresses
case "addresses":
addr := strings.Split(d.Value, "|")
pe.addresses = addr
// percison is the write precision for InfluxDB
case "precision":
pe.precision = d.Value
// writeinterval is an optional delay between batches
case "writeinterval":
pe.wdelay = d.Value
// queryinterval is an optional delay between the batches
case "queryinterval":
pe.qdelay = d.Value
// database is the InfluxDB database to target for both writes and queries
case "database":
pe.database = d.Value
// username for the target database
case "username":
pe.username = d.Value
// username for the target database
case "password":
pe.password = d.Value
// use https if the there is a value for ssl
case "ssl":
if d.Value == "true" {
pe.ssl = true
}
// concurrency is the number concurrent writers to the database
case "writeconcurrency":
conc := parseInt(d.Value)
pe.wconc = conc
// Reset the ConcurrencyLimiter
pe.wc.NewMax(conc)
// concurrentqueries is the number of concurrent queries to run against the database
case "queryconcurrency":
conc := parseInt(d.Value)
pe.qconc = conc
// Reset the ConcurrencyLimiter
pe.rc.NewMax(conc)
}
// Decrement the tracker
d.Tracer.Done()
pe.Unlock()
}
}