github.com/rcrowley/go-metrics@v0.0.0-20201227073835-cf1acfcdf475/debug_test.go (about) 1 package metrics 2 3 import ( 4 "runtime" 5 "runtime/debug" 6 "testing" 7 "time" 8 ) 9 10 func BenchmarkDebugGCStats(b *testing.B) { 11 r := NewRegistry() 12 RegisterDebugGCStats(r) 13 b.ResetTimer() 14 for i := 0; i < b.N; i++ { 15 CaptureDebugGCStatsOnce(r) 16 } 17 } 18 19 func TestDebugGCStatsBlocking(t *testing.T) { 20 if g := runtime.GOMAXPROCS(0); g < 2 { 21 t.Skipf("skipping TestDebugGCMemStatsBlocking with GOMAXPROCS=%d\n", g) 22 return 23 } 24 ch := make(chan int) 25 go testDebugGCStatsBlocking(ch) 26 var gcStats debug.GCStats 27 t0 := time.Now() 28 debug.ReadGCStats(&gcStats) 29 t1 := time.Now() 30 t.Log("i++ during debug.ReadGCStats:", <-ch) 31 go testDebugGCStatsBlocking(ch) 32 d := t1.Sub(t0) 33 t.Log(d) 34 time.Sleep(d) 35 t.Log("i++ during time.Sleep:", <-ch) 36 } 37 38 func testDebugGCStatsBlocking(ch chan int) { 39 i := 0 40 for { 41 select { 42 case ch <- i: 43 return 44 default: 45 i++ 46 } 47 } 48 } 49 50 func TestDebugGCStatsDoubleRegister(t *testing.T) { 51 r := NewRegistry() 52 RegisterDebugGCStats(r) 53 var storedGauge = (r.Get("debug.GCStats.LastGC")).(Gauge) 54 55 runtime.GC() 56 CaptureDebugGCStatsOnce(r) 57 58 firstGC := storedGauge.Value() 59 if 0 == firstGC { 60 t.Errorf("firstGC got %d, expected > 0", firstGC) 61 } 62 63 time.Sleep(time.Millisecond) 64 65 RegisterDebugGCStats(r) 66 runtime.GC() 67 CaptureDebugGCStatsOnce(r) 68 if lastGC := storedGauge.Value(); firstGC == lastGC { 69 t.Errorf("lastGC got %d, expected a higher timestamp value", lastGC) 70 } 71 }