Documentation
¶
Overview ¶
Package benchparse provides utilities for parsing benchmark results. Parsed results are split by sub-benchmarks, with support for sub-benchmarks with names of the form 'var_name=var_value'
Index ¶
Examples ¶
Constants ¶
const ErrNotMeasured = errorString("not measured")
ErrNotMeasured indicates that a specific output was not measured.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type BenchInputs ¶
type BenchInputs struct {
VarValues []BenchVarValue // sub-benchmark names of the form some_var=some_val
Subs []BenchSub // remaining components of a sub-benchmark
MaxProcs int // the value of GOMAXPROCS when the benchmark was run
}
BenchInputs define a sub-benchmark. For example a benchmark with a full name 'BenchmarkMyType/some_method/foo=2/bar=baz-4' would be defined by the Subs=[some_method], the VarValues=[foo=2 bar=baz], and MaxProcs=4.
func (BenchInputs) String ¶
func (b BenchInputs) String() string
String returns the string representation of the BenchInputs. This should be equivalent to the portion of the benchmark name following the name of the top-level benchmark, but formatting of VarValues may vary slightly.
type BenchOutputs ¶
type BenchOutputs struct {
N int // number of iterations
// contains filtered or unexported fields
}
BenchOutputs are the outputs of a single benchmark run.
func (BenchOutputs) AllocedBytesPerOp ¶
func (b BenchOutputs) AllocedBytesPerOp() (uint64, error)
AllocedBytesPerOp returns the bytes allocated per iteration. This is measured if either '-test.benchmem' is set when running the benchmark or if testing.B.ReportAllocs() is called.
If not measured ErrNotMeasured is returned.
func (BenchOutputs) AllocsPerOp ¶
func (b BenchOutputs) AllocsPerOp() (uint64, error)
AllocsPerOp returns the allocs per iteration. This is measured if either '-test.benchmem' is set when running the benchmark or if testing.B.ReportAllocs() is called.
If not measured ErrNotMeasured is returned.
func (BenchOutputs) MBPerS ¶
func (b BenchOutputs) MBPerS() (float64, error)
MBPerS returns the MB processed per second. This is measured if testing.B.SetBytes() is called.
If not measured ErrNotMeasured is returned.
func (BenchOutputs) NsPerOp ¶
func (b BenchOutputs) NsPerOp() (float64, error)
NsPerOp returns the nanoseconds per iteration. If not measured ErrNotMeasured is returned.
func (BenchOutputs) String ¶
func (b BenchOutputs) String() string
type BenchRes ¶
type BenchRes struct {
Inputs BenchInputs // the input variables
Outputs BenchOutputs // the output result
}
BenchRes represents a result from a single benchmark run. This corresponds to one line from the testing.B output.
type BenchSub ¶
type BenchSub struct {
Name string
// contains filtered or unexported fields
}
BenchSub represents an input to the benchmark represented by a sub-benchmark with a name NOT of the form 'var_name=var_value'.
type BenchVarValue ¶
type BenchVarValue struct {
Name string
Value interface{}
// contains filtered or unexported fields
}
BenchVarValue represents an input to the benchmark represented by a sub-benchmark with a name of the form 'var_name=var_value'.
func (BenchVarValue) String ¶
func (b BenchVarValue) String() string
String returns the string representation of the BenchVarValue with the form 'var_name=var_value'. Currently the default string format ('%v') is used for the actual value, meaning the string representation of a BenchVarValue may vary slightly from the original input (for example precision of floating point values).
type Benchmark ¶
Benchmark represents a single top-level benchmark and it's results.
func ParseBenchmarks ¶
ParseBenchmarks extracts a list of Benchmarks from testing.B output.
Example ¶
r := strings.NewReader(`
BenchmarkMath/areaUnder/y=sin(x)/delta=0.001000/start_x=-2/end_x=1/abs_val=true-4 21801 55357 ns/op 0 B/op 0 allocs/op
BenchmarkMath/areaUnder/y=2x+3/delta=1.000000/start_x=-1/end_x=2/abs_val=false-4 88335925 13.3 ns/op 0 B/op 0 allocs/op
BenchmarkMath/max/y=2x+3/delta=0.001000/start_x=-2/end_x=1-4 56282 20361 ns/op 0 B/op 0 allocs/op
BenchmarkMath/max/y=sin(x)/delta=1.000000/start_x=-1/end_x=2-4 16381138 62.7 ns/op 0 B/op 0 allocs/op
`)
benches, err := ParseBenchmarks(r)
if err != nil {
log.Fatal(err)
}
for _, bench := range benches {
fmt.Printf("bench name: %s\n", bench.Name)
for _, res := range bench.Results {
var (
varValues = make([]string, len(res.Inputs.VarValues))
otherSubs = make([]string, len(res.Inputs.Subs))
)
for i, varVal := range res.Inputs.VarValues {
varValues[i] = varVal.String()
}
for i, sub := range res.Inputs.Subs {
otherSubs[i] = sub.String()
}
fmt.Printf("var values = %q\n", varValues)
fmt.Printf("other subs = %q\n", otherSubs)
nsPerOp, err := res.Outputs.NsPerOp()
if err != nil {
log.Fatal(err)
}
fmt.Printf("ns per op = %.2f\n", nsPerOp)
}
}
Output: bench name: BenchmarkMath var values = ["y=sin(x)" "delta=0.001" "start_x=-2" "end_x=1" "abs_val=true"] other subs = ["areaUnder"] ns per op = 55357.00 var values = ["y=2x+3" "delta=1" "start_x=-1" "end_x=2" "abs_val=false"] other subs = ["areaUnder"] ns per op = 13.30 var values = ["y=2x+3" "delta=0.001" "start_x=-2" "end_x=1"] other subs = ["max"] ns per op = 20361.00 var values = ["y=sin(x)" "delta=1" "start_x=-1" "end_x=2"] other subs = ["max"] ns per op = 62.70
func (Benchmark) GroupResults ¶
func (b Benchmark) GroupResults(groupBy []string) GroupedResults
GroupResults groups a benchmarks results by a specified set of input variable names. For example a Benchmark with Results corresponding to the cases [/foo=1/bar=baz /foo=2/bar=baz /foo=1/bar=qux /foo=2/bar=qux] grouped by ['foo'] would have 2 groups of results (those with Inputs where foo=1 and those with Inputs where foo=2).
Example ¶
r := strings.NewReader(`
BenchmarkMath/areaUnder/y=sin(x)/delta=0.001000/start_x=-2/end_x=1/abs_val=true-4 21801 55357 ns/op 0 B/op 0 allocs/op
BenchmarkMath/areaUnder/y=2x+3/delta=1.000000/start_x=-1/end_x=2/abs_val=false-4 88335925 13.3 ns/op 0 B/op 0 allocs/op
BenchmarkMath/max/y=2x+3/delta=0.001000/start_x=-2/end_x=1-4 56282 20361 ns/op 0 B/op 0 allocs/op
BenchmarkMath/max/y=sin(x)/delta=1.000000/start_x=-1/end_x=2-4 16381138 62.7 ns/op 0 B/op 0 allocs/op
`)
benches, err := ParseBenchmarks(r)
if err != nil {
log.Fatal(err)
}
groupedResults := benches[0].GroupResults([]string{"y"})
// sort by key names to ensure consistent iteration order
groupNames := make([]string, len(groupedResults))
i := 0
for k := range groupedResults {
groupNames[i] = k
i++
}
sort.Strings(groupNames)
for _, k := range groupNames {
fmt.Println(k)
v := groupedResults[k]
times := make([]float64, len(v))
for i, res := range v {
nsPerOp, err := res.Outputs.NsPerOp()
if err != nil {
log.Fatal(err)
}
times[i] = nsPerOp
}
fmt.Printf("ns per op = %v\n", times)
}
Output: y=2x+3 ns per op = [13.3 20361] y=sin(x) ns per op = [55357 62.7]
type GroupedResults ¶
GroupedResults represents a grouping of benchmark results.