github.com/varialus/godfly@v0.0.0-20130904042352-1934f9f095ab/src/pkg/runtime/debug/garbage_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 debug 6 7 import ( 8 "runtime" 9 "testing" 10 "time" 11 ) 12 13 func TestReadGCStats(t *testing.T) { 14 defer SetGCPercent(SetGCPercent(-1)) 15 16 var stats GCStats 17 var mstats runtime.MemStats 18 var min, max time.Duration 19 20 // First ReadGCStats will allocate, second should not, 21 // especially if we follow up with an explicit garbage collection. 22 stats.PauseQuantiles = make([]time.Duration, 10) 23 ReadGCStats(&stats) 24 runtime.GC() 25 26 // Assume these will return same data: no GC during ReadGCStats. 27 ReadGCStats(&stats) 28 runtime.ReadMemStats(&mstats) 29 30 if stats.NumGC != int64(mstats.NumGC) { 31 t.Errorf("stats.NumGC = %d, but mstats.NumGC = %d", stats.NumGC, mstats.NumGC) 32 } 33 if stats.PauseTotal != time.Duration(mstats.PauseTotalNs) { 34 t.Errorf("stats.PauseTotal = %d, but mstats.PauseTotalNs = %d", stats.PauseTotal, mstats.PauseTotalNs) 35 } 36 if stats.LastGC.UnixNano() != int64(mstats.LastGC) { 37 t.Errorf("stats.LastGC.UnixNano = %d, but mstats.LastGC = %d", stats.LastGC.UnixNano(), mstats.LastGC) 38 } 39 n := int(mstats.NumGC) 40 if n > len(mstats.PauseNs) { 41 n = len(mstats.PauseNs) 42 } 43 if len(stats.Pause) != n { 44 t.Errorf("len(stats.Pause) = %d, want %d", len(stats.Pause), n) 45 } else { 46 off := (int(mstats.NumGC) + len(mstats.PauseNs) - 1) % len(mstats.PauseNs) 47 for i := 0; i < n; i++ { 48 dt := stats.Pause[i] 49 if dt != time.Duration(mstats.PauseNs[off]) { 50 t.Errorf("stats.Pause[%d] = %d, want %d", i, dt, mstats.PauseNs[off]) 51 } 52 if max < dt { 53 max = dt 54 } 55 if min > dt || i == 0 { 56 min = dt 57 } 58 off = (off + len(mstats.PauseNs) - 1) % len(mstats.PauseNs) 59 } 60 } 61 62 q := stats.PauseQuantiles 63 nq := len(q) 64 if q[0] != min || q[nq-1] != max { 65 t.Errorf("stats.PauseQuantiles = [%d, ..., %d], want [%d, ..., %d]", q[0], q[nq-1], min, max) 66 } 67 68 for i := 0; i < nq-1; i++ { 69 if q[i] > q[i+1] { 70 t.Errorf("stats.PauseQuantiles[%d]=%d > stats.PauseQuantiles[%d]=%d", i, q[i], i+1, q[i+1]) 71 } 72 } 73 } 74 75 var big = make([]byte, 1<<20) 76 77 func TestFreeOSMemory(t *testing.T) { 78 var ms1, ms2 runtime.MemStats 79 80 if big == nil { 81 t.Skip("test is not reliable when run multiple times") 82 } 83 big = nil 84 runtime.GC() 85 runtime.ReadMemStats(&ms1) 86 FreeOSMemory() 87 runtime.ReadMemStats(&ms2) 88 if ms1.HeapReleased >= ms2.HeapReleased { 89 t.Errorf("released before=%d; released after=%d; did not go up", ms1.HeapReleased, ms2.HeapReleased) 90 } 91 } 92 93 func TestSetGCPercent(t *testing.T) { 94 // Test that the variable is being set and returned correctly. 95 // Assume the percentage itself is implemented fine during GC, 96 // which is harder to test. 97 old := SetGCPercent(123) 98 new := SetGCPercent(old) 99 if new != 123 { 100 t.Errorf("SetGCPercent(123); SetGCPercent(x) = %d, want 123", new) 101 } 102 }