debug_test.go 1.4 KB

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