Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generate local graph #2

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ benchgraph -help
Usage of benchgraph:
-apiurl string
url to server api (default "http://benchgraph.codingberg.com")
-local
generates the response locally
-oba value
comma-separated list of benchmark arguments (default [])
-obn value
Expand All @@ -85,6 +87,17 @@ cat out|benchgraph -title="Graph1: Benchmark F(x) in ns/op" -obn="F2,F3,F4" -oba
cat out|benchgraph -title="Graph2: Benchmark F(x) in ns/op" -obn="F2,F3,F4" -oba="0F,F0,F00,F000,F0000,F00000,F000000,F0000000"
```

To have all in local, you can also use the **-local** option :

```bash
go test -bench . > out

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
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
```

It will generates on your temp folder, a local html file.

## Online Demo

Here we analyze efficiency of different algorithms for computing parity of uint64 numbers:
Expand Down
48 changes: 46 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"net/http"
"net/url"
"os"
"text/template"

"github.com/fatih/color"
"golang.org/x/tools/benchmark/parse"
Expand All @@ -35,6 +36,41 @@ func uploadData(apiUrl, data, title string) (string, error) {
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
}

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
Expand All @@ -44,6 +80,7 @@ func main() {
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")
islocal := flag.Bool("local", false, "generates the response locally")
flag.Parse()

var skipBenchNamesParsing, skipBenchArgsParsing bool
Expand Down Expand Up @@ -109,11 +146,18 @@ func main() {
}

fmt.Println()
fmt.Println("Waiting for server response ...")

data := graphData(benchResults, oBenchNames, oBenchArgs)

graphUrl, err := uploadData(*apiUrl, string(data), *title)
var graphUrl string
var err error
if !*islocal {
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)
Expand Down
58 changes: 58 additions & 0 deletions template.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package main

var templateHTML = `<html>

<head>
<meta charset="UTF-8">
<meta name="robots" content="noindex,nofollow,noarchive,nosnippet,noodp,noydir">
<title>{{.Title}}</title>
<link href="https://ajax.googleapis.com/ajax/static/modules/gviz/1.0/core/tooltip.css" rel="stylesheet" type="text/css">
</head>

<body>

<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script type="text/javascript">
$(window).resize(function() {
drawChart2o();
});

// Load the Visualization API and the chart package.
google.load('visualization', '1.0', {
'packages': ['corechart']
});

// Set a callback to run when the Google Visualization API is loaded.
google.setOnLoadCallback(drawChart2o);

// Callback that creates and populates a data table,
// instantiates the chart, passes in the data and
// draws it.
function drawChart2o() {
// Create the data table.
var data = google.visualization.arrayToDataTable({{.Data}});

// Set chart options
var options = {
title: '{{.Title}}',
curveType: 'function',
legend: {
position: 'bottom'
}
};

// Instantiate and draw our chart, passing in some options.
var chart = new google.visualization.LineChart(document.getElementById('chart_div_2o'));
chart.draw(data, options);
}
</script>
<script src="https://www.google.com/uds/?file=visualization&amp;v=1.0&amp;packages=corechart" type="text/javascript"></script>
<link href="https://www.google.com/uds/api/visualization/1.0/40ff64b1d9d6b3213524485974f36cc0/ui+en.css" type="text/css" rel="stylesheet">
<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>

<div id="chart_div_2o" style="height:600px;width:100%;"></div>

</body>

</html>`