-
Notifications
You must be signed in to change notification settings - Fork 59
/
results.go
311 lines (278 loc) · 9.62 KB
/
results.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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
/*
*
* Copyright 2021 The Vitess Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* /
*/
package microbench
import (
"fmt"
"sort"
"strconv"
"strings"
"time"
"unicode"
"github.com/vitessio/arewefastyet/go/storage"
"github.com/dustin/go-humanize"
"github.com/vitessio/arewefastyet/go/tools/math"
)
type (
// Result contains all the metrics measured by a microbenchmark.
Result struct {
Ops float64
NSPerOp float64
MBPerSec float64
BytesPerOp float64
AllocsPerOp float64
}
// BenchmarkId represents the identification of a microbenchmark.
BenchmarkId struct {
PkgName string
Name string
SubBenchmarkName string
}
// Details refers to a single microbenchmark.
Details struct {
BenchmarkId
GitRef string
StartedAt string
Result Result
}
// Comparison allows comparison of two Result
// that share the same BenchmarkId.
Comparison struct {
BenchmarkId
Right, Left Result
// Difference between Right and Left.
Diff Result
}
DetailsArray []Details
ComparisonArray []Comparison
)
// NewDetails creates a new Details.
func NewDetails(benchmarkId BenchmarkId, gitRef string, startedAt string, result Result) *Details {
return &Details{
BenchmarkId: benchmarkId,
GitRef: gitRef,
Result: result,
StartedAt: startedAt,
}
}
// NewBenchmarkId creates a new BenchmarkId.
func NewBenchmarkId(pkgName string, name string, subBenchmarkName string) *BenchmarkId {
return &BenchmarkId{
PkgName: pkgName,
Name: name,
SubBenchmarkName: subBenchmarkName,
}
}
// NewResult creates a new Result.
func NewResult(ops, NSPerOp, MBPerSec, BytesPerOp, AllocsPerOp float64) *Result {
return &Result{
Ops: ops,
NSPerOp: NSPerOp,
MBPerSec: MBPerSec,
BytesPerOp: BytesPerOp,
AllocsPerOp: AllocsPerOp,
}
}
// MergeDetails merges two DetailsArray into a single ComparisonArray.
func MergeDetails(rightMbd, leftMbd DetailsArray) (compareMbs ComparisonArray) {
for _, details := range rightMbd {
compareMb := Comparison{
BenchmarkId: details.BenchmarkId,
Right: details.Result,
}
for j := 0; j < len(leftMbd); j++ {
if leftMbd[j].BenchmarkId == details.BenchmarkId {
compareMb.Left = leftMbd[j].Result
compareMb.Diff.NSPerOp = (compareMb.Right.NSPerOp - compareMb.Left.NSPerOp) / compareMb.Right.NSPerOp * -100
compareMb.Diff.Ops = (compareMb.Right.Ops - compareMb.Left.Ops) / compareMb.Right.Ops * 100
compareMb.Diff.BytesPerOp = (compareMb.Right.BytesPerOp - compareMb.Left.BytesPerOp) / compareMb.Right.BytesPerOp * -100
compareMb.Diff.MBPerSec = (compareMb.Right.MBPerSec - compareMb.Left.MBPerSec) / compareMb.Right.MBPerSec * -100
compareMb.Diff.AllocsPerOp = (compareMb.Right.AllocsPerOp - compareMb.Left.AllocsPerOp) / compareMb.Right.AllocsPerOp * -100
math.CheckForNaN(&compareMb.Diff, 0)
break
}
}
compareMbs = append(compareMbs, compareMb)
}
if compareMbs == nil {
return ComparisonArray{
Comparison{},
}
}
return compareMbs
}
// ReduceSimpleMedianByName reduces a DetailsArray by merging
// all Details with the same benchmark name into a single
// one. The results of each Details correspond to the median
// of the merged elements.
func (mbd DetailsArray) ReduceSimpleMedianByName() (reduceMbd DetailsArray) {
sort.SliceStable(mbd, func(i, j int) bool {
if mbd[i].PkgName == mbd[j].PkgName {
if mbd[i].Name == mbd[j].Name {
return mbd[i].SubBenchmarkName < mbd[j].SubBenchmarkName
}
return mbd[i].Name < mbd[j].Name
}
return mbd[i].PkgName < mbd[j].PkgName
})
reduceMbd = mbd.mergeUsingCondition(func(i, j int) bool {
return mbd[i].Name == mbd[j].Name && mbd[i].PkgName == mbd[j].PkgName && mbd[i].SubBenchmarkName == mbd[j].SubBenchmarkName
})
return reduceMbd
}
// ReduceSimpleMedianByGitRef reduces a DetailsArray by merging
// all Details with the same git ref into a single
// one. The results of each Details correspond to the median
// of the merged elements.
func (mbd DetailsArray) ReduceSimpleMedianByGitRef() (reduceMbd DetailsArray) {
sort.SliceStable(mbd, func(i, j int) bool {
return mbd[i].GitRef < mbd[j].GitRef
})
reduceMbd = mbd.mergeUsingCondition(func(i, j int) bool {
return mbd[i].GitRef == mbd[j].GitRef
})
return reduceMbd
}
// SortByDate will sort the given DetailsArray by ascending date
func (mbd DetailsArray) SortByDate() {
sort.SliceStable(mbd, func(i, j int) bool {
parseLeft, err := time.Parse(time.RFC3339, mbd[i].StartedAt)
if err != nil {
return false
}
parseRight, err := time.Parse(time.RFC3339, mbd[j].StartedAt)
if err != nil {
return false
}
return parseRight.After(parseLeft)
})
}
// mergeUsingCondition is used to merge the DetailsArray based on the compare condition provided
func (mbd DetailsArray) mergeUsingCondition(compareCondition func(i, j int) bool) (reduceMbd DetailsArray) {
for i := 0; i < len(mbd); {
var j int
var interOps []float64
var interNSPerOp []float64
var interMBPerSec []float64
var interBytesPerOp []float64
var interAllocsPerOp []float64
for j = i; j < len(mbd) && compareCondition(i, j); j++ {
interOps = append(interOps, mbd[j].Result.Ops)
interNSPerOp = append(interNSPerOp, mbd[j].Result.NSPerOp)
interMBPerSec = append(interMBPerSec, mbd[j].Result.MBPerSec)
interBytesPerOp = append(interBytesPerOp, mbd[j].Result.BytesPerOp)
interAllocsPerOp = append(interAllocsPerOp, mbd[j].Result.AllocsPerOp)
}
interOpsResult := math.MedianFloat(interOps)
interNSPerOpResult := math.MedianFloat(interNSPerOp)
interMBPerSecResult := math.MedianFloat(interMBPerSec)
interBytesPerOpResult := math.MedianFloat(interBytesPerOp)
interAllocsPerOpResult := math.MedianFloat(interAllocsPerOp)
reduceMbd = append(reduceMbd, *NewDetails(*NewBenchmarkId(mbd[i].PkgName, mbd[i].Name, mbd[i].SubBenchmarkName), mbd[i].GitRef, mbd[i].StartedAt, *NewResult(interOpsResult, interNSPerOpResult, interMBPerSecResult, interBytesPerOpResult, interAllocsPerOpResult)))
i = j
}
return reduceMbd
}
// GetResultsForGitRef will fetch and return a DetailsArray
// containing all the Details linked to the given git commit SHA.
func GetResultsForGitRef(ref string, client storage.SQLClient) (mrs DetailsArray, err error) {
result, err := client.Select("select m.pkg_name, m.name, md.name, md.n, md.ns_per_op, md.bytes_per_op,"+
" md.allocs_per_op, md.mb_per_sec FROM execution e, microbenchmark m, microbenchmark_details md where m.git_ref = ? AND "+
"md.microbenchmark_no = m.microbenchmark_no and e.uuid = m.exec_uuid and e.status = \"finished\" order by m.microbenchmark_no desc", ref)
if err != nil {
return nil, err
}
defer result.Close()
for result.Next() {
var res Details
res.GitRef = ref
err = result.Scan(&res.PkgName, &res.Name, &res.SubBenchmarkName, &res.Result.Ops, &res.Result.NSPerOp, &res.Result.BytesPerOp,
&res.Result.AllocsPerOp, &res.Result.MBPerSec)
if err != nil {
return nil, err
}
mrs = append(mrs, res)
}
return mrs, nil
}
// GetLatestResultsFor will fetch and return a DetailsArray
// containing all the Details linked to latest runs of the given benchmark name.
func GetLatestResultsFor(name, subBenchmarkName string, count int, client storage.SQLClient) (mrs DetailsArray, err error) {
query := "select m.pkg_name, m.name, md.name, m.git_ref , md.n, md.ns_per_op, md.bytes_per_op," +
" md.allocs_per_op, md.mb_per_sec, m.started_at from (select microbenchmark_no, pkg_name, name, microbenchmark.git_ref, started_at" +
" from microbenchmark join execution on exec_uuid = uuid where name = ? and source = \"cron\" and status = \"finished\" order by started_at desc limit ?) m, " +
"microbenchmark_details md where md.microbenchmark_no = m.microbenchmark_no and md.name = ?"
rows, err := client.Select(query, name, count, subBenchmarkName)
if err != nil {
return nil, err
}
defer rows.Close()
for rows.Next() {
var res Details
err = rows.Scan(&res.PkgName, &res.Name, &res.SubBenchmarkName, &res.GitRef, &res.Result.Ops, &res.Result.NSPerOp, &res.Result.BytesPerOp,
&res.Result.AllocsPerOp, &res.Result.MBPerSec, &res.StartedAt)
if err != nil {
return nil, err
}
mrs = append(mrs, res)
}
return mrs, nil
}
func (r Result) OpsStr() string {
if r.Ops == 0 {
return "N/A"
}
return humanize.Comma(int64(r.Ops))
}
func (r Result) NSPerOpStr() string {
if r.NSPerOp == 0 {
return "N/A"
}
return humanize.FormatFloat("#,###.#", r.NSPerOp)
}
func (r Result) NSPerOpToDurationStr() string {
if r.NSPerOp == 0 {
return "N/A"
}
dur, _ := time.ParseDuration(fmt.Sprintf("%fns", r.NSPerOp))
str := dur.String()
i := strings.IndexFunc(str, func(r rune) bool {
return !unicode.IsNumber(r) && r != '.'
})
durStr := str[:i]
durUnit := str[i:]
durFloat, _ := strconv.ParseFloat(durStr, 64)
return fmt.Sprintf("%.2f %s", durFloat, durUnit)
}
func (r Result) MBPerSecStr() string {
if r.MBPerSec == 0 {
return "N/A"
}
return humanize.Bytes(uint64(r.MBPerSec)) + "/s"
}
func (r Result) BytesPerOpStr() string {
if r.BytesPerOp == 0 {
return "N/A"
}
return humanize.Bytes(uint64(r.BytesPerOp)) + "/op"
}
func (r Result) AllocsPerOpStr() string {
if r.AllocsPerOp == 0 {
return "N/A"
}
return humanize.SI(r.AllocsPerOp, "")
}