1 // Copyright 2011 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 TestGcSys(t
*testing
.T
) {
16 /* gccgo does not have a go command
17 if os.Getenv("GOGC") == "off" {
18 t.Skip("skipping test; GOGC=off in environment")
20 data := struct{ Short bool }{testing.Short()}
21 got := executeTest(t, testGCSysSource, &data)
24 t.Fatalf("expected %q, but got %q", want, got)
29 const testGCSysSource
= `
39 memstats := new(runtime.MemStats)
41 runtime.ReadMemStats(memstats)
44 runtime.MemProfileRate = 0 // disable profiler
50 for i := 0; i < itercount; i++ {
54 // Should only be using a few MB.
55 // We allocated 100 MB or (if not short) 1 GB.
56 runtime.ReadMemStats(memstats)
57 if sys > memstats.Sys {
60 sys = memstats.Sys - sys
63 fmt.Printf("using too much memory: %d bytes\n", sys)
69 func workthegc() []byte {
70 return make([]byte, 1029)
74 func TestGcDeepNesting(t
*testing
.T
) {
75 type T
[2][2][2][2][2][2][2][2][2][2]*int
78 // Prevent the compiler from applying escape analysis.
79 // This makes sure new(T) is allocated on heap, not on the stack.
82 a
[0][0][0][0][0][0][0][0][0][0] = new(int)
83 *a
[0][0][0][0][0][0][0][0][0][0] = 13
85 if *a
[0][0][0][0][0][0][0][0][0][0] != 13 {
90 func TestGcHashmapIndirection(t
*testing
.T
) {
91 defer debug
.SetGCPercent(debug
.SetGCPercent(1))
97 for i
:= 0; i
< 2000; i
++ {
104 func TestGcArraySlice(t
*testing
.T
) {
111 for i
:= 0; i
< 10; i
++ {
116 p
.nextbuf
= head
.buf
[:]
121 for p
:= head
; p
!= nil; p
= p
.next
{
123 t
.Fatal("corrupted heap")
128 func TestGcRescan(t
*testing
.T
) {
139 for i
:= 0; i
< 10; i
++ {
141 p
.c
= make(chan error
)
151 for p
:= head
; p
!= nil; p
= p
.nexty
{
153 t
.Fatal("corrupted heap")
158 func TestGcLastTime(t
*testing
.T
) {
159 ms
:= new(runtime
.MemStats
)
160 t0
:= time
.Now().UnixNano()
162 t1
:= time
.Now().UnixNano()
163 runtime
.ReadMemStats(ms
)
164 last
:= int64(ms
.LastGC
)
165 if t0
> last || last
> t1
{
166 t
.Fatalf("bad last GC time: got %v, want [%v, %v]", last
, t0
, t1
)
170 func BenchmarkSetTypeNoPtr1(b
*testing
.B
) {
175 for i
:= 0; i
< b
.N
; i
++ {
180 func BenchmarkSetTypeNoPtr2(b
*testing
.B
) {
185 for i
:= 0; i
< b
.N
; i
++ {
190 func BenchmarkSetTypePtr1(b
*testing
.B
) {
195 for i
:= 0; i
< b
.N
; i
++ {
200 func BenchmarkSetTypePtr2(b
*testing
.B
) {
205 for i
:= 0; i
< b
.N
; i
++ {
211 func BenchmarkAllocation(b
*testing
.B
) {
215 ngo
:= runtime
.GOMAXPROCS(0)
216 work
:= make(chan bool, b
.N
+ngo
)
217 result
:= make(chan *T
)
218 for i
:= 0; i
< b
.N
; i
++ {
221 for i
:= 0; i
< ngo
; i
++ {
224 for i
:= 0; i
< ngo
; i
++ {
228 for i
:= 0; i
< 1000; i
++ {
235 for i
:= 0; i
< ngo
; i
++ {