2015-09-04 14:27:44 +00:00
|
|
|
// Copyright 2014 The Prometheus Authors
|
|
|
|
// 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
|
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
|
|
|
|
package model
|
|
|
|
|
|
|
|
import (
|
|
|
|
"runtime"
|
|
|
|
"sync"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestLabelsToSignature(t *testing.T) {
|
|
|
|
var scenarios = []struct {
|
|
|
|
in map[string]string
|
|
|
|
out uint64
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
in: map[string]string{},
|
|
|
|
out: 14695981039346656037,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
in: map[string]string{"name": "garland, briggs", "fear": "love is not enough"},
|
|
|
|
out: 5799056148416392346,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, scenario := range scenarios {
|
|
|
|
actual := LabelsToSignature(scenario.in)
|
|
|
|
|
|
|
|
if actual != scenario.out {
|
|
|
|
t.Errorf("%d. expected %d, got %d", i, scenario.out, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMetricToFingerprint(t *testing.T) {
|
|
|
|
var scenarios = []struct {
|
2017-11-05 15:06:22 +00:00
|
|
|
in LabelSet
|
2015-09-04 14:27:44 +00:00
|
|
|
out Fingerprint
|
|
|
|
}{
|
|
|
|
{
|
2017-11-05 15:06:22 +00:00
|
|
|
in: LabelSet{},
|
2015-09-04 14:27:44 +00:00
|
|
|
out: 14695981039346656037,
|
|
|
|
},
|
|
|
|
{
|
2017-11-05 15:06:22 +00:00
|
|
|
in: LabelSet{"name": "garland, briggs", "fear": "love is not enough"},
|
2015-09-04 14:27:44 +00:00
|
|
|
out: 5799056148416392346,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, scenario := range scenarios {
|
2017-11-05 15:06:22 +00:00
|
|
|
actual := labelSetToFingerprint(scenario.in)
|
2015-09-04 14:27:44 +00:00
|
|
|
|
|
|
|
if actual != scenario.out {
|
|
|
|
t.Errorf("%d. expected %d, got %d", i, scenario.out, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMetricToFastFingerprint(t *testing.T) {
|
|
|
|
var scenarios = []struct {
|
2017-11-05 15:06:22 +00:00
|
|
|
in LabelSet
|
2015-09-04 14:27:44 +00:00
|
|
|
out Fingerprint
|
|
|
|
}{
|
|
|
|
{
|
2017-11-05 15:06:22 +00:00
|
|
|
in: LabelSet{},
|
2015-09-04 14:27:44 +00:00
|
|
|
out: 14695981039346656037,
|
|
|
|
},
|
|
|
|
{
|
2017-11-05 15:06:22 +00:00
|
|
|
in: LabelSet{"name": "garland, briggs", "fear": "love is not enough"},
|
2015-09-04 14:27:44 +00:00
|
|
|
out: 12952432476264840823,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, scenario := range scenarios {
|
2017-11-05 15:06:22 +00:00
|
|
|
actual := labelSetToFastFingerprint(scenario.in)
|
2015-09-04 14:27:44 +00:00
|
|
|
|
|
|
|
if actual != scenario.out {
|
|
|
|
t.Errorf("%d. expected %d, got %d", i, scenario.out, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSignatureForLabels(t *testing.T) {
|
|
|
|
var scenarios = []struct {
|
|
|
|
in Metric
|
|
|
|
labels LabelNames
|
|
|
|
out uint64
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
in: Metric{},
|
|
|
|
labels: nil,
|
|
|
|
out: 14695981039346656037,
|
|
|
|
},
|
2017-11-05 15:06:22 +00:00
|
|
|
{
|
|
|
|
in: Metric{},
|
|
|
|
labels: LabelNames{"empty"},
|
|
|
|
out: 7187873163539638612,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
in: Metric{"name": "garland, briggs", "fear": "love is not enough"},
|
|
|
|
labels: LabelNames{"empty"},
|
|
|
|
out: 7187873163539638612,
|
|
|
|
},
|
2015-09-04 14:27:44 +00:00
|
|
|
{
|
|
|
|
in: Metric{"name": "garland, briggs", "fear": "love is not enough"},
|
|
|
|
labels: LabelNames{"fear", "name"},
|
|
|
|
out: 5799056148416392346,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
in: Metric{"name": "garland, briggs", "fear": "love is not enough", "foo": "bar"},
|
|
|
|
labels: LabelNames{"fear", "name"},
|
|
|
|
out: 5799056148416392346,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
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,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, scenario := range scenarios {
|
2017-11-05 15:06:22 +00:00
|
|
|
actual := SignatureForLabels(scenario.in, scenario.labels...)
|
2015-09-04 14:27:44 +00:00
|
|
|
|
|
|
|
if actual != scenario.out {
|
|
|
|
t.Errorf("%d. expected %d, got %d", i, scenario.out, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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: 5799056148416392346,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
in: Metric{"name": "garland, briggs", "fear": "love is not enough"},
|
|
|
|
labels: map[LabelName]struct{}{},
|
|
|
|
out: 5799056148416392346,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
in: Metric{"name": "garland, briggs", "fear": "love is not enough"},
|
|
|
|
labels: nil,
|
|
|
|
out: 5799056148416392346,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, scenario := range scenarios {
|
|
|
|
actual := SignatureWithoutLabels(scenario.in, scenario.labels)
|
|
|
|
|
|
|
|
if actual != scenario.out {
|
|
|
|
t.Errorf("%d. expected %d, got %d", i, scenario.out, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func benchmarkLabelToSignature(b *testing.B, l map[string]string, e uint64) {
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
if a := LabelsToSignature(l); a != e {
|
|
|
|
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) {
|
|
|
|
benchmarkLabelToSignature(b, map[string]string{"first-label": "first-label-value"}, 5146282821936882169)
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkLabelToSignatureDouble(b *testing.B) {
|
|
|
|
benchmarkLabelToSignature(b, map[string]string{"first-label": "first-label-value", "second-label": "second-label-value"}, 3195800080984914717)
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkLabelToSignatureTriple(b *testing.B) {
|
|
|
|
benchmarkLabelToSignature(b, map[string]string{"first-label": "first-label-value", "second-label": "second-label-value", "third-label": "third-label-value"}, 13843036195897128121)
|
|
|
|
}
|
|
|
|
|
2017-11-05 15:06:22 +00:00
|
|
|
func benchmarkMetricToFingerprint(b *testing.B, ls LabelSet, e Fingerprint) {
|
2015-09-04 14:27:44 +00:00
|
|
|
for i := 0; i < b.N; i++ {
|
2017-11-05 15:06:22 +00:00
|
|
|
if a := labelSetToFingerprint(ls); a != e {
|
|
|
|
b.Fatalf("expected signature of %d for %s, got %d", e, ls, a)
|
2015-09-04 14:27:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkMetricToFingerprintScalar(b *testing.B) {
|
|
|
|
benchmarkMetricToFingerprint(b, nil, 14695981039346656037)
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkMetricToFingerprintSingle(b *testing.B) {
|
2017-11-05 15:06:22 +00:00
|
|
|
benchmarkMetricToFingerprint(b, LabelSet{"first-label": "first-label-value"}, 5146282821936882169)
|
2015-09-04 14:27:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkMetricToFingerprintDouble(b *testing.B) {
|
2017-11-05 15:06:22 +00:00
|
|
|
benchmarkMetricToFingerprint(b, LabelSet{"first-label": "first-label-value", "second-label": "second-label-value"}, 3195800080984914717)
|
2015-09-04 14:27:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkMetricToFingerprintTriple(b *testing.B) {
|
2017-11-05 15:06:22 +00:00
|
|
|
benchmarkMetricToFingerprint(b, LabelSet{"first-label": "first-label-value", "second-label": "second-label-value", "third-label": "third-label-value"}, 13843036195897128121)
|
2015-09-04 14:27:44 +00:00
|
|
|
}
|
|
|
|
|
2017-11-05 15:06:22 +00:00
|
|
|
func benchmarkMetricToFastFingerprint(b *testing.B, ls LabelSet, e Fingerprint) {
|
2015-09-04 14:27:44 +00:00
|
|
|
for i := 0; i < b.N; i++ {
|
2017-11-05 15:06:22 +00:00
|
|
|
if a := labelSetToFastFingerprint(ls); a != e {
|
|
|
|
b.Fatalf("expected signature of %d for %s, got %d", e, ls, a)
|
2015-09-04 14:27:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkMetricToFastFingerprintScalar(b *testing.B) {
|
|
|
|
benchmarkMetricToFastFingerprint(b, nil, 14695981039346656037)
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkMetricToFastFingerprintSingle(b *testing.B) {
|
2017-11-05 15:06:22 +00:00
|
|
|
benchmarkMetricToFastFingerprint(b, LabelSet{"first-label": "first-label-value"}, 5147259542624943964)
|
2015-09-04 14:27:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkMetricToFastFingerprintDouble(b *testing.B) {
|
2017-11-05 15:06:22 +00:00
|
|
|
benchmarkMetricToFastFingerprint(b, LabelSet{"first-label": "first-label-value", "second-label": "second-label-value"}, 18269973311206963528)
|
2015-09-04 14:27:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkMetricToFastFingerprintTriple(b *testing.B) {
|
2017-11-05 15:06:22 +00:00
|
|
|
benchmarkMetricToFastFingerprint(b, LabelSet{"first-label": "first-label-value", "second-label": "second-label-value", "third-label": "third-label-value"}, 15738406913934009676)
|
2015-09-04 14:27:44 +00:00
|
|
|
}
|
|
|
|
|
2017-11-05 15:06:22 +00:00
|
|
|
func BenchmarkEmptyLabelSignature(b *testing.B) {
|
2015-09-04 14:27:44 +00:00
|
|
|
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 {
|
2017-11-05 15:06:22 +00:00
|
|
|
b.Fatal("expected LabelsToSignature with empty labels not to perform allocations")
|
2015-09-04 14:27:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-05 15:06:22 +00:00
|
|
|
func benchmarkMetricToFastFingerprintConc(b *testing.B, ls LabelSet, e Fingerprint, concLevel int) {
|
2015-09-04 14:27:44 +00:00
|
|
|
var start, end sync.WaitGroup
|
|
|
|
start.Add(1)
|
|
|
|
end.Add(concLevel)
|
|
|
|
|
|
|
|
for i := 0; i < concLevel; i++ {
|
|
|
|
go func() {
|
|
|
|
start.Wait()
|
|
|
|
for j := b.N / concLevel; j >= 0; j-- {
|
2017-11-05 15:06:22 +00:00
|
|
|
if a := labelSetToFastFingerprint(ls); a != e {
|
|
|
|
b.Fatalf("expected signature of %d for %s, got %d", e, ls, a)
|
2015-09-04 14:27:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
end.Done()
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
b.ResetTimer()
|
|
|
|
start.Done()
|
|
|
|
end.Wait()
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkMetricToFastFingerprintTripleConc1(b *testing.B) {
|
2017-11-05 15:06:22 +00:00
|
|
|
benchmarkMetricToFastFingerprintConc(b, LabelSet{"first-label": "first-label-value", "second-label": "second-label-value", "third-label": "third-label-value"}, 15738406913934009676, 1)
|
2015-09-04 14:27:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkMetricToFastFingerprintTripleConc2(b *testing.B) {
|
2017-11-05 15:06:22 +00:00
|
|
|
benchmarkMetricToFastFingerprintConc(b, LabelSet{"first-label": "first-label-value", "second-label": "second-label-value", "third-label": "third-label-value"}, 15738406913934009676, 2)
|
2015-09-04 14:27:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkMetricToFastFingerprintTripleConc4(b *testing.B) {
|
2017-11-05 15:06:22 +00:00
|
|
|
benchmarkMetricToFastFingerprintConc(b, LabelSet{"first-label": "first-label-value", "second-label": "second-label-value", "third-label": "third-label-value"}, 15738406913934009676, 4)
|
2015-09-04 14:27:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkMetricToFastFingerprintTripleConc8(b *testing.B) {
|
2017-11-05 15:06:22 +00:00
|
|
|
benchmarkMetricToFastFingerprintConc(b, LabelSet{"first-label": "first-label-value", "second-label": "second-label-value", "third-label": "third-label-value"}, 15738406913934009676, 8)
|
2015-09-04 14:27:44 +00:00
|
|
|
}
|