| 1 |
package stressClient |
| 2 |
|
| 3 |
import ( |
| 4 |
"strings" |
| 5 |
"sync" |
| 6 |
) |
| 7 |
|
| 8 |
|
| 9 |
type Type int |
| 10 |
|
| 11 |
|
| 12 |
const ( |
| 13 |
Write Type = iota |
| 14 |
Query |
| 15 |
) |
| 16 |
|
| 17 |
func startStressClient(packageCh <-chan Package, directiveCh <-chan Directive, responseCh chan<- Response, testID string) { |
| 18 |
|
| 19 |
c := &stressClient{ |
| 20 |
testID: testID, |
| 21 |
|
| 22 |
addresses: []string{"localhost:8086"}, |
| 23 |
ssl: false, |
| 24 |
username: "", |
| 25 |
password: "", |
| 26 |
precision: "ns", |
| 27 |
database: "stress", |
| 28 |
startDate: "2016-01-01", |
| 29 |
qdelay: "0s", |
| 30 |
wdelay: "0s", |
| 31 |
|
| 32 |
wconc: 10, |
| 33 |
qconc: 5, |
| 34 |
|
| 35 |
packageChan: packageCh, |
| 36 |
directiveChan: directiveCh, |
| 37 |
|
| 38 |
responseChan: responseCh, |
| 39 |
} |
| 40 |
|
| 41 |
go c.listen() |
| 42 |
|
| 43 |
go c.directiveListen() |
| 44 |
} |
| 45 |
|
| 46 |
type stressClient struct { |
| 47 |
testID string |
| 48 |
|
| 49 |
|
| 50 |
addresses []string |
| 51 |
precision string |
| 52 |
startDate string |
| 53 |
database string |
| 54 |
wdelay string |
| 55 |
qdelay string |
| 56 |
username string |
| 57 |
password string |
| 58 |
ssl bool |
| 59 |
|
| 60 |
|
| 61 |
packageChan <-chan Package |
| 62 |
directiveChan <-chan Directive |
| 63 |
|
| 64 |
|
| 65 |
responseChan chan<- Response |
| 66 |
|
| 67 |
|
| 68 |
sync.WaitGroup |
| 69 |
sync.Mutex |
| 70 |
|
| 71 |
|
| 72 |
wconc int |
| 73 |
qconc int |
| 74 |
|
| 75 |
|
| 76 |
wc *ConcurrencyLimiter |
| 77 |
rc *ConcurrencyLimiter |
| 78 |
} |
| 79 |
|
| 80 |
|
| 81 |
func newTestStressClient(url string) (*stressClient, chan Directive, chan Package) { |
| 82 |
pkgChan := make(chan Package) |
| 83 |
dirChan := make(chan Directive) |
| 84 |
pe := &stressClient{ |
| 85 |
testID: "foo_id", |
| 86 |
addresses: []string{url}, |
| 87 |
precision: "s", |
| 88 |
startDate: "2016-01-01", |
| 89 |
database: "fooDatabase", |
| 90 |
wdelay: "50ms", |
| 91 |
qdelay: "50ms", |
| 92 |
ssl: false, |
| 93 |
username: "", |
| 94 |
password: "", |
| 95 |
wconc: 5, |
| 96 |
qconc: 5, |
| 97 |
packageChan: pkgChan, |
| 98 |
directiveChan: dirChan, |
| 99 |
wc: NewConcurrencyLimiter(1), |
| 100 |
rc: NewConcurrencyLimiter(1), |
| 101 |
} |
| 102 |
return pe, dirChan, pkgChan |
| 103 |
} |
| 104 |
|
| 105 |
|
| 106 |
func (sc *stressClient) listen() { |
| 107 |
defer sc.Wait() |
| 108 |
sc.wc = NewConcurrencyLimiter(sc.wconc) |
| 109 |
sc.rc = NewConcurrencyLimiter(sc.qconc) |
| 110 |
l := NewConcurrencyLimiter((sc.wconc + sc.qconc) * 2) |
| 111 |
counter := 0 |
| 112 |
for p := range sc.packageChan { |
| 113 |
l.Increment() |
| 114 |
go func(p Package) { |
| 115 |
defer l.Decrement() |
| 116 |
switch p.T { |
| 117 |
case Write: |
| 118 |
sc.spinOffWritePackage(p, (counter % len(sc.addresses))) |
| 119 |
case Query: |
| 120 |
sc.spinOffQueryPackage(p, (counter % len(sc.addresses))) |
| 121 |
} |
| 122 |
}(p) |
| 123 |
counter++ |
| 124 |
} |
| 125 |
|
| 126 |
} |
| 127 |
|
| 128 |
|
| 129 |
func (sc *stressClient) directiveListen() { |
| 130 |
for d := range sc.directiveChan { |
| 131 |
sc.Lock() |
| 132 |
switch d.Property { |
| 133 |
|
| 134 |
|
| 135 |
case "addresses": |
| 136 |
addr := strings.Split(d.Value, "|") |
| 137 |
sc.addresses = addr |
| 138 |
|
| 139 |
case "precision": |
| 140 |
sc.precision = d.Value |
| 141 |
|
| 142 |
case "writeinterval": |
| 143 |
sc.wdelay = d.Value |
| 144 |
|
| 145 |
case "queryinterval": |
| 146 |
sc.qdelay = d.Value |
| 147 |
|
| 148 |
case "database": |
| 149 |
sc.database = d.Value |
| 150 |
|
| 151 |
case "username": |
| 152 |
sc.username = d.Value |
| 153 |
|
| 154 |
case "password": |
| 155 |
sc.password = d.Value |
| 156 |
|
| 157 |
case "ssl": |
| 158 |
if d.Value == "true" { |
| 159 |
sc.ssl = true |
| 160 |
} |
| 161 |
|
| 162 |
case "writeconcurrency": |
| 163 |
conc := parseInt(d.Value) |
| 164 |
sc.wconc = conc |
| 165 |
sc.wc.NewMax(conc) |
| 166 |
|
| 167 |
case "queryconcurrency": |
| 168 |
conc := parseInt(d.Value) |
| 169 |
sc.qconc = conc |
| 170 |
sc.rc.NewMax(conc) |
| 171 |
} |
| 172 |
d.Tracer.Done() |
| 173 |
sc.Unlock() |
| 174 |
} |
| 175 |
} |