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
6
// http://www.apache.org/licenses/LICENSE-2.0
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.
23
dto "github.com/prometheus/client_model/go"
26
func listenGaugeStream(vals, result chan float64, done chan struct{}) {
45
func TestGaugeConcurrency(t *testing.T) {
46
it := func(n uint32) bool {
47
mutations := int(n % 10000)
48
concLevel := int(n%15 + 1)
50
var start, end sync.WaitGroup
54
sStream := make(chan float64, mutations*concLevel)
55
result := make(chan float64)
56
done := make(chan struct{})
58
go listenGaugeStream(sStream, result, done)
64
gge := NewGauge(GaugeOpts{
66
Help: "no help can be found here",
68
for i := 0; i < concLevel; i++ {
69
vals := make([]float64, mutations)
70
for j := 0; j < mutations; j++ {
71
vals[j] = rand.Float64() - 0.5
74
go func(vals []float64) {
76
for _, v := range vals {
85
if expected, got := <-result, math.Float64frombits(gge.(*value).valBits); math.Abs(expected-got) > 0.000001 {
86
t.Fatalf("expected approx. %f, got %f", expected, got)
92
if err := quick.Check(it, nil); err != nil {
97
func TestGaugeVecConcurrency(t *testing.T) {
98
it := func(n uint32) bool {
99
mutations := int(n % 10000)
100
concLevel := int(n%15 + 1)
101
vecLength := int(n%5 + 1)
103
var start, end sync.WaitGroup
107
sStreams := make([]chan float64, vecLength)
108
results := make([]chan float64, vecLength)
109
done := make(chan struct{})
111
for i := 0; i < vecLength; i++ {
112
sStreams[i] = make(chan float64, mutations*concLevel)
113
results[i] = make(chan float64)
114
go listenGaugeStream(sStreams[i], results[i], done)
125
Help: "no help can be found here",
129
for i := 0; i < concLevel; i++ {
130
vals := make([]float64, mutations)
131
pick := make([]int, mutations)
132
for j := 0; j < mutations; j++ {
133
vals[j] = rand.Float64() - 0.5
134
pick[j] = rand.Intn(vecLength)
137
go func(vals []float64) {
139
for i, v := range vals {
140
sStreams[pick[i]] <- v
141
gge.WithLabelValues(string('A' + pick[i])).Add(v)
148
for i := range sStreams {
149
if expected, got := <-results[i], math.Float64frombits(gge.WithLabelValues(string('A'+i)).(*value).valBits); math.Abs(expected-got) > 0.000001 {
150
t.Fatalf("expected approx. %f, got %f", expected, got)
157
if err := quick.Check(it, nil); err != nil {
162
func TestGaugeFunc(t *testing.T) {
167
ConstLabels: Labels{"a": "1", "b": "2"},
169
func() float64 { return 3.1415 },
172
if expected, got := `Desc{fqName: "test_name", help: "test help", constLabels: {a="1",b="2"}, variableLabels: []}`, gf.Desc().String(); expected != got {
173
t.Errorf("expected %q, got %q", expected, got)
179
if expected, got := `label:<name:"a" value:"1" > label:<name:"b" value:"2" > gauge:<value:3.1415 > `, m.String(); expected != got {
180
t.Errorf("expected %q, got %q", expected, got)