Commit 9e30b708 authored by Shenghou Ma's avatar Shenghou Ma

all: set GOMAXPROCS to 1 when counting mallocs

also fix an annoying test that relies on $GOROOT be set.
Fixes #3690.

R=golang-dev, bradfitz
CC=golang-dev
https://golang.org/cl/6844086
parent 5b425cc3
...@@ -50,6 +50,7 @@ func BenchmarkEndToEndByteBuffer(b *testing.B) { ...@@ -50,6 +50,7 @@ func BenchmarkEndToEndByteBuffer(b *testing.B) {
} }
func TestCountEncodeMallocs(t *testing.T) { func TestCountEncodeMallocs(t *testing.T) {
defer runtime.GOMAXPROCS(runtime.GOMAXPROCS(1))
var buf bytes.Buffer var buf bytes.Buffer
enc := NewEncoder(&buf) enc := NewEncoder(&buf)
bench := &Bench{7, 3.2, "now is the time", []byte("for all good men")} bench := &Bench{7, 3.2, "now is the time", []byte("for all good men")}
...@@ -69,6 +70,7 @@ func TestCountEncodeMallocs(t *testing.T) { ...@@ -69,6 +70,7 @@ func TestCountEncodeMallocs(t *testing.T) {
} }
func TestCountDecodeMallocs(t *testing.T) { func TestCountDecodeMallocs(t *testing.T) {
defer runtime.GOMAXPROCS(runtime.GOMAXPROCS(1))
var buf bytes.Buffer var buf bytes.Buffer
enc := NewEncoder(&buf) enc := NewEncoder(&buf)
bench := &Bench{7, 3.2, "now is the time", []byte("for all good men")} bench := &Bench{7, 3.2, "now is the time", []byte("for all good men")}
......
...@@ -581,6 +581,7 @@ var mallocTest = []struct { ...@@ -581,6 +581,7 @@ var mallocTest = []struct {
var _ bytes.Buffer var _ bytes.Buffer
func TestCountMallocs(t *testing.T) { func TestCountMallocs(t *testing.T) {
defer runtime.GOMAXPROCS(runtime.GOMAXPROCS(1))
for _, mt := range mallocTest { for _, mt := range mallocTest {
const N = 100 const N = 100
memstats := new(runtime.MemStats) memstats := new(runtime.MemStats)
......
...@@ -180,6 +180,7 @@ func allocBytes(f func()) uint64 { ...@@ -180,6 +180,7 @@ func allocBytes(f func()) uint64 {
// does not cause deep recursion and in turn allocate too much memory. // does not cause deep recursion and in turn allocate too much memory.
// Test case for issue 3807. // Test case for issue 3807.
func TestMulUnbalanced(t *testing.T) { func TestMulUnbalanced(t *testing.T) {
defer runtime.GOMAXPROCS(runtime.GOMAXPROCS(1))
x := rndNat(50000) x := rndNat(50000)
y := rndNat(40) y := rndNat(40)
allocSize := allocBytes(func() { allocSize := allocBytes(func() {
......
...@@ -188,6 +188,7 @@ type errorfer interface { ...@@ -188,6 +188,7 @@ type errorfer interface {
} }
func doHeaderWriteSubset(n int, t errorfer) { func doHeaderWriteSubset(n int, t errorfer) {
defer runtime.GOMAXPROCS(runtime.GOMAXPROCS(1))
h := Header(map[string][]string{ h := Header(map[string][]string{
"Content-Length": {"123"}, "Content-Length": {"123"},
"Content-Type": {"text/plain"}, "Content-Type": {"text/plain"},
...@@ -204,7 +205,7 @@ func doHeaderWriteSubset(n int, t errorfer) { ...@@ -204,7 +205,7 @@ func doHeaderWriteSubset(n int, t errorfer) {
var m1 runtime.MemStats var m1 runtime.MemStats
runtime.ReadMemStats(&m1) runtime.ReadMemStats(&m1)
if mallocs := m1.Mallocs - m0.Mallocs; n >= 100 && mallocs >= uint64(n) { if mallocs := m1.Mallocs - m0.Mallocs; n >= 100 && mallocs >= uint64(n) {
// TODO(bradfitz,rsc): once we can sort with allocating, // TODO(bradfitz,rsc): once we can sort without allocating,
// make this an error. See http://golang.org/issue/3761 // make this an error. See http://golang.org/issue/3761
// t.Errorf("did %d mallocs (>= %d iterations); should have avoided mallocs", mallocs, n) // t.Errorf("did %d mallocs (>= %d iterations); should have avoided mallocs", mallocs, n)
} }
......
...@@ -446,6 +446,7 @@ func dialHTTP() (*Client, error) { ...@@ -446,6 +446,7 @@ func dialHTTP() (*Client, error) {
} }
func countMallocs(dial func() (*Client, error), t *testing.T) uint64 { func countMallocs(dial func() (*Client, error), t *testing.T) uint64 {
defer runtime.GOMAXPROCS(runtime.GOMAXPROCS(1))
once.Do(startServer) once.Do(startServer)
client, err := dial() client, err := dial()
if err != nil { if err != nil {
......
...@@ -91,6 +91,7 @@ var wincleantests = []PathTest{ ...@@ -91,6 +91,7 @@ var wincleantests = []PathTest{
} }
func TestClean(t *testing.T) { func TestClean(t *testing.T) {
defer runtime.GOMAXPROCS(runtime.GOMAXPROCS(1))
tests := cleantests tests := cleantests
if runtime.GOOS == "windows" { if runtime.GOOS == "windows" {
for i := range tests { for i := range tests {
...@@ -897,7 +898,7 @@ func TestDriveLetterInEvalSymlinks(t *testing.T) { ...@@ -897,7 +898,7 @@ func TestDriveLetterInEvalSymlinks(t *testing.T) {
} }
func TestBug3486(t *testing.T) { // http://code.google.com/p/go/issues/detail?id=3486 func TestBug3486(t *testing.T) { // http://code.google.com/p/go/issues/detail?id=3486
root, err := filepath.EvalSymlinks(os.Getenv("GOROOT")) root, err := filepath.EvalSymlinks(runtime.GOROOT())
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
......
...@@ -64,6 +64,7 @@ var cleantests = []PathTest{ ...@@ -64,6 +64,7 @@ var cleantests = []PathTest{
} }
func TestClean(t *testing.T) { func TestClean(t *testing.T) {
defer runtime.GOMAXPROCS(runtime.GOMAXPROCS(1))
for _, test := range cleantests { for _, test := range cleantests {
if s := Clean(test.path); s != test.result { if s := Clean(test.path); s != test.result {
t.Errorf("Clean(%q) = %q, want %q", test.path, s, test.result) t.Errorf("Clean(%q) = %q, want %q", test.path, s, test.result)
......
...@@ -2012,6 +2012,7 @@ func TestAddr(t *testing.T) { ...@@ -2012,6 +2012,7 @@ func TestAddr(t *testing.T) {
} }
func noAlloc(t *testing.T, n int, f func(int)) { func noAlloc(t *testing.T, n int, f func(int)) {
defer runtime.GOMAXPROCS(runtime.GOMAXPROCS(1))
// once to prime everything // once to prime everything
f(-1) f(-1)
memstats := new(runtime.MemStats) memstats := new(runtime.MemStats)
...@@ -2021,12 +2022,9 @@ func noAlloc(t *testing.T, n int, f func(int)) { ...@@ -2021,12 +2022,9 @@ func noAlloc(t *testing.T, n int, f func(int)) {
for j := 0; j < n; j++ { for j := 0; j < n; j++ {
f(j) f(j)
} }
// A few allocs may happen in the testing package when GOMAXPROCS > 1, so don't
// require zero mallocs.
// A new thread, one of which will be created if GOMAXPROCS>1, does 6 allocations.
runtime.ReadMemStats(memstats) runtime.ReadMemStats(memstats)
mallocs := memstats.Mallocs - oldmallocs mallocs := memstats.Mallocs - oldmallocs
if mallocs > 10 { if mallocs > 0 {
t.Fatalf("%d mallocs after %d iterations", mallocs, n) t.Fatalf("%d mallocs after %d iterations", mallocs, n)
} }
} }
......
...@@ -10,6 +10,7 @@ import ( ...@@ -10,6 +10,7 @@ import (
) )
func TestGcSys(t *testing.T) { func TestGcSys(t *testing.T) {
defer runtime.GOMAXPROCS(runtime.GOMAXPROCS(1))
memstats := new(runtime.MemStats) memstats := new(runtime.MemStats)
runtime.GC() runtime.GC()
runtime.ReadMemStats(memstats) runtime.ReadMemStats(memstats)
......
...@@ -39,6 +39,7 @@ func OkAmount(size, n uintptr) bool { ...@@ -39,6 +39,7 @@ func OkAmount(size, n uintptr) bool {
} }
func AllocAndFree(size, count int) { func AllocAndFree(size, count int) {
defer runtime.GOMAXPROCS(runtime.GOMAXPROCS(1))
if *chatty { if *chatty {
fmt.Printf("size=%d count=%d ...\n", size, count) fmt.Printf("size=%d count=%d ...\n", size, count)
} }
......
...@@ -44,6 +44,7 @@ var ( ...@@ -44,6 +44,7 @@ var (
) )
func TestCountMallocs(t *testing.T) { func TestCountMallocs(t *testing.T) {
defer runtime.GOMAXPROCS(runtime.GOMAXPROCS(1))
for _, mt := range mallocTest { for _, mt := range mallocTest {
const N = 100 const N = 100
memstats := new(runtime.MemStats) memstats := new(runtime.MemStats)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment