forked from influxdata/influxdb-comparisons
-
Notifications
You must be signed in to change notification settings - Fork 0
/
result.go
197 lines (164 loc) · 4.8 KB
/
result.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
package report
import (
"reflect"
"strconv"
"strings"
"time"
)
// ReportParams is holder for common parameters across load and query reports
type ReportParams struct {
DBType string
ReportDatabaseName string
ReportHost string
ReportUser string
ReportPassword string
ReportTags [][2]string
Hostname string
DestinationUrl string
ReportOrgId string
ReportAuthToken string
Workers int
ItemLimit int
}
// LoadReportParams is holder of bulk load specific parameters
type LoadReportParams struct {
ReportParams
IsGzip bool
BatchSize int
}
// type QueryReportParams is holder of bulk query specific parameters
type QueryReportParams struct {
ReportParams
BurnIn int64
}
type ExtraVal struct {
Name string
Value interface{}
}
// ReportLoadResult send results from bulk load to an influxdb according to the given parameters
func ReportLoadResult(params *LoadReportParams, totalItems int64, valueRate float64, inputSpeed float64, loadDuration time.Duration, extraVals ...ExtraVal) error {
c, p, err := initReport(¶ms.ReportParams, "load_benchmarks")
if err != nil {
return err
}
p.AddTag("gzip", strconv.FormatBool(params.IsGzip))
p.AddTag("batch_size", strconv.Itoa(params.BatchSize))
p.AddInt64Field("total_items", totalItems)
p.AddFloat64Field("values_rate", valueRate)
p.AddFloat64Field("input_rate", inputSpeed)
p.AddFloat64Field("duration", loadDuration.Seconds())
for _, v := range extraVals {
switch v.Value.(type) {
case float64:
p.AddFloat64Field(v.Name, v.Value.(float64))
break
case int64:
p.AddInt64Field(v.Name, v.Value.(int64))
break
case int:
p.AddInt64Field(v.Name, int64(v.Value.(int)))
break
default:
panic("unsupported type " + reflect.TypeOf(v.Value).String())
}
}
err = finishReport(c, p)
return err
}
// initReport prepares a Point and a Collector instance for sending a result report
func initReport(params *ReportParams, measurement string) (*Collector, *Point, error) {
var c *Collector
if params.ReportOrgId == "" {
c = NewCollector(params.ReportHost, params.ReportDatabaseName, params.ReportUser, params.ReportPassword)
} else {
c = NewCollectorV2(params.ReportHost, params.ReportOrgId, params.ReportDatabaseName, params.ReportAuthToken)
}
err := c.CreateDatabase()
if err != nil {
return nil, nil, err
}
p := GetPointFromGlobalPool()
p.Init(measurement, time.Now().UnixNano())
for _, tagpair := range params.ReportTags {
p.AddTag(tagpair[0], tagpair[1])
}
p.AddTag("client_hostname", params.Hostname)
p.AddTag("server_url", strings.Replace(params.DestinationUrl, ",", "\\,", -1))
if len(params.DBType) > 0 {
p.AddTag("database_type", params.DBType)
}
p.AddTag("item_limit", strconv.Itoa(params.ItemLimit))
p.AddTag("workers", strconv.Itoa(params.Workers))
return c, p, nil
}
//finishReport finalizes sending result report and cleaning data
func finishReport(c *Collector, p *Point) error {
c.Put(p)
c.PrepBatch()
err := c.SendBatch()
PutPointIntoGlobalPool(p)
return err
}
const escapes = "\t\n\f\r ,="
var escaper = strings.NewReplacer(
"\t", `\t`,
"\n", `\n`,
"\f", `\f`,
"\r", `\r`,
`,`, `\,`,
` `, `\ `,
`=`, `\=`,
)
func Escape(s string) string {
if strings.ContainsAny(s, escapes) {
return escaper.Replace(s)
} else {
return s
}
}
//ReportQueryResult send result from bulk query benchmark to an influxdb according to the given parameters
func ReportQueryResult(params *QueryReportParams, queryName string, minQueryTime float64, meanQueryTime float64, maxQueryTime float64, totalQueries int64, queryDuration time.Duration, extraVals ...ExtraVal) error {
c, p, err := initReport(¶ms.ReportParams, "query_benchmarks")
if err != nil {
return err
}
p.AddTag("burn_in", strconv.Itoa(int(params.BurnIn)))
p.AddTag("query_name", Escape(queryName))
p.AddFloat64Field("min_time", minQueryTime)
if minQueryTime > 0 {
p.AddFloat64Field("min_rate", 1000/minQueryTime)
} else {
p.AddFloat64Field("min_rate", -1)
}
p.AddFloat64Field("mean_time", meanQueryTime)
if meanQueryTime > 0 {
p.AddFloat64Field("mean_rate", 1000/meanQueryTime)
} else {
p.AddFloat64Field("mean_rate", -1)
}
p.AddFloat64Field("max_time", maxQueryTime)
if maxQueryTime > 0 {
p.AddFloat64Field("max_rate", 1000/maxQueryTime)
} else {
p.AddFloat64Field("max_rate", -1)
}
p.AddInt64Field("total_items", totalQueries)
p.AddFloat64Field("duration", queryDuration.Seconds())
for _, v := range extraVals {
switch v.Value.(type) {
case float64:
p.AddFloat64Field(v.Name, v.Value.(float64))
break
case int64:
p.AddInt64Field(v.Name, v.Value.(int64))
break
case int:
p.AddInt64Field(v.Name, int64(v.Value.(int)))
break
default:
panic("unsupported type " + reflect.TypeOf(v.Value).String())
}
}
err = finishReport(c, p)
return err
}