2015-02-02 17:14:36 +03:00
|
|
|
// Copyright 2014 The Prometheus Authors
|
2014-05-07 22:08:33 +04:00
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
2013-02-12 05:36:06 +04:00
|
|
|
//
|
2014-05-07 22:08:33 +04:00
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
2012-05-20 01:59:25 +04:00
|
|
|
|
2013-04-03 20:33:32 +04:00
|
|
|
package prometheus
|
2012-05-20 01:59:25 +04:00
|
|
|
|
|
|
|
import (
|
2014-05-07 22:08:33 +04:00
|
|
|
"math"
|
|
|
|
"math/rand"
|
|
|
|
"sync"
|
2013-01-19 17:48:30 +04:00
|
|
|
"testing"
|
2014-05-07 22:08:33 +04:00
|
|
|
"testing/quick"
|
2016-11-18 18:29:59 +03:00
|
|
|
"time"
|
2014-06-23 16:15:35 +04:00
|
|
|
|
2015-02-27 18:12:59 +03:00
|
|
|
dto "github.com/prometheus/client_model/go"
|
2023-08-11 22:17:55 +03:00
|
|
|
"google.golang.org/protobuf/proto"
|
2012-05-20 01:59:25 +04:00
|
|
|
)
|
|
|
|
|
2014-05-07 22:08:33 +04:00
|
|
|
func listenGaugeStream(vals, result chan float64, done chan struct{}) {
|
|
|
|
var sum float64
|
|
|
|
outer:
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-done:
|
|
|
|
close(vals)
|
|
|
|
for v := range vals {
|
|
|
|
sum += v
|
|
|
|
}
|
|
|
|
break outer
|
|
|
|
case v := <-vals:
|
|
|
|
sum += v
|
|
|
|
}
|
2013-01-19 17:48:30 +04:00
|
|
|
}
|
2014-05-07 22:08:33 +04:00
|
|
|
result <- sum
|
|
|
|
close(result)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGaugeConcurrency(t *testing.T) {
|
|
|
|
it := func(n uint32) bool {
|
|
|
|
mutations := int(n % 10000)
|
|
|
|
concLevel := int(n%15 + 1)
|
|
|
|
|
|
|
|
var start, end sync.WaitGroup
|
|
|
|
start.Add(1)
|
|
|
|
end.Add(concLevel)
|
|
|
|
|
|
|
|
sStream := make(chan float64, mutations*concLevel)
|
|
|
|
result := make(chan float64)
|
|
|
|
done := make(chan struct{})
|
|
|
|
|
|
|
|
go listenGaugeStream(sStream, result, done)
|
|
|
|
go func() {
|
|
|
|
end.Wait()
|
|
|
|
close(done)
|
|
|
|
}()
|
|
|
|
|
|
|
|
gge := NewGauge(GaugeOpts{
|
|
|
|
Name: "test_gauge",
|
|
|
|
Help: "no help can be found here",
|
|
|
|
})
|
|
|
|
for i := 0; i < concLevel; i++ {
|
|
|
|
vals := make([]float64, mutations)
|
|
|
|
for j := 0; j < mutations; j++ {
|
|
|
|
vals[j] = rand.Float64() - 0.5
|
|
|
|
}
|
|
|
|
|
|
|
|
go func(vals []float64) {
|
|
|
|
start.Wait()
|
|
|
|
for _, v := range vals {
|
|
|
|
sStream <- v
|
|
|
|
gge.Add(v)
|
|
|
|
}
|
|
|
|
end.Done()
|
|
|
|
}(vals)
|
|
|
|
}
|
|
|
|
start.Done()
|
|
|
|
|
2018-01-19 18:21:07 +03:00
|
|
|
if expected, got := <-result, math.Float64frombits(gge.(*gauge).valBits); math.Abs(expected-got) > 0.000001 {
|
2014-05-07 22:08:33 +04:00
|
|
|
t.Fatalf("expected approx. %f, got %f", expected, got)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
2013-01-19 17:48:30 +04:00
|
|
|
}
|
2012-05-20 01:59:25 +04:00
|
|
|
|
2014-05-07 22:08:33 +04:00
|
|
|
if err := quick.Check(it, nil); err != nil {
|
|
|
|
t.Fatal(err)
|
2013-01-19 17:48:30 +04:00
|
|
|
}
|
2014-05-07 22:08:33 +04:00
|
|
|
}
|
2012-05-20 01:59:25 +04:00
|
|
|
|
2014-05-07 22:08:33 +04:00
|
|
|
func TestGaugeVecConcurrency(t *testing.T) {
|
|
|
|
it := func(n uint32) bool {
|
|
|
|
mutations := int(n % 10000)
|
|
|
|
concLevel := int(n%15 + 1)
|
|
|
|
vecLength := int(n%5 + 1)
|
2012-05-20 01:59:25 +04:00
|
|
|
|
2014-05-07 22:08:33 +04:00
|
|
|
var start, end sync.WaitGroup
|
|
|
|
start.Add(1)
|
|
|
|
end.Add(concLevel)
|
|
|
|
|
|
|
|
sStreams := make([]chan float64, vecLength)
|
|
|
|
results := make([]chan float64, vecLength)
|
|
|
|
done := make(chan struct{})
|
2012-05-20 01:59:25 +04:00
|
|
|
|
2014-05-07 22:08:33 +04:00
|
|
|
for i := 0; i < vecLength; i++ {
|
|
|
|
sStreams[i] = make(chan float64, mutations*concLevel)
|
|
|
|
results[i] = make(chan float64)
|
|
|
|
go listenGaugeStream(sStreams[i], results[i], done)
|
2013-01-19 17:48:30 +04:00
|
|
|
}
|
2012-05-20 01:59:25 +04:00
|
|
|
|
2014-05-07 22:08:33 +04:00
|
|
|
go func() {
|
|
|
|
end.Wait()
|
|
|
|
close(done)
|
|
|
|
}()
|
2012-05-20 01:59:25 +04:00
|
|
|
|
2014-05-07 22:08:33 +04:00
|
|
|
gge := NewGaugeVec(
|
|
|
|
GaugeOpts{
|
|
|
|
Name: "test_gauge",
|
|
|
|
Help: "no help can be found here",
|
|
|
|
},
|
|
|
|
[]string{"label"},
|
|
|
|
)
|
|
|
|
for i := 0; i < concLevel; i++ {
|
|
|
|
vals := make([]float64, mutations)
|
|
|
|
pick := make([]int, mutations)
|
|
|
|
for j := 0; j < mutations; j++ {
|
|
|
|
vals[j] = rand.Float64() - 0.5
|
|
|
|
pick[j] = rand.Intn(vecLength)
|
|
|
|
}
|
|
|
|
|
|
|
|
go func(vals []float64) {
|
|
|
|
start.Wait()
|
|
|
|
for i, v := range vals {
|
|
|
|
sStreams[pick[i]] <- v
|
2020-07-06 03:58:12 +03:00
|
|
|
gge.WithLabelValues(string('A' + rune(pick[i]))).Add(v)
|
2014-05-07 22:08:33 +04:00
|
|
|
}
|
|
|
|
end.Done()
|
|
|
|
}(vals)
|
2013-01-19 17:48:30 +04:00
|
|
|
}
|
2014-05-07 22:08:33 +04:00
|
|
|
start.Done()
|
2012-05-20 01:59:25 +04:00
|
|
|
|
2014-05-07 22:08:33 +04:00
|
|
|
for i := range sStreams {
|
2020-07-06 03:58:12 +03:00
|
|
|
if expected, got := <-results[i], math.Float64frombits(gge.WithLabelValues(string('A'+rune(i))).(*gauge).valBits); math.Abs(expected-got) > 0.000001 {
|
2014-05-07 22:08:33 +04:00
|
|
|
t.Fatalf("expected approx. %f, got %f", expected, got)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
2012-05-20 01:59:25 +04:00
|
|
|
|
2014-05-07 22:08:33 +04:00
|
|
|
if err := quick.Check(it, nil); err != nil {
|
|
|
|
t.Fatal(err)
|
2013-01-19 17:48:30 +04:00
|
|
|
}
|
2012-05-20 01:59:25 +04:00
|
|
|
}
|
2014-06-23 16:15:35 +04:00
|
|
|
|
|
|
|
func TestGaugeFunc(t *testing.T) {
|
|
|
|
gf := NewGaugeFunc(
|
|
|
|
GaugeOpts{
|
|
|
|
Name: "test_name",
|
|
|
|
Help: "test help",
|
|
|
|
ConstLabels: Labels{"a": "1", "b": "2"},
|
|
|
|
},
|
|
|
|
func() float64 { return 3.1415 },
|
|
|
|
)
|
|
|
|
|
2023-06-27 14:21:36 +03:00
|
|
|
if expected, got := `Desc{fqName: "test_name", help: "test help", constLabels: {a="1",b="2"}, variableLabels: {}}`, gf.Desc().String(); expected != got {
|
2014-06-23 16:15:35 +04:00
|
|
|
t.Errorf("expected %q, got %q", expected, got)
|
|
|
|
}
|
|
|
|
|
|
|
|
m := &dto.Metric{}
|
|
|
|
gf.Write(m)
|
|
|
|
|
2023-08-11 22:17:55 +03:00
|
|
|
expected := &dto.Metric{
|
|
|
|
Label: []*dto.LabelPair{
|
|
|
|
{Name: proto.String("a"), Value: proto.String("1")},
|
|
|
|
{Name: proto.String("b"), Value: proto.String("2")},
|
|
|
|
},
|
|
|
|
Gauge: &dto.Gauge{
|
|
|
|
Value: proto.Float64(3.1415),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
if !proto.Equal(expected, m) {
|
|
|
|
t.Errorf("expected %q, got %q", expected, m)
|
2014-06-23 16:15:35 +04:00
|
|
|
}
|
|
|
|
}
|
2016-11-18 18:29:59 +03:00
|
|
|
|
|
|
|
func TestGaugeSetCurrentTime(t *testing.T) {
|
|
|
|
g := NewGauge(GaugeOpts{
|
|
|
|
Name: "test_name",
|
|
|
|
Help: "test help",
|
|
|
|
})
|
|
|
|
g.SetToCurrentTime()
|
|
|
|
unixTime := float64(time.Now().Unix())
|
|
|
|
|
|
|
|
m := &dto.Metric{}
|
|
|
|
g.Write(m)
|
|
|
|
|
|
|
|
delta := unixTime - m.GetGauge().GetValue()
|
|
|
|
// This is just a smoke test to make sure SetToCurrentTime is not
|
|
|
|
// totally off. Tests with current time involved are hard...
|
|
|
|
if math.Abs(delta) > 5 {
|
|
|
|
t.Errorf("Gauge set to current time deviates from current time by more than 5s, delta is %f seconds", delta)
|
|
|
|
}
|
|
|
|
}
|