forked from codingberg/benchgraph
-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.go
174 lines (137 loc) · 3.65 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
package main
import (
"bufio"
"errors"
"flag"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"os"
"text/template"
"github.com/fatih/color"
"golang.org/x/tools/benchmark/parse"
)
// uploadData sends data to server and expects graph url.
func uploadData(apiUrl, data, title string) (string, error) {
resp, err := http.PostForm(apiUrl, url.Values{"data": {data}, "title": {title}})
if err != nil {
return "", err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return "", err
}
if resp.StatusCode != 200 {
return "", errors.New("Server din't return graph URL")
}
return string(body), nil
}
func writeLocallyData(data, title string) (string, error) {
t := template.New("Benchgraph Template")
t, err := t.Parse(templateHTML)
if err != nil {
return "", err
}
tmpfile, err := ioutil.TempFile("", "benchgraph")
if err != nil {
return "", err
}
// allow world-readable (mode 0644)
tmpfile.Chmod(0644)
model := struct {
Title, Data string
}{
title,
data,
}
err = t.Execute(tmpfile, model)
if err != nil {
return "", err
}
tmpfile.Close()
newName := fmt.Sprintf("%s.html", tmpfile.Name())
err = os.Rename(tmpfile.Name(), newName)
if err != nil {
newName = tmpfile.Name()
}
return newName, nil
}
func main() {
var oBenchNames, oBenchArgs stringList
// graph elements will be ordered as in benchmark output by default - unless the order was specified here
flag.Var(&oBenchNames, "obn", "comma-separated list of benchmark names")
flag.Var(&oBenchArgs, "oba", "comma-separated list of benchmark arguments")
title := flag.String("title", "Graph: Benchmark results in ns/op", "title of a graph")
apiUrl := flag.String("apiurl", "http://benchgraph.codingberg.com", "url to server api")
publish := flag.Bool("publish", false, "publish the response publicly")
flag.Parse()
var skipBenchNamesParsing, skipBenchArgsParsing bool
if oBenchNames.Len() > 0 {
skipBenchNamesParsing = true
}
if oBenchArgs.Len() > 0 {
skipBenchArgsParsing = true
}
benchResults := make(BenchNameSet)
// parse Golang benchmark results, line by line
scan := bufio.NewScanner(os.Stdin)
green := color.New(color.FgGreen).SprintfFunc()
red := color.New(color.FgRed).SprintFunc()
for scan.Scan() {
line := scan.Text()
mark := green("√")
b, err := parse.ParseLine(line)
if err != nil {
mark = red("?")
}
// read bench name and arguments
if b != nil {
name, arg, _, err := parseNameArgThread(b.Name)
if err != nil {
mark = red("!")
fmt.Printf("%s %s\n", mark, line)
continue
}
if !skipBenchNamesParsing && !oBenchNames.stringInList(name) {
oBenchNames.Add(name)
}
if !skipBenchArgsParsing && !oBenchArgs.stringInList(arg) {
oBenchArgs.Add(arg)
}
if _, ok := benchResults[name]; !ok {
benchResults[name] = make(BenchArgSet)
}
benchResults[name][arg] = b.NsPerOp
}
fmt.Printf("%s %s\n", mark, line)
}
if err := scan.Err(); err != nil {
fmt.Fprintf(os.Stderr, "reading standard input: %v", err)
os.Exit(1)
}
if len(benchResults) == 0 {
fmt.Fprintf(os.Stderr, "no data to show.\n\n")
os.Exit(1)
}
fmt.Println()
data := graphData(benchResults, oBenchNames, oBenchArgs)
var graphUrl string
var err error
if *publish {
fmt.Println("Waiting for server response ...")
graphUrl, err = uploadData(*apiUrl, string(data), *title)
} else {
graphUrl, err = writeLocallyData(string(data), *title)
}
if err != nil {
fmt.Fprintf(os.Stderr, "uploading data: %v", err)
os.Exit(1)
}
fmt.Println("=========================================")
fmt.Println()
fmt.Println(graphUrl)
fmt.Println()
fmt.Println("=========================================")
}