diff --git a/promql/cache_symbols_test.go b/promql/cache_symbols_test.go new file mode 100644 index 0000000000..d4ebf99d19 --- /dev/null +++ b/promql/cache_symbols_test.go @@ -0,0 +1,86 @@ +// Copyright 2021 The Prometheus Authors +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package promql + +import ( + "context" + "fmt" + "github.com/go-kit/log" + "github.com/go-kit/log/level" + "github.com/prometheus/prometheus/tsdb" + "go.opentelemetry.io/otel" + "os" + "testing" + "time" +) + +func BenchmarkCountSingleInstance(b *testing.B) { + benchmarkQuery("count(bid_request_count{instance='vad-gp-bac12-1:11108'})", b) +} + +func BenchmarkCountAll(b *testing.B) { + benchmarkQuery("count(bid_request_count)", b) +} + +func BenchmarkCountSingleInstanceOver1h(b *testing.B) { + benchmarkQuery("count(count_over_time(bid_request_count{instance='vad-gp-bac12-1:11108'}[1h]))", b) +} + +func BenchmarkCountAllOver1h(b *testing.B) { + benchmarkQuery("count(count_over_time(bid_request_count[1h]))", b) +} + +func benchmarkQuery(query string, b *testing.B) { + // Open a TSDB for reading and/or writing. + l := log.NewLogfmtLogger(log.NewSyncWriter(os.Stderr)) + l = level.NewFilter(l, level.AllowError()) + // TSDB contains a single production block, containing a high cardinality series (~150K) + db, err := tsdb.Open("/temp/test-tsdb", l, nil, tsdb.DefaultOptions(), nil) + noErr(err) + + // Create engine for querying + otel.Tracer("name").Start(context.Background(), "Run") + eng := NewEngine(EngineOpts{ + Logger: l, + Reg: nil, + MaxSamples: 50000000, + Timeout: 60 * time.Second, + EnablePerStepStats: true, + }) + noErr(err) + + for n := 0; n < b.N; n++ { + q, err := eng.NewInstantQuery(context.Background(), db, &PrometheusQueryOpts{}, query, time.UnixMilli(1688922000001)) + noErr(err) + q.Stats().Samples.EnablePerStepStats = true + b.StartTimer() + q.Exec(context.Background()) + b.StopTimer() + + if n == 0 { + fmt.Printf("Peak samples: %v, Total samples: %v", q.Stats().Samples.PeakSamples, q.Stats().Samples.TotalSamples) + fmt.Println() + } + } + + // Clean up any last resources when done. + err = db.Close() + noErr(err) +} + +func noErr(err error) { + if err != nil { + panic(err) + } +} diff --git a/tsdb/index/index.go b/tsdb/index/index.go index 911b1a6ecc..a8bdd66e38 100644 --- a/tsdb/index/index.go +++ b/tsdb/index/index.go @@ -1477,7 +1477,11 @@ func (r *Reader) lookupSymbol(ctx context.Context, o uint32) (string, error) { if s, ok := r.nameSymbols[o]; ok { return s, nil } - return r.symbols.Lookup(o) + s, err := r.symbols.Lookup(o) + if err == nil { + r.nameSymbols[o] = s + } + return s, err } // Symbols returns an iterator over the symbols that exist within the index.