github.com/linapex/ethereum-dpos-chinese@v0.0.0-20190316121959-b78b3a4a1ece/metrics/debug_test.go (about)

     1  
     2  //<developer>
     3  //    <name>linapex 曹一峰</name>
     4  //    <email>linapex@163.com</email>
     5  //    <wx>superexc</wx>
     6  //    <qqgroup>128148617</qqgroup>
     7  //    <url>https://jsq.ink</url>
     8  //    <role>pku engineer</role>
     9  //    <date>2019-03-16 12:09:41</date>
    10  //</624342647875571712>
    11  
    12  package metrics
    13  
    14  import (
    15  	"runtime"
    16  	"runtime/debug"
    17  	"testing"
    18  	"time"
    19  )
    20  
    21  func BenchmarkDebugGCStats(b *testing.B) {
    22  	r := NewRegistry()
    23  	RegisterDebugGCStats(r)
    24  	b.ResetTimer()
    25  	for i := 0; i < b.N; i++ {
    26  		CaptureDebugGCStatsOnce(r)
    27  	}
    28  }
    29  
    30  func TestDebugGCStatsBlocking(t *testing.T) {
    31  	if g := runtime.GOMAXPROCS(0); g < 2 {
    32  		t.Skipf("skipping TestDebugGCMemStatsBlocking with GOMAXPROCS=%d\n", g)
    33  		return
    34  	}
    35  	ch := make(chan int)
    36  	go testDebugGCStatsBlocking(ch)
    37  	var gcStats debug.GCStats
    38  	t0 := time.Now()
    39  	debug.ReadGCStats(&gcStats)
    40  	t1 := time.Now()
    41  	t.Log("i++ during debug.ReadGCStats:", <-ch)
    42  	go testDebugGCStatsBlocking(ch)
    43  	d := t1.Sub(t0)
    44  	t.Log(d)
    45  	time.Sleep(d)
    46  	t.Log("i++ during time.Sleep:", <-ch)
    47  }
    48  
    49  func testDebugGCStatsBlocking(ch chan int) {
    50  	i := 0
    51  	for {
    52  		select {
    53  		case ch <- i:
    54  			return
    55  		default:
    56  			i++
    57  		}
    58  	}
    59  }
    60