mirror of
https://github.com/golang/go
synced 2024-11-18 18:54:42 -07:00
285f6fe2f6
This is intended to replace the awk-based misc/benchcmp. It mostly matches the existing misc/benchcmp. Notable changes: * Written in Go. * Minor whitespace changes in the output; the tabular nature of the output is preserved, as is most number formatting and verbiage. * Comparisons within each section are sorted from highest change to lowest. * Proper handling of multiple benchmarks with the same name (issue 7016). * Does not omit benchmark comparisons for which the new value is zero. * Adds -changed flag to only show benchmarks whose value have changed. Useful for memory-oriented, large-scale benchmark comparisons. * Has tests. * Formats small ns measurements with extra precision. Updates golang/go#7016. LGTM=r R=golang-codereviews, dave, dvyukov, oleku.konko, bradfitz, gobot, r CC=golang-codereviews https://golang.org/cl/47980043
124 lines
2.9 KiB
Go
124 lines
2.9 KiB
Go
// Copyright 2014 The Go Authors. All rights reserved.
|
|
// Use of this source code is governed by a BSD-style
|
|
// license that can be found in the LICENSE file.
|
|
|
|
package main
|
|
|
|
import (
|
|
"bufio"
|
|
"bytes"
|
|
"fmt"
|
|
"io"
|
|
"strconv"
|
|
"strings"
|
|
)
|
|
|
|
// Flags used by Bench.Measured to indicate
|
|
// which measurements a Bench contains.
|
|
const (
|
|
NsOp = 1 << iota
|
|
MbS
|
|
BOp
|
|
AllocsOp
|
|
)
|
|
|
|
// Bench is one run of a single benchmark.
|
|
type Bench struct {
|
|
Name string // benchmark name
|
|
N int // number of iterations
|
|
NsOp float64 // nanoseconds per iteration
|
|
MbS float64 // MB processed per second
|
|
BOp uint64 // bytes allocated per iteration
|
|
AllocsOp uint64 // allocs per iteration
|
|
Measured int // which measurements were recorded
|
|
}
|
|
|
|
// ParseLine extracts a Bench from a single line of testing.B output.
|
|
func ParseLine(line string) (*Bench, error) {
|
|
fields := strings.Fields(line)
|
|
|
|
// Two required, positional fields: Name and iterations.
|
|
if len(fields) < 2 {
|
|
return nil, fmt.Errorf("two fields required, have %d", len(fields))
|
|
}
|
|
if !strings.HasPrefix(fields[0], "Benchmark") {
|
|
return nil, fmt.Errorf(`first field does not start with "Benchmark`)
|
|
}
|
|
n, err := strconv.Atoi(fields[1])
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
b := &Bench{Name: fields[0], N: n}
|
|
|
|
// Parse any remaining pairs of fields; we've parsed one pair already.
|
|
for i := 1; i < len(fields)/2; i++ {
|
|
b.parseMeasurement(fields[i*2], fields[i*2+1])
|
|
}
|
|
return b, nil
|
|
}
|
|
|
|
func (b *Bench) parseMeasurement(quant string, unit string) {
|
|
switch unit {
|
|
case "ns/op":
|
|
if f, err := strconv.ParseFloat(quant, 64); err == nil {
|
|
b.NsOp = f
|
|
b.Measured |= NsOp
|
|
}
|
|
case "MB/s":
|
|
if f, err := strconv.ParseFloat(quant, 64); err == nil {
|
|
b.MbS = f
|
|
b.Measured |= MbS
|
|
}
|
|
case "B/op":
|
|
if i, err := strconv.ParseUint(quant, 10, 64); err == nil {
|
|
b.BOp = i
|
|
b.Measured |= BOp
|
|
}
|
|
case "allocs/op":
|
|
if i, err := strconv.ParseUint(quant, 10, 64); err == nil {
|
|
b.AllocsOp = i
|
|
b.Measured |= AllocsOp
|
|
}
|
|
}
|
|
}
|
|
|
|
func (b *Bench) String() string {
|
|
buf := new(bytes.Buffer)
|
|
fmt.Fprintf(buf, "%s %d", b.Name, b.N)
|
|
if b.Measured&NsOp != 0 {
|
|
fmt.Fprintf(buf, " %.2f ns/op", b.NsOp)
|
|
}
|
|
if b.Measured&MbS != 0 {
|
|
fmt.Fprintf(buf, " %.2f MB/s", b.MbS)
|
|
}
|
|
if b.Measured&BOp != 0 {
|
|
fmt.Fprintf(buf, " %d B/op", b.BOp)
|
|
}
|
|
if b.Measured&AllocsOp != 0 {
|
|
fmt.Fprintf(buf, " %d allocs/op", b.AllocsOp)
|
|
}
|
|
return buf.String()
|
|
}
|
|
|
|
// BenchSet is a collection of benchmarks from one
|
|
// testing.B run, keyed by name to faciliate comparison.
|
|
type BenchSet map[string][]*Bench
|
|
|
|
// Parse extracts a BenchSet from testing.B output. Parse
|
|
// preserves the order of benchmarks that have identical names.
|
|
func ParseBenchSet(r io.Reader) (BenchSet, error) {
|
|
bb := make(BenchSet)
|
|
scan := bufio.NewScanner(r)
|
|
for scan.Scan() {
|
|
if b, err := ParseLine(scan.Text()); err == nil {
|
|
bb[b.Name] = append(bb[b.Name], b)
|
|
}
|
|
}
|
|
|
|
if err := scan.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return bb, nil
|
|
}
|