Debian Codegraph @

stress/v2/statement/insert.go

raw symbols SCIP
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 // InsertStatement is a Statement Implementation that creates points to be written to the target InfluxDB instance
16 type InsertStatement struct {
17 TestID string
18 StatementID string
19
20 // Statement Name
21 Name string
22
23 // Template string for points. Filled by the output of stringers
24 TemplateString string
25
26 // TagCount is used to find the number of series in the dataset
27 TagCount int
28
29 // The Tracer prevents InsertStatement.Run() from returning early
30 Tracer *stressClient.Tracer
31
32 // Timestamp is #points to write and percision
33 Timestamp *Timestamp
34
35 // Templates turn into stringers
36 Templates Templates
37 stringers Stringers
38
39 // Number of series in this insert Statement
40 series int
41
42 // Returns the proper time for the next point
43 time func() int64
44
45 // Concurrency utiliities
46 sync.WaitGroup
47 sync.Mutex
48
49 // Timer for runtime and pps calculation
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 // SetID statisfies the Statement Interface
63 func (i *InsertStatement) SetID(s string) {
64 i.StatementID = s
65 }
66
67 // SetVars sets up the environment for InsertStatement to call it's Run function
68 func (i *InsertStatement) SetVars(s *stressClient.StressTest) chan<- string {
69 // Set the #series at 1 to start
70 i.series = 1
71
72 // Num series is the product of the cardinality of the tags
73 for _, tmpl := range i.Templates[0:i.TagCount] {
74 i.series *= tmpl.numSeries()
75 }
76
77 // make stringers from the templates
78 i.stringers = i.Templates.Init(i.series)
79
80 // Set the time function, keeps track of 'time' of the points being created
81 i.time = i.Timestamp.Time(s.StartDate, i.series, s.Precision)
82
83 // Set a commune on the StressTest
84 s.Lock()
85 comCh := s.SetCommune(i.Name)
86 s.Unlock()
87
88 // Set the tracer
89 i.Tracer = stressClient.NewTracer(i.tags())
90
91 return comCh
92 }
93
94 // Run statisfies the Statement Interface
95 func (i *InsertStatement) Run(s *stressClient.StressTest) {
96
97 // Set variables on the InsertStatement and make the comCh
98 comCh := i.SetVars(s)
99
100 // TODO: Refactor to eleminate the ctr
101 // Start the counter
102 ctr := 0
103
104 // Create the first bytes buffer
105 buf := bytes.NewBuffer([]byte{})
106
107 runtime := time.Now()
108
109 for k := 0; k < i.Timestamp.Count; k++ {
110
111 // Increment the counter. ctr == k + 1?
112 ctr++
113
114 // Make the point from the template string and the stringers
115 point := fmt.Sprintf(i.TemplateString, i.stringers.Eval(i.time)...)
116
117 // Add the string to the buffer
118 buf.WriteString(point)
119 // Add a newline char to seperate the points
120 buf.WriteString("\n")
121
122 // If len(batch) == batchSize then send it
123 if ctr%s.BatchSize == 0 && ctr != 0 {
124 b := buf.Bytes()
125 // Trimming the trailing newline character
126 b = b[0 : len(b)-1]
127
128 // Create the package
129 p := stressClient.NewPackage(stressClient.Write, b, i.StatementID, i.Tracer)
130
131 // Use Tracer to wait for all operations to finish
132 i.Tracer.Add(1)
133
134 // Send the package
135 s.SendPackage(p)
136
137 // Reset the bytes Buffer
138 temp := bytes.NewBuffer([]byte{})
139 buf = temp
140 }
141
142 // TODO: Racy
143 // Has to do with InsertStatement and QueryStatement communication
144 if len(comCh) < cap(comCh) {
145 select {
146 case comCh <- point:
147 break
148 default:
149 break
150 }
151 }
152
153 }
154
155 // If There are additional points remaining in the buffer send them before exiting
156 if buf.Len() != 0 {
157 b := buf.Bytes()
158 // Trimming the trailing newline character
159 b = b[0 : len(b)-1]
160
161 // Create the package
162 p := stressClient.NewPackage(stressClient.Write, b, i.StatementID, i.Tracer)
163
164 // Use Tracer to wait for all operations to finish
165 i.Tracer.Add(1)
166
167 // Send the package
168 s.SendPackage(p)
169 }
170
171 // Wait for all tracers to decrement
172 i.Tracer.Wait()
173
174 // Stop the timer
175 i.runtime = time.Since(runtime)
176 }
177
178 // Report statisfies the Statement Interface
179 func (i *InsertStatement) Report(s *stressClient.StressTest) string {
180 // Pull data via StressTest client
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 }