benchstat: move bulk of logic into library

Change-Id: I90c73aa77b97b4921e839b376616646604e85a09
Reviewed-on: https://go-review.googlesource.com/35942
Reviewed-by: Quentin Smith <quentin@golang.org>
diff --git a/cmd/benchstat/data.go b/benchstat/data.go
similarity index 90%
rename from cmd/benchstat/data.go
rename to benchstat/data.go
index 998dd1e..0284216 100644
--- a/cmd/benchstat/data.go
+++ b/benchstat/data.go
@@ -2,7 +2,7 @@
 // Use of this source code is governed by a BSD-style
 // license that can be found in the LICENSE file.
 
-package main
+package benchstat
 
 import (
 	"fmt"
@@ -21,6 +21,18 @@
 
 	// Metrics holds the accumulated metrics for each key.
 	Metrics map[Key]*Metrics
+
+	// DeltaTest is the test to use to decide if a change is significant.
+	// If nil, it defaults to UTest.
+	DeltaTest DeltaTest
+
+	// Alpha is the p-value cutoff to report a change as significant.
+	// If zero, it defaults to 0.05.
+	Alpha float64
+
+	// AddGeoMean specifies whether to add a line to the table
+	// showing the geometric mean of all the benchmark results.
+	AddGeoMean bool
 }
 
 // A Key identifies one metric (e.g., "ns/op", "B/op") from one
diff --git a/cmd/benchstat/delta.go b/benchstat/delta.go
similarity index 98%
rename from cmd/benchstat/delta.go
rename to benchstat/delta.go
index dc91c8f..2a28653 100644
--- a/cmd/benchstat/delta.go
+++ b/benchstat/delta.go
@@ -4,7 +4,7 @@
 
 // Significance tests.
 
-package main
+package benchstat
 
 import (
 	"errors"
diff --git a/cmd/benchstat/html.go b/benchstat/html.go
similarity index 98%
rename from cmd/benchstat/html.go
rename to benchstat/html.go
index 6961d84..5f03d60 100644
--- a/cmd/benchstat/html.go
+++ b/benchstat/html.go
@@ -2,7 +2,7 @@
 // Use of this source code is governed by a BSD-style
 // license that can be found in the LICENSE file.
 
-package main
+package benchstat
 
 import (
 	"bytes"
diff --git a/cmd/benchstat/scaler.go b/benchstat/scaler.go
similarity index 99%
rename from cmd/benchstat/scaler.go
rename to benchstat/scaler.go
index 3b24858..c775caa 100644
--- a/cmd/benchstat/scaler.go
+++ b/benchstat/scaler.go
@@ -2,7 +2,7 @@
 // Use of this source code is governed by a BSD-style
 // license that can be found in the LICENSE file.
 
-package main
+package benchstat
 
 import "fmt"
 
diff --git a/cmd/benchstat/table.go b/benchstat/table.go
similarity index 94%
rename from cmd/benchstat/table.go
rename to benchstat/table.go
index 0d664ba..e5249c1 100644
--- a/cmd/benchstat/table.go
+++ b/benchstat/table.go
@@ -2,7 +2,7 @@
 // Use of this source code is governed by a BSD-style
 // license that can be found in the LICENSE file.
 
-package main
+package benchstat
 
 import (
 	"fmt"
@@ -29,7 +29,16 @@
 }
 
 // Tables returns tables comparing the benchmarks in the collection.
-func (c *Collection) Tables(deltaTest DeltaTest) []*Table {
+func (c *Collection) Tables() []*Table {
+	deltaTest := c.DeltaTest
+	if deltaTest == nil {
+		deltaTest = UTest
+	}
+	alpha := c.Alpha
+	if alpha == 0 {
+		alpha = 0.05
+	}
+
 	// Update statistics.
 	for _, m := range c.Metrics {
 		m.computeStats()
@@ -80,7 +89,7 @@
 					row.Note = "(all equal)"
 				} else if testerr != nil {
 					row.Note = fmt.Sprintf("(%s)", testerr)
-				} else if pval < *flagAlpha {
+				} else if pval < alpha {
 					pct := ((new.Mean / old.Mean) - 1.0) * 100.0
 					row.Delta = fmt.Sprintf("%+.2f%%", pct)
 					if pct < 0 == (table.Metric != "speed") { // smaller is better, except speeds
@@ -98,7 +107,7 @@
 		}
 
 		if len(table.Rows) > 0 {
-			if *flagGeomean {
+			if c.AddGeoMean {
 				addGeomean(c, table, key.Unit, table.OldNewDelta)
 			}
 			tables = append(tables, table)
diff --git a/cmd/benchstat/text.go b/benchstat/text.go
similarity index 99%
rename from cmd/benchstat/text.go
rename to benchstat/text.go
index 2654344..36d2d50 100644
--- a/cmd/benchstat/text.go
+++ b/benchstat/text.go
@@ -2,7 +2,7 @@
 // Use of this source code is governed by a BSD-style
 // license that can be found in the LICENSE file.
 
-package main
+package benchstat
 
 import (
 	"bytes"
diff --git a/cmd/benchstat/main.go b/cmd/benchstat/main.go
index 1182884..570b186 100644
--- a/cmd/benchstat/main.go
+++ b/cmd/benchstat/main.go
@@ -98,6 +98,8 @@
 	"log"
 	"os"
 	"strings"
+
+	"golang.org/x/perf/benchstat"
 )
 
 func usage() {
@@ -114,14 +116,14 @@
 	flagHTML      = flag.Bool("html", false, "print results as an HTML table")
 )
 
-var deltaTestNames = map[string]DeltaTest{
-	"none":   NoDeltaTest,
-	"u":      UTest,
-	"u-test": UTest,
-	"utest":  UTest,
-	"t":      TTest,
-	"t-test": TTest,
-	"ttest":  TTest,
+var deltaTestNames = map[string]benchstat.DeltaTest{
+	"none":   benchstat.NoDeltaTest,
+	"u":      benchstat.UTest,
+	"u-test": benchstat.UTest,
+	"utest":  benchstat.UTest,
+	"t":      benchstat.TTest,
+	"t-test": benchstat.TTest,
+	"ttest":  benchstat.TTest,
 }
 
 func main() {
@@ -134,7 +136,11 @@
 		flag.Usage()
 	}
 
-	c := new(Collection)
+	c := &benchstat.Collection{
+		Alpha:      *flagAlpha,
+		AddGeoMean: *flagGeomean,
+		DeltaTest:  deltaTest,
+	}
 	for _, file := range flag.Args() {
 		data, err := ioutil.ReadFile(file)
 		if err != nil {
@@ -143,14 +149,14 @@
 		c.AddConfig(file, data)
 	}
 
-	tables := c.Tables(deltaTest)
+	tables := c.Tables()
 
 	var buf bytes.Buffer
 	if *flagHTML {
 		buf.WriteString(htmlStyle)
-		FormatHTML(&buf, tables)
+		benchstat.FormatHTML(&buf, tables)
 	} else {
-		FormatText(&buf, tables)
+		benchstat.FormatText(&buf, tables)
 	}
 	os.Stdout.Write(buf.Bytes())
 }