Debian Codegraph @

runtime_test.go

raw symbols SCIP
1 package metrics
2
3 import (
4 "runtime"
5 "testing"
6 "time"
7 )
8
9 func BenchmarkRuntimeMemStats(b *testing.B) {
10 r := NewRegistry()
11 RegisterRuntimeMemStats(r)
12 b.ResetTimer()
13 for i := 0; i < b.N; i++ {
14 CaptureRuntimeMemStatsOnce(r)
15 }
16 }
17
18 func TestRuntimeMemStats(t *testing.T) {
19 r := NewRegistry()
20 RegisterRuntimeMemStats(r)
21 CaptureRuntimeMemStatsOnce(r)
22 zero := runtimeMetrics.MemStats.PauseNs.Count() // Get a "zero" since GC may have run before these tests.
23 runtime.GC()
24 CaptureRuntimeMemStatsOnce(r)
25 if count := runtimeMetrics.MemStats.PauseNs.Count(); 1 != count-zero {
26 t.Fatal(count - zero)
27 }
28 runtime.GC()
29 runtime.GC()
30 CaptureRuntimeMemStatsOnce(r)
31 if count := runtimeMetrics.MemStats.PauseNs.Count(); 3 != count-zero {
32 t.Fatal(count - zero)
33 }
34 for i := 0; i < 256; i++ {
35 runtime.GC()
36 }
37 CaptureRuntimeMemStatsOnce(r)
38 if count := runtimeMetrics.MemStats.PauseNs.Count(); 259 != count-zero {
39 t.Fatal(count - zero)
40 }
41 for i := 0; i < 257; i++ {
42 runtime.GC()
43 }
44 CaptureRuntimeMemStatsOnce(r)
45 if count := runtimeMetrics.MemStats.PauseNs.Count(); 515 != count-zero { // We lost one because there were too many GCs between captures.
46 t.Fatal(count - zero)
47 }
48 }
49
50 func TestRuntimeMemStatsBlocking(t *testing.T) {
51 if g := runtime.GOMAXPROCS(0); g < 2 {
52 t.Skipf("skipping TestRuntimeMemStatsBlocking with GOMAXPROCS=%d\n", g)
53 }
54 ch := make(chan int)
55 go testRuntimeMemStatsBlocking(ch)
56 var memStats runtime.MemStats
57 t0 := time.Now()
58 runtime.ReadMemStats(&memStats)
59 t1 := time.Now()
60 t.Log("i++ during runtime.ReadMemStats:", <-ch)
61 go testRuntimeMemStatsBlocking(ch)
62 d := t1.Sub(t0)
63 t.Log(d)
64 time.Sleep(d)
65 t.Log("i++ during time.Sleep:", <-ch)
66 }
67
68 func testRuntimeMemStatsBlocking(ch chan int) {
69 i := 0
70 for {
71 select {
72 case ch <- i:
73 return
74 default:
75 i++
76 }
77 }
78 }