| 1 |
package statement |
| 2 |
|
| 3 |
import ( |
| 4 |
"fmt" |
| 5 |
"log" |
| 6 |
"time" |
| 7 |
|
| 8 |
"github.com/influxdata/influxdb/models" |
| 9 |
"github.com/influxdata/influxdb/stress/v2/stress_client" |
| 10 |
) |
| 11 |
|
| 12 |
|
| 13 |
type QueryStatement struct { |
| 14 |
StatementID string |
| 15 |
Name string |
| 16 |
|
| 17 |
|
| 18 |
TemplateString string |
| 19 |
Args []string |
| 20 |
|
| 21 |
|
| 22 |
Count int |
| 23 |
|
| 24 |
|
| 25 |
Tracer *stressClient.Tracer |
| 26 |
|
| 27 |
|
| 28 |
runtime time.Duration |
| 29 |
} |
| 30 |
|
| 31 |
|
| 32 |
func (i *QueryStatement) tags() map[string]string { |
| 33 |
tags := make(map[string]string) |
| 34 |
return tags |
| 35 |
} |
| 36 |
|
| 37 |
|
| 38 |
func (i *QueryStatement) SetID(s string) { |
| 39 |
i.StatementID = s |
| 40 |
} |
| 41 |
|
| 42 |
|
| 43 |
func (i *QueryStatement) Run(s *stressClient.StressTest) { |
| 44 |
|
| 45 |
i.Tracer = stressClient.NewTracer(i.tags()) |
| 46 |
|
| 47 |
vals := make(map[string]interface{}) |
| 48 |
|
| 49 |
var point models.Point |
| 50 |
|
| 51 |
runtime := time.Now() |
| 52 |
|
| 53 |
for j := 0; j < i.Count; j++ { |
| 54 |
|
| 55 |
|
| 56 |
if len(i.Args) == 0 { |
| 57 |
b := []byte(i.TemplateString) |
| 58 |
|
| 59 |
|
| 60 |
p := stressClient.NewPackage(stressClient.Query, b, i.StatementID, i.Tracer) |
| 61 |
|
| 62 |
|
| 63 |
i.Tracer.Add(1) |
| 64 |
|
| 65 |
|
| 66 |
s.SendPackage(p) |
| 67 |
|
| 68 |
} else { |
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
s.Lock() |
| 75 |
point = s.GetPoint(i.Name, s.Precision) |
| 76 |
s.Unlock() |
| 77 |
|
| 78 |
setMapValues(vals, point) |
| 79 |
|
| 80 |
|
| 81 |
b := []byte(fmt.Sprintf(i.TemplateString, setArgs(vals, i.Args)...)) |
| 82 |
|
| 83 |
|
| 84 |
p := stressClient.NewPackage(stressClient.Query, b, i.StatementID, i.Tracer) |
| 85 |
|
| 86 |
|
| 87 |
i.Tracer.Add(1) |
| 88 |
|
| 89 |
|
| 90 |
s.SendPackage(p) |
| 91 |
|
| 92 |
} |
| 93 |
} |
| 94 |
|
| 95 |
|
| 96 |
i.Tracer.Wait() |
| 97 |
|
| 98 |
|
| 99 |
i.runtime = time.Since(runtime) |
| 100 |
} |
| 101 |
|
| 102 |
|
| 103 |
func (i *QueryStatement) Report(s *stressClient.StressTest) string { |
| 104 |
|
| 105 |
allData := s.GetStatementResults(i.StatementID, "query") |
| 106 |
|
| 107 |
if len(allData) == 0 || allData[0].Series == nil { |
| 108 |
log.Fatalf("No data returned for query report\n Statement Name: %v\n Statement ID: %v\n", i.Name, i.StatementID) |
| 109 |
} |
| 110 |
|
| 111 |
qr := &queryReport{ |
| 112 |
name: i.Name, |
| 113 |
columns: allData[0].Series[0].Columns, |
| 114 |
values: allData[0].Series[0].Values, |
| 115 |
} |
| 116 |
|
| 117 |
responseTimes := responseTimes(qr.columns, qr.values) |
| 118 |
|
| 119 |
qr.percentile = percentile(responseTimes) |
| 120 |
qr.avgResponseTime = avgDuration(responseTimes) |
| 121 |
qr.stdDevResponseTime = stddevDuration(responseTimes) |
| 122 |
qr.successfulReads = countSuccesses(qr.columns, qr.values) |
| 123 |
qr.responseBytes = numberBytes(qr.columns, qr.values) |
| 124 |
|
| 125 |
return qr.String() |
| 126 |
} |
| 127 |
|
| 128 |
func getRandomTagPair(m models.Tags) string { |
| 129 |
for k, v := range m { |
| 130 |
return fmt.Sprintf("%v='%v'", k, v) |
| 131 |
} |
| 132 |
|
| 133 |
return "" |
| 134 |
} |
| 135 |
|
| 136 |
func getRandomFieldKey(m map[string]interface{}) string { |
| 137 |
for k := range m { |
| 138 |
return fmt.Sprintf("%v", k) |
| 139 |
} |
| 140 |
|
| 141 |
return "" |
| 142 |
} |
| 143 |
|
| 144 |
func setMapValues(m map[string]interface{}, p models.Point) { |
| 145 |
fields, err := p.Fields() |
| 146 |
if err != nil { |
| 147 |
panic(err) |
| 148 |
} |
| 149 |
m["%f"] = getRandomFieldKey(fields) |
| 150 |
m["%m"] = string(p.Name()) |
| 151 |
m["%t"] = getRandomTagPair(p.Tags()) |
| 152 |
m["%a"] = p.UnixNano() |
| 153 |
} |
| 154 |
|
| 155 |
func setArgs(m map[string]interface{}, args []string) []interface{} { |
| 156 |
values := make([]interface{}, len(args)) |
| 157 |
for i, arg := range args { |
| 158 |
values[i] = m[arg] |
| 159 |
} |
| 160 |
return values |
| 161 |
} |