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.
15 func TestMemStats(t
*testing
.T
) {
16 // Test that MemStats has sane values.
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
) {
33 for i
:= 0; i
< b
.N
; i
++ {
35 x
^= uintptr(unsafe
.Pointer(p
))
40 func BenchmarkMalloc16(b
*testing
.B
) {
42 for i
:= 0; i
< b
.N
; i
++ {
44 x
^= uintptr(unsafe
.Pointer(p
))
49 func BenchmarkMallocTypeInfo8(b
*testing
.B
) {
51 for i
:= 0; i
< b
.N
; i
++ {
53 p
[8 / unsafe
.Sizeof(uintptr(0))]*int
55 x
^= uintptr(unsafe
.Pointer(p
))
60 func BenchmarkMallocTypeInfo16(b
*testing
.B
) {
62 for i
:= 0; i
< b
.N
; i
++ {
64 p
[16 / unsafe
.Sizeof(uintptr(0))]*int
66 x
^= uintptr(unsafe
.Pointer(p
))
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{}) {
87 benchHelper(b
, *n
, read
)
90 func BenchmarkGoroutineBlocking(b
*testing
.B
) {
91 read
:= func(ch
chan struct{}) {
93 if _
, ok
:= <-ch
; !ok
{
98 benchHelper(b
, *n
, read
)
101 func BenchmarkGoroutineForRange(b
*testing
.B
) {
102 read
:= func(ch
chan struct{}) {
106 benchHelper(b
, *n
, read
)
109 func benchHelper(b
*testing
.B
, n
int, read
func(chan struct{})) {
110 m
:= make([]chan struct{}, n
)
112 m
[i
] = make(chan struct{}, 1)
119 for i
:= 0; i
< b
.N
; i
++ {
120 for _
, ch
:= range m
{
125 time
.Sleep(10 * time
.Millisecond
)
131 for _
, ch
:= range m
{
134 time
.Sleep(10 * time
.Millisecond
)
137 func BenchmarkGoroutineIdle(b
*testing
.B
) {
138 quit
:= make(chan struct{})
142 for i
:= 0; i
< *n
; i
++ {
149 for i
:= 0; i
< b
.N
; i
++ {
155 time
.Sleep(10 * time
.Millisecond
)