Hi all,

Sorry if this is an FAQ, but then my Google-foo has failed me. :-/

It seems to me that the test functions in a bla_test.go file influence
the benchmark functions in the same file. In the snippet below, if I
comment out the body of the test function, I get better benchmark
numbers than when I leave it in. Suspecting some GC "leftovers" from
the test, I've tried adding "runtime.GC(); b.ResetTimer()" to the
benchmark but that doesn't actually help.

Before I dig through the source for Go's testing module itself, I
wanted to see if this is a well-known issue, maybe with a well-known
workaround?

Best,
Peter

-----CUT-----
$ go version
go version go1.7.5 linux/amd64
$ cat bla_test.go
package main

import "testing"
import "container/list"
import "runtime"

const size = 2270

func TestPushBack(t *testing.T) {
//    /*
    var q list.List
    for i := 0; i < size; i++ {
        q.PushBack(i)
    }
    for i := 0; q.Len() > 0; i++ {
        e := q.Front()
        x := e.Value.(int)
        q.Remove(e)
        if x != i {
            t.Errorf("q.PopFront() = %d, want %d", x, i)
        }
    }
//    */
}

func BenchmarkPushBack(b *testing.B) {
    runtime.GC()
    b.ResetTimer()
    for i := 0; i < b.N; i++ {
        var q list.List
        for n := 0; n < size; n++ {
            q.PushBack(n)
        }
    }
}
$ go test -bench .
BenchmarkPushBack-2           5000        378367 ns/op
PASS
ok      X    1.937s
$ go test -bench .
BenchmarkPushBack-2           5000        378196 ns/op
PASS
ok      X    1.936s
$ vi bla_test.go # comment out test code
$ go test -bench .
BenchmarkPushBack-2           5000        356310 ns/op
PASS
ok      X    1.825s
$ go test -bench .
BenchmarkPushBack-2           5000        354530 ns/op
PASS
ok      X    1.816s

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to