2017-01-05 14:13:01 +00:00
|
|
|
package tsdb
|
|
|
|
|
|
|
|
import (
|
2017-02-09 00:13:16 +00:00
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
2017-01-05 14:13:01 +00:00
|
|
|
"testing"
|
2017-02-14 23:54:52 +00:00
|
|
|
"unsafe"
|
2017-01-05 14:13:01 +00:00
|
|
|
|
2017-02-14 23:54:52 +00:00
|
|
|
"github.com/fabxc/tsdb/labels"
|
2017-03-08 15:54:13 +00:00
|
|
|
"github.com/pkg/errors"
|
2017-02-14 23:54:52 +00:00
|
|
|
|
|
|
|
promlabels "github.com/prometheus/prometheus/pkg/labels"
|
|
|
|
"github.com/prometheus/prometheus/pkg/textparse"
|
2017-01-05 14:13:01 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
2017-02-09 00:13:16 +00:00
|
|
|
func BenchmarkCreateSeries(b *testing.B) {
|
2017-02-15 05:54:59 +00:00
|
|
|
lbls, err := readPrometheusLabels("cmd/tsdb/testdata.1m", 1e6)
|
2017-02-09 00:13:16 +00:00
|
|
|
require.NoError(b, err)
|
|
|
|
|
|
|
|
b.Run("", func(b *testing.B) {
|
|
|
|
dir, err := ioutil.TempDir("", "create_series_bench")
|
|
|
|
require.NoError(b, err)
|
|
|
|
defer os.RemoveAll(dir)
|
|
|
|
|
|
|
|
h, err := createHeadBlock(dir, 0, nil, 0, 1)
|
|
|
|
require.NoError(b, err)
|
|
|
|
|
|
|
|
b.ReportAllocs()
|
|
|
|
b.ResetTimer()
|
|
|
|
|
|
|
|
for _, l := range lbls[:b.N] {
|
|
|
|
h.create(l.Hash(), l)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2017-02-14 23:54:52 +00:00
|
|
|
|
2017-02-15 05:54:59 +00:00
|
|
|
func readPrometheusLabels(fn string, n int) ([]labels.Labels, error) {
|
|
|
|
f, err := os.Open(fn)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
b, err := ioutil.ReadAll(f)
|
2017-02-14 23:54:52 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
p := textparse.New(b)
|
|
|
|
i := 0
|
|
|
|
var mets []labels.Labels
|
|
|
|
hashes := map[uint64]struct{}{}
|
|
|
|
|
|
|
|
for p.Next() && i < n {
|
|
|
|
m := make(labels.Labels, 0, 10)
|
|
|
|
p.Metric((*promlabels.Labels)(unsafe.Pointer(&m)))
|
|
|
|
|
|
|
|
h := m.Hash()
|
|
|
|
if _, ok := hashes[h]; ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
mets = append(mets, m)
|
|
|
|
hashes[h] = struct{}{}
|
|
|
|
i++
|
|
|
|
}
|
2017-03-08 15:54:13 +00:00
|
|
|
if err := p.Err(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if i != n {
|
|
|
|
return mets, errors.Errorf("requested %d metrics but found %d", n, i)
|
|
|
|
}
|
|
|
|
return mets, nil
|
2017-02-14 23:54:52 +00:00
|
|
|
}
|