| 1 |
package stressClient |
| 2 |
|
| 3 |
import ( |
| 4 |
"fmt" |
| 5 |
"log" |
| 6 |
"sync" |
| 7 |
|
| 8 |
influx "github.com/influxdata/influxdb/client/v2" |
| 9 |
) |
| 10 |
|
| 11 |
|
| 12 |
func NewStressTest() *StressTest { |
| 13 |
|
| 14 |
packageCh := make(chan Package) |
| 15 |
directiveCh := make(chan Directive) |
| 16 |
responseCh := make(chan Response) |
| 17 |
|
| 18 |
clnt, _ := influx.NewHTTPClient(influx.HTTPConfig{ |
| 19 |
Addr: fmt.Sprintf("http://%v/", "localhost:8086"), |
| 20 |
}) |
| 21 |
|
| 22 |
s := &StressTest{ |
| 23 |
TestDB: "_stressTest", |
| 24 |
Precision: "s", |
| 25 |
StartDate: "2016-01-02", |
| 26 |
BatchSize: 5000, |
| 27 |
|
| 28 |
packageChan: packageCh, |
| 29 |
directiveChan: directiveCh, |
| 30 |
|
| 31 |
ResultsClient: clnt, |
| 32 |
ResultsChan: responseCh, |
| 33 |
communes: make(map[string]*commune), |
| 34 |
TestID: randStr(10), |
| 35 |
} |
| 36 |
|
| 37 |
|
| 38 |
startStressClient(packageCh, directiveCh, responseCh, s.TestID) |
| 39 |
|
| 40 |
|
| 41 |
s.resultsListen() |
| 42 |
|
| 43 |
return s |
| 44 |
} |
| 45 |
|
| 46 |
|
| 47 |
func NewTestStressTest() (*StressTest, chan Package, chan Directive) { |
| 48 |
|
| 49 |
packageCh := make(chan Package) |
| 50 |
directiveCh := make(chan Directive) |
| 51 |
|
| 52 |
s := &StressTest{ |
| 53 |
TestDB: "_stressTest", |
| 54 |
Precision: "s", |
| 55 |
StartDate: "2016-01-02", |
| 56 |
BatchSize: 5000, |
| 57 |
|
| 58 |
directiveChan: directiveCh, |
| 59 |
packageChan: packageCh, |
| 60 |
|
| 61 |
communes: make(map[string]*commune), |
| 62 |
TestID: randStr(10), |
| 63 |
} |
| 64 |
|
| 65 |
return s, packageCh, directiveCh |
| 66 |
} |
| 67 |
|
| 68 |
|
| 69 |
type StressTest struct { |
| 70 |
TestID string |
| 71 |
TestDB string |
| 72 |
|
| 73 |
Precision string |
| 74 |
StartDate string |
| 75 |
BatchSize int |
| 76 |
|
| 77 |
sync.WaitGroup |
| 78 |
sync.Mutex |
| 79 |
|
| 80 |
packageChan chan<- Package |
| 81 |
directiveChan chan<- Directive |
| 82 |
|
| 83 |
ResultsChan chan Response |
| 84 |
communes map[string]*commune |
| 85 |
ResultsClient influx.Client |
| 86 |
} |
| 87 |
|
| 88 |
|
| 89 |
func (st *StressTest) SendPackage(p Package) { |
| 90 |
st.packageChan <- p |
| 91 |
} |
| 92 |
|
| 93 |
|
| 94 |
func (st *StressTest) SendDirective(d Directive) { |
| 95 |
st.directiveChan <- d |
| 96 |
} |
| 97 |
|
| 98 |
|
| 99 |
func (st *StressTest) resultsListen() { |
| 100 |
st.createDatabase(st.TestDB) |
| 101 |
go func() { |
| 102 |
bp := st.NewResultsPointBatch() |
| 103 |
for resp := range st.ResultsChan { |
| 104 |
switch resp.Point.Name() { |
| 105 |
case "done": |
| 106 |
st.ResultsClient.Write(bp) |
| 107 |
resp.Tracer.Done() |
| 108 |
default: |
| 109 |
|
| 110 |
pt, err := resp.AddTags(st.tags()) |
| 111 |
if err != nil { |
| 112 |
panic(err) |
| 113 |
} |
| 114 |
|
| 115 |
bp = st.batcher(pt, bp) |
| 116 |
resp.Tracer.Done() |
| 117 |
} |
| 118 |
} |
| 119 |
}() |
| 120 |
} |
| 121 |
|
| 122 |
|
| 123 |
func (st *StressTest) NewResultsPointBatch() influx.BatchPoints { |
| 124 |
bp, _ := influx.NewBatchPoints(influx.BatchPointsConfig{ |
| 125 |
Database: st.TestDB, |
| 126 |
Precision: "ns", |
| 127 |
}) |
| 128 |
return bp |
| 129 |
} |
| 130 |
|
| 131 |
|
| 132 |
func (st *StressTest) batcher(pt *influx.Point, bp influx.BatchPoints) influx.BatchPoints { |
| 133 |
if len(bp.Points()) <= 5000 { |
| 134 |
bp.AddPoint(pt) |
| 135 |
} else { |
| 136 |
err := st.ResultsClient.Write(bp) |
| 137 |
if err != nil { |
| 138 |
log.Fatalf("Error writing performance stats\n error: %v\n", err) |
| 139 |
} |
| 140 |
bp = st.NewResultsPointBatch() |
| 141 |
} |
| 142 |
return bp |
| 143 |
} |
| 144 |
|
| 145 |
|
| 146 |
func (st *StressTest) createDatabase(db string) { |
| 147 |
query := fmt.Sprintf("CREATE DATABASE %v", db) |
| 148 |
res, err := st.ResultsClient.Query(influx.Query{Command: query}) |
| 149 |
if err != nil { |
| 150 |
log.Fatalf("error: no running influx server at localhost:8086") |
| 151 |
if res.Error() != nil { |
| 152 |
log.Fatalf("error: no running influx server at localhost:8086") |
| 153 |
} |
| 154 |
} |
| 155 |
} |
| 156 |
|
| 157 |
|
| 158 |
func (st *StressTest) GetStatementResults(sID, t string) (res []influx.Result) { |
| 159 |
qryStr := fmt.Sprintf(`SELECT * FROM "%v" WHERE statement_id = '%v'`, t, sID) |
| 160 |
return st.queryTestResults(qryStr) |
| 161 |
} |
| 162 |
|
| 163 |
|
| 164 |
func (st *StressTest) queryTestResults(qry string) (res []influx.Result) { |
| 165 |
response, err := st.ResultsClient.Query(influx.Query{Command: qry, Database: st.TestDB}) |
| 166 |
if err == nil { |
| 167 |
if response.Error() != nil { |
| 168 |
log.Fatalf("Error sending results query\n error: %v\n", response.Error()) |
| 169 |
} |
| 170 |
} |
| 171 |
if response.Results[0].Series == nil { |
| 172 |
return nil |
| 173 |
} |
| 174 |
return response.Results |
| 175 |
} |