Standard library / testing

testing.B.RunParallel()

Distribute benchmark iterations among goroutines; each worker loops on PB.Next. Use synchronization for shared state. Expected stdout: 'true\nPASS\n'. Run as a _test.go file using the Go test harness.

Formal Syntax & Structure

go syntaxgo
func (b *B) RunParallel(body func(*PB))

Parameters & Attributes

NameTypeDescription
bodyfunc(*PB)Worker callback that consumes PB.Next iterations.

Working Example

Example Snippetgo
package main
import (  "fmt"  "sync/atomic"  "testing")
func TestReference(t *testing.T) {  r := testing.Benchmark(func(b *testing.B) {    var n atomic.Int64    b.RunParallel(func(pb *testing.PB) {      for pb.Next() {        n.Add(1)      }    })    if n.Load() != int64(b.N) {      b.Fatal("count")    }  })  fmt.Println(r.N > 0)}
Go Reference