Debian Codegraph @

stress/v2/stress_client/stress_client.go

raw symbols SCIP
1 package stressClient
2
3 import (
4 "strings"
5 "sync"
6 )
7
8 // Type refers to the different Package types
9 type Type int
10
11 // There are two package types, Write and Query
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 // start listening for writes and queries
41 go c.listen()
42 // start listening for state changes
43 go c.directiveListen()
44 }
45
46 type stressClient struct {
47 testID string
48
49 // State for the Stress Test
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 // Channels from statements
61 packageChan <-chan Package
62 directiveChan <-chan Directive
63
64 // Response channel
65 responseChan chan<- Response
66
67 // Concurrency utilities
68 sync.WaitGroup
69 sync.Mutex
70
71 // Concurrency Limit for Writes and Reads
72 wconc int
73 qconc int
74
75 // Manage Read and Write concurrency seperately
76 wc *ConcurrencyLimiter
77 rc *ConcurrencyLimiter
78 }
79
80 // NewTestStressClient returns a blank stressClient for testing
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 // stressClient starts listening for Packages on the main channel
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 // Set handles all SET requests for test state
129 func (sc *stressClient) directiveListen() {
130 for d := range sc.directiveChan {
131 sc.Lock()
132 switch d.Property {
133 // addresses is a []string of target InfluxDB instance(s) for the test
134 // comes in as a "|" seperated array of addresses
135 case "addresses":
136 addr := strings.Split(d.Value, "|")
137 sc.addresses = addr
138 // percison is the write precision for InfluxDB
139 case "precision":
140 sc.precision = d.Value
141 // writeinterval is an optional delay between batches
142 case "writeinterval":
143 sc.wdelay = d.Value
144 // queryinterval is an optional delay between the batches
145 case "queryinterval":
146 sc.qdelay = d.Value
147 // database is the InfluxDB database to target for both writes and queries
148 case "database":
149 sc.database = d.Value
150 // username for the target database
151 case "username":
152 sc.username = d.Value
153 // username for the target database
154 case "password":
155 sc.password = d.Value
156 // use https if sent true
157 case "ssl":
158 if d.Value == "true" {
159 sc.ssl = true
160 }
161 // concurrency is the number concurrent writers to the database
162 case "writeconcurrency":
163 conc := parseInt(d.Value)
164 sc.wconc = conc
165 sc.wc.NewMax(conc)
166 // concurrentqueries is the number of concurrent queriers database
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 }