Debian Codegraph @

stress/v2/statement/report.go

raw symbols SCIP
1 package statement
2
3 import (
4 "encoding/json"
5 "fmt"
6 "log"
7 "math"
8 "sort"
9 "time"
10
11 influx "github.com/influxdata/influxdb/client/v2"
12 )
13
14 // TODO: Refactor this file to utilize a common interface
15 // This will make adding new reports easier in the future
16
17 // Runs performance numbers for insert statements
18 type insertReport struct {
19 name string
20 numRetries int
21 pointsPerSecond int
22 successfulWrites int
23 avgRequestBytes int
24 avgResponseTime time.Duration
25 stdDevResponseTime time.Duration
26 percentile time.Duration
27
28 columns []string
29 values [][]interface{}
30 }
31
32 // Returns the version of the report that is output to STDOUT
33 func (ir *insertReport) String() string {
34 tmplString := `Write Statement: %v
35 Points/Sec: %v
36 Resp Time Average: %v
37 Resp Time Standard Deviation: %v
38 95th Percentile Write Response: %v
39 Average Request Bytes: %v
40 Successful Write Reqs: %v
41 Retries: %v`
42
43 return fmt.Sprintf(tmplString,
44 ir.name,
45 ir.pointsPerSecond,
46 ir.avgResponseTime,
47 ir.stdDevResponseTime,
48 ir.percentile,
49 ir.avgRequestBytes,
50 ir.successfulWrites,
51 ir.numRetries)
52 }
53
54 // Returns a point representation of the report to be written to the ResultsDB
55 func (ir *insertReport) Point() *influx.Point {
56 measurement := "testDefault"
57 tags := map[string]string{}
58 fields := map[string]interface{}{"field": "blank"}
59 point, err := influx.NewPoint(measurement, tags, fields, time.Now())
60 if err != nil {
61 log.Fatalf("Error creating insertReport point\n measurement: %v\n tags: %v\n fields: %v\n error: %v\n", measurement, tags, fields, err)
62 }
63 return point
64 }
65
66 // Runs performance numbers for query statements
67 type queryReport struct {
68 name string
69 successfulReads int
70 responseBytes int
71 stddevResponseBytes int
72 avgResponseTime time.Duration
73 stdDevResponseTime time.Duration
74 percentile time.Duration
75
76 columns []string
77 values [][]interface{}
78 }
79
80 // Returns the version of the report that is output to STDOUT
81 func (qr *queryReport) String() string {
82 tmplString := `Query Statement: %v
83 Resp Time Average: %v
84 Resp Time Standard Deviation: %v
85 95th Percentile Read Response: %v
86 Query Resp Bytes Average: %v bytes
87 Successful Queries: %v`
88
89 return fmt.Sprintf(tmplString,
90 qr.name,
91 qr.avgResponseTime,
92 qr.stdDevResponseTime,
93 qr.percentile,
94 qr.responseBytes,
95 qr.successfulReads)
96 }
97
98 // Returns a point representation of the report to be written to the ResultsDB
99 func (qr *queryReport) Point() *influx.Point {
100 measurement := "testDefault"
101 tags := map[string]string{}
102 fields := map[string]interface{}{"field": "blank"}
103 point, err := influx.NewPoint(measurement, tags, fields, time.Now())
104 if err != nil {
105 log.Fatalf("Error creating queryReport point\n measurement: %v\n tags: %v\n fields: %v\n error: %v\n", measurement, tags, fields, err)
106 }
107 return point
108 }
109
110 // Runs performance numbers for InfluxQL statements
111 type influxQlReport struct {
112 statement string
113 responseTime time.Duration
114 success bool
115
116 columns []string
117 values [][]interface{}
118 }
119
120 // Returns the version of the report that is output to STDOUT
121 func (iqlr *influxQlReport) String() string {
122 // Fancy format success
123 var success string
124 switch iqlr.success {
125 case true:
126 success = "[√]"
127 case false:
128 success = "[X]"
129 }
130 return fmt.Sprintf("%v '%v' -> %v", success, iqlr.statement, iqlr.responseTime)
131 }
132
133 // Returns a point representation of the report to be written to the ResultsDB
134 func (iqlr *influxQlReport) Point() *influx.Point {
135 measurement := "testDefault"
136 tags := map[string]string{}
137 fields := map[string]interface{}{"field": "blank"}
138 point, err := influx.NewPoint(measurement, tags, fields, time.Now())
139 if err != nil {
140 log.Fatalf("Error creating influxQL point\n measurement: %v\n tags: %v\n fields: %v\n error: %v\n", measurement, tags, fields, err)
141 }
142 return point
143 }
144
145 // Given a field or tag name this function returns the index where the values are found
146 func getColumnIndex(col string, columns []string) int {
147 index := -1
148 for i, column := range columns {
149 if column == col {
150 index = i
151 }
152 }
153 return index
154 }
155
156 // Given a full set of results pulls the average num_bytes
157 func numberBytes(columns []string, values [][]interface{}) int {
158 out := 0
159 index := getColumnIndex("num_bytes", columns)
160 for _, val := range values {
161 reqBytes, err := val[index].(json.Number).Int64()
162 if err != nil {
163 log.Fatalf("Error coercing json.Number to Int64\n json.Number:%v\n error: %v\n", val[index], err)
164 }
165 out += int(reqBytes)
166 }
167 return out / len(values)
168 }
169
170 // Counts the number of 200(query) or 204(write) responses and returns them
171 func countSuccesses(columns []string, values [][]interface{}) (out int) {
172 index := getColumnIndex("status_code", columns)
173 for _, val := range values {
174 status, err := val[index].(json.Number).Int64()
175 if err != nil {
176 log.Fatalf("Error coercing json.Number to Int64\n json.Number:%v\n error: %v\n", val[index], err)
177 }
178 if status == 204 || status == 200 {
179 out++
180 }
181 }
182 return out
183 }
184
185 // Counts number of 500 status codes
186 func countRetries(columns []string, values [][]interface{}) (out int) {
187 index := getColumnIndex("status_code", columns)
188 for _, val := range values {
189 status, err := val[index].(json.Number).Int64()
190 if err != nil {
191 log.Fatalf("Error coercing json.Number to Int64\n json.Number:%v\n error: %v\n", val[index], err)
192 }
193 if status == 500 {
194 out++
195 }
196 }
197 return out
198 }
199
200 // Pulls out the response_time_ns values and formats them into ResponseTimes for reporting
201 func responseTimes(columns []string, values [][]interface{}) (rs ResponseTimes) {
202 rs = make([]ResponseTime, 0)
203 index := getColumnIndex("response_time_ns", columns)
204 for _, val := range values {
205 respTime, err := val[index].(json.Number).Int64()
206 if err != nil {
207 log.Fatalf("Error coercing json.Number to Int64\n json.Number:%v\n error: %v\n", val[index], err)
208 }
209 rs = append(rs, NewResponseTime(int(respTime)))
210 }
211 return rs
212 }
213
214 // Returns the 95th perecntile response time
215 func percentile(rs ResponseTimes) time.Duration {
216 sort.Sort(rs)
217 return time.Duration(rs[(len(rs) * 19 / 20)].Value)
218 }
219
220 // Returns the average response time
221 func avgDuration(rs ResponseTimes) (out time.Duration) {
222 for _, t := range rs {
223 out += time.Duration(t.Value)
224 }
225 return out / time.Duration(len(rs))
226 }
227
228 // Returns the standard deviation of a sample of response times
229 func stddevDuration(rs ResponseTimes) (out time.Duration) {
230 avg := avgDuration(rs)
231
232 for _, t := range rs {
233 out += (avg - time.Duration(t.Value)) * (avg - time.Duration(t.Value))
234 }
235
236 return time.Duration(int64(math.Sqrt(float64(out) / float64(len(rs)))))
237 }