Standard library / testing
testing.Benchmark()
Run a benchmark function programmatically and return its measured result. Timing depends on the machine. Expected stdout: 'true\nPASS\n'. Run as a _test.go file using the Go test harness.
Formal Syntax & Structure
go syntaxgo
func Benchmark(f func(b *B)) BenchmarkResultParameters & Attributes
| Name | Type | Description |
|---|---|---|
| f | func(b *B) | Function under test or callback to run. |
Return Value
BenchmarkResult
Result of the operation described above.
Working Example
Example Snippetgo
package main
import ( "fmt" "testing")
func TestReference(t *testing.T) { r := testing.Benchmark(func(b *testing.B) { for j := 0; j < b.N; j++ { } }) fmt.Println(r.N > 0)}