github.com/yinchengtsinghua/golang-Eos-dpos-Ethereum@v0.0.0-20190121132951-92cc4225ed8e/metrics/debug_test.go (about)

     1  
     2  //此源码被清华学神尹成大魔王专业翻译分析并修改
     3  //尹成QQ77025077
     4  //尹成微信18510341407
     5  //尹成所在QQ群721929980
     6  //尹成邮箱 yinc13@mails.tsinghua.edu.cn
     7  //尹成毕业于清华大学,微软区块链领域全球最有价值专家
     8  //https://mvp.microsoft.com/zh-cn/PublicProfile/4033620
     9  package metrics
    10  
    11  import (
    12  	"runtime"
    13  	"runtime/debug"
    14  	"testing"
    15  	"time"
    16  )
    17  
    18  func BenchmarkDebugGCStats(b *testing.B) {
    19  	r := NewRegistry()
    20  	RegisterDebugGCStats(r)
    21  	b.ResetTimer()
    22  	for i := 0; i < b.N; i++ {
    23  		CaptureDebugGCStatsOnce(r)
    24  	}
    25  }
    26  
    27  func TestDebugGCStatsBlocking(t *testing.T) {
    28  	if g := runtime.GOMAXPROCS(0); g < 2 {
    29  		t.Skipf("skipping TestDebugGCMemStatsBlocking with GOMAXPROCS=%d\n", g)
    30  		return
    31  	}
    32  	ch := make(chan int)
    33  	go testDebugGCStatsBlocking(ch)
    34  	var gcStats debug.GCStats
    35  	t0 := time.Now()
    36  	debug.ReadGCStats(&gcStats)
    37  	t1 := time.Now()
    38  	t.Log("i++ during debug.ReadGCStats:", <-ch)
    39  	go testDebugGCStatsBlocking(ch)
    40  	d := t1.Sub(t0)
    41  	t.Log(d)
    42  	time.Sleep(d)
    43  	t.Log("i++ during time.Sleep:", <-ch)
    44  }
    45  
    46  func testDebugGCStatsBlocking(ch chan int) {
    47  	i := 0
    48  	for {
    49  		select {
    50  		case ch <- i:
    51  			return
    52  		default:
    53  			i++
    54  		}
    55  	}
    56  }