testing: add AllocsPerRun
This CL also replaces similar loops in other stdlib
package tests with calls to AllocsPerRun.
Fixes #4461.
R=minux.ma, rsc
CC=golang-dev
https://golang.org/cl/7002055
diff --git a/src/pkg/path/path_test.go b/src/pkg/path/path_test.go
index 52cbb49..220ec1a 100644
--- a/src/pkg/path/path_test.go
+++ b/src/pkg/path/path_test.go
@@ -5,7 +5,6 @@
package path
import (
- "runtime"
"testing"
)
@@ -64,7 +63,6 @@
}
func TestClean(t *testing.T) {
- defer runtime.GOMAXPROCS(runtime.GOMAXPROCS(1))
for _, test := range cleantests {
if s := Clean(test.path); s != test.result {
t.Errorf("Clean(%q) = %q, want %q", test.path, s, test.result)
@@ -74,20 +72,12 @@
}
}
- var ms runtime.MemStats
- runtime.ReadMemStats(&ms)
- allocs := -ms.Mallocs
- const rounds = 100
- for i := 0; i < rounds; i++ {
- for _, test := range cleantests {
- Clean(test.result)
+ for _, test := range cleantests {
+ allocs := testing.AllocsPerRun(100, func() { Clean(test.result) })
+ if allocs > 0 {
+ t.Errorf("Clean(%q): %v allocs, want zero", test.result, allocs)
}
}
- runtime.ReadMemStats(&ms)
- allocs += ms.Mallocs
- if allocs >= rounds {
- t.Errorf("Clean cleaned paths: %d allocations per test round, want zero", allocs/rounds)
- }
}
type SplitTest struct {