github.com/aigarnetwork/aigar@v0.0.0-20191115204914-d59a6eb70f8e/metrics/runtime_test.go (about)

     1  //  Copyright 2018 The go-ethereum Authors
     2  //  Copyright 2019 The go-aigar Authors
     3  //  This file is part of the go-aigar library.
     4  //
     5  //  The go-aigar library is free software: you can redistribute it and/or modify
     6  //  it under the terms of the GNU Lesser General Public License as published by
     7  //  the Free Software Foundation, either version 3 of the License, or
     8  //  (at your option) any later version.
     9  //
    10  //  The go-aigar library is distributed in the hope that it will be useful,
    11  //  but WITHOUT ANY WARRANTY; without even the implied warranty of
    12  //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    13  //  GNU Lesser General Public License for more details.
    14  //
    15  //  You should have received a copy of the GNU Lesser General Public License
    16  //  along with the go-aigar library. If not, see <http://www.gnu.org/licenses/>.
    17  
    18  package metrics
    19  
    20  import (
    21  	"runtime"
    22  	"testing"
    23  	"time"
    24  )
    25  
    26  func BenchmarkRuntimeMemStats(b *testing.B) {
    27  	r := NewRegistry()
    28  	RegisterRuntimeMemStats(r)
    29  	b.ResetTimer()
    30  	for i := 0; i < b.N; i++ {
    31  		CaptureRuntimeMemStatsOnce(r)
    32  	}
    33  }
    34  
    35  func TestRuntimeMemStats(t *testing.T) {
    36  	r := NewRegistry()
    37  	RegisterRuntimeMemStats(r)
    38  	CaptureRuntimeMemStatsOnce(r)
    39  	zero := runtimeMetrics.MemStats.PauseNs.Count() // Get a "zero" since GC may have run before these tests.
    40  	runtime.GC()
    41  	CaptureRuntimeMemStatsOnce(r)
    42  	if count := runtimeMetrics.MemStats.PauseNs.Count(); 1 != count-zero {
    43  		t.Fatal(count - zero)
    44  	}
    45  	runtime.GC()
    46  	runtime.GC()
    47  	CaptureRuntimeMemStatsOnce(r)
    48  	if count := runtimeMetrics.MemStats.PauseNs.Count(); 3 != count-zero {
    49  		t.Fatal(count - zero)
    50  	}
    51  	for i := 0; i < 256; i++ {
    52  		runtime.GC()
    53  	}
    54  	CaptureRuntimeMemStatsOnce(r)
    55  	if count := runtimeMetrics.MemStats.PauseNs.Count(); 259 != count-zero {
    56  		t.Fatal(count - zero)
    57  	}
    58  	for i := 0; i < 257; i++ {
    59  		runtime.GC()
    60  	}
    61  	CaptureRuntimeMemStatsOnce(r)
    62  	if count := runtimeMetrics.MemStats.PauseNs.Count(); 515 != count-zero { // We lost one because there were too many GCs between captures.
    63  		t.Fatal(count - zero)
    64  	}
    65  }
    66  
    67  func TestRuntimeMemStatsNumThread(t *testing.T) {
    68  	r := NewRegistry()
    69  	RegisterRuntimeMemStats(r)
    70  	CaptureRuntimeMemStatsOnce(r)
    71  
    72  	if value := runtimeMetrics.NumThread.Value(); value < 1 {
    73  		t.Fatalf("got NumThread: %d, wanted at least 1", value)
    74  	}
    75  }
    76  
    77  func TestRuntimeMemStatsBlocking(t *testing.T) {
    78  	if g := runtime.GOMAXPROCS(0); g < 2 {
    79  		t.Skipf("skipping TestRuntimeMemStatsBlocking with GOMAXPROCS=%d\n", g)
    80  	}
    81  	ch := make(chan int)
    82  	go testRuntimeMemStatsBlocking(ch)
    83  	var memStats runtime.MemStats
    84  	t0 := time.Now()
    85  	runtime.ReadMemStats(&memStats)
    86  	t1 := time.Now()
    87  	t.Log("i++ during runtime.ReadMemStats:", <-ch)
    88  	go testRuntimeMemStatsBlocking(ch)
    89  	d := t1.Sub(t0)
    90  	t.Log(d)
    91  	time.Sleep(d)
    92  	t.Log("i++ during time.Sleep:", <-ch)
    93  }
    94  
    95  func testRuntimeMemStatsBlocking(ch chan int) {
    96  	i := 0
    97  	for {
    98  		select {
    99  		case ch <- i:
   100  			return
   101  		default:
   102  			i++
   103  		}
   104  	}
   105  }