// Ensure tags can be marshaled into a byte slice. func TestMarshalTags(t *testing.T) { for i, tt := range []struct { tags map[string]string result []byte }{ { tags: nil, result: nil, }, { tags: map[string]string{"foo": "bar"}, result: []byte(`foo|bar`), }, { tags: map[string]string{"foo": "bar", "baz": "battttt"}, result: []byte(`baz|foo|battttt|bar`), }, { tags: map[string]string{"baz": "battttt", "foo": "bar"}, result: []byte(`baz|foo|battttt|bar`), }, } { result := tsdb.MarshalTags(tt.tags) if !bytes.Equal(result, tt.result) { t.Fatalf("%d. unexpected result: exp=%s, got=%s", i, tt.result, result) } } }
func genTestSeries(mCnt, tCnt, vCnt int) []*TestSeries { measurements := genStrList("measurement", mCnt) tagSets := NewTagSetGenerator(tCnt, vCnt).AllSets() series := []*TestSeries{} for _, m := range measurements { for _, ts := range tagSets { series = append(series, &TestSeries{ Measurement: m, Series: tsdb.NewSeries(fmt.Sprintf("%s:%s", m, string(tsdb.MarshalTags(ts))), models.NewTags(ts)), }) } } return series }
func benchmarkMarshalTags(b *testing.B, keyN int) { const keySize, valueSize = 8, 15 // Generate tag map. tags := make(map[string]string) for i := 0; i < keyN; i++ { tags[fmt.Sprintf("%0*d", keySize, i)] = fmt.Sprintf("%0*d", valueSize, i) } // Unmarshal map into byte slice. b.ReportAllocs() for i := 0; i < b.N; i++ { tsdb.MarshalTags(tags) } }