forked from ChrisGora/benchgraph
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
128 lines (99 loc) · 2.82 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
package main
import (
"bufio"
"errors"
"flag"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"os"
"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 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")
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()
fmt.Println("Waiting for server response ...")
data := graphData(benchResults, oBenchNames, oBenchArgs)
graphUrl, err := uploadData(*apiUrl, 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("=========================================")
}