Debian Codegraph @

prometheus/gauge_test.go

raw
1 // Copyright 2014 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 "math"
18 "math/rand"
19 "sync"
20 "testing"
21 "testing/quick"
22 "time"
23
24 dto "github.com/prometheus/client_model/go"
25 "google.golang.org/protobuf/proto"
26 )
27
28 func listenGaugeStream(vals, result chan float64, done chan struct{}) {
29 var sum float64
30 outer:
31 for {
32 select {
33 case <-done:
34 close(vals)
35 for v := range vals {
36 sum += v
37 }
38 break outer
39 case v := <-vals:
40 sum += v
41 }
42 }
43 result <- sum
44 close(result)
45 }
46
47 func TestGaugeConcurrency(t *testing.T) {
48 it := func(n uint32) bool {
49 mutations := int(n % 10000)
50 concLevel := int(n%15 + 1)
51
52 var start, end sync.WaitGroup
53 start.Add(1)
54 end.Add(concLevel)
55
56 sStream := make(chan float64, mutations*concLevel)
57 result := make(chan float64)
58 done := make(chan struct{})
59
60 go listenGaugeStream(sStream, result, done)
61 go func() {
62 end.Wait()
63 close(done)
64 }()
65
66 gge := NewGauge(GaugeOpts{
67 Name: "test_gauge",
68 Help: "no help can be found here",
69 })
70 for i := 0; i < concLevel; i++ {
71 vals := make([]float64, mutations)
72 for j := 0; j < mutations; j++ {
73 vals[j] = rand.Float64() - 0.5
74 }
75
76 go func(vals []float64) {
77 start.Wait()
78 for _, v := range vals {
79 sStream <- v
80 gge.Add(v)
81 }
82 end.Done()
83 }(vals)
84 }
85 start.Done()
86
87 if expected, got := <-result, math.Float64frombits(gge.(*gauge).valBits); math.Abs(expected-got) > 0.000001 {
88 t.Fatalf("expected approx. %f, got %f", expected, got)
89 return false
90 }
91 return true
92 }
93
94 if err := quick.Check(it, nil); err != nil {
95 t.Fatal(err)
96 }
97 }
98
99 func TestGaugeVecConcurrency(t *testing.T) {
100 it := func(n uint32) bool {
101 mutations := int(n % 10000)
102 concLevel := int(n%15 + 1)
103 vecLength := int(n%5 + 1)
104
105 var start, end sync.WaitGroup
106 start.Add(1)
107 end.Add(concLevel)
108
109 sStreams := make([]chan float64, vecLength)
110 results := make([]chan float64, vecLength)
111 done := make(chan struct{})
112
113 for i := 0; i < vecLength; i++ {
114 sStreams[i] = make(chan float64, mutations*concLevel)
115 results[i] = make(chan float64)
116 go listenGaugeStream(sStreams[i], results[i], done)
117 }
118
119 go func() {
120 end.Wait()
121 close(done)
122 }()
123
124 gge := NewGaugeVec(
125 GaugeOpts{
126 Name: "test_gauge",
127 Help: "no help can be found here",
128 },
129 []string{"label"},
130 )
131 for i := 0; i < concLevel; i++ {
132 vals := make([]float64, mutations)
133 pick := make([]int, mutations)
134 for j := 0; j < mutations; j++ {
135 vals[j] = rand.Float64() - 0.5
136 pick[j] = rand.Intn(vecLength)
137 }
138
139 go func(vals []float64) {
140 start.Wait()
141 for i, v := range vals {
142 sStreams[pick[i]] <- v
143 gge.WithLabelValues(string('A' + rune(pick[i]))).Add(v)
144 }
145 end.Done()
146 }(vals)
147 }
148 start.Done()
149
150 for i := range sStreams {
151 if expected, got := <-results[i], math.Float64frombits(gge.WithLabelValues(string('A'+rune(i))).(*gauge).valBits); math.Abs(expected-got) > 0.000001 {
152 t.Fatalf("expected approx. %f, got %f", expected, got)
153 return false
154 }
155 }
156 return true
157 }
158
159 if err := quick.Check(it, nil); err != nil {
160 t.Fatal(err)
161 }
162 }
163
164 func TestGaugeFunc(t *testing.T) {
165 gf := NewGaugeFunc(
166 GaugeOpts{
167 Name: "test_name",
168 Help: "test help",
169 ConstLabels: Labels{"a": "1", "b": "2"},
170 },
171 func() float64 { return 3.1415 },
172 )
173
174 if expected, got := `Desc{fqName: "test_name", help: "test help", constLabels: {a="1",b="2"}, variableLabels: {}}`, gf.Desc().String(); expected != got {
175 t.Errorf("expected %q, got %q", expected, got)
176 }
177
178 m := &dto.Metric{}
179 gf.Write(m)
180
181 expected := &dto.Metric{
182 Label: []*dto.LabelPair{
183 {Name: proto.String("a"), Value: proto.String("1")},
184 {Name: proto.String("b"), Value: proto.String("2")},
185 },
186 Gauge: &dto.Gauge{
187 Value: proto.Float64(3.1415),
188 },
189 }
190
191 if !proto.Equal(expected, m) {
192 t.Errorf("expected %q, got %q", expected, m)
193 }
194 }
195
196 func TestGaugeSetCurrentTime(t *testing.T) {
197 g := NewGauge(GaugeOpts{
198 Name: "test_name",
199 Help: "test help",
200 })
201 g.SetToCurrentTime()
202 unixTime := float64(time.Now().Unix())
203
204 m := &dto.Metric{}
205 g.Write(m)
206
207 delta := unixTime - m.GetGauge().GetValue()
208 // This is just a smoke test to make sure SetToCurrentTime is not
209 // totally off. Tests with current time involved are hard...
210 if math.Abs(delta) > 5 {
211 t.Errorf("Gauge set to current time deviates from current time by more than 5s, delta is %f seconds", delta)
212 }
213 }