2015-02-02 17:14:36 +03:00
|
|
|
// Copyright 2014 The Prometheus Authors
|
2014-05-07 22:08:33 +04:00
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
2013-01-19 17:48:30 +04:00
|
|
|
//
|
2014-05-07 22:08:33 +04:00
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
2013-01-19 17:48:30 +04:00
|
|
|
|
2014-04-25 14:22:17 +04:00
|
|
|
package model
|
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"
|
|
|
|
)
|
|
|
|
|
2015-03-03 15:23:11 +03:00
|
|
|
func TestLabelsToSignature(t *testing.T) {
|
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"},
|
2014-06-26 15:58:15 +04:00
|
|
|
out: 12952432476264840823,
|
2013-01-19 17:48:30 +04:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, scenario := range scenarios {
|
2014-04-09 22:38:51 +04:00
|
|
|
actual := LabelsToSignature(scenario.in)
|
2013-01-19 17:48:30 +04:00
|
|
|
|
|
|
|
if actual != scenario.out {
|
2014-04-02 15:31:22 +04:00
|
|
|
t.Errorf("%d. expected %d, got %d", i, scenario.out, actual)
|
2013-01-19 17:48:30 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-03 15:23:11 +03:00
|
|
|
func TestMetricToFingerprint(t *testing.T) {
|
|
|
|
var scenarios = []struct {
|
|
|
|
in Metric
|
|
|
|
out Fingerprint
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
in: Metric{},
|
|
|
|
out: 14695981039346656037,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
in: Metric{"name": "garland, briggs", "fear": "love is not enough"},
|
|
|
|
out: 12952432476264840823,
|
|
|
|
},
|
2013-09-11 19:16:34 +04:00
|
|
|
}
|
|
|
|
|
2015-03-03 15:23:11 +03:00
|
|
|
for i, scenario := range scenarios {
|
|
|
|
actual := metricToFingerprint(scenario.in)
|
2013-09-11 19:16:34 +04:00
|
|
|
|
2015-03-03 15:23:11 +03:00
|
|
|
if actual != scenario.out {
|
|
|
|
t.Errorf("%d. expected %d, got %d", i, scenario.out, actual)
|
|
|
|
}
|
2013-09-11 19:16:34 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-03 15:23:11 +03:00
|
|
|
func TestSignatureForLabels(t *testing.T) {
|
|
|
|
var scenarios = []struct {
|
|
|
|
in Metric
|
|
|
|
labels LabelNames
|
|
|
|
out uint64
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
in: Metric{},
|
|
|
|
labels: nil,
|
|
|
|
out: 14695981039346656037,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
in: Metric{"name": "garland, briggs", "fear": "love is not enough"},
|
|
|
|
labels: LabelNames{"fear", "name"},
|
|
|
|
out: 12952432476264840823,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
in: Metric{"name": "garland, briggs", "fear": "love is not enough", "foo": "bar"},
|
|
|
|
labels: LabelNames{"fear", "name"},
|
|
|
|
out: 12952432476264840823,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
in: Metric{"name": "garland, briggs", "fear": "love is not enough"},
|
|
|
|
labels: LabelNames{},
|
|
|
|
out: 14695981039346656037,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
in: Metric{"name": "garland, briggs", "fear": "love is not enough"},
|
|
|
|
labels: nil,
|
|
|
|
out: 14695981039346656037,
|
|
|
|
},
|
2013-01-19 17:48:30 +04:00
|
|
|
}
|
2014-04-14 20:45:16 +04:00
|
|
|
|
2015-03-03 15:23:11 +03:00
|
|
|
for i, scenario := range scenarios {
|
|
|
|
actual := SignatureForLabels(scenario.in, scenario.labels)
|
|
|
|
|
|
|
|
if actual != scenario.out {
|
|
|
|
t.Errorf("%d. expected %d, got %d", i, scenario.out, actual)
|
2014-04-14 20:45:16 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-03 15:23:11 +03:00
|
|
|
func TestSignatureWithoutLabels(t *testing.T) {
|
|
|
|
var scenarios = []struct {
|
|
|
|
in Metric
|
|
|
|
labels map[LabelName]struct{}
|
|
|
|
out uint64
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
in: Metric{},
|
|
|
|
labels: nil,
|
|
|
|
out: 14695981039346656037,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
in: Metric{"name": "garland, briggs", "fear": "love is not enough"},
|
|
|
|
labels: map[LabelName]struct{}{"fear": struct{}{}, "name": struct{}{}},
|
|
|
|
out: 14695981039346656037,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
in: Metric{"name": "garland, briggs", "fear": "love is not enough", "foo": "bar"},
|
|
|
|
labels: map[LabelName]struct{}{"foo": struct{}{}},
|
|
|
|
out: 12952432476264840823,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
in: Metric{"name": "garland, briggs", "fear": "love is not enough"},
|
|
|
|
labels: map[LabelName]struct{}{},
|
|
|
|
out: 12952432476264840823,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
in: Metric{"name": "garland, briggs", "fear": "love is not enough"},
|
|
|
|
labels: nil,
|
|
|
|
out: 12952432476264840823,
|
|
|
|
},
|
|
|
|
}
|
2014-04-14 20:45:16 +04:00
|
|
|
|
2015-03-03 15:23:11 +03:00
|
|
|
for i, scenario := range scenarios {
|
|
|
|
actual := SignatureWithoutLabels(scenario.in, scenario.labels)
|
2014-04-14 20:45:16 +04:00
|
|
|
|
2015-03-03 15:23:11 +03:00
|
|
|
if actual != scenario.out {
|
|
|
|
t.Errorf("%d. expected %d, got %d", i, scenario.out, actual)
|
|
|
|
}
|
|
|
|
}
|
2014-04-14 20:45:16 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
func benchmarkLabelToSignature(b *testing.B, l map[string]string, e uint64) {
|
|
|
|
for i := 0; i < b.N; i++ {
|
2014-04-09 22:38:51 +04:00
|
|
|
if a := LabelsToSignature(l); a != e {
|
2014-04-14 20:45:16 +04:00
|
|
|
b.Fatalf("expected signature of %d for %s, got %d", e, l, a)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkLabelToSignatureScalar(b *testing.B) {
|
|
|
|
benchmarkLabelToSignature(b, nil, 14695981039346656037)
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkLabelToSignatureSingle(b *testing.B) {
|
2014-06-26 15:58:15 +04:00
|
|
|
benchmarkLabelToSignature(b, map[string]string{"first-label": "first-label-value"}, 5147259542624943964)
|
2014-04-14 20:45:16 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkLabelToSignatureDouble(b *testing.B) {
|
2014-06-26 15:58:15 +04:00
|
|
|
benchmarkLabelToSignature(b, map[string]string{"first-label": "first-label-value", "second-label": "second-label-value"}, 18269973311206963528)
|
2014-04-14 20:45:16 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkLabelToSignatureTriple(b *testing.B) {
|
2014-06-26 15:58:15 +04:00
|
|
|
benchmarkLabelToSignature(b, map[string]string{"first-label": "first-label-value", "second-label": "second-label-value", "third-label": "third-label-value"}, 15738406913934009676)
|
2014-04-14 20:45:16 +04:00
|
|
|
}
|
2015-03-03 15:23:11 +03:00
|
|
|
|
|
|
|
func benchmarkMetricToFingerprint(b *testing.B, m Metric, e Fingerprint) {
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
if a := metricToFingerprint(m); a != e {
|
|
|
|
b.Fatalf("expected signature of %d for %s, got %d", e, m, a)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkMetricToFingerprintScalar(b *testing.B) {
|
|
|
|
benchmarkMetricToFingerprint(b, nil, 14695981039346656037)
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkMetricToFingerprintSingle(b *testing.B) {
|
|
|
|
benchmarkMetricToFingerprint(b, Metric{"first-label": "first-label-value"}, 5147259542624943964)
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkMetricToFingerprintDouble(b *testing.B) {
|
|
|
|
benchmarkMetricToFingerprint(b, Metric{"first-label": "first-label-value", "second-label": "second-label-value"}, 18269973311206963528)
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkMetricToFingerprintTriple(b *testing.B) {
|
|
|
|
benchmarkMetricToFingerprint(b, Metric{"first-label": "first-label-value", "second-label": "second-label-value", "third-label": "third-label-value"}, 15738406913934009676)
|
|
|
|
}
|
|
|
|
|
|
|
|
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")
|
|
|
|
}
|
|
|
|
}
|