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 @@ ...@@ -2,7 +2,7 @@
// Use of this source code is governed by a BSD-style // Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file. // license that can be found in the LICENSE file.
package main package benchstat
import ( import (
"fmt" "fmt"
...@@ -21,6 +21,18 @@ type Collection struct { ...@@ -21,6 +21,18 @@ type Collection struct {
// Metrics holds the accumulated metrics for each key. // Metrics holds the accumulated metrics for each key.
Metrics map[Key]*Metrics 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 // A Key identifies one metric (e.g., "ns/op", "B/op") from one
......
...@@ -4,7 +4,7 @@ ...@@ -4,7 +4,7 @@
// Significance tests. // Significance tests.
package main package benchstat
import ( import (
"errors" "errors"
......
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
// Use of this source code is governed by a BSD-style // Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file. // license that can be found in the LICENSE file.
package main package benchstat
import ( import (
"bytes" "bytes"
......
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
// Use of this source code is governed by a BSD-style // Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file. // license that can be found in the LICENSE file.
package main package benchstat
import "fmt" import "fmt"
......
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
// Use of this source code is governed by a BSD-style // Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file. // license that can be found in the LICENSE file.
package main package benchstat
import ( import (
"fmt" "fmt"
...@@ -29,7 +29,16 @@ type Row struct { ...@@ -29,7 +29,16 @@ type Row struct {
} }
// Tables returns tables comparing the benchmarks in the collection. // 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. // Update statistics.
for _, m := range c.Metrics { for _, m := range c.Metrics {
m.computeStats() m.computeStats()
...@@ -80,7 +89,7 @@ func (c *Collection) Tables(deltaTest DeltaTest) []*Table { ...@@ -80,7 +89,7 @@ func (c *Collection) Tables(deltaTest DeltaTest) []*Table {
row.Note = "(all equal)" row.Note = "(all equal)"
} else if testerr != nil { } else if testerr != nil {
row.Note = fmt.Sprintf("(%s)", testerr) row.Note = fmt.Sprintf("(%s)", testerr)
} else if pval < *flagAlpha { } else if pval < alpha {
pct := ((new.Mean / old.Mean) - 1.0) * 100.0 pct := ((new.Mean / old.Mean) - 1.0) * 100.0
row.Delta = fmt.Sprintf("%+.2f%%", pct) row.Delta = fmt.Sprintf("%+.2f%%", pct)
if pct < 0 == (table.Metric != "speed") { // smaller is better, except speeds if pct < 0 == (table.Metric != "speed") { // smaller is better, except speeds
...@@ -98,7 +107,7 @@ func (c *Collection) Tables(deltaTest DeltaTest) []*Table { ...@@ -98,7 +107,7 @@ func (c *Collection) Tables(deltaTest DeltaTest) []*Table {
} }
if len(table.Rows) > 0 { if len(table.Rows) > 0 {
if *flagGeomean { if c.AddGeoMean {
addGeomean(c, table, key.Unit, table.OldNewDelta) addGeomean(c, table, key.Unit, table.OldNewDelta)
} }
tables = append(tables, table) tables = append(tables, table)
......
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
// Use of this source code is governed by a BSD-style // Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file. // license that can be found in the LICENSE file.
package main package benchstat
import ( import (
"bytes" "bytes"
......
...@@ -98,6 +98,8 @@ import ( ...@@ -98,6 +98,8 @@ import (
"log" "log"
"os" "os"
"strings" "strings"
"golang.org/x/perf/benchstat"
) )
func usage() { func usage() {
...@@ -114,14 +116,14 @@ var ( ...@@ -114,14 +116,14 @@ var (
flagHTML = flag.Bool("html", false, "print results as an HTML table") flagHTML = flag.Bool("html", false, "print results as an HTML table")
) )
var deltaTestNames = map[string]DeltaTest{ var deltaTestNames = map[string]benchstat.DeltaTest{
"none": NoDeltaTest, "none": benchstat.NoDeltaTest,
"u": UTest, "u": benchstat.UTest,
"u-test": UTest, "u-test": benchstat.UTest,
"utest": UTest, "utest": benchstat.UTest,
"t": TTest, "t": benchstat.TTest,
"t-test": TTest, "t-test": benchstat.TTest,
"ttest": TTest, "ttest": benchstat.TTest,
} }
func main() { func main() {
...@@ -134,7 +136,11 @@ func main() { ...@@ -134,7 +136,11 @@ func main() {
flag.Usage() flag.Usage()
} }
c := new(Collection) c := &benchstat.Collection{
Alpha: *flagAlpha,
AddGeoMean: *flagGeomean,
DeltaTest: deltaTest,
}
for _, file := range flag.Args() { for _, file := range flag.Args() {
data, err := ioutil.ReadFile(file) data, err := ioutil.ReadFile(file)
if err != nil { if err != nil {
...@@ -143,14 +149,14 @@ func main() { ...@@ -143,14 +149,14 @@ func main() {
c.AddConfig(file, data) c.AddConfig(file, data)
} }
tables := c.Tables(deltaTest) tables := c.Tables()
var buf bytes.Buffer var buf bytes.Buffer
if *flagHTML { if *flagHTML {
buf.WriteString(htmlStyle) buf.WriteString(htmlStyle)
FormatHTML(&buf, tables) benchstat.FormatHTML(&buf, tables)
} else { } else {
FormatText(&buf, tables) benchstat.FormatText(&buf, tables)
} }
os.Stdout.Write(buf.Bytes()) 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