Skip to content

Commit

Permalink
refactor test code
Browse files Browse the repository at this point in the history
Signed-off-by: SungJin1212 <[email protected]>
  • Loading branch information
SungJin1212 committed Nov 6, 2024
1 parent 626cece commit fe26a6b
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 73 deletions.
24 changes: 6 additions & 18 deletions integration/e2e/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,14 +155,6 @@ func GenerateHistogramSeriesV2(name string, ts time.Time, i uint32, floatHistogr
tsMillis := TimeToMilliseconds(ts)

st := writev2.NewSymbolTable()
st.Symbolize("__name__")
st.Symbolize(name)

for _, label := range additionalLabels {
st.Symbolize(label.Name)
st.Symbolize(label.Value)
}
symbols = st.Symbols()

lbs := labels.Labels{labels.Label{Name: "__name__", Value: name}}
for _, lbl := range additionalLabels {
Expand All @@ -184,10 +176,12 @@ func GenerateHistogramSeriesV2(name string, ts time.Time, i uint32, floatHistogr

// Generate the series
series = append(series, cortexpbv2.TimeSeries{
LabelsRefs: cortexpbv2.GetLabelsRefsFromLabels(st.Symbols(), lbs),
LabelsRefs: st.SymbolizeLabels(lbs, nil),
Histograms: []cortexpbv2.Histogram{ph},
})

symbols = st.Symbols()

return
}

Expand Down Expand Up @@ -235,31 +229,25 @@ func GenerateSeriesV2(name string, ts time.Time, additionalLabels ...prompb.Labe
value := rand.Float64()

st := writev2.NewSymbolTable()
st.Symbolize("__name__")
st.Symbolize(name)

lbs := labels.Labels{{Name: labels.MetricName, Value: name}}

for _, label := range additionalLabels {
st.Symbolize(label.Name)
st.Symbolize(label.Value)
lbs = append(lbs, labels.Label{
Name: label.Name,
Value: label.Value,
})
}
symbols = st.Symbols()
labelRefs := cortexpbv2.GetLabelsRefsFromLabels(symbols, lbs)
// Generate the series
series = append(series, cortexpbv2.TimeSeries{
LabelsRefs: labelRefs,
// Generate the series
LabelsRefs: st.SymbolizeLabels(lbs, nil),
Samples: []cortexpbv2.Sample{
{Value: value, Timestamp: tsMillis},
},
Metadata: cortexpbv2.Metadata{
Type: cortexpbv2.METRIC_TYPE_GAUGE,
},
})
symbols = st.Symbols()

// Generate the expected vector when querying it
metric := model.Metric{}
Expand Down
8 changes: 3 additions & 5 deletions pkg/cortexpbv2/compatv2.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,9 @@ import (
// ToWriteRequestV2 converts matched slices of Labels, Samples, and Histograms into a WriteRequest proto.
func ToWriteRequestV2(lbls []labels.Labels, samples []Sample, histograms []Histogram, metadata []Metadata, source WriteRequest_SourceEnum, additionalSymbols ...string) *WriteRequest {
st := writev2.NewSymbolTable()
labelRefs := make([][]uint32, 0, len(lbls))
for _, lbl := range lbls {
lbl.Range(func(l labels.Label) {
st.Symbolize(l.Name)
st.Symbolize(l.Value)
})
labelRefs = append(labelRefs, st.SymbolizeLabels(lbl, nil))
}

for _, s := range additionalSymbols {
Expand All @@ -31,7 +29,7 @@ func ToWriteRequestV2(lbls []labels.Labels, samples []Sample, histograms []Histo
i := 0
for i < len(samples) || i < len(histograms) || i < len(metadata) {
ts := TimeSeries{}
ts.LabelsRefs = GetLabelsRefsFromLabels(symbols, lbls[i])
ts.LabelsRefs = labelRefs[i]
if i < len(samples) {
ts.Samples = append(ts.Samples, samples[i])
}
Expand Down
21 changes: 5 additions & 16 deletions pkg/distributor/distributor_prw2_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1263,14 +1263,10 @@ func TestDistributorPRW2_Push_ShouldGuaranteeShardingTokenConsistencyOverTheTime
func makeWriteRequestV2WithSamples(startTimestampMs int64, samples int, metadata int) *cortexpbv2.WriteRequest {
request := &cortexpbv2.WriteRequest{}
st := writev2.NewSymbolTable()
st.Symbolize("__name__")
st.Symbolize("foo")
st.Symbolize("bar")
st.Symbolize("baz")
st.SymbolizeLabels(labels.Labels{{Name: "__name__", Value: "foo"}, {Name: "bar", Value: "baz"}}, nil)

for i := 0; i < samples; i++ {
st.Symbolize("sample")
st.Symbolize(fmt.Sprintf("%d", i))
st.SymbolizeLabels(labels.Labels{{Name: "sample", Value: fmt.Sprintf("%d", i)}, {Name: "bar", Value: "baz"}}, nil)
request.Timeseries = append(request.Timeseries, makeTimeseriesV2FromST(
[]cortexpb.LabelAdapter{
{Name: model.MetricNameLabel, Value: "foo"},
Expand Down Expand Up @@ -1794,14 +1790,10 @@ func TestDistributorPRW2_MetricsMetadata(t *testing.T) {
func makeWriteRequestV2WithHistogram(startTimestampMs int64, histogram int, metadata int) *cortexpbv2.WriteRequest {
request := &cortexpbv2.WriteRequest{}
st := writev2.NewSymbolTable()
st.Symbolize("__name__")
st.Symbolize("foo")
st.Symbolize("bar")
st.Symbolize("baz")
st.SymbolizeLabels(labels.Labels{{Name: "__name__", Value: "foo"}, {Name: "bar", Value: "baz"}}, nil)

for i := 0; i < histogram; i++ {
st.Symbolize("histogram")
st.Symbolize(fmt.Sprintf("%d", i))
st.SymbolizeLabels(labels.Labels{{Name: "histogram", Value: fmt.Sprintf("%d", i)}}, nil)
request.Timeseries = append(request.Timeseries, makeTimeseriesV2FromST(
[]cortexpb.LabelAdapter{
{Name: model.MetricNameLabel, Value: "foo"},
Expand Down Expand Up @@ -1856,10 +1848,7 @@ func makeTimeseriesV2FromST(labels []cortexpb.LabelAdapter, st *writev2.SymbolsT

func makeWriteRequestV2Timeseries(labels []cortexpb.LabelAdapter, ts int64, value int, histogram bool, metadata bool) cortexpbv2.TimeSeries {
st := writev2.NewSymbolTable()
for _, lb := range labels {
st.Symbolize(lb.Name)
st.Symbolize(lb.Value)
}
st.SymbolizeLabels(cortexpb.FromLabelAdaptersToLabels(labels), nil)

var helpRef uint32
if metadata {
Expand Down
42 changes: 8 additions & 34 deletions pkg/ingester/ingester_prw2_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1888,10 +1888,7 @@ func Benchmark_IngesterPRW2_PushOnError(b *testing.B) {

st := writev2.NewSymbolTable()
for _, lbs := range metrics {
for _, lb := range lbs {
st.Symbolize(lb.Name)
st.Symbolize(lb.Value)
}
st.SymbolizeLabels(lbs, nil)
}
// Push out of order samples.
for n := 0; n < b.N; n++ {
Expand Down Expand Up @@ -1922,10 +1919,7 @@ func Benchmark_IngesterPRW2_PushOnError(b *testing.B) {

st := writev2.NewSymbolTable()
for _, lbs := range metrics {
for _, lb := range lbs {
st.Symbolize(lb.Name)
st.Symbolize(lb.Value)
}
st.SymbolizeLabels(lbs, nil)
}
for n := 0; n < b.N; n++ {
_, err := ingester.PushV2(ctx, cortexpbv2.ToWriteRequestV2(metrics, samples, nil, nil, cortexpbv2.API)) // nolint:errcheck
Expand All @@ -1952,10 +1946,7 @@ func Benchmark_IngesterPRW2_PushOnError(b *testing.B) {
runBenchmark: func(b *testing.B, ingester *Ingester, metrics []labels.Labels, samples []cortexpbv2.Sample) {
st := writev2.NewSymbolTable()
for _, lbs := range metrics {
for _, lb := range lbs {
st.Symbolize(lb.Name)
st.Symbolize(lb.Value)
}
st.SymbolizeLabels(lbs, nil)
}
// Push series with different labels than the one already pushed.
for n := 0; n < b.N; n++ {
Expand All @@ -1982,10 +1973,7 @@ func Benchmark_IngesterPRW2_PushOnError(b *testing.B) {
runBenchmark: func(b *testing.B, ingester *Ingester, metrics []labels.Labels, samples []cortexpbv2.Sample) {
st := writev2.NewSymbolTable()
for _, lbs := range metrics {
for _, lb := range lbs {
st.Symbolize(lb.Name)
st.Symbolize(lb.Value)
}
st.SymbolizeLabels(lbs, nil)
}
// Push series with different labels than the one already pushed.
for n := 0; n < b.N; n++ {
Expand All @@ -2011,10 +1999,7 @@ func Benchmark_IngesterPRW2_PushOnError(b *testing.B) {
runBenchmark: func(b *testing.B, ingester *Ingester, metrics []labels.Labels, samples []cortexpbv2.Sample) {
st := writev2.NewSymbolTable()
for _, lbs := range metrics {
for _, lb := range lbs {
st.Symbolize(lb.Name)
st.Symbolize(lb.Value)
}
st.SymbolizeLabels(lbs, nil)
}
// Push series with different labels than the one already pushed.
for n := 0; n < b.N; n++ {
Expand All @@ -2038,10 +2023,7 @@ func Benchmark_IngesterPRW2_PushOnError(b *testing.B) {
runBenchmark: func(b *testing.B, ingester *Ingester, metrics []labels.Labels, samples []cortexpbv2.Sample) {
st := writev2.NewSymbolTable()
for _, lbs := range metrics {
for _, lb := range lbs {
st.Symbolize(lb.Name)
st.Symbolize(lb.Value)
}
st.SymbolizeLabels(lbs, nil)
}
for n := 0; n < b.N; n++ {
_, err := ingester.PushV2(ctx, cortexpbv2.ToWriteRequestV2(metrics, samples, nil, nil, cortexpbv2.API))
Expand All @@ -2063,10 +2045,7 @@ func Benchmark_IngesterPRW2_PushOnError(b *testing.B) {
runBenchmark: func(b *testing.B, ingester *Ingester, metrics []labels.Labels, samples []cortexpbv2.Sample) {
st := writev2.NewSymbolTable()
for _, lbs := range metrics {
for _, lb := range lbs {
st.Symbolize(lb.Name)
st.Symbolize(lb.Value)
}
st.SymbolizeLabels(lbs, nil)
}
for n := 0; n < b.N; n++ {
_, err := ingester.PushV2(ctx, cortexpbv2.ToWriteRequestV2(metrics, samples, nil, nil, cortexpbv2.API))
Expand Down Expand Up @@ -4412,14 +4391,9 @@ func writeRequestSingleSeriesV2(lbls labels.Labels, samples []cortexpbv2.Sample)
}

st := writev2.NewSymbolTable()
for _, lb := range lbls {
st.Symbolize(lb.Name)
st.Symbolize(lb.Value)
}

ts := cortexpbv2.TimeSeries{}
ts.Samples = samples
ts.LabelsRefs = cortexpbv2.GetLabelsRefsFromLabels(st.Symbols(), lbls)
ts.LabelsRefs = st.SymbolizeLabels(lbls, nil)
req.Timeseries = append(req.Timeseries, ts)
req.Symbols = st.Symbols()

Expand Down

0 comments on commit fe26a6b

Please sign in to comment.