forked from mirror/client_golang
Merge "Cache signature of an empty label set"
This commit is contained in:
commit
ad41ea8439
|
@ -14,9 +14,16 @@ import (
|
|||
"github.com/prometheus/client_golang/model"
|
||||
)
|
||||
|
||||
// cache the signature of an empty label set.
|
||||
var emptyLabelSignature = fnv.New64a().Sum64()
|
||||
|
||||
// LabelsToSignature provides a way of building a unique signature
|
||||
// (i.e., fingerprint) for a given label set sequence.
|
||||
func labelsToSignature(labels map[string]string) uint64 {
|
||||
if len(labels) == 0 {
|
||||
return emptyLabelSignature
|
||||
}
|
||||
|
||||
names := make(model.LabelNames, 0, len(labels))
|
||||
for name := range labels {
|
||||
names = append(names, model.LabelName(name))
|
||||
|
|
|
@ -7,6 +7,7 @@
|
|||
package prometheus
|
||||
|
||||
import (
|
||||
"runtime"
|
||||
"testing"
|
||||
)
|
||||
|
||||
|
@ -38,6 +39,25 @@ func TestLabelToSignature(t *testing.T) {
|
|||
testLabelsToSignature(t)
|
||||
}
|
||||
|
||||
func TestEmptyLabelSignature(t *testing.T) {
|
||||
input := []map[string]string{nil, {}}
|
||||
|
||||
var ms runtime.MemStats
|
||||
runtime.ReadMemStats(&ms)
|
||||
|
||||
alloc := ms.Alloc
|
||||
|
||||
for _, labels := range input {
|
||||
labelsToSignature(labels)
|
||||
}
|
||||
|
||||
runtime.ReadMemStats(&ms)
|
||||
|
||||
if got := ms.Alloc; alloc != got {
|
||||
t.Fatal("expected labelsToSignature with empty labels not to perform allocations")
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkLabelToSignature(b *testing.B) {
|
||||
for i := 0; i < b.N; i++ {
|
||||
testLabelsToSignature(b)
|
||||
|
|
Loading…
Reference in New Issue