VictoriaMetrics/lib/storage/storage_timing_test.go
rtm0 1b03d7e6de
Fix inconsistent error handling in Storage.AddRows() (#6583)
`Storage.AddRows()` returns an error only in one case: when
`Storage.updatePerDateData()` fails to unmarshal a `metricNameRaw`. But
the same error is treated as a warning when it happens inside
`Storage.add()` or returned by `Storage.prefillNextIndexDB()`.

This commit fixes this inconsistency by treating the error returned by
`Storage.updatePerDateData()` as a warning as well. As a result
`Storage.add()` does not need a return value anymore and so doesn't
`Storage.AddRows()`.

Additionally, this commit adds a unit test that checks all cases that
result in a row not being added to the storage.

---------

Signed-off-by: Artem Fetishev <wwctrsrx@gmail.com>
Co-authored-by: Nikolay <nik@victoriametrics.com>
2024-07-17 12:55:07 +02:00

56 lines
1.3 KiB
Go

package storage
import (
"fmt"
"os"
"sync/atomic"
"testing"
)
func BenchmarkStorageAddRows(b *testing.B) {
for _, rowsPerBatch := range []int{1, 10, 100, 1000} {
b.Run(fmt.Sprintf("rowsPerBatch_%d", rowsPerBatch), func(b *testing.B) {
benchmarkStorageAddRows(b, rowsPerBatch)
})
}
}
func benchmarkStorageAddRows(b *testing.B, rowsPerBatch int) {
path := fmt.Sprintf("BenchmarkStorageAddRows_%d", rowsPerBatch)
s := MustOpenStorage(path, 0, 0, 0)
defer func() {
s.MustClose()
if err := os.RemoveAll(path); err != nil {
b.Fatalf("cannot remove storage at %q: %s", path, err)
}
}()
var globalOffset atomic.Uint64
b.SetBytes(int64(rowsPerBatch))
b.ReportAllocs()
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
mrs := make([]MetricRow, rowsPerBatch)
var mn MetricName
mn.MetricGroup = []byte("rps")
mn.Tags = []Tag{
{[]byte("job"), []byte("webservice")},
{[]byte("instance"), []byte("1.2.3.4")},
}
for pb.Next() {
offset := int(globalOffset.Add(uint64(rowsPerBatch)))
for i := 0; i < rowsPerBatch; i++ {
mn.AccountID = uint32(i)
mn.ProjectID = uint32(i % 3)
mr := &mrs[i]
mr.MetricNameRaw = mn.marshalRaw(mr.MetricNameRaw[:0])
mr.Timestamp = int64(offset + i)
mr.Value = float64(offset + i)
}
s.AddRows(mrs, defaultPrecisionBits)
}
})
b.StopTimer()
}