2019-07-07 18:44:26 +02:00
|
|
|
package prometheus
|
|
|
|
|
|
|
|
import (
|
2019-08-20 21:52:49 +02:00
|
|
|
"math"
|
2022-12-17 01:54:57 +01:00
|
|
|
"net/http"
|
2019-08-20 21:52:49 +02:00
|
|
|
"reflect"
|
2024-10-18 13:41:43 +02:00
|
|
|
"runtime"
|
2019-07-07 22:34:04 +02:00
|
|
|
"testing"
|
2019-08-20 21:52:49 +02:00
|
|
|
|
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/app/vmselect/netstorage"
|
2019-07-07 18:44:26 +02:00
|
|
|
)
|
|
|
|
|
2020-07-20 14:28:36 +02:00
|
|
|
func TestRemoveEmptyValuesAndTimeseries(t *testing.T) {
|
2019-08-20 21:52:49 +02:00
|
|
|
f := func(tss []netstorage.Result, tssExpected []netstorage.Result) {
|
|
|
|
t.Helper()
|
2020-07-20 14:28:36 +02:00
|
|
|
tss = removeEmptyValuesAndTimeseries(tss)
|
2019-08-20 21:52:49 +02:00
|
|
|
if !reflect.DeepEqual(tss, tssExpected) {
|
|
|
|
t.Fatalf("unexpected result; got %v; want %v", tss, tssExpected)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
f(nil, nil)
|
|
|
|
f([]netstorage.Result{
|
|
|
|
{
|
|
|
|
Timestamps: []int64{100, 200, 300},
|
|
|
|
Values: []float64{1, 2, 3},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Timestamps: []int64{100, 200, 300, 400},
|
|
|
|
Values: []float64{nan, nan, 3, nan},
|
|
|
|
},
|
2020-07-20 14:28:36 +02:00
|
|
|
{
|
|
|
|
Timestamps: []int64{1, 2},
|
|
|
|
Values: []float64{nan, nan},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Timestamps: nil,
|
|
|
|
Values: nil,
|
|
|
|
},
|
2019-08-20 21:52:49 +02:00
|
|
|
}, []netstorage.Result{
|
|
|
|
{
|
|
|
|
Timestamps: []int64{100, 200, 300},
|
|
|
|
Values: []float64{1, 2, 3},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Timestamps: []int64{300},
|
|
|
|
Values: []float64{3},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-07-05 16:56:54 +02:00
|
|
|
func TestAdjustLastPoints(t *testing.T) {
|
2020-07-05 17:17:02 +02:00
|
|
|
f := func(tss []netstorage.Result, start, end int64, tssExpected []netstorage.Result) {
|
2020-07-05 16:56:54 +02:00
|
|
|
t.Helper()
|
2020-07-05 17:17:02 +02:00
|
|
|
tss = adjustLastPoints(tss, start, end)
|
2020-07-05 16:56:54 +02:00
|
|
|
for i, ts := range tss {
|
|
|
|
for j, value := range ts.Values {
|
|
|
|
expectedValue := tssExpected[i].Values[j]
|
|
|
|
if math.IsNaN(expectedValue) {
|
|
|
|
if !math.IsNaN(value) {
|
2020-07-14 11:45:42 +02:00
|
|
|
t.Fatalf("unexpected value for time series #%d at position %d; got %v; want nan", i, j, value)
|
2020-07-05 16:56:54 +02:00
|
|
|
}
|
|
|
|
} else if expectedValue != value {
|
2020-07-14 11:45:42 +02:00
|
|
|
t.Fatalf("unexpected value for time series #%d at position %d; got %v; want %v", i, j, value, expectedValue)
|
2020-07-05 16:56:54 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(ts.Timestamps, tssExpected[i].Timestamps) {
|
2020-07-14 11:45:42 +02:00
|
|
|
t.Fatalf("unexpected timestamps for time series #%d; got %v; want %v", i, tss, tssExpected)
|
2020-07-05 16:56:54 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
nan := math.NaN()
|
|
|
|
|
2020-07-05 17:17:02 +02:00
|
|
|
f(nil, 300, 500, nil)
|
2020-07-05 16:56:54 +02:00
|
|
|
|
|
|
|
f([]netstorage.Result{
|
|
|
|
{
|
|
|
|
Timestamps: []int64{100, 200, 300, 400, 500},
|
|
|
|
Values: []float64{1, 2, 3, 4, nan},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Timestamps: []int64{100, 200, 300, 400, 500},
|
|
|
|
Values: []float64{1, 2, 3, nan, nan},
|
|
|
|
},
|
2020-07-05 17:17:02 +02:00
|
|
|
}, 400, 500, []netstorage.Result{
|
2020-07-05 16:56:54 +02:00
|
|
|
{
|
|
|
|
Timestamps: []int64{100, 200, 300, 400, 500},
|
|
|
|
Values: []float64{1, 2, 3, 4, 4},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Timestamps: []int64{100, 200, 300, 400, 500},
|
|
|
|
Values: []float64{1, 2, 3, nan, nan},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
f([]netstorage.Result{
|
|
|
|
{
|
|
|
|
Timestamps: []int64{100, 200, 300, 400, 500},
|
|
|
|
Values: []float64{1, 2, 3, nan, nan},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Timestamps: []int64{100, 200, 300, 400, 500},
|
|
|
|
Values: []float64{1, 2, nan, nan, nan},
|
|
|
|
},
|
2020-07-05 17:17:02 +02:00
|
|
|
}, 300, 500, []netstorage.Result{
|
2020-07-05 16:56:54 +02:00
|
|
|
{
|
|
|
|
Timestamps: []int64{100, 200, 300, 400, 500},
|
|
|
|
Values: []float64{1, 2, 3, 3, 3},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Timestamps: []int64{100, 200, 300, 400, 500},
|
|
|
|
Values: []float64{1, 2, nan, nan, nan},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
f([]netstorage.Result{
|
|
|
|
{
|
|
|
|
Timestamps: []int64{100, 200, 300, 400, 500},
|
|
|
|
Values: []float64{1, 2, nan, nan, nan},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Timestamps: []int64{100, 200, 300, 400, 500},
|
|
|
|
Values: []float64{1, nan, nan, nan, nan},
|
|
|
|
},
|
|
|
|
}, 500, 300, []netstorage.Result{
|
|
|
|
{
|
|
|
|
Timestamps: []int64{100, 200, 300, 400, 500},
|
|
|
|
Values: []float64{1, 2, nan, nan, nan},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Timestamps: []int64{100, 200, 300, 400, 500},
|
|
|
|
Values: []float64{1, nan, nan, nan, nan},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
f([]netstorage.Result{
|
|
|
|
{
|
|
|
|
Timestamps: []int64{100, 200, 300, 400, 500},
|
|
|
|
Values: []float64{1, 2, 3, 4, nan},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Timestamps: []int64{100, 200, 300, 400},
|
|
|
|
Values: []float64{1, 2, 3, 4},
|
|
|
|
},
|
2020-07-05 17:17:02 +02:00
|
|
|
}, 400, 500, []netstorage.Result{
|
2020-07-05 16:56:54 +02:00
|
|
|
{
|
|
|
|
Timestamps: []int64{100, 200, 300, 400, 500},
|
|
|
|
Values: []float64{1, 2, 3, 4, 4},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Timestamps: []int64{100, 200, 300, 400},
|
|
|
|
Values: []float64{1, 2, 3, 4},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
f([]netstorage.Result{
|
|
|
|
{
|
|
|
|
Timestamps: []int64{100, 200, 300, 400, 500},
|
|
|
|
Values: []float64{1, 2, 3, nan, nan},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Timestamps: []int64{100, 200, 300},
|
|
|
|
Values: []float64{1, 2, nan},
|
|
|
|
},
|
2020-07-05 17:17:02 +02:00
|
|
|
}, 300, 600, []netstorage.Result{
|
2020-07-05 16:56:54 +02:00
|
|
|
{
|
|
|
|
Timestamps: []int64{100, 200, 300, 400, 500},
|
|
|
|
Values: []float64{1, 2, 3, 3, 3},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Timestamps: []int64{100, 200, 300},
|
|
|
|
Values: []float64{1, 2, nan},
|
|
|
|
},
|
|
|
|
})
|
2020-07-14 11:45:42 +02:00
|
|
|
|
|
|
|
// Check for timestamps outside the configured time range.
|
|
|
|
// See https://github.com/VictoriaMetrics/VictoriaMetrics/issues/625
|
|
|
|
f([]netstorage.Result{
|
|
|
|
{
|
|
|
|
Timestamps: []int64{100, 200, 300, 400, 500},
|
|
|
|
Values: []float64{1, 2, 3, nan, nan},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Timestamps: []int64{100, 200, 300},
|
|
|
|
Values: []float64{1, 2, 45},
|
|
|
|
},
|
|
|
|
}, 250, 400, []netstorage.Result{
|
|
|
|
{
|
|
|
|
Timestamps: []int64{100, 200, 300, 400, 500},
|
|
|
|
Values: []float64{1, 2, 3, nan, nan},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Timestamps: []int64{100, 200, 300},
|
|
|
|
Values: []float64{1, 2, 2},
|
|
|
|
},
|
|
|
|
})
|
2020-07-05 16:56:54 +02:00
|
|
|
}
|
2022-12-17 01:54:57 +01:00
|
|
|
|
2022-12-17 02:12:26 +01:00
|
|
|
func TestGetLatencyOffsetMillisecondsSuccess(t *testing.T) {
|
|
|
|
f := func(url string, expectedOffset int64) {
|
|
|
|
t.Helper()
|
2023-02-23 03:53:05 +01:00
|
|
|
r, err := http.NewRequest(http.MethodGet, url, nil)
|
2022-12-17 02:12:26 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected error in NewRequest(%q): %s", url, err)
|
|
|
|
}
|
|
|
|
offset, err := getLatencyOffsetMilliseconds(r)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected error: %s", err)
|
|
|
|
}
|
|
|
|
if offset != expectedOffset {
|
|
|
|
t.Fatalf("unexpected offset got %d; want %d", offset, expectedOffset)
|
2022-12-17 01:54:57 +01:00
|
|
|
}
|
|
|
|
}
|
2022-12-17 02:12:26 +01:00
|
|
|
f("http://localhost", latencyOffset.Milliseconds())
|
|
|
|
f("http://localhost?latency_offset=1.234s", 1234)
|
|
|
|
}
|
2022-12-17 01:54:57 +01:00
|
|
|
|
2022-12-17 02:12:26 +01:00
|
|
|
func TestGetLatencyOffsetMillisecondsFailure(t *testing.T) {
|
|
|
|
f := func(url string) {
|
|
|
|
t.Helper()
|
2023-02-23 03:53:05 +01:00
|
|
|
r, err := http.NewRequest(http.MethodGet, url, nil)
|
2022-12-17 02:12:26 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected error in NewRequest(%q): %s", url, err)
|
|
|
|
}
|
|
|
|
if _, err := getLatencyOffsetMilliseconds(r); err == nil {
|
|
|
|
t.Fatalf("expecting non-nil error")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
f("http://localhost?latency_offset=foobar")
|
2022-12-17 01:54:57 +01:00
|
|
|
}
|
2024-10-18 13:41:43 +02:00
|
|
|
|
|
|
|
func TestCalculateMaxMetricsLimitByResource(t *testing.T) {
|
|
|
|
f := func(maxConcurrentRequest, remainingMemory, expect int) {
|
|
|
|
t.Helper()
|
|
|
|
maxMetricsLimit := calculateMaxUniqueTimeSeriesForResource(maxConcurrentRequest, remainingMemory)
|
|
|
|
if maxMetricsLimit != expect {
|
|
|
|
t.Fatalf("unexpected max metrics limit: got %d, want %d", maxMetricsLimit, expect)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Skip when GOARCH=386
|
|
|
|
if runtime.GOARCH != "386" {
|
|
|
|
// 8 CPU & 32 GiB
|
|
|
|
f(16, int(math.Round(32*1024*1024*1024*0.4)), 4294967)
|
|
|
|
// 4 CPU & 32 GiB
|
|
|
|
f(8, int(math.Round(32*1024*1024*1024*0.4)), 8589934)
|
|
|
|
}
|
|
|
|
|
|
|
|
// 2 CPU & 4 GiB
|
|
|
|
f(4, int(math.Round(4*1024*1024*1024*0.4)), 2147483)
|
|
|
|
|
|
|
|
// other edge cases
|
|
|
|
f(0, int(math.Round(4*1024*1024*1024*0.4)), 2e9)
|
|
|
|
f(4, 0, 0)
|
|
|
|
|
|
|
|
}
|