Fix test import of labels, simplify constructor names

pull/5805/head
Fabian Reinartz 8 years ago
parent da2beb3e6d
commit 0a94f58f1a

@ -1,6 +1,10 @@
package tsdb
import "testing"
import (
"testing"
"github.com/fabxc/tsdb/labels"
)
func BenchmarkLabelSetFromMap(b *testing.B) {
m := map[string]string{
@ -11,11 +15,11 @@ func BenchmarkLabelSetFromMap(b *testing.B) {
"namespace": "system",
"status": "500",
}
var ls Labels
var ls labels.Labels
b.ReportAllocs()
for i := 0; i < b.N; i++ {
ls = LabelsFromMap(m)
ls = labels.FromMap(m)
}
_ = ls
}
@ -29,7 +33,7 @@ func BenchmarkMapFromLabels(b *testing.B) {
"namespace": "system",
"status": "500",
}
ls := LabelsFromMap(m)
ls := labels.FromMap(m)
b.ResetTimer()
b.ReportAllocs()
@ -49,7 +53,7 @@ func BenchmarkLabelSetEquals(b *testing.B) {
"namespace": "system",
"status": "500",
}
ls := LabelsFromMap(m)
ls := labels.FromMap(m)
var res bool
b.ResetTimer()

@ -67,9 +67,9 @@ func (ls Labels) Map() map[string]string {
return m
}
// NewLabels returns a sorted Labels from the given labels.
// New returns a sorted Labels from the given labels.
// The caller has to guarantee that all label names are unique.
func NewLabels(ls ...Label) Labels {
func New(ls ...Label) Labels {
set := make(Labels, 0, len(ls))
for _, l := range ls {
set = append(set, l)
@ -79,11 +79,11 @@ func NewLabels(ls ...Label) Labels {
return set
}
// LabelsFromMap returns new sorted Labels from the given map.
func LabelsFromMap(m map[string]string) Labels {
// FromMap returns new sorted Labels from the given map.
func FromMap(m map[string]string) Labels {
l := make([]Label, 0, len(m))
for k, v := range m {
l = append(l, Label{Name: k, Value: v})
}
return NewLabels(l...)
return New(l...)
}

@ -689,7 +689,6 @@ func (b *BufferedSeriesIterator) Seek(t int64) bool {
if !ok {
return false
}
b.buf.add(b.it.Values())
for b.Next() {
if ts, _ := b.Values(); ts >= t {

@ -5,6 +5,7 @@ import (
"sort"
"testing"
"github.com/fabxc/tsdb/labels"
"github.com/stretchr/testify/require"
)
@ -21,11 +22,11 @@ func (m *mockSeriesIterator) Next() bool { return m.next() }
func (m *mockSeriesIterator) Err() error { return m.err() }
type mockSeries struct {
labels func() Labels
labels func() labels.Labels
iterator func() SeriesIterator
}
func (m *mockSeries) Labels() Labels { return m.labels() }
func (m *mockSeries) Labels() labels.Labels { return m.labels() }
func (m *mockSeries) Iterator() SeriesIterator { return m.iterator() }
type listSeriesIterator struct {
@ -60,17 +61,6 @@ func (it *listSeriesIterator) Err() error {
return nil
}
// func TestChainedSeriesIterator(t *testing.T) {
// cases := []struct {
// series []Series
// }{}
// for _, c := range cases {
// }
// }
type mockSeriesSet struct {
next func() bool
series func() Series
@ -98,7 +88,7 @@ func newListSeriesSet(list []Series) *mockSeriesSet {
func TestShardSeriesSet(t *testing.T) {
newSeries := func(l map[string]string, s []sample) Series {
return &mockSeries{
labels: func() Labels { return LabelsFromMap(l) },
labels: func() labels.Labels { return labels.FromMap(l) },
iterator: func() SeriesIterator { return newListSeriesIterator(s) },
}
}
@ -183,41 +173,41 @@ func expandSeriesIterator(it SeriesIterator) (r []sample, err error) {
func TestCompareLabels(t *testing.T) {
cases := []struct {
a, b []Label
a, b []labels.Label
res int
}{
{
a: []Label{},
b: []Label{},
a: []labels.Label{},
b: []labels.Label{},
res: 0,
},
{
a: []Label{{"a", ""}},
b: []Label{{"a", ""}, {"b", ""}},
a: []labels.Label{{"a", ""}},
b: []labels.Label{{"a", ""}, {"b", ""}},
res: -1,
},
{
a: []Label{{"a", ""}},
b: []Label{{"a", ""}},
a: []labels.Label{{"a", ""}},
b: []labels.Label{{"a", ""}},
res: 0,
},
{
a: []Label{{"aa", ""}, {"aa", ""}},
b: []Label{{"aa", ""}, {"ab", ""}},
a: []labels.Label{{"aa", ""}, {"aa", ""}},
b: []labels.Label{{"aa", ""}, {"ab", ""}},
res: -1,
},
{
a: []Label{{"aa", ""}, {"abb", ""}},
b: []Label{{"aa", ""}, {"ab", ""}},
a: []labels.Label{{"aa", ""}, {"abb", ""}},
b: []labels.Label{{"aa", ""}, {"ab", ""}},
res: 1,
},
{
a: []Label{
a: []labels.Label{
{"__name__", "go_gc_duration_seconds"},
{"job", "prometheus"},
{"quantile", "0.75"},
},
b: []Label{
b: []labels.Label{
{"__name__", "go_gc_duration_seconds"},
{"job", "prometheus"},
{"quantile", "1"},
@ -227,7 +217,7 @@ func TestCompareLabels(t *testing.T) {
}
for _, c := range cases {
// Use constructor to ensure sortedness.
a, b := NewLabels(c.a...), NewLabels(c.b...)
a, b := labels.New(c.a...), labels.New(c.b...)
require.Equal(t, c.res, compareLabels(a, b))
}

@ -42,7 +42,7 @@ func BenchmarkLabelsClone(b *testing.B) {
"datacenter": "eu-west-1",
"pod_name": "abcdef-99999-defee",
}
l := labels.LabelsFromMap(m)
l := labels.FromMap(m)
for i := 0; i < b.N; i++ {
res := make(labels.Labels, len(l))
@ -89,7 +89,7 @@ func BenchmarkLabelSetAccess(b *testing.B) {
"datacenter": "eu-west-1",
"pod_name": "abcdef-99999-defee",
}
ls := labels.LabelsFromMap(m)
ls := labels.FromMap(m)
var v string

Loading…
Cancel
Save