2022-09-19 11:26:18 +00:00
|
|
|
package remotewrite
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2023-02-22 10:26:19 +00:00
|
|
|
"math"
|
2022-09-19 11:26:18 +00:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/prompbmarshal"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestPushWriteRequest(t *testing.T) {
|
2023-02-21 02:38:49 +00:00
|
|
|
rowsCounts := []int{1, 10, 100, 1e3, 1e4}
|
|
|
|
expectedBlockLensProm := []int{216, 1848, 16424, 169882, 1757876}
|
|
|
|
expectedBlockLensVM := []int{138, 492, 3927, 34995, 288476}
|
|
|
|
for i, rowsCount := range rowsCounts {
|
|
|
|
expectedBlockLenProm := expectedBlockLensProm[i]
|
|
|
|
expectedBlockLenVM := expectedBlockLensVM[i]
|
2022-09-19 11:26:18 +00:00
|
|
|
t.Run(fmt.Sprintf("%d", rowsCount), func(t *testing.T) {
|
2023-02-21 02:38:49 +00:00
|
|
|
testPushWriteRequest(t, rowsCount, expectedBlockLenProm, expectedBlockLenVM)
|
2022-09-19 11:26:18 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-21 02:38:49 +00:00
|
|
|
func testPushWriteRequest(t *testing.T, rowsCount, expectedBlockLenProm, expectedBlockLenVM int) {
|
2023-02-22 10:26:19 +00:00
|
|
|
f := func(isVMRemoteWrite bool, expectedBlockLen int, tolerancePrc float64) {
|
2023-02-21 02:38:49 +00:00
|
|
|
t.Helper()
|
|
|
|
wr := newTestWriteRequest(rowsCount, 20)
|
|
|
|
pushBlockLen := 0
|
2023-11-24 12:42:11 +00:00
|
|
|
pushBlock := func(block []byte) bool {
|
2023-02-21 02:38:49 +00:00
|
|
|
if pushBlockLen > 0 {
|
|
|
|
panic(fmt.Errorf("BUG: pushBlock called multiple times; pushBlockLen=%d at first call, len(block)=%d at second call", pushBlockLen, len(block)))
|
|
|
|
}
|
|
|
|
pushBlockLen = len(block)
|
2023-11-24 12:42:11 +00:00
|
|
|
return true
|
2023-02-21 02:38:49 +00:00
|
|
|
}
|
2023-11-25 09:31:30 +00:00
|
|
|
if !tryPushWriteRequest(wr, pushBlock, isVMRemoteWrite) {
|
|
|
|
t.Fatalf("cannot push data to to remote storage")
|
|
|
|
}
|
2023-02-22 10:26:19 +00:00
|
|
|
if math.Abs(float64(pushBlockLen-expectedBlockLen)/float64(expectedBlockLen)*100) > tolerancePrc {
|
|
|
|
t.Fatalf("unexpected block len for rowsCount=%d, isVMRemoteWrite=%v; got %d bytes; expecting %d bytes +- %.0f%%",
|
|
|
|
rowsCount, isVMRemoteWrite, pushBlockLen, expectedBlockLen, tolerancePrc)
|
|
|
|
}
|
2022-09-19 11:26:18 +00:00
|
|
|
}
|
2023-02-21 02:38:49 +00:00
|
|
|
|
|
|
|
// Check Prometheus remote write
|
2023-02-22 10:26:19 +00:00
|
|
|
f(false, expectedBlockLenProm, 0)
|
2023-02-21 02:38:49 +00:00
|
|
|
|
|
|
|
// Check VictoriaMetrics remote write
|
2023-02-22 10:26:19 +00:00
|
|
|
f(true, expectedBlockLenVM, 15)
|
2022-09-19 11:26:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func newTestWriteRequest(seriesCount, labelsCount int) *prompbmarshal.WriteRequest {
|
|
|
|
var wr prompbmarshal.WriteRequest
|
|
|
|
for i := 0; i < seriesCount; i++ {
|
|
|
|
var labels []prompbmarshal.Label
|
|
|
|
for j := 0; j < labelsCount; j++ {
|
|
|
|
labels = append(labels, prompbmarshal.Label{
|
2022-09-19 12:13:43 +00:00
|
|
|
Name: fmt.Sprintf("label_%d_%d", i, j),
|
2022-09-19 11:26:18 +00:00
|
|
|
Value: fmt.Sprintf("value_%d_%d", i, j),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
wr.Timeseries = append(wr.Timeseries, prompbmarshal.TimeSeries{
|
|
|
|
Labels: labels,
|
|
|
|
Samples: []prompbmarshal.Sample{
|
|
|
|
{
|
2022-09-19 12:13:43 +00:00
|
|
|
Value: float64(i),
|
|
|
|
Timestamp: 1000 * int64(i),
|
2022-09-19 11:26:18 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
return &wr
|
|
|
|
}
|