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.
13 func TestReadGCStats(t
*testing
.T
) {
14 defer SetGCPercent(SetGCPercent(-1))
17 var mstats runtime
.MemStats
18 var min
, max time
.Duration
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)
26 // Assume these will return same data: no GC during ReadGCStats.
28 runtime
.ReadMemStats(&mstats
)
30 if stats
.NumGC
!= int64(mstats
.NumGC
) {
31 t
.Errorf("stats.NumGC = %d, but mstats.NumGC = %d", stats
.NumGC
, mstats
.NumGC
)
33 if stats
.PauseTotal
!= time
.Duration(mstats
.PauseTotalNs
) {
34 t
.Errorf("stats.PauseTotal = %d, but mstats.PauseTotalNs = %d", stats
.PauseTotal
, mstats
.PauseTotalNs
)
36 if stats
.LastGC
.UnixNano() != int64(mstats
.LastGC
) {
37 t
.Errorf("stats.LastGC.UnixNano = %d, but mstats.LastGC = %d", stats
.LastGC
.UnixNano(), mstats
.LastGC
)
39 n
:= int(mstats
.NumGC
)
40 if n
> len(mstats
.PauseNs
) {
41 n
= len(mstats
.PauseNs
)
43 if len(stats
.Pause
) != n
{
44 t
.Errorf("len(stats.Pause) = %d, want %d", len(stats
.Pause
), n
)
46 off
:= (int(mstats
.NumGC
) + len(mstats
.PauseNs
) - 1) % len(mstats
.PauseNs
)
47 for i
:= 0; i
< n
; i
++ {
49 if dt
!= time
.Duration(mstats
.PauseNs
[off
]) {
50 t
.Errorf("stats.Pause[%d] = %d, want %d", i
, dt
, mstats
.PauseNs
[off
])
55 if min
> dt || i
== 0 {
58 off
= (off
+ len(mstats
.PauseNs
) - 1) % len(mstats
.PauseNs
)
62 q
:= stats
.PauseQuantiles
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
)
68 for i
:= 0; i
< nq
-1; i
++ {
70 t
.Errorf("stats.PauseQuantiles[%d]=%d > stats.PauseQuantiles[%d]=%d", i
, q
[i
], i
+1, q
[i
+1])
75 var big
= make([]byte, 1<<20)
77 func TestFreeOSMemory(t
*testing
.T
) {
78 var ms1
, ms2 runtime
.MemStats
81 t
.Skip("test is not reliable when run multiple times")
85 runtime
.ReadMemStats(&ms1
)
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
)
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
)
100 t
.Errorf("SetGCPercent(123); SetGCPercent(x) = %d, want 123", new)