Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 21 additions & 8 deletions benchmark/parse/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,15 @@ const (

// 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
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
OtherMetrics map[string]float64 // other metrics recorded by testing.B.ReportMetric
}

// ParseLine extracts a Benchmark from a single line of testing.B
Expand Down Expand Up @@ -83,6 +84,13 @@ func (b *Benchmark) parseMeasurement(quant string, unit string) {
b.AllocsPerOp = i
b.Measured |= AllocsPerOp
}
default:
if f, err := strconv.ParseFloat(quant, 64); err == nil {
if b.OtherMetrics == nil {
b.OtherMetrics = map[string]float64{}
}
b.OtherMetrics[unit] = f
}
}
}

Expand All @@ -101,6 +109,11 @@ func (b *Benchmark) String() string {
if (b.Measured & AllocsPerOp) != 0 {
fmt.Fprintf(buf, " %d allocs/op", b.AllocsPerOp)
}
if b.OtherMetrics != nil {
for unit, quant := range b.OtherMetrics {
fmt.Fprintf(buf, " %.2f %s", quant, unit)
}
}
return buf.String()
}

Expand Down
26 changes: 26 additions & 0 deletions benchmark/parse/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,27 @@ func TestParseLine(t *testing.T) {
want: &Benchmark{
Name: "BenchmarkBridge",
N: 100000000,
OtherMetrics: map[string]float64{
"smoots": 19.6,
},
},
},
{
line: "PASS",
err: true,
},
{
line: "BenchmarkSort/num_elems=20-4 3542373 340 ns/op 46.0 compares/op",
want: &Benchmark{
Name: "BenchmarkSort/num_elems=20-4",
N: 3542373,
NsPerOp: 340,
Measured: NsPerOp,
OtherMetrics: map[string]float64{
"compares/op": 46.0,
},
},
},
}

for _, tt := range cases {
Expand Down Expand Up @@ -195,6 +210,17 @@ func TestString(t *testing.T) {
},
wanted: "BenchmarkTest 100000000 5 allocs/op",
},
{
name: "otherMetricsTest",
input: &Benchmark{
Name: "BenchmarkSort/num_elems=20-4",
N: 3542373,
OtherMetrics: map[string]float64{
"compares/op": 46.0,
},
},
wanted: "BenchmarkSort/num_elems=20-4 3542373 46.00 compares/op",
},
}

for _, tt := range tests {
Expand Down