github.com/neatio-net/neatio@v1.7.3-0.20231114194659-f4d7a2226baa/utilities/metrics/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  }