TSDB: compute size of name and value of each label

Signed-off-by: Bryan Boreham <bjboreham@gmail.com>
This commit is contained in:
Bryan Boreham 2024-04-30 21:36:06 +01:00
parent f96d06a975
commit 7d3d7ec16e
5 changed files with 10 additions and 10 deletions

View file

@ -488,6 +488,6 @@ func (b *ScratchBuilder) Overwrite(ls *Labels) {
}
// SizeOfLabels returns the approximate space required for n copies of a label.
func SizeOfLabels(value string, n uint64) uint64 {
return (uint64(len(value)) + uint64(unsafe.Sizeof(value))) * n
func SizeOfLabels(name, value string, n uint64) uint64 {
return (uint64(len(name)) + uint64(unsafe.Sizeof(name)) + uint64(len(value)) + uint64(unsafe.Sizeof(value))) * n
}

View file

@ -807,6 +807,6 @@ func (b *ScratchBuilder) Overwrite(ls *Labels) {
}
// SizeOfLabels returns the approximate space required for n copies of a label.
func SizeOfLabels(value string, n uint64) uint64 {
return uint64(len(value)) + n*2 // Assuming most symbol-table entries are 2 bytes long.
func SizeOfLabels(name, value string, n uint64) uint64 {
return uint64(len(name)+len(value)) + n*4 // Assuming most symbol-table entries are 2 bytes long.
}

View file

@ -703,6 +703,6 @@ func (b *ScratchBuilder) SetSymbolTable(_ *SymbolTable) {
}
// SizeOfLabels returns the approximate space required for n copies of a label.
func SizeOfLabels(value string, n uint64) uint64 {
return uint64(len(value)+sizeVarint(uint64(len(value)))) * n
func SizeOfLabels(name, value string, n uint64) uint64 {
return uint64(len(name)+sizeVarint(uint64(len(name)))+len(value)+sizeVarint(uint64(len(value)))) * n
}

View file

@ -164,7 +164,7 @@ type PostingsStats struct {
// Stats calculates the cardinality statistics from postings.
// Caller can pass in a function which computes the space required for n series with a given label.
func (p *MemPostings) Stats(label string, limit int, bytes func(string, uint64) uint64) *PostingsStats {
func (p *MemPostings) Stats(label string, limit int, labelSizeFunc func(string, string, uint64) uint64) *PostingsStats {
var size uint64
p.mtx.RLock()
@ -192,7 +192,7 @@ func (p *MemPostings) Stats(label string, limit int, bytes func(string, uint64)
}
seriesCnt := uint64(len(values))
labelValuePairs.push(Stat{Name: n + "=" + name, Count: seriesCnt})
size += bytes(name, seriesCnt)
size += labelSizeFunc(n, name, seriesCnt)
}
labelValueLength.push(Stat{Name: n, Count: size})
}

View file

@ -950,7 +950,7 @@ func TestMemPostingsStats(t *testing.T) {
p.Add(2, labels.FromStrings("label", "value1"))
// call the Stats method to calculate the cardinality statistics
stats := p.Stats("label", 10, func(s string, n uint64) uint64 { return uint64(len(s)) * n })
stats := p.Stats("label", 10, func(name, value string, n uint64) uint64 { return uint64(len(name)+len(value)) * n })
// assert that the expected statistics were calculated
require.Equal(t, uint64(2), stats.CardinalityMetricsStats[0].Count)
@ -959,7 +959,7 @@ func TestMemPostingsStats(t *testing.T) {
require.Equal(t, uint64(3), stats.CardinalityLabelStats[0].Count)
require.Equal(t, "label", stats.CardinalityLabelStats[0].Name)
require.Equal(t, uint64(24), stats.LabelValueStats[0].Count)
require.Equal(t, uint64(44), stats.LabelValueStats[0].Count)
require.Equal(t, "label", stats.LabelValueStats[0].Name)
require.Equal(t, uint64(2), stats.LabelValuePairsStats[0].Count)