Looking for go Answers? Try Ask4KnowledgeBase
Looking for go Keywords? Try Ask4Keywords

Go Benchmark tests


If you want to measure benchmarks add a testing method like this:


package sum

// Sum calculates the sum of two integers
func Sum(a, b int) int {
    return a + b


package sum

import "testing"

func BenchmarkSum(b *testing.B) {
    for i := 0; i < b.N; i++ {
        _ = Sum(2, 3)

Then in order to run a simple benchmark:

$ go test -bench=. 
BenchmarkSum-8    2000000000             0.49 ns/op
ok      so/sum    1.027s