github.com/spotify/syslog-redirector-golang@v0.0.0-20140320174030-4859f03d829a/src/pkg/runtime/malloc_test.go (about)

     1  // Copyright 2013 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package runtime_test
     6  
     7  import (
     8  	"flag"
     9  	. "runtime"
    10  	"testing"
    11  	"time"
    12  	"unsafe"
    13  )
    14  
    15  func TestMemStats(t *testing.T) {
    16  	// Test that MemStats has sane values.
    17  	st := new(MemStats)
    18  	ReadMemStats(st)
    19  	if st.HeapSys == 0 || st.StackSys == 0 || st.MSpanSys == 0 || st.MCacheSys == 0 ||
    20  		st.BuckHashSys == 0 || st.GCSys == 0 || st.OtherSys == 0 {
    21  		t.Fatalf("Zero sys value: %+v", *st)
    22  	}
    23  	if st.Sys != st.HeapSys+st.StackSys+st.MSpanSys+st.MCacheSys+
    24  		st.BuckHashSys+st.GCSys+st.OtherSys {
    25  		t.Fatalf("Bad sys value: %+v", *st)
    26  	}
    27  }
    28  
    29  var mallocSink uintptr
    30  
    31  func BenchmarkMalloc8(b *testing.B) {
    32  	var x uintptr
    33  	for i := 0; i < b.N; i++ {
    34  		p := new(int64)
    35  		x ^= uintptr(unsafe.Pointer(p))
    36  	}
    37  	mallocSink = x
    38  }
    39  
    40  func BenchmarkMalloc16(b *testing.B) {
    41  	var x uintptr
    42  	for i := 0; i < b.N; i++ {
    43  		p := new([2]int64)
    44  		x ^= uintptr(unsafe.Pointer(p))
    45  	}
    46  	mallocSink = x
    47  }
    48  
    49  func BenchmarkMallocTypeInfo8(b *testing.B) {
    50  	var x uintptr
    51  	for i := 0; i < b.N; i++ {
    52  		p := new(struct {
    53  			p [8 / unsafe.Sizeof(uintptr(0))]*int
    54  		})
    55  		x ^= uintptr(unsafe.Pointer(p))
    56  	}
    57  	mallocSink = x
    58  }
    59  
    60  func BenchmarkMallocTypeInfo16(b *testing.B) {
    61  	var x uintptr
    62  	for i := 0; i < b.N; i++ {
    63  		p := new(struct {
    64  			p [16 / unsafe.Sizeof(uintptr(0))]*int
    65  		})
    66  		x ^= uintptr(unsafe.Pointer(p))
    67  	}
    68  	mallocSink = x
    69  }
    70  
    71  var n = flag.Int("n", 1000, "number of goroutines")
    72  
    73  func BenchmarkGoroutineSelect(b *testing.B) {
    74  	quit := make(chan struct{})
    75  	read := func(ch chan struct{}) {
    76  		for {
    77  			select {
    78  			case _, ok := <-ch:
    79  				if !ok {
    80  					return
    81  				}
    82  			case <-quit:
    83  				return
    84  			}
    85  		}
    86  	}
    87  	benchHelper(b, *n, read)
    88  }
    89  
    90  func BenchmarkGoroutineBlocking(b *testing.B) {
    91  	read := func(ch chan struct{}) {
    92  		for {
    93  			if _, ok := <-ch; !ok {
    94  				return
    95  			}
    96  		}
    97  	}
    98  	benchHelper(b, *n, read)
    99  }
   100  
   101  func BenchmarkGoroutineForRange(b *testing.B) {
   102  	read := func(ch chan struct{}) {
   103  		for _ = range ch {
   104  		}
   105  	}
   106  	benchHelper(b, *n, read)
   107  }
   108  
   109  func benchHelper(b *testing.B, n int, read func(chan struct{})) {
   110  	m := make([]chan struct{}, n)
   111  	for i := range m {
   112  		m[i] = make(chan struct{}, 1)
   113  		go read(m[i])
   114  	}
   115  	b.StopTimer()
   116  	b.ResetTimer()
   117  	GC()
   118  
   119  	for i := 0; i < b.N; i++ {
   120  		for _, ch := range m {
   121  			if ch != nil {
   122  				ch <- struct{}{}
   123  			}
   124  		}
   125  		time.Sleep(10 * time.Millisecond)
   126  		b.StartTimer()
   127  		GC()
   128  		b.StopTimer()
   129  	}
   130  
   131  	for _, ch := range m {
   132  		close(ch)
   133  	}
   134  	time.Sleep(10 * time.Millisecond)
   135  }
   136  
   137  func BenchmarkGoroutineIdle(b *testing.B) {
   138  	quit := make(chan struct{})
   139  	fn := func() {
   140  		<-quit
   141  	}
   142  	for i := 0; i < *n; i++ {
   143  		go fn()
   144  	}
   145  
   146  	GC()
   147  	b.ResetTimer()
   148  
   149  	for i := 0; i < b.N; i++ {
   150  		GC()
   151  	}
   152  
   153  	b.StopTimer()
   154  	close(quit)
   155  	time.Sleep(10 * time.Millisecond)
   156  }