runtime_test.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. package metrics
  2. import (
  3. "runtime"
  4. "testing"
  5. "time"
  6. )
  7. func TestRuntimeMemStatsDoubleRegister(t *testing.T) {
  8. r := NewRegistry()
  9. RegisterRuntimeMemStats(r)
  10. storedGauge := r.Get("runtime.MemStats.LastGC").(Gauge)
  11. runtime.GC()
  12. CaptureRuntimeMemStatsOnce(r)
  13. firstGC := storedGauge.Value()
  14. if 0 == firstGC {
  15. t.Errorf("firstGC got %d, expected timestamp > 0", firstGC)
  16. }
  17. time.Sleep(time.Millisecond)
  18. RegisterRuntimeMemStats(r)
  19. runtime.GC()
  20. CaptureRuntimeMemStatsOnce(r)
  21. if lastGC := storedGauge.Value(); firstGC == lastGC {
  22. t.Errorf("lastGC got %d, expected a higher timestamp value", lastGC)
  23. }
  24. }
  25. func BenchmarkRuntimeMemStats(b *testing.B) {
  26. r := NewRegistry()
  27. RegisterRuntimeMemStats(r)
  28. b.ResetTimer()
  29. for i := 0; i < b.N; i++ {
  30. CaptureRuntimeMemStatsOnce(r)
  31. }
  32. }
  33. func TestRuntimeMemStats(t *testing.T) {
  34. r := NewRegistry()
  35. RegisterRuntimeMemStats(r)
  36. CaptureRuntimeMemStatsOnce(r)
  37. zero := runtimeMetrics.MemStats.PauseNs.Count() // Get a "zero" since GC may have run before these tests.
  38. runtime.GC()
  39. CaptureRuntimeMemStatsOnce(r)
  40. if count := runtimeMetrics.MemStats.PauseNs.Count(); 1 != count-zero {
  41. t.Fatal(count - zero)
  42. }
  43. runtime.GC()
  44. runtime.GC()
  45. CaptureRuntimeMemStatsOnce(r)
  46. if count := runtimeMetrics.MemStats.PauseNs.Count(); 3 != count-zero {
  47. t.Fatal(count - zero)
  48. }
  49. for i := 0; i < 256; i++ {
  50. runtime.GC()
  51. }
  52. CaptureRuntimeMemStatsOnce(r)
  53. if count := runtimeMetrics.MemStats.PauseNs.Count(); 259 != count-zero {
  54. t.Fatal(count - zero)
  55. }
  56. for i := 0; i < 257; i++ {
  57. runtime.GC()
  58. }
  59. CaptureRuntimeMemStatsOnce(r)
  60. if count := runtimeMetrics.MemStats.PauseNs.Count(); 515 != count-zero { // We lost one because there were too many GCs between captures.
  61. t.Fatal(count - zero)
  62. }
  63. }
  64. func TestRuntimeMemStatsNumThread(t *testing.T) {
  65. r := NewRegistry()
  66. RegisterRuntimeMemStats(r)
  67. CaptureRuntimeMemStatsOnce(r)
  68. if value := runtimeMetrics.NumThread.Value(); value < 1 {
  69. t.Fatalf("got NumThread: %d, wanted at least 1", value)
  70. }
  71. }
  72. func TestRuntimeMemStatsBlocking(t *testing.T) {
  73. if g := runtime.GOMAXPROCS(0); g < 2 {
  74. t.Skipf("skipping TestRuntimeMemStatsBlocking with GOMAXPROCS=%d\n", g)
  75. }
  76. ch := make(chan int)
  77. go testRuntimeMemStatsBlocking(ch)
  78. var memStats runtime.MemStats
  79. t0 := time.Now()
  80. runtime.ReadMemStats(&memStats)
  81. t1 := time.Now()
  82. t.Log("i++ during runtime.ReadMemStats:", <-ch)
  83. go testRuntimeMemStatsBlocking(ch)
  84. d := t1.Sub(t0)
  85. t.Log(d)
  86. time.Sleep(d)
  87. t.Log("i++ during time.Sleep:", <-ch)
  88. }
  89. func testRuntimeMemStatsBlocking(ch chan int) {
  90. i := 0
  91. for {
  92. select {
  93. case ch <- i:
  94. return
  95. default:
  96. i++
  97. }
  98. }
  99. }