Standard library / testing
testing.B.ReportMetric()
Record a custom metric per benchmark result under a unit name. Expected stdout: '7\nPASS\n'. Run as a _test.go file using the Go test harness.
Formal Syntax & Structure
go syntaxgo
func (b *B) ReportMetric(n float64, unit string)Parameters & Attributes
| Name | Type | Description |
|---|---|---|
| n | float64 | Byte limit or numeric measurement, depending on the API. |
| unit | string | Metric unit label. |
Working Example
Example Snippetgo
package main
import ( "fmt" "testing")
func TestReference(t *testing.T) { r := testing.Benchmark(func(b *testing.B) { b.ReportMetric(7, "items/op") for j := 0; j < b.N; j++ { } }) fmt.Println(r.Extra["items/op"])}