Skip to content
This repository has been archived by the owner on Apr 2, 2024. It is now read-only.

Commit

Permalink
Adds support for benchmarking new series creation.
Browse files Browse the repository at this point in the history
Signed-off-by: Harkishen-Singh <harkishensingh@hotmail.com>
  • Loading branch information
Harkishen-Singh committed Jul 27, 2022
1 parent 23655d9 commit e7f16a8
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 1 deletion.
29 changes: 28 additions & 1 deletion pkg/tests/end_to_end_tests/metric_ingest_bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,19 @@
package end_to_end_tests

import (
"context"
"fmt"
"testing"

"github.com/walle/targz"

"github.com/jackc/pgx/v4/pgxpool"
"github.com/stretchr/testify/require"
"github.com/timescale/promscale/pkg/pgmodel/cache"
"github.com/timescale/promscale/pkg/pgmodel/ingestor"
"github.com/timescale/promscale/pkg/pgxconn"
"github.com/timescale/promscale/pkg/prompb"
"github.com/timescale/promscale/pkg/tests/testsupport"
"github.com/walle/targz"
)

var prometheusDataGzip = "../testdata/prometheus-data.tar.gz"
Expand Down Expand Up @@ -74,3 +77,27 @@ func BenchmarkMetricIngest(b *testing.B) {
b.StopTimer()
})
}

func BenchmarkNewSeriesIngestion(b *testing.B) {
seriesGen, err := testsupport.NewSeriesGenerator(10, 100, 4)
require.NoError(b, err)

ts := seriesGen.GetTimeseries()

withDB(b, "bench_e2e_new_series_ingest", func(db *pgxpool.Pool, t testing.TB) {
metricsIngestor, err := ingestor.NewPgxIngestorForTests(pgxconn.NewPgxConn(db), &ingestor.Cfg{
NumCopiers: 8,
InvertedLabelsCacheSize: cache.DefaultConfig.InvertedLabelsCacheSize,
})
require.NoError(b, err)
defer metricsIngestor.Close()

b.ResetTimer()
b.ReportAllocs()
_, _, _ = metricsIngestor.Ingest(context.Background(), &prompb.WriteRequest{Timeseries: ts})

numSeries := 0
require.NoError(b, db.QueryRow(context.Background(), "SELECT count(*) FROM _prom_catalog.series").Scan(&numSeries))
require.Equal(b, 1000, numSeries)
})
}
69 changes: 69 additions & 0 deletions pkg/tests/testsupport/series_gen.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
// This file and its contents are licensed under the Apache License 2.0.
// Please see the included NOTICE for copyright information and
// LICENSE for a copy of the license.

package testsupport

import (
"fmt"
"math/rand"

"github.com/timescale/promscale/pkg/prompb"
)

type seriesGen struct {
ts []prompb.TimeSeries
}

const (
labelKeyPrefix = "test_k_"
labelValuePrefix = "test_v_"
metricNamePrefix = "test_metric_"
)

var samples = []prompb.Sample{{Timestamp: 1, Value: 1}}

// NewSeriesGenerator generates distinct timeseries equal to numMetrics * numSeriesPerMetric, such that
// each timeseries has `labels` count of labels. The label keys are common across the series, but their
// values are random, thereby creating a new series.
func NewSeriesGenerator(numMetrics, numSeriesPerMetric, labels int) (*seriesGen, error) {
if labels < 2 {
return nil, fmt.Errorf("minLabels cannot be less than 2")
}
totalSeries := numMetrics * numSeriesPerMetric
ts := make([]prompb.TimeSeries, 0, totalSeries)

labels -= 1 // Since 1 label will be occupied by __name__
labelKeys := make([]string, labels)
for i := 0; i < labels; i++ {
labelKeys[i] = fmt.Sprintf("%s%d", labelKeyPrefix, i)
}

for i := 0; i < numMetrics; i++ {
metricName := randomText(metricNamePrefix)
metric := prompb.Label{Name: "__name__", Value: metricName}

for j := 0; j < numSeriesPerMetric; j++ {
serie := []prompb.Label{metric}

for k := 0; k < labels; k++ {
// The keys remain the same across the series, but the values change, everytime creating a new series.
serie = append(serie, prompb.Label{Name: labelKeys[k], Value: randomText(labelValuePrefix)})
}
ts = append(ts, prompb.TimeSeries{
Labels: serie,
Samples: samples,
})
}
}
return &seriesGen{ts}, nil
}

func (s *seriesGen) GetTimeseries() []prompb.TimeSeries {
return s.ts
}

func randomText(prefix string) string {
suffix := rand.Int() // #nosec
return fmt.Sprintf("%s%d", prefix, suffix)
}

0 comments on commit e7f16a8

Please sign in to comment.