-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
143 lines (124 loc) · 3.22 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
// Copyright 2020 The Scriggo Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main
import (
"encoding/json"
"flag"
"fmt"
"io"
"os"
"regexp"
"strings"
"github.com/cep21/benchparse"
)
func main() {
var p, i string
var h bool
flag.StringVar(&p, "p", "", "filter the benchmarks by program name")
flag.StringVar(&i, "i", "", "filter the benchmarks by interpreter, "+
"accepted values are Scriggo,Yaegi, Tengo, GoLua, GopherLua.\n"+
"\t\texample: -i=Scriggo,Yaegi")
flag.BoolVar(&h, "h", false, "print this help")
flag.Usage = func() {
fmt.Fprintf(os.Stderr, "bench2json returns a json encoded representation of the benchmark data passed by command line\n"+
"Benhmark names are expected to respect the following format:\n"+
"Benchmark[INTERPRETER NAME]/[PROGRAM NAME].[FILE EXTENSION]-[PROC NUM]\n"+
"the benchmarks are expected to be run with the -test.benchmem option\n")
flag.VisitAll(func(f *flag.Flag) {
fmt.Fprintf(os.Stderr, " -%s %s\n", f.Name, f.Usage)
})
fmt.Fprintf(os.Stderr, "\nUsage example: $ go test -bench=. -test.benchmem | bench2json\n")
}
flag.Parse()
if h {
flag.Usage()
os.Exit(0)
}
var interpreters []string
if i != "" {
interpreters = strings.Split(i, ",")
for i, interp := range interpreters {
interpreters[i] = strings.ToLower(interp)
switch interpreters[i] {
case "golua":
interpreters[i] = "go-lua"
case "gopherlua":
interpreters[i] = "gopher-lua"
}
}
}
res := encodeBenchmarkData(os.Stdin, options{
Interpreters: interpreters,
Program: p,
})
fmt.Println(res)
}
type options struct {
Interpreters []string
Program string
}
var benchmarkProgram = regexp.MustCompile(`Benchmark(.+)\/(.+)\..+-\d+`)
type Benchmark struct {
Program string
Results map[string]BenchmarkResult
}
type BenchmarkResult struct {
Time float64
Allocs float64
}
const TimeColumn = 0
const AllocsColumn = 2
func encodeBenchmarkData(reader io.Reader, o options) string {
d := benchparse.Decoder{}
run, err := d.Decode(reader)
if err != nil {
panic(err)
}
benchmarkOf := map[string]Benchmark{}
for _, result := range run.Results {
res := benchmarkProgram.FindStringSubmatch(result.Name)
interpreter := strings.ToLower(res[1])
switch interpreter {
case "golua":
interpreter = "go-lua"
case "gopherlua":
interpreter = "gopher-lua"
}
program := res[2]
if o.Program != "" && o.Program != program {
continue
}
if len(o.Interpreters) > 0 {
var inList bool
for _, i := range o.Interpreters {
if i == interpreter {
inList = true
break
}
}
if !inList {
continue
}
}
if _, ok := benchmarkOf[program]; !ok {
benchmarkOf[program] = Benchmark{
Program: program,
Results: map[string]BenchmarkResult{},
}
}
benchmarkOf[program].Results[interpreter] = BenchmarkResult{
Time: result.Values[TimeColumn].Value,
Allocs: result.Values[AllocsColumn].Value,
}
}
benchmarks := make([]Benchmark, 0, len(benchmarkOf))
for _, b := range benchmarkOf {
benchmarks = append(benchmarks, b)
}
encoded, err := json.MarshalIndent(benchmarks, "", " ")
if err != nil {
panic(err)
}
return string(encoded)
}