Golang testing.AllocsPerRun() function example

package testing

Golang testing.AllocsPerRun() function usage example

 package main

 import (
  "fmt"
  "testing"
 )

 func main() {

  runTests := func() {
 fmt.Println("running tests")
  }

  allocs := testing.AllocsPerRun(10, runTests)
  if allocs != 0 {
 fmt.Printf("expected no allocs for tests, got %v", allocs)
  }

 }

Output :

running tests

running tests

running tests

running tests

running tests

running tests

running tests

running tests

running tests

running tests

running tests

expected no allocs for tests, got 3

Reference :

http://golang.org/pkg/testing/#AllocsPerRun

Advertisement