2019-11-23 00:16:17 +02:00
|
|
|
package metrics
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
|
|
|
"math"
|
2019-11-23 23:58:18 +02:00
|
|
|
"reflect"
|
2019-11-23 00:16:17 +02:00
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2024-05-19 11:06:13 +03:00
|
|
|
func TestHistogramMerge(t *testing.T) {
|
|
|
|
name := `TestHistogramMerge`
|
|
|
|
h := NewHistogram(name)
|
|
|
|
// Write data to histogram
|
|
|
|
for i := 98; i < 218; i++ {
|
|
|
|
h.Update(float64(i))
|
|
|
|
}
|
|
|
|
|
|
|
|
b := NewHistogram("test")
|
|
|
|
for i := 98; i < 218; i++ {
|
|
|
|
h.Update(float64(i))
|
|
|
|
}
|
|
|
|
|
|
|
|
h.Merge(b)
|
|
|
|
|
|
|
|
// Make sure the histogram prints <prefix>_bucket on marshalTo call
|
|
|
|
testMarshalTo(t, h, "prefix", `prefix_bucket{vmrange="8.799e+01...1.000e+02"} 6
|
|
|
|
prefix_bucket{vmrange="1.000e+02...1.136e+02"} 26
|
|
|
|
prefix_bucket{vmrange="1.136e+02...1.292e+02"} 32
|
|
|
|
prefix_bucket{vmrange="1.292e+02...1.468e+02"} 34
|
|
|
|
prefix_bucket{vmrange="1.468e+02...1.668e+02"} 40
|
|
|
|
prefix_bucket{vmrange="1.668e+02...1.896e+02"} 46
|
|
|
|
prefix_bucket{vmrange="1.896e+02...2.154e+02"} 52
|
|
|
|
prefix_bucket{vmrange="2.154e+02...2.448e+02"} 4
|
|
|
|
prefix_sum 37800
|
|
|
|
prefix_count 240
|
|
|
|
`)
|
|
|
|
}
|
|
|
|
|
2019-11-25 13:03:26 +02:00
|
|
|
func TestGetVMRange(t *testing.T) {
|
2021-02-15 14:15:01 +02:00
|
|
|
f := func(bucketIdx int, vmrangeExpected string) {
|
2019-11-23 00:16:17 +02:00
|
|
|
t.Helper()
|
2021-02-15 14:15:01 +02:00
|
|
|
vmrange := getVMRange(bucketIdx)
|
2019-11-25 13:03:26 +02:00
|
|
|
if vmrange != vmrangeExpected {
|
2021-02-15 14:15:01 +02:00
|
|
|
t.Fatalf("unexpected vmrange for bucketIdx=%d; got %s; want %s", bucketIdx, vmrange, vmrangeExpected)
|
2019-11-23 00:16:17 +02:00
|
|
|
}
|
|
|
|
}
|
2021-02-15 14:15:01 +02:00
|
|
|
f(0, "1.000e-09...1.136e-09")
|
|
|
|
f(1, "1.136e-09...1.292e-09")
|
|
|
|
f(bucketsPerDecimal-1, "8.799e-09...1.000e-08")
|
|
|
|
f(bucketsPerDecimal, "1.000e-08...1.136e-08")
|
|
|
|
f(bucketsPerDecimal*(-e10Min)-1, "8.799e-01...1.000e+00")
|
|
|
|
f(bucketsPerDecimal*(-e10Min), "1.000e+00...1.136e+00")
|
|
|
|
f(bucketsPerDecimal*(e10Max-e10Min)-1, "8.799e+17...1.000e+18")
|
2019-11-23 00:16:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestHistogramSerial(t *testing.T) {
|
|
|
|
name := `TestHistogramSerial`
|
|
|
|
h := NewHistogram(name)
|
|
|
|
|
|
|
|
// Verify that the histogram is invisible in the output of WritePrometheus when it has no data.
|
|
|
|
var bb bytes.Buffer
|
|
|
|
WritePrometheus(&bb, false)
|
|
|
|
result := bb.String()
|
|
|
|
if strings.Contains(result, name) {
|
|
|
|
t.Fatalf("histogram %s shouldn't be visible in the WritePrometheus output; got\n%s", name, result)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Write data to histogram
|
2019-11-25 13:03:26 +02:00
|
|
|
for i := 98; i < 218; i++ {
|
2019-11-23 00:16:17 +02:00
|
|
|
h.Update(float64(i))
|
|
|
|
}
|
|
|
|
|
2021-02-15 14:15:01 +02:00
|
|
|
// Make sure the histogram prints <prefix>_bucket on marshalTo call
|
|
|
|
testMarshalTo(t, h, "prefix", `prefix_bucket{vmrange="8.799e+01...1.000e+02"} 3
|
|
|
|
prefix_bucket{vmrange="1.000e+02...1.136e+02"} 13
|
|
|
|
prefix_bucket{vmrange="1.136e+02...1.292e+02"} 16
|
|
|
|
prefix_bucket{vmrange="1.292e+02...1.468e+02"} 17
|
|
|
|
prefix_bucket{vmrange="1.468e+02...1.668e+02"} 20
|
|
|
|
prefix_bucket{vmrange="1.668e+02...1.896e+02"} 23
|
|
|
|
prefix_bucket{vmrange="1.896e+02...2.154e+02"} 26
|
|
|
|
prefix_bucket{vmrange="2.154e+02...2.448e+02"} 2
|
|
|
|
prefix_sum 18900
|
|
|
|
prefix_count 120
|
|
|
|
`)
|
|
|
|
testMarshalTo(t, h, ` m{foo="bar"}`, ` m_bucket{foo="bar",vmrange="8.799e+01...1.000e+02"} 3
|
|
|
|
m_bucket{foo="bar",vmrange="1.000e+02...1.136e+02"} 13
|
|
|
|
m_bucket{foo="bar",vmrange="1.136e+02...1.292e+02"} 16
|
|
|
|
m_bucket{foo="bar",vmrange="1.292e+02...1.468e+02"} 17
|
|
|
|
m_bucket{foo="bar",vmrange="1.468e+02...1.668e+02"} 20
|
|
|
|
m_bucket{foo="bar",vmrange="1.668e+02...1.896e+02"} 23
|
|
|
|
m_bucket{foo="bar",vmrange="1.896e+02...2.154e+02"} 26
|
|
|
|
m_bucket{foo="bar",vmrange="2.154e+02...2.448e+02"} 2
|
|
|
|
m_sum{foo="bar"} 18900
|
|
|
|
m_count{foo="bar"} 120
|
|
|
|
`)
|
2019-11-25 13:03:26 +02:00
|
|
|
|
|
|
|
// Verify Reset
|
|
|
|
h.Reset()
|
|
|
|
bb.Reset()
|
|
|
|
WritePrometheus(&bb, false)
|
|
|
|
result = bb.String()
|
|
|
|
if strings.Contains(result, name) {
|
|
|
|
t.Fatalf("unexpected histogram %s in the WritePrometheus output; got\n%s", name, result)
|
|
|
|
}
|
2019-11-23 00:16:17 +02:00
|
|
|
|
|
|
|
// Verify supported ranges
|
2019-11-25 13:03:26 +02:00
|
|
|
for e10 := -100; e10 < 100; e10++ {
|
2021-02-15 14:15:01 +02:00
|
|
|
for offset := 0; offset < bucketsPerDecimal; offset++ {
|
|
|
|
m := 1 + math.Pow(bucketMultiplier, float64(offset))
|
2019-11-25 13:03:26 +02:00
|
|
|
f1 := m * math.Pow10(e10)
|
|
|
|
h.Update(f1)
|
2021-02-15 14:15:01 +02:00
|
|
|
f2 := (m + 0.5*bucketMultiplier) * math.Pow10(e10)
|
2019-11-25 13:03:26 +02:00
|
|
|
h.Update(f2)
|
2021-02-15 14:15:01 +02:00
|
|
|
f3 := (m + 2*bucketMultiplier) * math.Pow10(e10)
|
2019-11-25 13:03:26 +02:00
|
|
|
h.Update(f3)
|
|
|
|
}
|
2019-11-23 00:16:17 +02:00
|
|
|
}
|
|
|
|
h.UpdateDuration(time.Now().Add(-time.Minute))
|
|
|
|
|
2019-11-23 23:58:18 +02:00
|
|
|
// Verify edge cases
|
2019-11-25 13:03:26 +02:00
|
|
|
h.Update(0)
|
2019-11-23 23:58:18 +02:00
|
|
|
h.Update(math.Inf(1))
|
2019-11-25 13:03:26 +02:00
|
|
|
h.Update(math.Inf(-1))
|
2019-11-23 23:58:18 +02:00
|
|
|
h.Update(math.NaN())
|
|
|
|
h.Update(-123)
|
2021-03-01 16:46:59 +02:00
|
|
|
// See https://github.com/VictoriaMetrics/VictoriaMetrics/issues/1096
|
|
|
|
h.Update(math.Float64frombits(0x3e112e0be826d695))
|
2019-11-23 23:58:18 +02:00
|
|
|
|
2019-11-23 00:16:17 +02:00
|
|
|
// Make sure the histogram becomes visible in the output of WritePrometheus,
|
|
|
|
// since now it contains values.
|
|
|
|
bb.Reset()
|
|
|
|
WritePrometheus(&bb, false)
|
|
|
|
result = bb.String()
|
|
|
|
if !strings.Contains(result, name) {
|
|
|
|
t.Fatalf("missing histogram %s in the WritePrometheus output; got\n%s", name, result)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestHistogramConcurrent(t *testing.T) {
|
|
|
|
name := "HistogramConcurrent"
|
|
|
|
h := NewHistogram(name)
|
|
|
|
err := testConcurrent(func() error {
|
2019-11-25 13:03:26 +02:00
|
|
|
for f := 0.6; f < 1.4; f += 0.1 {
|
|
|
|
h.Update(f)
|
2019-11-23 00:16:17 +02:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2021-02-15 14:15:01 +02:00
|
|
|
testMarshalTo(t, h, "prefix", `prefix_bucket{vmrange="5.995e-01...6.813e-01"} 5
|
|
|
|
prefix_bucket{vmrange="6.813e-01...7.743e-01"} 5
|
|
|
|
prefix_bucket{vmrange="7.743e-01...8.799e-01"} 5
|
|
|
|
prefix_bucket{vmrange="8.799e-01...1.000e+00"} 10
|
|
|
|
prefix_bucket{vmrange="1.000e+00...1.136e+00"} 5
|
|
|
|
prefix_bucket{vmrange="1.136e+00...1.292e+00"} 5
|
|
|
|
prefix_bucket{vmrange="1.292e+00...1.468e+00"} 5
|
|
|
|
prefix_sum 38
|
|
|
|
prefix_count 40
|
|
|
|
`)
|
2019-11-23 23:58:18 +02:00
|
|
|
|
|
|
|
var labels []string
|
2019-11-25 13:03:26 +02:00
|
|
|
var counts []uint64
|
|
|
|
h.VisitNonZeroBuckets(func(label string, count uint64) {
|
2019-11-23 23:58:18 +02:00
|
|
|
labels = append(labels, label)
|
2019-11-25 13:03:26 +02:00
|
|
|
counts = append(counts, count)
|
2019-11-23 23:58:18 +02:00
|
|
|
})
|
2021-02-15 14:15:01 +02:00
|
|
|
labelsExpected := []string{
|
|
|
|
"5.995e-01...6.813e-01",
|
|
|
|
"6.813e-01...7.743e-01",
|
|
|
|
"7.743e-01...8.799e-01",
|
|
|
|
"8.799e-01...1.000e+00",
|
|
|
|
"1.000e+00...1.136e+00",
|
|
|
|
"1.136e+00...1.292e+00",
|
|
|
|
"1.292e+00...1.468e+00",
|
|
|
|
}
|
2019-11-23 23:58:18 +02:00
|
|
|
if !reflect.DeepEqual(labels, labelsExpected) {
|
|
|
|
t.Fatalf("unexpected labels; got %v; want %v", labels, labelsExpected)
|
|
|
|
}
|
2021-02-15 14:15:01 +02:00
|
|
|
countsExpected := []uint64{5, 5, 5, 10, 5, 5, 5}
|
2019-11-25 13:03:26 +02:00
|
|
|
if !reflect.DeepEqual(counts, countsExpected) {
|
|
|
|
t.Fatalf("unexpected counts; got %v; want %v", counts, countsExpected)
|
2019-11-23 23:58:18 +02:00
|
|
|
}
|
2019-11-23 00:16:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestHistogramWithTags(t *testing.T) {
|
|
|
|
name := `TestHistogram{tag="foo"}`
|
|
|
|
h := NewHistogram(name)
|
|
|
|
h.Update(123)
|
|
|
|
|
|
|
|
var bb bytes.Buffer
|
|
|
|
WritePrometheus(&bb, false)
|
|
|
|
result := bb.String()
|
2021-02-15 14:15:01 +02:00
|
|
|
namePrefixWithTag := `TestHistogram_bucket{tag="foo",vmrange="1.136e+02...1.292e+02"} 1` + "\n"
|
2019-11-23 00:16:17 +02:00
|
|
|
if !strings.Contains(result, namePrefixWithTag) {
|
|
|
|
t.Fatalf("missing histogram %s in the WritePrometheus output; got\n%s", namePrefixWithTag, result)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetOrCreateHistogramSerial(t *testing.T) {
|
|
|
|
name := "GetOrCreateHistogramSerial"
|
|
|
|
if err := testGetOrCreateHistogram(name); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetOrCreateHistogramConcurrent(t *testing.T) {
|
|
|
|
name := "GetOrCreateHistogramConcurrent"
|
|
|
|
err := testConcurrent(func() error {
|
|
|
|
return testGetOrCreateHistogram(name)
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func testGetOrCreateHistogram(name string) error {
|
|
|
|
h1 := GetOrCreateHistogram(name)
|
|
|
|
for i := 0; i < 10; i++ {
|
|
|
|
h2 := GetOrCreateHistogram(name)
|
|
|
|
if h1 != h2 {
|
|
|
|
return fmt.Errorf("unexpected histogram returned; got %p; want %p", h2, h1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|