debug_test.go 849 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  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. }