gitlab.com/aquachain/aquachain@v1.17.16-rc3.0.20221018032414-e3ddf1e1c055/common/metrics/runtime_test.go (about)

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