github.com/MetalBlockchain/subnet-evm@v0.4.9/metrics/runtime_test.go (about)

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