debug_test.go 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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. zero := debugMetrics.GCStats.NumGC.Value() // Get a "zero" since GC may have run before these tests.
  49. runtime.GC()
  50. CaptureDebugGCStatsOnce(r)
  51. if numGC := debugMetrics.GCStats.NumGC.Value(); 1 != numGC - zero {
  52. t.Errorf("NumGC got %d, expected 1", numGC)
  53. }
  54. RegisterDebugGCStats(r)
  55. if numGC := debugMetrics.GCStats.NumGC.Value(); 1 != numGC - zero {
  56. t.Errorf("NumGC got %d, expected 1", numGC - zero)
  57. }
  58. }