...

Source file src/github.com/rcrowley/go-metrics/runtime_test.go

Documentation: github.com/rcrowley/go-metrics

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

View as plain text