Daily bump.
[official-gcc.git] / libgo / go / runtime / malloc_test.go
blob054f6a7d7445a7e98cfd57003e8f07fbe6575585
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.
5 package runtime_test
7 import (
8 "flag"
9 . "runtime"
10 "testing"
11 "time"
12 "unsafe"
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)
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)
29 var mallocSink uintptr
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))
37 mallocSink = x
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))
46 mallocSink = x
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
55 x ^= uintptr(unsafe.Pointer(p))
57 mallocSink = x
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
66 x ^= uintptr(unsafe.Pointer(p))
68 mallocSink = x
71 var n = flag.Int("n", 1000, "number of goroutines")
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
82 case <-quit:
83 return
87 benchHelper(b, *n, read)
90 func BenchmarkGoroutineBlocking(b *testing.B) {
91 read := func(ch chan struct{}) {
92 for {
93 if _, ok := <-ch; !ok {
94 return
98 benchHelper(b, *n, read)
101 func BenchmarkGoroutineForRange(b *testing.B) {
102 read := func(ch chan struct{}) {
103 for _ = range ch {
106 benchHelper(b, *n, read)
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])
115 b.StopTimer()
116 b.ResetTimer()
117 GC()
119 for i := 0; i < b.N; i++ {
120 for _, ch := range m {
121 if ch != nil {
122 ch <- struct{}{}
125 time.Sleep(10 * time.Millisecond)
126 b.StartTimer()
127 GC()
128 b.StopTimer()
131 for _, ch := range m {
132 close(ch)
134 time.Sleep(10 * time.Millisecond)
137 func BenchmarkGoroutineIdle(b *testing.B) {
138 quit := make(chan struct{})
139 fn := func() {
140 <-quit
142 for i := 0; i < *n; i++ {
143 go fn()
146 GC()
147 b.ResetTimer()
149 for i := 0; i < b.N; i++ {
150 GC()
153 b.StopTimer()
154 close(quit)
155 time.Sleep(10 * time.Millisecond)