2017-04-10 18:59:45 +00:00
|
|
|
// Copyright 2017 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.
|
|
|
|
|
2017-03-06 13:34:49 +00:00
|
|
|
package labels
|
|
|
|
|
|
|
|
import (
|
2017-12-14 06:46:46 +00:00
|
|
|
"bufio"
|
2017-03-06 13:34:49 +00:00
|
|
|
"fmt"
|
|
|
|
"math/rand"
|
|
|
|
"os"
|
2017-03-17 09:07:10 +00:00
|
|
|
"sort"
|
2017-12-14 06:46:46 +00:00
|
|
|
"strings"
|
2017-03-06 13:34:49 +00:00
|
|
|
"testing"
|
|
|
|
|
2017-12-07 01:13:00 +00:00
|
|
|
"github.com/prometheus/tsdb/testutil"
|
2017-03-06 13:34:49 +00:00
|
|
|
)
|
|
|
|
|
2017-04-28 13:53:48 +00:00
|
|
|
func TestCompareAndEquals(t *testing.T) {
|
2017-03-06 13:34:49 +00:00
|
|
|
cases := []struct {
|
|
|
|
a, b []Label
|
|
|
|
res int
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
a: []Label{},
|
|
|
|
b: []Label{},
|
|
|
|
res: 0,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
a: []Label{{"a", ""}},
|
|
|
|
b: []Label{{"a", ""}, {"b", ""}},
|
|
|
|
res: -1,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
a: []Label{{"a", ""}},
|
|
|
|
b: []Label{{"a", ""}},
|
|
|
|
res: 0,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
a: []Label{{"aa", ""}, {"aa", ""}},
|
|
|
|
b: []Label{{"aa", ""}, {"ab", ""}},
|
|
|
|
res: -1,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
a: []Label{{"aa", ""}, {"abb", ""}},
|
|
|
|
b: []Label{{"aa", ""}, {"ab", ""}},
|
|
|
|
res: 1,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
a: []Label{
|
|
|
|
{"__name__", "go_gc_duration_seconds"},
|
|
|
|
{"job", "prometheus"},
|
|
|
|
{"quantile", "0.75"},
|
|
|
|
},
|
|
|
|
b: []Label{
|
|
|
|
{"__name__", "go_gc_duration_seconds"},
|
|
|
|
{"job", "prometheus"},
|
|
|
|
{"quantile", "1"},
|
|
|
|
},
|
|
|
|
res: -1,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
a: []Label{
|
|
|
|
{"handler", "prometheus"},
|
|
|
|
{"instance", "localhost:9090"},
|
|
|
|
},
|
|
|
|
b: []Label{
|
|
|
|
{"handler", "query"},
|
|
|
|
{"instance", "localhost:9090"},
|
|
|
|
},
|
|
|
|
res: -1,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, c := range cases {
|
|
|
|
// Use constructor to ensure sortedness.
|
|
|
|
a, b := New(c.a...), New(c.b...)
|
|
|
|
|
2017-12-07 01:13:00 +00:00
|
|
|
testutil.Equals(t, c.res, Compare(a, b))
|
|
|
|
testutil.Equals(t, c.res == 0, a.Equals(b))
|
2017-03-06 13:34:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-17 09:07:10 +00:00
|
|
|
func BenchmarkSliceSort(b *testing.B) {
|
|
|
|
lbls, err := readPrometheusLabels("../testdata/1m.series", 900000)
|
2017-12-07 01:13:00 +00:00
|
|
|
testutil.Ok(b, err)
|
2017-03-06 13:34:49 +00:00
|
|
|
|
2017-03-17 09:07:10 +00:00
|
|
|
for len(lbls) < 20e6 {
|
|
|
|
lbls = append(lbls, lbls...)
|
|
|
|
}
|
|
|
|
for i := range lbls {
|
|
|
|
j := rand.Intn(i + 1)
|
|
|
|
lbls[i], lbls[j] = lbls[j], lbls[i]
|
|
|
|
}
|
2017-03-06 13:34:49 +00:00
|
|
|
|
2017-03-17 09:07:10 +00:00
|
|
|
for _, k := range []int{
|
|
|
|
100, 5000, 50000, 300000, 900000, 5e6, 20e6,
|
|
|
|
} {
|
|
|
|
b.Run(fmt.Sprintf("%d", k), func(b *testing.B) {
|
|
|
|
b.ReportAllocs()
|
2017-03-06 13:34:49 +00:00
|
|
|
|
2017-03-17 09:07:10 +00:00
|
|
|
for a := 0; a < b.N; a++ {
|
|
|
|
b.StopTimer()
|
|
|
|
cl := make(Slice, k)
|
|
|
|
copy(cl, Slice(lbls[:k]))
|
|
|
|
b.StartTimer()
|
2017-03-06 13:34:49 +00:00
|
|
|
|
2017-03-17 09:07:10 +00:00
|
|
|
sort.Sort(cl)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2017-03-06 13:34:49 +00:00
|
|
|
}
|
|
|
|
|
2017-03-17 09:07:10 +00:00
|
|
|
func readPrometheusLabels(fn string, n int) ([]Labels, error) {
|
|
|
|
f, err := os.Open(fn)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer f.Close()
|
2017-03-06 13:34:49 +00:00
|
|
|
|
2017-12-14 06:46:46 +00:00
|
|
|
scanner := bufio.NewScanner(f)
|
2017-03-06 13:34:49 +00:00
|
|
|
|
2017-03-17 09:07:10 +00:00
|
|
|
var mets []Labels
|
|
|
|
hashes := map[uint64]struct{}{}
|
2017-12-14 06:46:46 +00:00
|
|
|
i := 0
|
2017-03-06 13:34:49 +00:00
|
|
|
|
2017-12-14 06:46:46 +00:00
|
|
|
for scanner.Scan() && i < n {
|
2017-03-17 09:07:10 +00:00
|
|
|
m := make(Labels, 0, 10)
|
2017-12-14 06:46:46 +00:00
|
|
|
|
|
|
|
r := strings.NewReplacer("\"", "", "{", "", "}", "")
|
|
|
|
s := r.Replace(scanner.Text())
|
|
|
|
|
|
|
|
labelChunks := strings.Split(s, ",")
|
|
|
|
for _, labelChunk := range labelChunks {
|
|
|
|
split := strings.Split(labelChunk, ":")
|
|
|
|
m = append(m, Label{Name: split[0], Value: split[1]})
|
|
|
|
}
|
2017-12-18 23:32:39 +00:00
|
|
|
// Order of the k/v labels matters, don't assume we'll always receive them already sorted.
|
|
|
|
sort.Sort(m)
|
2017-03-17 09:07:10 +00:00
|
|
|
h := m.Hash()
|
|
|
|
if _, ok := hashes[h]; ok {
|
|
|
|
continue
|
2017-03-06 13:34:49 +00:00
|
|
|
}
|
2017-03-17 09:07:10 +00:00
|
|
|
mets = append(mets, m)
|
|
|
|
hashes[h] = struct{}{}
|
|
|
|
i++
|
2017-03-06 13:34:49 +00:00
|
|
|
}
|
2017-03-17 09:07:10 +00:00
|
|
|
return mets, nil
|
2017-03-06 13:34:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkLabelSetFromMap(b *testing.B) {
|
|
|
|
m := map[string]string{
|
|
|
|
"job": "node",
|
|
|
|
"instance": "123.123.1.211:9090",
|
|
|
|
"path": "/api/v1/namespaces/<namespace>/deployments/<name>",
|
|
|
|
"method": "GET",
|
|
|
|
"namespace": "system",
|
|
|
|
"status": "500",
|
|
|
|
}
|
|
|
|
var ls Labels
|
|
|
|
b.ReportAllocs()
|
|
|
|
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
ls = FromMap(m)
|
|
|
|
}
|
|
|
|
_ = ls
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkMapFromLabels(b *testing.B) {
|
|
|
|
m := map[string]string{
|
|
|
|
"job": "node",
|
|
|
|
"instance": "123.123.1.211:9090",
|
|
|
|
"path": "/api/v1/namespaces/<namespace>/deployments/<name>",
|
|
|
|
"method": "GET",
|
|
|
|
"namespace": "system",
|
|
|
|
"status": "500",
|
|
|
|
}
|
|
|
|
ls := FromMap(m)
|
|
|
|
|
|
|
|
b.ResetTimer()
|
|
|
|
b.ReportAllocs()
|
|
|
|
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
m = ls.Map()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkLabelSetEquals(b *testing.B) {
|
|
|
|
// The vast majority of comparisons will be against a matching label set.
|
|
|
|
m := map[string]string{
|
|
|
|
"job": "node",
|
|
|
|
"instance": "123.123.1.211:9090",
|
|
|
|
"path": "/api/v1/namespaces/<namespace>/deployments/<name>",
|
|
|
|
"method": "GET",
|
|
|
|
"namespace": "system",
|
|
|
|
"status": "500",
|
|
|
|
}
|
|
|
|
ls := FromMap(m)
|
|
|
|
var res bool
|
|
|
|
|
|
|
|
b.ResetTimer()
|
|
|
|
b.ReportAllocs()
|
|
|
|
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
res = ls.Equals(ls)
|
|
|
|
}
|
|
|
|
_ = res
|
|
|
|
}
|
2017-09-05 09:45:18 +00:00
|
|
|
|
|
|
|
func BenchmarkLabelSetHash(b *testing.B) {
|
|
|
|
// The vast majority of comparisons will be against a matching label set.
|
|
|
|
m := map[string]string{
|
|
|
|
"job": "node",
|
|
|
|
"instance": "123.123.1.211:9090",
|
|
|
|
"path": "/api/v1/namespaces/<namespace>/deployments/<name>",
|
|
|
|
"method": "GET",
|
|
|
|
"namespace": "system",
|
|
|
|
"status": "500",
|
|
|
|
}
|
|
|
|
ls := FromMap(m)
|
|
|
|
var res uint64
|
|
|
|
|
|
|
|
b.ResetTimer()
|
|
|
|
b.ReportAllocs()
|
|
|
|
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
res += ls.Hash()
|
|
|
|
}
|
|
|
|
fmt.Println(res)
|
|
|
|
}
|