diff options
author | Dominik Honnef <dominik@honnef.co> | 2015-01-09 09:47:44 +0100 |
---|---|---|
committer | Rob Pike <r@golang.org> | 2015-01-15 01:28:17 +0000 |
commit | 9f20d8c1983155428edf49b3f12ea85580740f91 (patch) | |
tree | 796b80b67c880cbe96ad666b7e01f6a78bdb0d8a /benchmark | |
parent | d272cc0504d044205c5de2943a4cd53110371dd9 (diff) | |
download | golang-x-tools-9f20d8c1983155428edf49b3f12ea85580740f91.tar.gz |
benchmark/parser, cmd/benchcmp: improve names of exported identifiers
Most of the names have been brought in line with the names used in
testing/BenchmarkResult. For example, NsOp becomes NsPerOp.
Additionally, "Bench" becomes "Benchmark" and "BenchSet" becomes "Set".
Change-Id: I7dfca68a804e285a87ab9692b5bb99ccb676da7f
Reviewed-on: https://go-review.googlesource.com/2610
Reviewed-by: Rob Pike <r@golang.org>
Diffstat (limited to 'benchmark')
-rw-r--r-- | benchmark/parse/parse.go | 87 | ||||
-rw-r--r-- | benchmark/parse/parse_test.go | 64 |
2 files changed, 76 insertions, 75 deletions
diff --git a/benchmark/parse/parse.go b/benchmark/parse/parse.go index 6464dea50..1fb622381 100644 --- a/benchmark/parse/parse.go +++ b/benchmark/parse/parse.go @@ -15,29 +15,30 @@ import ( "strings" ) -// Flags used by Bench.Measured to indicate -// which measurements a Bench contains. +// Flags used by Benchmark.Measured to indicate +// which measurements a Benchmark contains. const ( - NsOp = 1 << iota - MbS - BOp - AllocsOp + NsPerOp = 1 << iota + MBPerS + AllocedBytesPerOp + AllocsPerOp ) -// 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 - Ord int // ordinal position within a benchmark run +// Benchmark is one run of a single benchmark. +type Benchmark struct { + Name string // benchmark name + 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 + Ord int // ordinal position within a benchmark run } -// ParseLine extracts a Bench from a single line of testing.B output. -func ParseLine(line string) (*Bench, error) { +// ParseLine extracts a Benchmark from a single line of testing.B +// output. +func ParseLine(line string) (*Benchmark, error) { fields := strings.Fields(line) // Two required, positional fields: Name and iterations. @@ -51,7 +52,7 @@ func ParseLine(line string) (*Bench, error) { if err != nil { return nil, err } - b := &Bench{Name: fields[0], N: n} + b := &Benchmark{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++ { @@ -60,58 +61,58 @@ func ParseLine(line string) (*Bench, error) { return b, nil } -func (b *Bench) parseMeasurement(quant string, unit string) { +func (b *Benchmark) 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 + b.NsPerOp = f + b.Measured |= NsPerOp } case "MB/s": if f, err := strconv.ParseFloat(quant, 64); err == nil { - b.MbS = f - b.Measured |= MbS + b.MBPerS = f + b.Measured |= MBPerS } case "B/op": if i, err := strconv.ParseUint(quant, 10, 64); err == nil { - b.BOp = i - b.Measured |= BOp + b.AllocedBytesPerOp = i + b.Measured |= AllocedBytesPerOp } case "allocs/op": if i, err := strconv.ParseUint(quant, 10, 64); err == nil { - b.AllocsOp = i - b.Measured |= AllocsOp + b.AllocsPerOp = i + b.Measured |= AllocsPerOp } } } -func (b *Bench) String() string { +func (b *Benchmark) 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 & NsPerOp) != 0 { + fmt.Fprintf(buf, " %.2f ns/op", b.NsPerOp) } - if b.Measured&MbS != 0 { - fmt.Fprintf(buf, " %.2f MB/s", b.MbS) + if (b.Measured & MBPerS) != 0 { + fmt.Fprintf(buf, " %.2f MB/s", b.MBPerS) } - if b.Measured&BOp != 0 { - fmt.Fprintf(buf, " %d B/op", b.BOp) + if (b.Measured & AllocedBytesPerOp) != 0 { + fmt.Fprintf(buf, " %d B/op", b.AllocedBytesPerOp) } - if b.Measured&AllocsOp != 0 { - fmt.Fprintf(buf, " %d allocs/op", b.AllocsOp) + if (b.Measured & AllocsPerOp) != 0 { + fmt.Fprintf(buf, " %d allocs/op", b.AllocsPerOp) } return buf.String() } -// BenchSet is a collection of benchmarks from one +// Set is a collection of benchmarks from one // testing.B run, keyed by name to facilitate comparison. -type BenchSet map[string][]*Bench +type Set map[string][]*Benchmark -// ParseBenchSet extracts a BenchSet from testing.B output. -// ParseBenchSet preserves the order of benchmarks that have identical +// ParseSet extracts a Set from testing.B output. +// ParseSet preserves the order of benchmarks that have identical // names. -func ParseBenchSet(r io.Reader) (BenchSet, error) { - bb := make(BenchSet) +func ParseSet(r io.Reader) (Set, error) { + bb := make(Set) scan := bufio.NewScanner(r) ord := 0 for scan.Scan() { diff --git a/benchmark/parse/parse_test.go b/benchmark/parse/parse_test.go index d96be6317..06db8489d 100644 --- a/benchmark/parse/parse_test.go +++ b/benchmark/parse/parse_test.go @@ -13,47 +13,47 @@ import ( func TestParseLine(t *testing.T) { cases := []struct { line string - want *Bench + want *Benchmark err bool // expect an error }{ { line: "BenchmarkEncrypt 100000000 19.6 ns/op", - want: &Bench{ + want: &Benchmark{ Name: "BenchmarkEncrypt", - N: 100000000, NsOp: 19.6, - Measured: NsOp, + N: 100000000, NsPerOp: 19.6, + Measured: NsPerOp, }, }, { line: "BenchmarkEncrypt 100000000 19.6 ns/op 817.77 MB/s", - want: &Bench{ + want: &Benchmark{ Name: "BenchmarkEncrypt", - N: 100000000, NsOp: 19.6, MbS: 817.77, - Measured: NsOp | MbS, + N: 100000000, NsPerOp: 19.6, MBPerS: 817.77, + Measured: NsPerOp | MBPerS, }, }, { line: "BenchmarkEncrypt 100000000 19.6 ns/op 817.77", - want: &Bench{ + want: &Benchmark{ Name: "BenchmarkEncrypt", - N: 100000000, NsOp: 19.6, - Measured: NsOp, + N: 100000000, NsPerOp: 19.6, + Measured: NsPerOp, }, }, { line: "BenchmarkEncrypt 100000000 19.6 ns/op 817.77 MB/s 5 allocs/op", - want: &Bench{ + want: &Benchmark{ Name: "BenchmarkEncrypt", - N: 100000000, NsOp: 19.6, MbS: 817.77, AllocsOp: 5, - Measured: NsOp | MbS | AllocsOp, + N: 100000000, NsPerOp: 19.6, MBPerS: 817.77, AllocsPerOp: 5, + Measured: NsPerOp | MBPerS | AllocsPerOp, }, }, { line: "BenchmarkEncrypt 100000000 19.6 ns/op 817.77 MB/s 3 B/op 5 allocs/op", - want: &Bench{ + want: &Benchmark{ Name: "BenchmarkEncrypt", - N: 100000000, NsOp: 19.6, MbS: 817.77, BOp: 3, AllocsOp: 5, - Measured: NsOp | MbS | BOp | AllocsOp, + N: 100000000, NsPerOp: 19.6, MBPerS: 817.77, AllocedBytesPerOp: 3, AllocsPerOp: 5, + Measured: NsPerOp | MBPerS | AllocedBytesPerOp | AllocsPerOp, }, }, // error handling cases @@ -67,7 +67,7 @@ func TestParseLine(t *testing.T) { }, { line: "BenchmarkBridge 100000000 19.6 smoots", // unknown unit - want: &Bench{ + want: &Benchmark{ Name: "BenchmarkBridge", N: 100000000, }, @@ -90,7 +90,7 @@ func TestParseLine(t *testing.T) { } } -func TestParseBenchSet(t *testing.T) { +func TestParseSet(t *testing.T) { // Test two things: // 1. The noise that can accompany testing.B output gets ignored. // 2. Benchmarks with the same name have their order preserved. @@ -111,42 +111,42 @@ func TestParseBenchSet(t *testing.T) { ok net/http 95.783s ` - want := BenchSet{ - "BenchmarkReadRequestApachebench": []*Bench{ + want := Set{ + "BenchmarkReadRequestApachebench": []*Benchmark{ { Name: "BenchmarkReadRequestApachebench", - N: 1000000, NsOp: 2960, MbS: 27.70, BOp: 839, AllocsOp: 9, - Measured: NsOp | MbS | BOp | AllocsOp, + N: 1000000, NsPerOp: 2960, MBPerS: 27.70, AllocedBytesPerOp: 839, AllocsPerOp: 9, + Measured: NsPerOp | MBPerS | AllocedBytesPerOp | AllocsPerOp, Ord: 2, }, }, - "BenchmarkClientServerParallel64": []*Bench{ + "BenchmarkClientServerParallel64": []*Benchmark{ { Name: "BenchmarkClientServerParallel64", - N: 50000, NsOp: 59192, BOp: 7028, AllocsOp: 60, - Measured: NsOp | BOp | AllocsOp, + N: 50000, NsPerOp: 59192, AllocedBytesPerOp: 7028, AllocsPerOp: 60, + Measured: NsPerOp | AllocedBytesPerOp | AllocsPerOp, Ord: 3, }, }, - "BenchmarkEncrypt": []*Bench{ + "BenchmarkEncrypt": []*Benchmark{ { Name: "BenchmarkEncrypt", - N: 100000000, NsOp: 19.6, - Measured: NsOp, + N: 100000000, NsPerOp: 19.6, + Measured: NsPerOp, Ord: 0, }, { Name: "BenchmarkEncrypt", - N: 5000000, NsOp: 517, - Measured: NsOp, + N: 5000000, NsPerOp: 517, + Measured: NsPerOp, Ord: 1, }, }, } - have, err := ParseBenchSet(strings.NewReader(in)) + have, err := ParseSet(strings.NewReader(in)) if err != nil { - t.Fatalf("unexpected err during ParseBenchSet: %v", err) + t.Fatalf("unexpected err during ParseSet: %v", err) } if !reflect.DeepEqual(want, have) { t.Errorf("parsed bench set incorrectly, want %v have %v", want, have) |