benchparse

package module
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 27, 2020 License: MIT Imports: 8 Imported by: 2

README

GoTest GoBuild GoDoc

benchparse

Utilities for parsing go benchmark results. Parsed results are split by sub-benchmarks, with support for sub-benchmarks with names of the form var_name=var_value.

See godoc for full package details and examples.

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

This section is empty.

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
	NsPerOp           float64 // nanoseconds per iteration
	AllocedBytesPerOp uint64  // bytes allocated per iteration
	AllocsPerOp       uint64  // allocs per iteration
	MBPerS            float64 // MB processed per second
	Measured          int     // which measurements were recorded
}

BenchOutputs are the outputs of a single benchmark run. Just the relevant parts of https://godoc.org/golang.org/x/tools/benchmark/parse#Benchmark.

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'.

func (BenchSub) String

func (b BenchSub) String() string

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

type Benchmark struct {
	Name    string
	Results []BenchRes
}

Benchmark represents a single top-level benchmark and it's results.

func ParseBenchmarks

func ParseBenchmarks(r io.Reader) ([]Benchmark, error)

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)
		fmt.Printf("ns per op = %.2f\n", res.Outputs.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 {
		times[i] = res.Outputs.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]

func (Benchmark) String

func (b Benchmark) String() string

String returns the string representation of the benchmark. This follows the same format as the testing.B output.

type GroupedResults

type GroupedResults map[string][]BenchRes

GroupedResults represents a grouping of benchmark results.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL