| 1 |
package statement |
| 2 |
|
| 3 |
import ( |
| 4 |
"bytes" |
| 5 |
"fmt" |
| 6 |
"log" |
| 7 |
"strconv" |
| 8 |
"strings" |
| 9 |
"sync" |
| 10 |
"time" |
| 11 |
|
| 12 |
"github.com/influxdata/influxdb/stress/v2/stress_client" |
| 13 |
) |
| 14 |
|
| 15 |
|
| 16 |
type InsertStatement struct { |
| 17 |
TestID string |
| 18 |
StatementID string |
| 19 |
|
| 20 |
|
| 21 |
Name string |
| 22 |
|
| 23 |
|
| 24 |
TemplateString string |
| 25 |
|
| 26 |
|
| 27 |
TagCount int |
| 28 |
|
| 29 |
|
| 30 |
Tracer *stressClient.Tracer |
| 31 |
|
| 32 |
|
| 33 |
Timestamp *Timestamp |
| 34 |
|
| 35 |
|
| 36 |
Templates Templates |
| 37 |
stringers Stringers |
| 38 |
|
| 39 |
|
| 40 |
series int |
| 41 |
|
| 42 |
|
| 43 |
time func() int64 |
| 44 |
|
| 45 |
|
| 46 |
sync.WaitGroup |
| 47 |
sync.Mutex |
| 48 |
|
| 49 |
|
| 50 |
runtime time.Duration |
| 51 |
} |
| 52 |
|
| 53 |
func (i *InsertStatement) tags() map[string]string { |
| 54 |
tags := map[string]string{ |
| 55 |
"number_fields": i.numFields(), |
| 56 |
"number_series": fmtInt(i.series), |
| 57 |
"number_points_write": fmtInt(i.Timestamp.Count), |
| 58 |
} |
| 59 |
return tags |
| 60 |
} |
| 61 |
|
| 62 |
|
| 63 |
func (i *InsertStatement) SetID(s string) { |
| 64 |
i.StatementID = s |
| 65 |
} |
| 66 |
|
| 67 |
|
| 68 |
func (i *InsertStatement) SetVars(s *stressClient.StressTest) chan<- string { |
| 69 |
|
| 70 |
i.series = 1 |
| 71 |
|
| 72 |
|
| 73 |
for _, tmpl := range i.Templates[0:i.TagCount] { |
| 74 |
i.series *= tmpl.numSeries() |
| 75 |
} |
| 76 |
|
| 77 |
|
| 78 |
i.stringers = i.Templates.Init(i.series) |
| 79 |
|
| 80 |
|
| 81 |
i.time = i.Timestamp.Time(s.StartDate, i.series, s.Precision) |
| 82 |
|
| 83 |
|
| 84 |
s.Lock() |
| 85 |
comCh := s.SetCommune(i.Name) |
| 86 |
s.Unlock() |
| 87 |
|
| 88 |
|
| 89 |
i.Tracer = stressClient.NewTracer(i.tags()) |
| 90 |
|
| 91 |
return comCh |
| 92 |
} |
| 93 |
|
| 94 |
|
| 95 |
func (i *InsertStatement) Run(s *stressClient.StressTest) { |
| 96 |
|
| 97 |
|
| 98 |
comCh := i.SetVars(s) |
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
ctr := 0 |
| 103 |
|
| 104 |
|
| 105 |
buf := bytes.NewBuffer([]byte{}) |
| 106 |
|
| 107 |
runtime := time.Now() |
| 108 |
|
| 109 |
for k := 0; k < i.Timestamp.Count; k++ { |
| 110 |
|
| 111 |
|
| 112 |
ctr++ |
| 113 |
|
| 114 |
|
| 115 |
point := fmt.Sprintf(i.TemplateString, i.stringers.Eval(i.time)...) |
| 116 |
|
| 117 |
|
| 118 |
buf.WriteString(point) |
| 119 |
|
| 120 |
buf.WriteString("\n") |
| 121 |
|
| 122 |
|
| 123 |
if ctr%s.BatchSize == 0 && ctr != 0 { |
| 124 |
b := buf.Bytes() |
| 125 |
|
| 126 |
b = b[0 : len(b)-1] |
| 127 |
|
| 128 |
|
| 129 |
p := stressClient.NewPackage(stressClient.Write, b, i.StatementID, i.Tracer) |
| 130 |
|
| 131 |
|
| 132 |
i.Tracer.Add(1) |
| 133 |
|
| 134 |
|
| 135 |
s.SendPackage(p) |
| 136 |
|
| 137 |
|
| 138 |
temp := bytes.NewBuffer([]byte{}) |
| 139 |
buf = temp |
| 140 |
} |
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
if len(comCh) < cap(comCh) { |
| 145 |
select { |
| 146 |
case comCh <- point: |
| 147 |
break |
| 148 |
default: |
| 149 |
break |
| 150 |
} |
| 151 |
} |
| 152 |
|
| 153 |
} |
| 154 |
|
| 155 |
|
| 156 |
if buf.Len() != 0 { |
| 157 |
b := buf.Bytes() |
| 158 |
|
| 159 |
b = b[0 : len(b)-1] |
| 160 |
|
| 161 |
|
| 162 |
p := stressClient.NewPackage(stressClient.Write, b, i.StatementID, i.Tracer) |
| 163 |
|
| 164 |
|
| 165 |
i.Tracer.Add(1) |
| 166 |
|
| 167 |
|
| 168 |
s.SendPackage(p) |
| 169 |
} |
| 170 |
|
| 171 |
|
| 172 |
i.Tracer.Wait() |
| 173 |
|
| 174 |
|
| 175 |
i.runtime = time.Since(runtime) |
| 176 |
} |
| 177 |
|
| 178 |
|
| 179 |
func (i *InsertStatement) Report(s *stressClient.StressTest) string { |
| 180 |
|
| 181 |
allData := s.GetStatementResults(i.StatementID, "write") |
| 182 |
|
| 183 |
if allData == nil || allData[0].Series == nil { |
| 184 |
log.Fatalf("No data returned for write report\n Statement Name: %v\n Statement ID: %v\n", i.Name, i.StatementID) |
| 185 |
} |
| 186 |
|
| 187 |
ir := &insertReport{ |
| 188 |
name: i.Name, |
| 189 |
columns: allData[0].Series[0].Columns, |
| 190 |
values: allData[0].Series[0].Values, |
| 191 |
} |
| 192 |
|
| 193 |
responseTimes := responseTimes(ir.columns, ir.values) |
| 194 |
|
| 195 |
ir.percentile = percentile(responseTimes) |
| 196 |
ir.avgResponseTime = avgDuration(responseTimes) |
| 197 |
ir.stdDevResponseTime = stddevDuration(responseTimes) |
| 198 |
ir.pointsPerSecond = int(float64(i.Timestamp.Count) / i.runtime.Seconds()) |
| 199 |
ir.numRetries = countRetries(ir.columns, ir.values) |
| 200 |
ir.successfulWrites = countSuccesses(ir.columns, ir.values) |
| 201 |
ir.avgRequestBytes = numberBytes(ir.columns, ir.values) |
| 202 |
|
| 203 |
return ir.String() |
| 204 |
} |
| 205 |
|
| 206 |
func (i *InsertStatement) numFields() string { |
| 207 |
pt := strings.Split(i.TemplateString, " ") |
| 208 |
fields := strings.Split(pt[1], ",") |
| 209 |
return fmtInt(len(fields)) |
| 210 |
} |
| 211 |
|
| 212 |
func fmtInt(i int) string { |
| 213 |
return strconv.FormatInt(int64(i), 10) |
| 214 |
} |