Debian Codegraph @

prometheus/go_collector_test.go

raw
1 // Copyright 2018 The Prometheus Authors
2 // Licensed under the Apache License, Version 2.0 (the "License");
3 // you may not use this file except in compliance with the License.
4 // You may obtain a copy of the License at
5 //
6 // http://www.apache.org/licenses/LICENSE-2.0
7 //
8 // Unless required by applicable law or agreed to in writing, software
9 // distributed under the License is distributed on an "AS IS" BASIS,
10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 // See the License for the specific language governing permissions and
12 // limitations under the License.
13
14 package prometheus
15
16 import (
17 "runtime"
18 "testing"
19 "time"
20
21 dto "github.com/prometheus/client_model/go"
22 )
23
24 func TestGoCollectorGoroutines(t *testing.T) {
25 var (
26 c = NewGoCollector()
27 metricCh = make(chan Metric)
28 waitCh = make(chan struct{})
29 endGoroutineCh = make(chan struct{})
30 endCollectionCh = make(chan struct{})
31 old = -1
32 )
33 defer func() {
34 close(endGoroutineCh)
35 // Drain the collect channel to prevent goroutine leak.
36 for {
37 select {
38 case <-metricCh:
39 case <-endCollectionCh:
40 return
41 }
42 }
43 }()
44
45 go func() {
46 c.Collect(metricCh)
47 for i := 1; i <= 10; i++ {
48 // Start 10 goroutines to be sure we'll detect an
49 // increase even if unrelated goroutines happen to
50 // terminate during this test.
51 go func(c <-chan struct{}) {
52 <-c
53 }(endGoroutineCh)
54 }
55 <-waitCh
56 c.Collect(metricCh)
57 close(endCollectionCh)
58 }()
59
60 for {
61 select {
62 case m := <-metricCh:
63 // m can be Gauge or Counter,
64 // currently just test the go_goroutines Gauge
65 // and ignore others.
66 if m.Desc().fqName != "go_goroutines" {
67 continue
68 }
69 pb := &dto.Metric{}
70 m.Write(pb)
71 if pb.GetGauge() == nil {
72 continue
73 }
74
75 if old == -1 {
76 old = int(pb.GetGauge().GetValue())
77 close(waitCh)
78 continue
79 }
80
81 if diff := old - int(pb.GetGauge().GetValue()); diff > -1 {
82 t.Errorf("want at least one new goroutine, got %d fewer", diff)
83 }
84 case <-time.After(1 * time.Second):
85 t.Fatalf("expected collect timed out")
86 }
87 break
88 }
89 }
90
91 func TestGoCollectorGC(t *testing.T) {
92 var (
93 c = NewGoCollector()
94 metricCh = make(chan Metric)
95 waitCh = make(chan struct{})
96 endCollectionCh = make(chan struct{})
97 oldGC uint64
98 oldPause float64
99 )
100
101 go func() {
102 c.Collect(metricCh)
103 // force GC
104 runtime.GC()
105 <-waitCh
106 c.Collect(metricCh)
107 close(endCollectionCh)
108 }()
109
110 defer func() {
111 // Drain the collect channel to prevent goroutine leak.
112 for {
113 select {
114 case <-metricCh:
115 case <-endCollectionCh:
116 return
117 }
118 }
119 }()
120
121 first := true
122 for {
123 select {
124 case metric := <-metricCh:
125 pb := &dto.Metric{}
126 metric.Write(pb)
127 if pb.GetSummary() == nil {
128 continue
129 }
130 if len(pb.GetSummary().Quantile) != 5 {
131 t.Errorf("expected 4 buckets, got %d", len(pb.GetSummary().Quantile))
132 }
133 for idx, want := range []float64{0.0, 0.25, 0.5, 0.75, 1.0} {
134 if *pb.GetSummary().Quantile[idx].Quantile != want {
135 t.Errorf("bucket #%d is off, got %f, want %f", idx, *pb.GetSummary().Quantile[idx].Quantile, want)
136 }
137 }
138 if first {
139 first = false
140 oldGC = *pb.GetSummary().SampleCount
141 oldPause = *pb.GetSummary().SampleSum
142 close(waitCh)
143 continue
144 }
145 if diff := *pb.GetSummary().SampleCount - oldGC; diff < 1 {
146 t.Errorf("want at least 1 new garbage collection run, got %d", diff)
147 }
148 if diff := *pb.GetSummary().SampleSum - oldPause; diff <= 0 {
149 t.Errorf("want an increase in pause time, got a change of %f", diff)
150 }
151 case <-time.After(1 * time.Second):
152 t.Fatalf("expected collect timed out")
153 }
154 break
155 }
156 }
157
158 func BenchmarkGoCollector(b *testing.B) {
159 c := NewGoCollector().(*goCollector)
160
161 b.ResetTimer()
162 for i := 0; i < b.N; i++ {
163 ch := make(chan Metric, 8)
164 go func() {
165 // Drain all metrics received until the
166 // channel is closed.
167 for range ch {
168 }
169 }()
170 c.Collect(ch)
171 close(ch)
172 }
173 }