mirror of
https://github.com/prometheus/prometheus.git
synced 2025-01-12 06:17:27 -08:00
TSDB: compute size of name and value of each label
Signed-off-by: Bryan Boreham <bjboreham@gmail.com>
This commit is contained in:
parent
bd4fd1d2f7
commit
afb5520bb1
|
@ -490,6 +490,6 @@ func (b *ScratchBuilder) Overwrite(ls *Labels) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// SizeOfLabels returns the approximate space required for n copies of a label.
|
// SizeOfLabels returns the approximate space required for n copies of a label.
|
||||||
func SizeOfLabels(value string, n uint64) uint64 {
|
func SizeOfLabels(name, value string, n uint64) uint64 {
|
||||||
return (uint64(len(value)) + uint64(unsafe.Sizeof(value))) * n
|
return (uint64(len(name)) + uint64(unsafe.Sizeof(name)) + uint64(len(value)) + uint64(unsafe.Sizeof(value))) * n
|
||||||
}
|
}
|
||||||
|
|
|
@ -817,6 +817,6 @@ func (b *ScratchBuilder) Overwrite(ls *Labels) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// SizeOfLabels returns the approximate space required for n copies of a label.
|
// SizeOfLabels returns the approximate space required for n copies of a label.
|
||||||
func SizeOfLabels(value string, n uint64) uint64 {
|
func SizeOfLabels(name, value string, n uint64) uint64 {
|
||||||
return uint64(len(value)) + n*2 // Assuming most symbol-table entries are 2 bytes long.
|
return uint64(len(name)+len(value)) + n*4 // Assuming most symbol-table entries are 2 bytes long.
|
||||||
}
|
}
|
||||||
|
|
|
@ -696,6 +696,6 @@ func (b *ScratchBuilder) SetSymbolTable(_ *SymbolTable) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// SizeOfLabels returns the approximate space required for n copies of a label.
|
// SizeOfLabels returns the approximate space required for n copies of a label.
|
||||||
func SizeOfLabels(value string, n uint64) uint64 {
|
func SizeOfLabels(name, value string, n uint64) uint64 {
|
||||||
return uint64(len(value)+sizeVarint(uint64(len(value)))) * n
|
return uint64(len(name)+sizeVarint(uint64(len(name)))+len(value)+sizeVarint(uint64(len(value)))) * n
|
||||||
}
|
}
|
||||||
|
|
|
@ -164,7 +164,7 @@ type PostingsStats struct {
|
||||||
|
|
||||||
// Stats calculates the cardinality statistics from postings.
|
// 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.
|
// 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
|
var size uint64
|
||||||
p.mtx.RLock()
|
p.mtx.RLock()
|
||||||
|
|
||||||
|
@ -192,7 +192,7 @@ func (p *MemPostings) Stats(label string, limit int, bytes func(string, uint64)
|
||||||
}
|
}
|
||||||
seriesCnt := uint64(len(values))
|
seriesCnt := uint64(len(values))
|
||||||
labelValuePairs.push(Stat{Name: n + "=" + name, Count: seriesCnt})
|
labelValuePairs.push(Stat{Name: n + "=" + name, Count: seriesCnt})
|
||||||
size += bytes(name, seriesCnt)
|
size += labelSizeFunc(n, name, seriesCnt)
|
||||||
}
|
}
|
||||||
labelValueLength.push(Stat{Name: n, Count: size})
|
labelValueLength.push(Stat{Name: n, Count: size})
|
||||||
}
|
}
|
||||||
|
|
|
@ -954,7 +954,7 @@ func TestMemPostingsStats(t *testing.T) {
|
||||||
p.Add(2, labels.FromStrings("label", "value1"))
|
p.Add(2, labels.FromStrings("label", "value1"))
|
||||||
|
|
||||||
// call the Stats method to calculate the cardinality statistics
|
// 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
|
// assert that the expected statistics were calculated
|
||||||
require.Equal(t, uint64(2), stats.CardinalityMetricsStats[0].Count)
|
require.Equal(t, uint64(2), stats.CardinalityMetricsStats[0].Count)
|
||||||
|
@ -963,7 +963,7 @@ func TestMemPostingsStats(t *testing.T) {
|
||||||
require.Equal(t, uint64(3), stats.CardinalityLabelStats[0].Count)
|
require.Equal(t, uint64(3), stats.CardinalityLabelStats[0].Count)
|
||||||
require.Equal(t, "label", stats.CardinalityLabelStats[0].Name)
|
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, "label", stats.LabelValueStats[0].Name)
|
||||||
|
|
||||||
require.Equal(t, uint64(2), stats.LabelValuePairsStats[0].Count)
|
require.Equal(t, uint64(2), stats.LabelValuePairsStats[0].Count)
|
||||||
|
|
Loading…
Reference in a new issue