-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
536 lines (437 loc) · 10.6 KB
/
main.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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
// Copyright 2015 Florian Orben. All rights reserved.
// Use of this source code is governed under the Apache License, Version 2.0
// that can be found in the LICENSE file.
// Package prettybenchmarks formats your go benchmarks into nice looking sorted tables
//
// Prettybenchmarks
//
// Works with and without -benchmem flag
//
// If you provide a time interval (either ns, µs (or us), ms, s), each benchmark's runtime will be
// converted to that interval. If left blank, a suitable value will automatically be chosen
//
// go test -bench=YOUR_PKG [-benchmem] | pb [timeinterval]
// Example
// go test -bench=. -benchmem | pb ms
//
package prettybenchmarks
import (
"bufio"
"flag"
"fmt"
"io"
"os"
"regexp"
"sort"
"strconv"
"strings"
"sync"
"time"
"github.com/apcera/termtables"
)
const (
fmtInt = "#,###."
fmtFloat = "#,###.###"
fmtFloatNS = "#,###."
)
type (
benchmark struct {
info *benchmarkInfo
results *results
}
benchmarkInfo struct {
hasFnIterations bool
benchmemUsed bool
suggestedTiming string
}
results map[string][]*result
result struct {
Name string
FnIterations int
Runs int
Speed float64
Bps int
Aps int
}
)
type sortByFnIterations []*result
func (b sortByFnIterations) Len() int { return len(b) }
func (b sortByFnIterations) Swap(i, j int) { b[i], b[j] = b[j], b[i] }
func (b sortByFnIterations) Less(i, j int) bool { return b[i].FnIterations < b[j].FnIterations }
var (
regExByWhitespace = regexp.MustCompile(`\s+`)
regExByRuns = regexp.MustCompile(`-\d+$`)
regExByIterations = regexp.MustCompile(`(?i:)(^Benchmark_?)`)
regExIsBenchmark = regExByIterations
linePassed = "PASS"
lineSkipped = "SKIP"
lineFail = "FAIL"
)
var (
lines [][]byte
unparsableLines []string
table *termtables.Table
bench *benchmark
timing string
)
func init() {
setTiming()
}
// Main is the entry point to parse benchmarks
// not intended for use in libraries, but has to be exported to ensure the tool can be called via 'pb'
func Main() {
reader := bufio.NewReader(os.Stdin)
quit := make(chan bool)
go loading(quit)
for {
text, err := reader.ReadBytes('\n')
if err != nil {
if err != io.EOF {
panic(err)
}
break
}
lines = append(lines, text)
}
close(quit)
if len(lines) == 0 {
return
}
bench = newBenchmark(lines)
table = termtables.CreateTable()
table.Style.Alignment = termtables.AlignRight
addTableHeader(table)
addTableBody(table)
fmt.Print("\r \n")
fmt.Println(table.Render())
fmt.Println(footer())
}
func newBenchmark(l [][]byte) *benchmark {
results := newResults(l)
return &benchmark{
info: newBenchmarkInfo(results),
results: results,
}
}
func newResults(l [][]byte) *results {
benchMap := make(results)
for _, l := range l {
bl, err := newResult(l)
if err != nil {
unparsableLines = append(unparsableLines, err.Error())
continue
}
if _, ok := benchMap[bl.Name]; !ok {
benchMap[bl.Name] = make([]*result, 0)
}
benchMap[bl.Name] = append(benchMap[bl.Name], bl)
}
for _, r := range benchMap {
sort.Sort(sortByFnIterations(r))
}
return &benchMap
}
func newResult(b []byte) (*result, error) {
var (
name string
fnIter int
bps int
aps int
err error
iter int
speed float64
)
s := string(b)
parts := regExByWhitespace.Split(s, -1)
if len(parts) < 4 || !regExIsBenchmark.MatchString(parts[0]) {
return nil, fmt.Errorf("%s", s)
}
nameRuns := regExByRuns.ReplaceAllString(parts[0], "")
nameIterations := regExByIterations.ReplaceAllString(nameRuns, "")
lastIndex := strings.LastIndex(nameIterations, "_")
if lastIndex > -1 {
name = nameIterations[:lastIndex]
fnIter, _ = strconv.Atoi(nameIterations[lastIndex+1:])
} else {
name = nameIterations
fnIter = -1
}
iter, err = strconv.Atoi(parts[1])
if err != nil {
iter = -1
}
speed, err = strconv.ParseFloat(parts[2], 64)
if err != nil {
speed = -1
}
if len(parts) > 5 {
bps, err = strconv.Atoi(parts[4])
if err != nil {
bps = -1
}
aps, err = strconv.Atoi(parts[6])
if err != nil {
aps = -1
}
} else {
// without benchmem
bps = -1
aps = -1
}
return &result{
Name: name,
FnIterations: fnIter,
Runs: iter,
Speed: speed,
Bps: bps,
Aps: aps,
}, nil
}
func newBenchmarkInfo(r *results) *benchmarkInfo {
var (
hasFnIter bool
benchmemUsed bool
wg sync.WaitGroup
)
wg.Add(3)
go func(r *results) {
timing = getSuggestedTiming(r)
wg.Done()
}(r)
go func(r *results) {
hasFnIter = hasFnIterations(r)
wg.Done()
}(r)
go func(r *results) {
benchmemUsed = isBenchmem(r)
wg.Done()
}(r)
wg.Wait()
switch timing {
case "ns":
// ns is default, dont't do anything
case "µs":
updateSpeedVals(r, float64(1e3))
case "ms":
updateSpeedVals(r, float64(1e6))
case "s":
updateSpeedVals(r, float64(1e9))
}
return &benchmarkInfo{hasFnIter, benchmemUsed, timing}
}
func getSuggestedTiming(r *results) string {
var (
slowest float64
suggestedTiming string
)
for _, bl := range *r {
for _, l := range bl {
if slowest < l.Speed {
slowest = l.Speed
}
}
}
if timing == "" {
switch {
case slowest <= 1e3:
suggestedTiming = "ns"
case slowest > 1e3 && slowest <= 1e6:
suggestedTiming = "µs"
case slowest > 1e6 && slowest <= 1e9:
suggestedTiming = "ms"
case slowest > 1e9:
suggestedTiming = "s"
}
} else {
suggestedTiming = timing
}
return suggestedTiming
}
func isBenchmem(r *results) bool {
var benchmemUsed bool
for _, bl := range *r {
for _, l := range bl {
if l.Aps > -1 && l.Bps > -1 {
benchmemUsed = true
}
}
}
return benchmemUsed
}
func hasFnIterations(r *results) bool {
var hasFnIterations bool
for _, bl := range *r {
for _, l := range bl {
if l.FnIterations > -1 {
hasFnIterations = true
}
}
}
return hasFnIterations
}
func updateSpeedVals(r *results, f float64) {
for _, bl := range *r {
for _, l := range bl {
l.Speed = l.Speed / f
}
}
}
func footer() string {
var footer []byte
footer = append(footer, []byte{10}...)
footer = append(footer, []byte((bold("Summary:"))+"\n")...)
footer = append(footer, []byte((bold("+------+"))+"\n")...)
for _, line := range unparsableLines {
tmp := strings.TrimSpace(line)
switch {
case tmp == linePassed:
footer = append(footer, []byte(green(bold(tmp))+"\n")...)
case tmp == lineSkipped:
footer = append(footer, []byte(gray(bold(tmp))+"\n")...)
case tmp == lineFail:
footer = append(footer, []byte(red(bold(tmp))+"\n")...)
default:
footer = append(footer, []byte(tmp+"\n")...)
}
}
return string(footer)
}
func addTableHeader(t *termtables.Table) {
var lenLongestName int
for name := range *bench.results {
if tmpLen := len(name); tmpLen > lenLongestName {
lenLongestName = tmpLen
}
}
// add padding to first col since alignment in header columns does not work
// padding of longest name + len("name") + 1 padding right
nameCol := make([]byte, 0, lenLongestName+4+1)
nameCol = append(nameCol, []byte("Name")...)
for i := 0; i < lenLongestName; i++ {
nameCol = append(nameCol, byte(32))
}
if bench.info.benchmemUsed {
if bench.info.hasFnIterations {
t.AddHeaders(bold(string(nameCol)), bold("Iterations"), bold("Runs"), bold(bench.info.suggestedTiming+"/op"), bold("B/op"), bold("allocations/op"))
} else {
t.AddHeaders(bold(string(nameCol)), bold("Runs"), bold(bench.info.suggestedTiming+"/op"), bold("B/op"), bold("allocations/op"))
}
} else {
if bench.info.hasFnIterations {
t.AddHeaders(bold(string(nameCol)), bold("Iterations"), bold("Runs"), bold(bench.info.suggestedTiming+"/op"))
} else {
t.AddHeaders(bold(string(nameCol)), bold("Runs"), bold(bench.info.suggestedTiming+"/op"))
}
}
}
func addTableBody(t *termtables.Table) {
floatFmt := fmtFloat
if bench.info.suggestedTiming == "ns" {
floatFmt = fmtFloatNS
}
i := len(*bench.results)
sorted := make([]string, 0, i)
for name := range *bench.results {
sorted = append(sorted, name)
}
sort.Sort(sort.StringSlice(sorted))
for _, benchName := range sorted {
results := (*bench.results)[benchName]
for j, b := range results {
var name string
if j == 0 {
name = bold(b.Name)
}
if bench.info.benchmemUsed {
if bench.info.hasFnIterations {
fnIterations := strconv.Itoa(b.FnIterations)
if fnIterations == "-1" {
fnIterations = ""
} else {
i, err := strconv.Atoi(fnIterations)
if err != nil {
fnIterations = ""
} else {
fnIterations = RenderInteger(fmtInt, i)
}
}
t.AddRow(name, fnIterations, RenderInteger(fmtInt, b.Runs), RenderFloat(floatFmt, b.Speed), RenderInteger(fmtInt, b.Bps), RenderInteger(fmtInt, b.Aps))
} else {
t.AddRow(name, RenderInteger(fmtInt, b.Runs), RenderFloat(floatFmt, b.Speed), RenderInteger(fmtInt, b.Bps), RenderInteger(fmtInt, b.Aps))
}
} else {
if bench.info.hasFnIterations {
fnIterations := strconv.Itoa(b.FnIterations)
if fnIterations == "-1" {
fnIterations = ""
} else {
i, err := strconv.Atoi(fnIterations)
if err != nil {
fnIterations = ""
} else {
fnIterations = RenderInteger(fmtInt, i)
}
}
t.AddRow(name, fnIterations, RenderInteger(fmtInt, b.Runs), RenderFloat(floatFmt, b.Speed))
} else {
t.AddRow(name, RenderInteger(fmtInt, b.Runs), RenderFloat(floatFmt, b.Speed))
}
}
}
i--
if i > 0 {
t.AddSeparator()
}
}
t.SetAlign(termtables.AlignLeft, 1)
}
//StringsContains checks if a string slice contains search element
func StringsContains(elements []string, needle string) bool {
for _, i := range elements {
if i == needle {
return true
}
}
return false
}
func setTiming() {
flag.Parse()
args := flag.Args()
if len(args) > 0 {
if lowerArg := strings.ToLower(args[0]); StringsContains([]string{"ns", "us", "µs", "ms", "s"}, lowerArg) {
if lowerArg == "us" {
lowerArg = "µs"
}
timing = lowerArg
}
}
}
func loading(q chan bool) {
states := []string{"|", "/", "-", "\\", "|", "/", "–", "\\"}
current := 0
for {
select {
case <-time.Tick(150 * time.Millisecond):
fmt.Printf("\r%s", states[current])
if current == len(states)-1 {
current = 0
} else {
current++
}
case <-q:
break
}
}
}
func bold(s string) string {
return fmt.Sprintf("\033[1m%s\033[0m", s)
}
func green(s string) string {
return fmt.Sprintf("\033[32m%s\033[0m", s)
}
func red(s string) string {
return fmt.Sprintf("\033[31m%s\033[0m", s)
}
func gray(s string) string {
return fmt.Sprintf("\033[90m%s\033[0m", s)
}