2013-04-03 20:33:32 +04:00
|
|
|
// Copyright (c) 2013, Prometheus Team
|
2013-01-19 17:48:30 +04:00
|
|
|
// All rights reserved.
|
|
|
|
//
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2013-04-03 20:33:32 +04:00
|
|
|
package prometheus
|
2013-01-19 17:48:30 +04:00
|
|
|
|
|
|
|
import (
|
2013-09-11 19:16:34 +04:00
|
|
|
"runtime"
|
2013-01-19 17:48:30 +04:00
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
2013-04-03 20:33:32 +04:00
|
|
|
func testLabelsToSignature(t tester) {
|
2013-01-19 17:48:30 +04:00
|
|
|
var scenarios = []struct {
|
|
|
|
in map[string]string
|
2013-06-27 20:46:16 +04:00
|
|
|
out uint64
|
2013-01-19 17:48:30 +04:00
|
|
|
}{
|
|
|
|
{
|
|
|
|
in: map[string]string{},
|
2013-06-27 20:46:16 +04:00
|
|
|
out: 14695981039346656037,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
in: map[string]string{"name": "garland, briggs", "fear": "love is not enough"},
|
|
|
|
out: 15753083015552662396,
|
2013-01-19 17:48:30 +04:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, scenario := range scenarios {
|
2013-04-03 20:33:32 +04:00
|
|
|
actual := labelsToSignature(scenario.in)
|
2013-01-19 17:48:30 +04:00
|
|
|
|
|
|
|
if actual != scenario.out {
|
|
|
|
t.Errorf("%d. expected %s, got %s", i, scenario.out, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestLabelToSignature(t *testing.T) {
|
|
|
|
testLabelsToSignature(t)
|
|
|
|
}
|
|
|
|
|
2013-09-11 19:16:34 +04:00
|
|
|
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")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-19 17:48:30 +04:00
|
|
|
func BenchmarkLabelToSignature(b *testing.B) {
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
testLabelsToSignature(b)
|
|
|
|
}
|
|
|
|
}
|