Commit c1e5ad7a authored by Russ Cox's avatar Russ Cox

benchstat: move bulk of logic into library

Change-Id: I90c73aa77b97b4921e839b376616646604e85a09
Reviewed-on: https://go-review.googlesource.com/35942Reviewed-by: default avatarQuentin Smith <quentin@golang.org>
parent 9c653598
......@@ -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 @@ type Collection struct {
// 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
......
......@@ -4,7 +4,7 @@
// Significance tests.
package main
package benchstat
import (
"errors"
......
......@@ -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"
......
......@@ -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"
......
......@@ -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 @@ type Row struct {
}
// 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 @@ func (c *Collection) Tables(deltaTest DeltaTest) []*Table {
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 @@ func (c *Collection) Tables(deltaTest DeltaTest) []*Table {
}
if len(table.Rows) > 0 {
if *flagGeomean {
if c.AddGeoMean {
addGeomean(c, table, key.Unit, table.OldNewDelta)
}
tables = append(tables, table)
......
......@@ -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"
......
......@@ -98,6 +98,8 @@ import (
"log"
"os"
"strings"
"golang.org/x/perf/benchstat"
)
func usage() {
......@@ -114,14 +116,14 @@ var (
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 @@ func main() {
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 @@ func main() {
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())
}
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment