Skip to content

Commit 4bebdbb

Browse files
committed
Generate locally graph
* Add -local flag (optional) * generate thanks to template an html file (on function writeLocallyData) * Change README.md
1 parent f9c026b commit 4bebdbb

File tree

3 files changed

+117
-2
lines changed

3 files changed

+117
-2
lines changed

README.md

+13
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ benchgraph -help
6060
Usage of benchgraph:
6161
-apiurl string
6262
url to server api (default "http://benchgraph.codingberg.com")
63+
-local
64+
generates the response locally
6365
-oba value
6466
comma-separated list of benchmark arguments (default [])
6567
-obn value
@@ -85,6 +87,17 @@ cat out|benchgraph -title="Graph1: Benchmark F(x) in ns/op" -obn="F2,F3,F4" -oba
8587
cat out|benchgraph -title="Graph2: Benchmark F(x) in ns/op" -obn="F2,F3,F4" -oba="0F,F0,F00,F000,F0000,F00000,F000000,F0000000"
8688
```
8789

90+
To have all in local, you can also use the **-local** option :
91+
92+
```bash
93+
go test -bench . > out
94+
95+
cat out|benchgraph -title="Graph1: Benchmark F(x) in ns/op" -obn="F2,F3,F4" -oba="F,FF,FFF,FFFF,FFFFF,FFFFFF,FFFFFFF,FFFFFFFF" -local
96+
cat out|benchgraph -title="Graph2: Benchmark F(x) in ns/op" -obn="F2,F3,F4" -oba="0F,F0,F00,F000,F0000,F00000,F000000,F0000000" -local
97+
```
98+
99+
It will generates on your temp folder, a local html file.
100+
88101
## Online Demo
89102

90103
Here we analyze efficiency of different algorithms for computing parity of uint64 numbers:

main.go

+46-2
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"net/http"
1010
"net/url"
1111
"os"
12+
"text/template"
1213

1314
"github.com/fatih/color"
1415
"golang.org/x/tools/benchmark/parse"
@@ -35,6 +36,41 @@ func uploadData(apiUrl, data, title string) (string, error) {
3536
return string(body), nil
3637
}
3738

39+
func writeLocallyData(data, title string) (string, error) {
40+
t := template.New("Benchgraph Template")
41+
t, err := t.Parse(templateHTML)
42+
if err != nil {
43+
return "", err
44+
}
45+
46+
tmpfile, err := ioutil.TempFile("", "benchgraph")
47+
if err != nil {
48+
return "", err
49+
}
50+
51+
model := struct {
52+
Title, Data string
53+
}{
54+
title,
55+
data,
56+
}
57+
58+
err = t.Execute(tmpfile, model)
59+
if err != nil {
60+
return "", err
61+
}
62+
63+
tmpfile.Close()
64+
newName := fmt.Sprintf("%s.html", tmpfile.Name())
65+
err = os.Rename(tmpfile.Name(), newName)
66+
if err != nil {
67+
newName = tmpfile.Name()
68+
}
69+
70+
return newName, nil
71+
72+
}
73+
3874
func main() {
3975

4076
var oBenchNames, oBenchArgs stringList
@@ -44,6 +80,7 @@ func main() {
4480
flag.Var(&oBenchArgs, "oba", "comma-separated list of benchmark arguments")
4581
title := flag.String("title", "Graph: Benchmark results in ns/op", "title of a graph")
4682
apiUrl := flag.String("apiurl", "http://benchgraph.codingberg.com", "url to server api")
83+
islocal := flag.Bool("local", false, "generates the response locally")
4784
flag.Parse()
4885

4986
var skipBenchNamesParsing, skipBenchArgsParsing bool
@@ -109,11 +146,18 @@ func main() {
109146
}
110147

111148
fmt.Println()
112-
fmt.Println("Waiting for server response ...")
113149

114150
data := graphData(benchResults, oBenchNames, oBenchArgs)
115151

116-
graphUrl, err := uploadData(*apiUrl, string(data), *title)
152+
var graphUrl string
153+
var err error
154+
if !*islocal {
155+
fmt.Println("Waiting for server response ...")
156+
graphUrl, err = uploadData(*apiUrl, string(data), *title)
157+
} else {
158+
graphUrl, err = writeLocallyData(string(data), *title)
159+
}
160+
117161
if err != nil {
118162
fmt.Fprintf(os.Stderr, "uploading data: %v", err)
119163
os.Exit(1)

template.go

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package main
2+
3+
var templateHTML = `<html>
4+
5+
<head>
6+
<meta charset="UTF-8">
7+
<meta name="robots" content="noindex,nofollow,noarchive,nosnippet,noodp,noydir">
8+
<title>{{.Title}}</title>
9+
<link href="https://ajax.googleapis.com/ajax/static/modules/gviz/1.0/core/tooltip.css" rel="stylesheet" type="text/css">
10+
</head>
11+
12+
<body>
13+
14+
<script type="text/javascript" src="https://www.google.com/jsapi"></script>
15+
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
16+
<script type="text/javascript">
17+
$(window).resize(function() {
18+
drawChart2o();
19+
});
20+
21+
// Load the Visualization API and the chart package.
22+
google.load('visualization', '1.0', {
23+
'packages': ['corechart']
24+
});
25+
26+
// Set a callback to run when the Google Visualization API is loaded.
27+
google.setOnLoadCallback(drawChart2o);
28+
29+
// Callback that creates and populates a data table,
30+
// instantiates the chart, passes in the data and
31+
// draws it.
32+
function drawChart2o() {
33+
// Create the data table.
34+
var data = google.visualization.arrayToDataTable({{.Data}});
35+
36+
// Set chart options
37+
var options = {
38+
title: '{{.Title}}',
39+
curveType: 'function',
40+
legend: {
41+
position: 'bottom'
42+
}
43+
};
44+
45+
// Instantiate and draw our chart, passing in some options.
46+
var chart = new google.visualization.LineChart(document.getElementById('chart_div_2o'));
47+
chart.draw(data, options);
48+
}
49+
</script>
50+
<script src="https://www.google.com/uds/?file=visualization&amp;v=1.0&amp;packages=corechart" type="text/javascript"></script>
51+
<link href="https://www.google.com/uds/api/visualization/1.0/40ff64b1d9d6b3213524485974f36cc0/ui+en.css" type="text/css" rel="stylesheet">
52+
<script src="https://www.google.com/uds/api/visualization/1.0/40ff64b1d9d6b3213524485974f36cc0/format+en,default+en,ui+en,corechart+en.I.js" type="text/javascript"></script>
53+
54+
<div id="chart_div_2o" style="height:600px;width:100%;"></div>
55+
56+
</body>
57+
58+
</html>`

0 commit comments

Comments
 (0)