| 
									
										
										
										
											2017-04-11 02:59:45 +08:00
										 |  |  | // Copyright 2017 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.
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-12-08 00:30:10 +08:00
										 |  |  | package main | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							| 
									
										
										
										
											2017-12-14 14:46:46 +08:00
										 |  |  | 	"bufio" | 
					
						
							| 
									
										
										
										
											2024-01-25 06:22:32 +08:00
										 |  |  | 	"bytes" | 
					
						
							| 
									
										
										
										
											2020-02-06 23:58:38 +08:00
										 |  |  | 	"context" | 
					
						
							| 
									
										
										
										
											2023-11-28 18:14:29 +08:00
										 |  |  | 	"errors" | 
					
						
							| 
									
										
										
										
											2016-12-08 00:30:10 +08:00
										 |  |  | 	"fmt" | 
					
						
							|  |  |  | 	"io" | 
					
						
							| 
									
										
										
										
											2024-09-10 09:41:53 +08:00
										 |  |  | 	"log/slog" | 
					
						
							| 
									
										
										
										
											2016-12-08 00:30:10 +08:00
										 |  |  | 	"os" | 
					
						
							|  |  |  | 	"path/filepath" | 
					
						
							|  |  |  | 	"runtime" | 
					
						
							|  |  |  | 	"runtime/pprof" | 
					
						
							| 
									
										
										
										
											2024-01-16 00:24:46 +08:00
										 |  |  | 	"slices" | 
					
						
							| 
									
										
										
										
											2018-05-08 19:35:06 +08:00
										 |  |  | 	"strconv" | 
					
						
							| 
									
										
										
										
											2017-12-14 14:46:46 +08:00
										 |  |  | 	"strings" | 
					
						
							| 
									
										
										
										
											2016-12-08 00:30:10 +08:00
										 |  |  | 	"sync" | 
					
						
							| 
									
										
										
										
											2017-10-20 00:14:37 +08:00
										 |  |  | 	"text/tabwriter" | 
					
						
							| 
									
										
										
										
											2016-12-08 00:30:10 +08:00
										 |  |  | 	"time" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-13 05:15:40 +08:00
										 |  |  | 	"github.com/alecthomas/units" | 
					
						
							| 
									
										
										
										
											2024-09-10 09:41:53 +08:00
										 |  |  | 	"github.com/prometheus/common/promslog" | 
					
						
							| 
									
										
										
										
											2025-03-22 23:46:13 +08:00
										 |  |  | 	"go.uber.org/atomic" | 
					
						
							| 
									
										
										
										
											2024-09-10 09:41:53 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-08 22:23:17 +08:00
										 |  |  | 	"github.com/prometheus/prometheus/model/labels" | 
					
						
							| 
									
										
										
										
											2023-07-08 20:39:33 +08:00
										 |  |  | 	"github.com/prometheus/prometheus/promql/parser" | 
					
						
							|  |  |  | 	"github.com/prometheus/prometheus/storage" | 
					
						
							| 
									
										
										
										
											2019-08-13 16:34:14 +08:00
										 |  |  | 	"github.com/prometheus/prometheus/tsdb" | 
					
						
							| 
									
										
										
										
											2023-07-08 20:39:33 +08:00
										 |  |  | 	"github.com/prometheus/prometheus/tsdb/chunkenc" | 
					
						
							| 
									
										
										
										
											2019-08-13 16:34:14 +08:00
										 |  |  | 	"github.com/prometheus/prometheus/tsdb/chunks" | 
					
						
							|  |  |  | 	tsdb_errors "github.com/prometheus/prometheus/tsdb/errors" | 
					
						
							| 
									
										
										
										
											2020-12-09 04:55:45 +08:00
										 |  |  | 	"github.com/prometheus/prometheus/tsdb/fileutil" | 
					
						
							| 
									
										
										
										
											2023-07-08 20:39:33 +08:00
										 |  |  | 	"github.com/prometheus/prometheus/tsdb/index" | 
					
						
							| 
									
										
										
										
											2016-12-08 00:30:10 +08:00
										 |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-24 02:35:50 +08:00
										 |  |  | const timeDelta = 30000 | 
					
						
							| 
									
										
										
										
											2016-12-08 00:30:10 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | type writeBenchmark struct { | 
					
						
							| 
									
										
										
										
											2017-09-09 19:11:12 +08:00
										 |  |  | 	outPath     string | 
					
						
							|  |  |  | 	samplesFile string | 
					
						
							|  |  |  | 	cleanup     bool | 
					
						
							|  |  |  | 	numMetrics  int | 
					
						
							| 
									
										
										
										
											2016-12-08 00:30:10 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-19 23:04:37 +08:00
										 |  |  | 	storage *tsdb.DB | 
					
						
							| 
									
										
										
										
											2016-12-08 00:30:10 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	cpuprof   *os.File | 
					
						
							|  |  |  | 	memprof   *os.File | 
					
						
							|  |  |  | 	blockprof *os.File | 
					
						
							| 
									
										
										
										
											2017-05-14 17:51:56 +08:00
										 |  |  | 	mtxprof   *os.File | 
					
						
							| 
									
										
										
										
											2024-09-10 09:41:53 +08:00
										 |  |  | 	logger    *slog.Logger | 
					
						
							| 
									
										
										
										
											2016-12-08 00:30:10 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-09 18:14:17 +08:00
										 |  |  | func benchmarkWrite(outPath, samplesFile string, numMetrics, numScrapes int) error { | 
					
						
							| 
									
										
										
										
											2020-07-24 02:35:50 +08:00
										 |  |  | 	b := &writeBenchmark{ | 
					
						
							|  |  |  | 		outPath:     outPath, | 
					
						
							|  |  |  | 		samplesFile: samplesFile, | 
					
						
							|  |  |  | 		numMetrics:  numMetrics, | 
					
						
							| 
									
										
										
										
											2024-09-10 09:41:53 +08:00
										 |  |  | 		logger:      promslog.New(&promslog.Config{}), | 
					
						
							| 
									
										
										
										
											2020-07-24 02:35:50 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-12-08 00:30:10 +08:00
										 |  |  | 	if b.outPath == "" { | 
					
						
							| 
									
										
										
										
											2022-03-08 21:08:53 +08:00
										 |  |  | 		dir, err := os.MkdirTemp("", "tsdb_bench") | 
					
						
							| 
									
										
										
										
											2016-12-08 00:30:10 +08:00
										 |  |  | 		if err != nil { | 
					
						
							| 
									
										
										
										
											2019-07-23 16:04:48 +08:00
										 |  |  | 			return err | 
					
						
							| 
									
										
										
										
											2016-12-08 00:30:10 +08:00
										 |  |  | 		} | 
					
						
							|  |  |  | 		b.outPath = dir | 
					
						
							|  |  |  | 		b.cleanup = true | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	if err := os.RemoveAll(b.outPath); err != nil { | 
					
						
							| 
									
										
										
										
											2019-07-23 16:04:48 +08:00
										 |  |  | 		return err | 
					
						
							| 
									
										
										
										
											2016-12-08 00:30:10 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2021-10-22 16:06:44 +08:00
										 |  |  | 	if err := os.MkdirAll(b.outPath, 0o777); err != nil { | 
					
						
							| 
									
										
										
										
											2019-07-23 16:04:48 +08:00
										 |  |  | 		return err | 
					
						
							| 
									
										
										
										
											2016-12-08 00:30:10 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	dir := filepath.Join(b.outPath, "storage") | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-09-10 09:41:53 +08:00
										 |  |  | 	st, err := tsdb.Open(dir, b.logger, nil, &tsdb.Options{ | 
					
						
							| 
									
										
										
										
											2020-02-12 00:34:09 +08:00
										 |  |  | 		RetentionDuration: int64(15 * 24 * time.Hour / time.Millisecond), | 
					
						
							|  |  |  | 		MinBlockDuration:  int64(2 * time.Hour / time.Millisecond), | 
					
						
							| 
									
										
										
										
											2021-06-05 22:29:32 +08:00
										 |  |  | 	}, tsdb.NewDBStats()) | 
					
						
							| 
									
										
										
										
											2016-12-21 07:02:37 +08:00
										 |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2019-07-23 16:04:48 +08:00
										 |  |  | 		return err | 
					
						
							| 
									
										
										
										
											2016-12-08 00:30:10 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2020-07-24 02:35:50 +08:00
										 |  |  | 	st.DisableCompactions() | 
					
						
							| 
									
										
										
										
											2016-12-21 07:02:37 +08:00
										 |  |  | 	b.storage = st | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-24 02:35:50 +08:00
										 |  |  | 	var lbs []labels.Labels | 
					
						
							| 
									
										
										
										
											2016-12-08 00:30:10 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-24 02:35:50 +08:00
										 |  |  | 	if _, err = measureTime("readData", func() error { | 
					
						
							| 
									
										
										
										
											2017-09-09 19:11:12 +08:00
										 |  |  | 		f, err := os.Open(b.samplesFile) | 
					
						
							| 
									
										
										
										
											2016-12-08 00:30:10 +08:00
										 |  |  | 		if err != nil { | 
					
						
							| 
									
										
										
										
											2019-07-23 16:04:48 +08:00
										 |  |  | 			return err | 
					
						
							| 
									
										
										
										
											2016-12-08 00:30:10 +08:00
										 |  |  | 		} | 
					
						
							|  |  |  | 		defer f.Close() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-24 02:35:50 +08:00
										 |  |  | 		lbs, err = readPrometheusLabels(f, b.numMetrics) | 
					
						
							| 
									
										
										
										
											2016-12-08 00:30:10 +08:00
										 |  |  | 		if err != nil { | 
					
						
							| 
									
										
										
										
											2019-07-23 16:04:48 +08:00
										 |  |  | 			return err | 
					
						
							| 
									
										
										
										
											2016-12-08 00:30:10 +08:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2019-07-23 16:04:48 +08:00
										 |  |  | 		return nil | 
					
						
							| 
									
										
										
										
											2020-07-24 02:35:50 +08:00
										 |  |  | 	}); err != nil { | 
					
						
							| 
									
										
										
										
											2019-07-23 16:04:48 +08:00
										 |  |  | 		return err | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-12-08 00:30:10 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-01 22:29:48 +08:00
										 |  |  | 	var total uint64 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-23 16:04:48 +08:00
										 |  |  | 	dur, err := measureTime("ingestScrapes", func() error { | 
					
						
							| 
									
										
										
										
											2019-08-13 16:34:14 +08:00
										 |  |  | 		if err := b.startProfiling(); err != nil { | 
					
						
							|  |  |  | 			return err | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2021-03-09 18:14:17 +08:00
										 |  |  | 		total, err = b.ingestScrapes(lbs, numScrapes) | 
					
						
							| 
									
										
										
										
											2017-02-01 22:29:48 +08:00
										 |  |  | 		if err != nil { | 
					
						
							| 
									
										
										
										
											2019-07-23 16:04:48 +08:00
										 |  |  | 			return err | 
					
						
							| 
									
										
										
										
											2016-12-08 00:30:10 +08:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2019-07-23 16:04:48 +08:00
										 |  |  | 		return nil | 
					
						
							| 
									
										
										
										
											2016-12-08 00:30:10 +08:00
										 |  |  | 	}) | 
					
						
							| 
									
										
										
										
											2019-07-23 16:04:48 +08:00
										 |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		return err | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2017-02-01 22:29:48 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	fmt.Println(" > total samples:", total) | 
					
						
							|  |  |  | 	fmt.Println(" > samples/sec:", float64(total)/dur.Seconds()) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-24 02:35:50 +08:00
										 |  |  | 	if _, err = measureTime("stopStorage", func() error { | 
					
						
							| 
									
										
										
										
											2016-12-21 07:02:37 +08:00
										 |  |  | 		if err := b.storage.Close(); err != nil { | 
					
						
							| 
									
										
										
										
											2019-07-23 16:04:48 +08:00
										 |  |  | 			return err | 
					
						
							| 
									
										
										
										
											2016-12-08 00:30:10 +08:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2020-07-24 02:35:50 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		return b.stopProfiling() | 
					
						
							|  |  |  | 	}); err != nil { | 
					
						
							| 
									
										
										
										
											2019-07-23 16:04:48 +08:00
										 |  |  | 		return err | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2020-07-24 02:35:50 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-23 16:04:48 +08:00
										 |  |  | 	return nil | 
					
						
							| 
									
										
										
										
											2016-12-08 00:30:10 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-01 22:29:48 +08:00
										 |  |  | func (b *writeBenchmark) ingestScrapes(lbls []labels.Labels, scrapeCount int) (uint64, error) { | 
					
						
							| 
									
										
										
										
											2024-03-24 02:44:30 +08:00
										 |  |  | 	var total atomic.Uint64 | 
					
						
							| 
									
										
										
										
											2016-12-08 00:30:10 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-02 18:09:19 +08:00
										 |  |  | 	for i := 0; i < scrapeCount; i += 100 { | 
					
						
							|  |  |  | 		var wg sync.WaitGroup | 
					
						
							| 
									
										
										
										
											2017-01-11 19:54:18 +08:00
										 |  |  | 		lbls := lbls | 
					
						
							|  |  |  | 		for len(lbls) > 0 { | 
					
						
							|  |  |  | 			l := 1000 | 
					
						
							|  |  |  | 			if len(lbls) < 1000 { | 
					
						
							|  |  |  | 				l = len(lbls) | 
					
						
							| 
									
										
										
										
											2016-12-08 00:30:10 +08:00
										 |  |  | 			} | 
					
						
							| 
									
										
										
										
											2017-01-11 19:54:18 +08:00
										 |  |  | 			batch := lbls[:l] | 
					
						
							|  |  |  | 			lbls = lbls[l:] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			wg.Add(1) | 
					
						
							|  |  |  | 			go func() { | 
					
						
							| 
									
										
										
										
											2024-03-24 02:44:30 +08:00
										 |  |  | 				defer wg.Done() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-07 19:42:53 +08:00
										 |  |  | 				n, err := b.ingestScrapesShard(batch, 100, int64(timeDelta*i)) | 
					
						
							| 
									
										
										
										
											2017-01-11 19:54:18 +08:00
										 |  |  | 				if err != nil { | 
					
						
							|  |  |  | 					// exitWithError(err)
 | 
					
						
							|  |  |  | 					fmt.Println(" err", err) | 
					
						
							|  |  |  | 				} | 
					
						
							| 
									
										
										
										
											2024-03-24 02:44:30 +08:00
										 |  |  | 				total.Add(n) | 
					
						
							| 
									
										
										
										
											2017-01-11 19:54:18 +08:00
										 |  |  | 			}() | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		wg.Wait() | 
					
						
							| 
									
										
										
										
											2016-12-08 00:30:10 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2017-07-13 22:15:13 +08:00
										 |  |  | 	fmt.Println("ingestion completed") | 
					
						
							| 
									
										
										
										
											2017-01-10 18:17:37 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-24 02:44:30 +08:00
										 |  |  | 	return total.Load(), nil | 
					
						
							| 
									
										
										
										
											2016-12-08 00:30:10 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-14 16:44:32 +08:00
										 |  |  | func (b *writeBenchmark) ingestScrapesShard(lbls []labels.Labels, scrapeCount int, baset int64) (uint64, error) { | 
					
						
							| 
									
										
										
										
											2017-01-11 19:54:18 +08:00
										 |  |  | 	ts := baset | 
					
						
							| 
									
										
										
										
											2016-12-08 00:30:10 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-12-08 17:04:24 +08:00
										 |  |  | 	type sample struct { | 
					
						
							| 
									
										
										
										
											2016-12-21 16:39:01 +08:00
										 |  |  | 		labels labels.Labels | 
					
						
							| 
									
										
										
										
											2016-12-08 17:04:24 +08:00
										 |  |  | 		value  int64 | 
					
						
							| 
									
										
										
										
											2021-11-06 18:10:04 +08:00
										 |  |  | 		ref    *storage.SeriesRef | 
					
						
							| 
									
										
										
										
											2016-12-08 17:04:24 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-14 16:44:32 +08:00
										 |  |  | 	scrape := make([]*sample, 0, len(lbls)) | 
					
						
							| 
									
										
										
										
											2016-12-08 17:04:24 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-14 16:44:32 +08:00
										 |  |  | 	for _, m := range lbls { | 
					
						
							| 
									
										
										
										
											2017-01-11 19:54:18 +08:00
										 |  |  | 		scrape = append(scrape, &sample{ | 
					
						
							|  |  |  | 			labels: m, | 
					
						
							| 
									
										
										
										
											2016-12-08 17:04:24 +08:00
										 |  |  | 			value:  123456789, | 
					
						
							| 
									
										
										
										
											2017-01-11 19:54:18 +08:00
										 |  |  | 		}) | 
					
						
							| 
									
										
										
										
											2016-12-08 17:04:24 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2017-01-10 18:17:37 +08:00
										 |  |  | 	total := uint64(0) | 
					
						
							| 
									
										
										
										
											2016-12-08 17:04:24 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-12-08 00:30:10 +08:00
										 |  |  | 	for i := 0; i < scrapeCount; i++ { | 
					
						
							| 
									
										
										
										
											2020-07-24 22:10:51 +08:00
										 |  |  | 		app := b.storage.Appender(context.TODO()) | 
					
						
							| 
									
										
										
										
											2017-06-07 19:42:53 +08:00
										 |  |  | 		ts += timeDelta | 
					
						
							| 
									
										
										
										
											2016-12-08 00:30:10 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-12-08 17:04:24 +08:00
										 |  |  | 		for _, s := range scrape { | 
					
						
							| 
									
										
										
										
											2016-12-09 17:00:14 +08:00
										 |  |  | 			s.value += 1000 | 
					
						
							| 
									
										
										
										
											2017-01-13 02:18:51 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-06 18:10:04 +08:00
										 |  |  | 			var ref storage.SeriesRef | 
					
						
							| 
									
										
										
										
											2021-02-18 20:07:00 +08:00
										 |  |  | 			if s.ref != nil { | 
					
						
							|  |  |  | 				ref = *s.ref | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2017-02-02 18:09:19 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-18 20:07:00 +08:00
										 |  |  | 			ref, err := app.Append(ref, s.labels, ts, float64(s.value)) | 
					
						
							|  |  |  | 			if err != nil { | 
					
						
							|  |  |  | 				panic(err) | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2017-01-13 02:18:51 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-18 20:07:00 +08:00
										 |  |  | 			if s.ref == nil { | 
					
						
							| 
									
										
										
										
											2017-01-13 02:18:51 +08:00
										 |  |  | 				s.ref = &ref | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2017-01-10 18:17:37 +08:00
										 |  |  | 			total++ | 
					
						
							| 
									
										
										
										
											2016-12-08 00:30:10 +08:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2016-12-21 07:02:37 +08:00
										 |  |  | 		if err := app.Commit(); err != nil { | 
					
						
							| 
									
										
										
										
											2017-01-10 18:17:37 +08:00
										 |  |  | 			return total, err | 
					
						
							| 
									
										
										
										
											2016-12-08 00:30:10 +08:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2017-01-10 18:17:37 +08:00
										 |  |  | 	return total, nil | 
					
						
							| 
									
										
										
										
											2016-12-08 00:30:10 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-23 16:04:48 +08:00
										 |  |  | func (b *writeBenchmark) startProfiling() error { | 
					
						
							| 
									
										
										
										
											2016-12-08 00:30:10 +08:00
										 |  |  | 	var err error | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// Start CPU profiling.
 | 
					
						
							|  |  |  | 	b.cpuprof, err = os.Create(filepath.Join(b.outPath, "cpu.prof")) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2022-05-24 14:58:59 +08:00
										 |  |  | 		return fmt.Errorf("bench: could not create cpu profile: %w", err) | 
					
						
							| 
									
										
										
										
											2016-12-08 00:30:10 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2018-09-20 16:33:52 +08:00
										 |  |  | 	if err := pprof.StartCPUProfile(b.cpuprof); err != nil { | 
					
						
							| 
									
										
										
										
											2022-05-24 14:58:59 +08:00
										 |  |  | 		return fmt.Errorf("bench: could not start CPU profile: %w", err) | 
					
						
							| 
									
										
										
										
											2018-09-20 16:33:52 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-12-08 00:30:10 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// Start memory profiling.
 | 
					
						
							|  |  |  | 	b.memprof, err = os.Create(filepath.Join(b.outPath, "mem.prof")) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2022-05-24 14:58:59 +08:00
										 |  |  | 		return fmt.Errorf("bench: could not create memory profile: %w", err) | 
					
						
							| 
									
										
										
										
											2016-12-08 00:30:10 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2017-05-14 17:51:56 +08:00
										 |  |  | 	runtime.MemProfileRate = 64 * 1024 | 
					
						
							| 
									
										
										
										
											2016-12-08 00:30:10 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// Start fatal profiling.
 | 
					
						
							|  |  |  | 	b.blockprof, err = os.Create(filepath.Join(b.outPath, "block.prof")) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2022-05-24 14:58:59 +08:00
										 |  |  | 		return fmt.Errorf("bench: could not create block profile: %w", err) | 
					
						
							| 
									
										
										
										
											2016-12-08 00:30:10 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2017-05-14 17:51:56 +08:00
										 |  |  | 	runtime.SetBlockProfileRate(20) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	b.mtxprof, err = os.Create(filepath.Join(b.outPath, "mutex.prof")) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2022-05-24 14:58:59 +08:00
										 |  |  | 		return fmt.Errorf("bench: could not create mutex profile: %w", err) | 
					
						
							| 
									
										
										
										
											2017-05-14 17:51:56 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	runtime.SetMutexProfileFraction(20) | 
					
						
							| 
									
										
										
										
											2019-07-23 16:04:48 +08:00
										 |  |  | 	return nil | 
					
						
							| 
									
										
										
										
											2016-12-08 00:30:10 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-20 16:33:52 +08:00
										 |  |  | func (b *writeBenchmark) stopProfiling() error { | 
					
						
							| 
									
										
										
										
											2016-12-08 00:30:10 +08:00
										 |  |  | 	if b.cpuprof != nil { | 
					
						
							|  |  |  | 		pprof.StopCPUProfile() | 
					
						
							|  |  |  | 		b.cpuprof.Close() | 
					
						
							|  |  |  | 		b.cpuprof = nil | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	if b.memprof != nil { | 
					
						
							| 
									
										
										
										
											2018-09-20 16:33:52 +08:00
										 |  |  | 		if err := pprof.Lookup("heap").WriteTo(b.memprof, 0); err != nil { | 
					
						
							| 
									
										
										
										
											2022-05-24 14:58:59 +08:00
										 |  |  | 			return fmt.Errorf("error writing mem profile: %w", err) | 
					
						
							| 
									
										
										
										
											2018-09-20 16:33:52 +08:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2016-12-08 00:30:10 +08:00
										 |  |  | 		b.memprof.Close() | 
					
						
							|  |  |  | 		b.memprof = nil | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	if b.blockprof != nil { | 
					
						
							| 
									
										
										
										
											2018-09-20 16:33:52 +08:00
										 |  |  | 		if err := pprof.Lookup("block").WriteTo(b.blockprof, 0); err != nil { | 
					
						
							| 
									
										
										
										
											2022-05-24 14:58:59 +08:00
										 |  |  | 			return fmt.Errorf("error writing block profile: %w", err) | 
					
						
							| 
									
										
										
										
											2018-09-20 16:33:52 +08:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2016-12-08 00:30:10 +08:00
										 |  |  | 		b.blockprof.Close() | 
					
						
							|  |  |  | 		b.blockprof = nil | 
					
						
							|  |  |  | 		runtime.SetBlockProfileRate(0) | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2017-05-14 17:51:56 +08:00
										 |  |  | 	if b.mtxprof != nil { | 
					
						
							| 
									
										
										
										
											2018-09-20 16:33:52 +08:00
										 |  |  | 		if err := pprof.Lookup("mutex").WriteTo(b.mtxprof, 0); err != nil { | 
					
						
							| 
									
										
										
										
											2022-05-24 14:58:59 +08:00
										 |  |  | 			return fmt.Errorf("error writing mutex profile: %w", err) | 
					
						
							| 
									
										
										
										
											2018-09-20 16:33:52 +08:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2017-05-14 17:51:56 +08:00
										 |  |  | 		b.mtxprof.Close() | 
					
						
							|  |  |  | 		b.mtxprof = nil | 
					
						
							|  |  |  | 		runtime.SetMutexProfileFraction(0) | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2018-09-20 16:33:52 +08:00
										 |  |  | 	return nil | 
					
						
							| 
									
										
										
										
											2016-12-08 00:30:10 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-23 16:04:48 +08:00
										 |  |  | func measureTime(stage string, f func() error) (time.Duration, error) { | 
					
						
							| 
									
										
										
										
											2016-12-08 00:30:10 +08:00
										 |  |  | 	fmt.Printf(">> start stage=%s\n", stage) | 
					
						
							|  |  |  | 	start := time.Now() | 
					
						
							| 
									
										
										
										
											2020-07-24 02:35:50 +08:00
										 |  |  | 	if err := f(); err != nil { | 
					
						
							| 
									
										
										
										
											2019-07-23 16:04:48 +08:00
										 |  |  | 		return 0, err | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2020-07-24 02:35:50 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-12-08 00:30:10 +08:00
										 |  |  | 	fmt.Printf(">> completed stage=%s duration=%s\n", stage, time.Since(start)) | 
					
						
							| 
									
										
										
										
											2019-07-23 16:04:48 +08:00
										 |  |  | 	return time.Since(start), nil | 
					
						
							| 
									
										
										
										
											2016-12-08 00:30:10 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-14 14:46:46 +08:00
										 |  |  | func readPrometheusLabels(r io.Reader, n int) ([]labels.Labels, error) { | 
					
						
							|  |  |  | 	scanner := bufio.NewScanner(r) | 
					
						
							| 
									
										
										
										
											2016-12-08 00:30:10 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-17 04:29:53 +08:00
										 |  |  | 	var mets []labels.Labels | 
					
						
							|  |  |  | 	hashes := map[uint64]struct{}{} | 
					
						
							| 
									
										
										
										
											2017-12-14 14:46:46 +08:00
										 |  |  | 	i := 0 | 
					
						
							| 
									
										
										
										
											2016-12-08 00:30:10 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-14 14:46:46 +08:00
										 |  |  | 	for scanner.Scan() && i < n { | 
					
						
							|  |  |  | 		r := strings.NewReplacer("\"", "", "{", "", "}", "") | 
					
						
							|  |  |  | 		s := r.Replace(scanner.Text()) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		labelChunks := strings.Split(s, ",") | 
					
						
							| 
									
										
										
										
											2025-01-24 21:31:13 +08:00
										 |  |  | 		m := make([]labels.Label, 0, len(labelChunks)) | 
					
						
							| 
									
										
										
										
											2017-12-14 14:46:46 +08:00
										 |  |  | 		for _, labelChunk := range labelChunks { | 
					
						
							|  |  |  | 			split := strings.Split(labelChunk, ":") | 
					
						
							|  |  |  | 			m = append(m, labels.Label{Name: split[0], Value: split[1]}) | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2022-02-27 22:36:53 +08:00
										 |  |  | 		ml := labels.New(m...) // This sorts by name - order of the k/v labels matters, don't assume we'll always receive them already sorted.
 | 
					
						
							|  |  |  | 		h := ml.Hash() | 
					
						
							| 
									
										
										
										
											2017-01-17 04:29:53 +08:00
										 |  |  | 		if _, ok := hashes[h]; ok { | 
					
						
							|  |  |  | 			continue | 
					
						
							| 
									
										
										
										
											2016-12-08 00:30:10 +08:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2022-02-27 22:36:53 +08:00
										 |  |  | 		mets = append(mets, ml) | 
					
						
							| 
									
										
										
										
											2017-01-17 04:29:53 +08:00
										 |  |  | 		hashes[h] = struct{}{} | 
					
						
							|  |  |  | 		i++ | 
					
						
							| 
									
										
										
										
											2016-12-08 00:30:10 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2017-12-14 14:46:46 +08:00
										 |  |  | 	return mets, nil | 
					
						
							| 
									
										
										
										
											2016-12-08 00:30:10 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-24 02:35:50 +08:00
										 |  |  | func listBlocks(path string, humanReadable bool) error { | 
					
						
							| 
									
										
										
										
											2023-11-30 00:49:01 +08:00
										 |  |  | 	db, err := tsdb.OpenDBReadOnly(path, "", nil) | 
					
						
							| 
									
										
										
										
											2020-07-24 02:35:50 +08:00
										 |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		return err | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	defer func() { | 
					
						
							| 
									
										
										
										
											2020-10-28 23:24:58 +08:00
										 |  |  | 		err = tsdb_errors.NewMulti(err, db.Close()).Err() | 
					
						
							| 
									
										
										
										
											2020-07-24 02:35:50 +08:00
										 |  |  | 	}() | 
					
						
							|  |  |  | 	blocks, err := db.Blocks() | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		return err | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2020-12-10 05:58:23 +08:00
										 |  |  | 	printBlocks(blocks, true, humanReadable) | 
					
						
							| 
									
										
										
										
											2020-07-24 02:35:50 +08:00
										 |  |  | 	return nil | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-10 05:58:23 +08:00
										 |  |  | func printBlocks(blocks []tsdb.BlockReader, writeHeader, humanReadable bool) { | 
					
						
							| 
									
										
										
										
											2020-12-28 23:51:45 +08:00
										 |  |  | 	tw := tabwriter.NewWriter(os.Stdout, 13, 0, 2, ' ', 0) | 
					
						
							| 
									
										
										
										
											2017-10-03 04:48:47 +08:00
										 |  |  | 	defer tw.Flush() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-10 05:58:23 +08:00
										 |  |  | 	if writeHeader { | 
					
						
							|  |  |  | 		fmt.Fprintln(tw, "BLOCK ULID\tMIN TIME\tMAX TIME\tDURATION\tNUM SAMPLES\tNUM CHUNKS\tNUM SERIES\tSIZE") | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-03 04:48:47 +08:00
										 |  |  | 	for _, b := range blocks { | 
					
						
							| 
									
										
										
										
											2017-10-11 17:02:57 +08:00
										 |  |  | 		meta := b.Meta() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-03 04:48:47 +08:00
										 |  |  | 		fmt.Fprintf(tw, | 
					
						
							| 
									
										
										
										
											2020-10-13 05:15:40 +08:00
										 |  |  | 			"%v\t%v\t%v\t%v\t%v\t%v\t%v\t%v\n", | 
					
						
							| 
									
										
										
										
											2017-10-11 17:02:57 +08:00
										 |  |  | 			meta.ULID, | 
					
						
							| 
									
										
										
										
											2024-09-11 04:32:03 +08:00
										 |  |  | 			getFormattedTime(meta.MinTime, humanReadable), | 
					
						
							|  |  |  | 			getFormattedTime(meta.MaxTime, humanReadable), | 
					
						
							| 
									
										
										
										
											2020-07-24 21:31:20 +08:00
										 |  |  | 			time.Duration(meta.MaxTime-meta.MinTime)*time.Millisecond, | 
					
						
							| 
									
										
										
										
											2017-10-11 17:02:57 +08:00
										 |  |  | 			meta.Stats.NumSamples, | 
					
						
							|  |  |  | 			meta.Stats.NumChunks, | 
					
						
							|  |  |  | 			meta.Stats.NumSeries, | 
					
						
							| 
									
										
										
										
											2024-09-11 04:32:03 +08:00
										 |  |  | 			getFormattedBytes(b.Size(), humanReadable), | 
					
						
							| 
									
										
										
										
											2017-10-03 04:48:47 +08:00
										 |  |  | 		) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2018-05-08 19:35:06 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-09-11 04:32:03 +08:00
										 |  |  | func getFormattedTime(timestamp int64, humanReadable bool) string { | 
					
						
							| 
									
										
										
										
											2020-07-24 02:35:50 +08:00
										 |  |  | 	if humanReadable { | 
					
						
							| 
									
										
										
										
											2020-03-30 00:35:39 +08:00
										 |  |  | 		return time.Unix(timestamp/1000, 0).UTC().String() | 
					
						
							| 
									
										
										
										
											2018-05-08 19:35:06 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	return strconv.FormatInt(timestamp, 10) | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2018-12-29 01:06:12 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-09-11 04:32:03 +08:00
										 |  |  | func getFormattedBytes(bytes int64, humanReadable bool) string { | 
					
						
							| 
									
										
										
										
											2020-10-13 05:15:40 +08:00
										 |  |  | 	if humanReadable { | 
					
						
							|  |  |  | 		return units.Base2Bytes(bytes).String() | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return strconv.FormatInt(bytes, 10) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-24 02:35:50 +08:00
										 |  |  | func openBlock(path, blockID string) (*tsdb.DBReadOnly, tsdb.BlockReader, error) { | 
					
						
							| 
									
										
										
										
											2023-11-30 00:49:01 +08:00
										 |  |  | 	db, err := tsdb.OpenDBReadOnly(path, "", nil) | 
					
						
							| 
									
										
										
										
											2020-07-24 02:35:50 +08:00
										 |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		return nil, nil, err | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2023-06-01 19:43:09 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	if blockID == "" { | 
					
						
							|  |  |  | 		blockID, err = db.LastBlockID() | 
					
						
							|  |  |  | 		if err != nil { | 
					
						
							|  |  |  | 			return nil, nil, err | 
					
						
							| 
									
										
										
										
											2020-07-24 02:35:50 +08:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2023-06-01 19:43:09 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-11 14:59:24 +08:00
										 |  |  | 	b, err := db.Block(blockID, tsdb.DefaultPostingsDecoderFactory) | 
					
						
							| 
									
										
										
										
											2023-06-01 19:43:09 +08:00
										 |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		return nil, nil, err | 
					
						
							| 
									
										
										
										
											2020-07-24 02:35:50 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2023-06-01 19:43:09 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	return db, b, nil | 
					
						
							| 
									
										
										
										
											2020-07-24 02:35:50 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-09-20 18:37:32 +08:00
										 |  |  | func analyzeBlock(ctx context.Context, path, blockID string, limit int, runExtended bool, matchers string) error { | 
					
						
							|  |  |  | 	var ( | 
					
						
							|  |  |  | 		selectors []*labels.Matcher | 
					
						
							|  |  |  | 		err       error | 
					
						
							|  |  |  | 	) | 
					
						
							|  |  |  | 	if len(matchers) > 0 { | 
					
						
							|  |  |  | 		selectors, err = parser.ParseMetricSelector(matchers) | 
					
						
							|  |  |  | 		if err != nil { | 
					
						
							|  |  |  | 			return err | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2020-07-24 02:35:50 +08:00
										 |  |  | 	db, block, err := openBlock(path, blockID) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		return err | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	defer func() { | 
					
						
							| 
									
										
										
										
											2020-10-28 23:24:58 +08:00
										 |  |  | 		err = tsdb_errors.NewMulti(err, db.Close()).Err() | 
					
						
							| 
									
										
										
										
											2020-07-24 02:35:50 +08:00
										 |  |  | 	}() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	meta := block.Meta() | 
					
						
							| 
									
										
										
										
											2019-07-23 16:04:48 +08:00
										 |  |  | 	fmt.Printf("Block ID: %s\n", meta.ULID) | 
					
						
							| 
									
										
										
										
											2018-12-29 01:06:12 +08:00
										 |  |  | 	// Presume 1ms resolution that Prometheus uses.
 | 
					
						
							|  |  |  | 	fmt.Printf("Duration: %s\n", (time.Duration(meta.MaxTime-meta.MinTime) * 1e6).String()) | 
					
						
							| 
									
										
										
										
											2023-09-20 18:37:32 +08:00
										 |  |  | 	fmt.Printf("Total Series: %d\n", meta.Stats.NumSeries) | 
					
						
							|  |  |  | 	if len(matchers) > 0 { | 
					
						
							|  |  |  | 		fmt.Printf("Matcher: %s\n", matchers) | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2020-07-24 02:35:50 +08:00
										 |  |  | 	ir, err := block.Index() | 
					
						
							| 
									
										
										
										
											2018-12-29 01:06:12 +08:00
										 |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2019-07-23 16:04:48 +08:00
										 |  |  | 		return err | 
					
						
							| 
									
										
										
										
											2018-12-29 01:06:12 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	defer ir.Close() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-09-20 18:37:32 +08:00
										 |  |  | 	allLabelNames, err := ir.LabelNames(ctx, selectors...) | 
					
						
							| 
									
										
										
										
											2018-12-29 01:06:12 +08:00
										 |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2019-07-23 16:04:48 +08:00
										 |  |  | 		return err | 
					
						
							| 
									
										
										
										
											2018-12-29 01:06:12 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	fmt.Printf("Label names: %d\n", len(allLabelNames)) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	type postingInfo struct { | 
					
						
							|  |  |  | 		key    string | 
					
						
							|  |  |  | 		metric uint64 | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	postingInfos := []postingInfo{} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	printInfo := func(postingInfos []postingInfo) { | 
					
						
							| 
									
										
										
										
											2023-10-16 22:23:26 +08:00
										 |  |  | 		slices.SortFunc(postingInfos, func(a, b postingInfo) int { | 
					
						
							|  |  |  | 			switch { | 
					
						
							|  |  |  | 			case b.metric < a.metric: | 
					
						
							|  |  |  | 				return -1 | 
					
						
							|  |  |  | 			case b.metric > a.metric: | 
					
						
							|  |  |  | 				return 1 | 
					
						
							|  |  |  | 			default: | 
					
						
							|  |  |  | 				return 0 | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		}) | 
					
						
							| 
									
										
										
										
											2018-12-29 01:06:12 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		for i, pc := range postingInfos { | 
					
						
							|  |  |  | 			if i >= limit { | 
					
						
							|  |  |  | 				break | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2020-06-22 17:38:10 +08:00
										 |  |  | 			fmt.Printf("%d %s\n", pc.metric, pc.key) | 
					
						
							| 
									
										
										
										
											2018-12-29 01:06:12 +08:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	labelsUncovered := map[string]uint64{} | 
					
						
							|  |  |  | 	labelpairsUncovered := map[string]uint64{} | 
					
						
							|  |  |  | 	labelpairsCount := map[string]uint64{} | 
					
						
							|  |  |  | 	entries := 0 | 
					
						
							| 
									
										
										
										
											2023-09-20 18:37:32 +08:00
										 |  |  | 	var ( | 
					
						
							|  |  |  | 		p    index.Postings | 
					
						
							|  |  |  | 		refs []storage.SeriesRef | 
					
						
							|  |  |  | 	) | 
					
						
							|  |  |  | 	if len(matchers) > 0 { | 
					
						
							| 
									
										
										
										
											2023-09-21 00:34:20 +08:00
										 |  |  | 		p, err = tsdb.PostingsForMatchers(ctx, ir, selectors...) | 
					
						
							| 
									
										
										
										
											2023-09-20 18:37:32 +08:00
										 |  |  | 		if err != nil { | 
					
						
							|  |  |  | 			return err | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		// Expand refs first and cache in memory.
 | 
					
						
							|  |  |  | 		// So later we don't have to expand again.
 | 
					
						
							|  |  |  | 		refs, err = index.ExpandPostings(p) | 
					
						
							|  |  |  | 		if err != nil { | 
					
						
							|  |  |  | 			return err | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		fmt.Printf("Matched series: %d\n", len(refs)) | 
					
						
							|  |  |  | 		p = index.NewListPostings(refs) | 
					
						
							|  |  |  | 	} else { | 
					
						
							|  |  |  | 		p, err = ir.Postings(ctx, "", "") // The special all key.
 | 
					
						
							|  |  |  | 		if err != nil { | 
					
						
							|  |  |  | 			return err | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2018-12-29 01:06:12 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2023-09-20 18:37:32 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-12-29 01:06:12 +08:00
										 |  |  | 	chks := []chunks.Meta{} | 
					
						
							| 
									
										
										
										
											2022-06-28 23:03:26 +08:00
										 |  |  | 	builder := labels.ScratchBuilder{} | 
					
						
							| 
									
										
										
										
											2018-12-29 01:06:12 +08:00
										 |  |  | 	for p.Next() { | 
					
						
							| 
									
										
										
										
											2022-12-16 02:19:15 +08:00
										 |  |  | 		if err = ir.Series(p.At(), &builder, &chks); err != nil { | 
					
						
							| 
									
										
										
										
											2019-07-23 16:04:48 +08:00
										 |  |  | 			return err | 
					
						
							| 
									
										
										
										
											2019-01-03 00:48:42 +08:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2018-12-29 01:06:12 +08:00
										 |  |  | 		// Amount of the block time range not covered by this series.
 | 
					
						
							|  |  |  | 		uncovered := uint64(meta.MaxTime-meta.MinTime) - uint64(chks[len(chks)-1].MaxTime-chks[0].MinTime) | 
					
						
							| 
									
										
										
										
											2022-12-16 02:19:15 +08:00
										 |  |  | 		builder.Labels().Range(func(lbl labels.Label) { | 
					
						
							| 
									
										
										
										
											2018-12-29 01:06:12 +08:00
										 |  |  | 			key := lbl.Name + "=" + lbl.Value | 
					
						
							|  |  |  | 			labelsUncovered[lbl.Name] += uncovered | 
					
						
							|  |  |  | 			labelpairsUncovered[key] += uncovered | 
					
						
							| 
									
										
										
										
											2019-01-03 00:48:42 +08:00
										 |  |  | 			labelpairsCount[key]++ | 
					
						
							|  |  |  | 			entries++ | 
					
						
							| 
									
										
										
										
											2022-02-27 22:36:53 +08:00
										 |  |  | 		}) | 
					
						
							| 
									
										
										
										
											2018-12-29 01:06:12 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	if p.Err() != nil { | 
					
						
							| 
									
										
										
										
											2019-07-23 16:04:48 +08:00
										 |  |  | 		return p.Err() | 
					
						
							| 
									
										
										
										
											2018-12-29 01:06:12 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	fmt.Printf("Postings (unique label pairs): %d\n", len(labelpairsUncovered)) | 
					
						
							|  |  |  | 	fmt.Printf("Postings entries (total label pairs): %d\n", entries) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	postingInfos = postingInfos[:0] | 
					
						
							|  |  |  | 	for k, m := range labelpairsUncovered { | 
					
						
							|  |  |  | 		postingInfos = append(postingInfos, postingInfo{k, uint64(float64(m) / float64(meta.MaxTime-meta.MinTime))}) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	fmt.Printf("\nLabel pairs most involved in churning:\n") | 
					
						
							|  |  |  | 	printInfo(postingInfos) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	postingInfos = postingInfos[:0] | 
					
						
							|  |  |  | 	for k, m := range labelsUncovered { | 
					
						
							|  |  |  | 		postingInfos = append(postingInfos, postingInfo{k, uint64(float64(m) / float64(meta.MaxTime-meta.MinTime))}) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	fmt.Printf("\nLabel names most involved in churning:\n") | 
					
						
							|  |  |  | 	printInfo(postingInfos) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	postingInfos = postingInfos[:0] | 
					
						
							|  |  |  | 	for k, m := range labelpairsCount { | 
					
						
							|  |  |  | 		postingInfos = append(postingInfos, postingInfo{k, m}) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	fmt.Printf("\nMost common label pairs:\n") | 
					
						
							|  |  |  | 	printInfo(postingInfos) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-21 16:07:50 +08:00
										 |  |  | 	postingInfos = postingInfos[:0] | 
					
						
							|  |  |  | 	for _, n := range allLabelNames { | 
					
						
							| 
									
										
										
										
											2023-09-20 18:37:32 +08:00
										 |  |  | 		values, err := ir.SortedLabelValues(ctx, n, selectors...) | 
					
						
							| 
									
										
										
										
											2019-06-21 16:07:50 +08:00
										 |  |  | 		if err != nil { | 
					
						
							| 
									
										
										
										
											2019-07-23 16:04:48 +08:00
										 |  |  | 			return err | 
					
						
							| 
									
										
										
										
											2019-06-21 16:07:50 +08:00
										 |  |  | 		} | 
					
						
							|  |  |  | 		var cumulativeLength uint64 | 
					
						
							| 
									
										
											  
											
												Replace StringTuples with []string
Benchmarks show slight cpu/allocs improvements.
benchmark                                                               old ns/op      new ns/op      delta
BenchmarkPostingsForMatchers/Head/n="1"-4                               269978625      235305110      -12.84%
BenchmarkPostingsForMatchers/Head/n="1",j="foo"-4                       129739974      121646193      -6.24%
BenchmarkPostingsForMatchers/Head/j="foo",n="1"-4                       123826274      122056253      -1.43%
BenchmarkPostingsForMatchers/Head/n="1",j!="foo"-4                      126962188      130038235      +2.42%
BenchmarkPostingsForMatchers/Head/i=~".*"-4                             6423653989     5991126455     -6.73%
BenchmarkPostingsForMatchers/Head/i=~".+"-4                             6934647521     7033370634     +1.42%
BenchmarkPostingsForMatchers/Head/i=~""-4                               1177781285     1121497736     -4.78%
BenchmarkPostingsForMatchers/Head/i!=""-4                               7033680256     7246094991     +3.02%
BenchmarkPostingsForMatchers/Head/n="1",i=~".*",j="foo"-4               293702332      287440212      -2.13%
BenchmarkPostingsForMatchers/Head/n="1",i=~".*",i!="2",j="foo"-4        307628268      307039964      -0.19%
BenchmarkPostingsForMatchers/Head/n="1",i!=""-4                         512247746      480003862      -6.29%
BenchmarkPostingsForMatchers/Head/n="1",i!="",j="foo"-4                 361199794      367066917      +1.62%
BenchmarkPostingsForMatchers/Head/n="1",i=~".+",j="foo"-4               478863761      476037784      -0.59%
BenchmarkPostingsForMatchers/Head/n="1",i=~"1.+",j="foo"-4              103394659      102902098      -0.48%
BenchmarkPostingsForMatchers/Head/n="1",i=~".+",i!="2",j="foo"-4        482552781      475453903      -1.47%
BenchmarkPostingsForMatchers/Head/n="1",i=~".+",i!~"2.*",j="foo"-4      559257389      589297047      +5.37%
BenchmarkPostingsForMatchers/Block/n="1"-4                              36492          37012          +1.42%
BenchmarkPostingsForMatchers/Block/n="1",j="foo"-4                      557788         611903         +9.70%
BenchmarkPostingsForMatchers/Block/j="foo",n="1"-4                      554443         573814         +3.49%
BenchmarkPostingsForMatchers/Block/n="1",j!="foo"-4                     553227         553826         +0.11%
BenchmarkPostingsForMatchers/Block/i=~".*"-4                            113855090      111707221      -1.89%
BenchmarkPostingsForMatchers/Block/i=~".+"-4                            133994674      136520728      +1.89%
BenchmarkPostingsForMatchers/Block/i=~""-4                              38138091       36299898       -4.82%
BenchmarkPostingsForMatchers/Block/i!=""-4                              28861213       27396723       -5.07%
BenchmarkPostingsForMatchers/Block/n="1",i=~".*",j="foo"-4              112699941      110853868      -1.64%
BenchmarkPostingsForMatchers/Block/n="1",i=~".*",i!="2",j="foo"-4       113198026      111389742      -1.60%
BenchmarkPostingsForMatchers/Block/n="1",i!=""-4                        28994069       27363804       -5.62%
BenchmarkPostingsForMatchers/Block/n="1",i!="",j="foo"-4                29709406       28589223       -3.77%
BenchmarkPostingsForMatchers/Block/n="1",i=~".+",j="foo"-4              134695119      135736971      +0.77%
BenchmarkPostingsForMatchers/Block/n="1",i=~"1.+",j="foo"-4             26783286       25826928       -3.57%
BenchmarkPostingsForMatchers/Block/n="1",i=~".+",i!="2",j="foo"-4       134733254      134116739      -0.46%
BenchmarkPostingsForMatchers/Block/n="1",i=~".+",i!~"2.*",j="foo"-4     160713937      158802768      -1.19%
benchmark                                                               old allocs     new allocs     delta
BenchmarkPostingsForMatchers/Head/n="1"-4                               36             36             +0.00%
BenchmarkPostingsForMatchers/Head/n="1",j="foo"-4                       38             38             +0.00%
BenchmarkPostingsForMatchers/Head/j="foo",n="1"-4                       38             38             +0.00%
BenchmarkPostingsForMatchers/Head/n="1",j!="foo"-4                      42             40             -4.76%
BenchmarkPostingsForMatchers/Head/i=~".*"-4                             61             59             -3.28%
BenchmarkPostingsForMatchers/Head/i=~".+"-4                             100088         100087         -0.00%
BenchmarkPostingsForMatchers/Head/i=~""-4                               100053         100051         -0.00%
BenchmarkPostingsForMatchers/Head/i!=""-4                               100087         100085         -0.00%
BenchmarkPostingsForMatchers/Head/n="1",i=~".*",j="foo"-4               44             42             -4.55%
BenchmarkPostingsForMatchers/Head/n="1",i=~".*",i!="2",j="foo"-4        50             48             -4.00%
BenchmarkPostingsForMatchers/Head/n="1",i!=""-4                         100076         100074         -0.00%
BenchmarkPostingsForMatchers/Head/n="1",i!="",j="foo"-4                 100077         100075         -0.00%
BenchmarkPostingsForMatchers/Head/n="1",i=~".+",j="foo"-4               100077         100074         -0.00%
BenchmarkPostingsForMatchers/Head/n="1",i=~"1.+",j="foo"-4              11167          11165          -0.02%
BenchmarkPostingsForMatchers/Head/n="1",i=~".+",i!="2",j="foo"-4        100082         100080         -0.00%
BenchmarkPostingsForMatchers/Head/n="1",i=~".+",i!~"2.*",j="foo"-4      111265         111261         -0.00%
BenchmarkPostingsForMatchers/Block/n="1"-4                              6              6              +0.00%
BenchmarkPostingsForMatchers/Block/n="1",j="foo"-4                      11             11             +0.00%
BenchmarkPostingsForMatchers/Block/j="foo",n="1"-4                      11             11             +0.00%
BenchmarkPostingsForMatchers/Block/n="1",j!="foo"-4                     15             13             -13.33%
BenchmarkPostingsForMatchers/Block/i=~".*"-4                            12             10             -16.67%
BenchmarkPostingsForMatchers/Block/i=~".+"-4                            100040         100038         -0.00%
BenchmarkPostingsForMatchers/Block/i=~""-4                              100045         100043         -0.00%
BenchmarkPostingsForMatchers/Block/i!=""-4                              100041         100039         -0.00%
BenchmarkPostingsForMatchers/Block/n="1",i=~".*",j="foo"-4              17             15             -11.76%
BenchmarkPostingsForMatchers/Block/n="1",i=~".*",i!="2",j="foo"-4       23             21             -8.70%
BenchmarkPostingsForMatchers/Block/n="1",i!=""-4                        100046         100044         -0.00%
BenchmarkPostingsForMatchers/Block/n="1",i!="",j="foo"-4                100050         100048         -0.00%
BenchmarkPostingsForMatchers/Block/n="1",i=~".+",j="foo"-4              100049         100047         -0.00%
BenchmarkPostingsForMatchers/Block/n="1",i=~"1.+",j="foo"-4             11150          11148          -0.02%
BenchmarkPostingsForMatchers/Block/n="1",i=~".+",i!="2",j="foo"-4       100055         100053         -0.00%
BenchmarkPostingsForMatchers/Block/n="1",i=~".+",i!~"2.*",j="foo"-4     111238         111234         -0.00%
benchmark                                                               old bytes     new bytes     delta
BenchmarkPostingsForMatchers/Head/n="1"-4                               10887816      10887817      +0.00%
BenchmarkPostingsForMatchers/Head/n="1",j="foo"-4                       5456648       5456648       +0.00%
BenchmarkPostingsForMatchers/Head/j="foo",n="1"-4                       5456648       5456648       +0.00%
BenchmarkPostingsForMatchers/Head/n="1",j!="foo"-4                      5456792       5456712       -0.00%
BenchmarkPostingsForMatchers/Head/i=~".*"-4                             258254408     258254328     -0.00%
BenchmarkPostingsForMatchers/Head/i=~".+"-4                             273912888     273912904     +0.00%
BenchmarkPostingsForMatchers/Head/i=~""-4                               17266680      17266600      -0.00%
BenchmarkPostingsForMatchers/Head/i!=""-4                               273912416     273912336     -0.00%
BenchmarkPostingsForMatchers/Head/n="1",i=~".*",j="foo"-4               7062578       7062498       -0.00%
BenchmarkPostingsForMatchers/Head/n="1",i=~".*",i!="2",j="foo"-4        7062770       7062690       -0.00%
BenchmarkPostingsForMatchers/Head/n="1",i!=""-4                         28152346      28152266      -0.00%
BenchmarkPostingsForMatchers/Head/n="1",i!="",j="foo"-4                 22721178      22721098      -0.00%
BenchmarkPostingsForMatchers/Head/n="1",i=~".+",j="foo"-4               22721336      22721224      -0.00%
BenchmarkPostingsForMatchers/Head/n="1",i=~"1.+",j="foo"-4              3623804       3623733       -0.00%
BenchmarkPostingsForMatchers/Head/n="1",i=~".+",i!="2",j="foo"-4        22721480      22721400      -0.00%
BenchmarkPostingsForMatchers/Head/n="1",i=~".+",i!~"2.*",j="foo"-4      24816652      24816444      -0.00%
BenchmarkPostingsForMatchers/Block/n="1"-4                              296           296           +0.00%
BenchmarkPostingsForMatchers/Block/n="1",j="foo"-4                      424           424           +0.00%
BenchmarkPostingsForMatchers/Block/j="foo",n="1"-4                      424           424           +0.00%
BenchmarkPostingsForMatchers/Block/n="1",j!="foo"-4                     1544          1464          -5.18%
BenchmarkPostingsForMatchers/Block/i=~".*"-4                            1606114       1606045       -0.00%
BenchmarkPostingsForMatchers/Block/i=~".+"-4                            17264709      17264629      -0.00%
BenchmarkPostingsForMatchers/Block/i=~""-4                              17264780      17264696      -0.00%
BenchmarkPostingsForMatchers/Block/i!=""-4                              17264680      17264600      -0.00%
BenchmarkPostingsForMatchers/Block/n="1",i=~".*",j="foo"-4              1606253       1606165       -0.01%
BenchmarkPostingsForMatchers/Block/n="1",i=~".*",i!="2",j="foo"-4       1606445       1606348       -0.01%
BenchmarkPostingsForMatchers/Block/n="1",i!=""-4                        17264808      17264728      -0.00%
BenchmarkPostingsForMatchers/Block/n="1",i!="",j="foo"-4                17264936      17264856      -0.00%
BenchmarkPostingsForMatchers/Block/n="1",i=~".+",j="foo"-4              17264965      17264885      -0.00%
BenchmarkPostingsForMatchers/Block/n="1",i=~"1.+",j="foo"-4             3148262       3148182       -0.00%
BenchmarkPostingsForMatchers/Block/n="1",i=~".+",i!="2",j="foo"-4       17265141      17265061      -0.00%
BenchmarkPostingsForMatchers/Block/n="1",i=~".+",i!~"2.*",j="foo"-4     20416944      20416784      -0.00%
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
											
										 
											2020-01-01 19:38:01 +08:00
										 |  |  | 		for _, str := range values { | 
					
						
							|  |  |  | 			cumulativeLength += uint64(len(str)) | 
					
						
							| 
									
										
										
										
											2019-06-21 16:07:50 +08:00
										 |  |  | 		} | 
					
						
							|  |  |  | 		postingInfos = append(postingInfos, postingInfo{n, cumulativeLength}) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	fmt.Printf("\nLabel names with highest cumulative label value length:\n") | 
					
						
							|  |  |  | 	printInfo(postingInfos) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-12-29 01:06:12 +08:00
										 |  |  | 	postingInfos = postingInfos[:0] | 
					
						
							|  |  |  | 	for _, n := range allLabelNames { | 
					
						
							| 
									
										
										
										
											2023-09-20 18:37:32 +08:00
										 |  |  | 		lv, err := ir.SortedLabelValues(ctx, n, selectors...) | 
					
						
							| 
									
										
										
										
											2018-12-29 01:06:12 +08:00
										 |  |  | 		if err != nil { | 
					
						
							| 
									
										
										
										
											2019-07-23 16:04:48 +08:00
										 |  |  | 			return err | 
					
						
							| 
									
										
										
										
											2018-12-29 01:06:12 +08:00
										 |  |  | 		} | 
					
						
							| 
									
										
											  
											
												Replace StringTuples with []string
Benchmarks show slight cpu/allocs improvements.
benchmark                                                               old ns/op      new ns/op      delta
BenchmarkPostingsForMatchers/Head/n="1"-4                               269978625      235305110      -12.84%
BenchmarkPostingsForMatchers/Head/n="1",j="foo"-4                       129739974      121646193      -6.24%
BenchmarkPostingsForMatchers/Head/j="foo",n="1"-4                       123826274      122056253      -1.43%
BenchmarkPostingsForMatchers/Head/n="1",j!="foo"-4                      126962188      130038235      +2.42%
BenchmarkPostingsForMatchers/Head/i=~".*"-4                             6423653989     5991126455     -6.73%
BenchmarkPostingsForMatchers/Head/i=~".+"-4                             6934647521     7033370634     +1.42%
BenchmarkPostingsForMatchers/Head/i=~""-4                               1177781285     1121497736     -4.78%
BenchmarkPostingsForMatchers/Head/i!=""-4                               7033680256     7246094991     +3.02%
BenchmarkPostingsForMatchers/Head/n="1",i=~".*",j="foo"-4               293702332      287440212      -2.13%
BenchmarkPostingsForMatchers/Head/n="1",i=~".*",i!="2",j="foo"-4        307628268      307039964      -0.19%
BenchmarkPostingsForMatchers/Head/n="1",i!=""-4                         512247746      480003862      -6.29%
BenchmarkPostingsForMatchers/Head/n="1",i!="",j="foo"-4                 361199794      367066917      +1.62%
BenchmarkPostingsForMatchers/Head/n="1",i=~".+",j="foo"-4               478863761      476037784      -0.59%
BenchmarkPostingsForMatchers/Head/n="1",i=~"1.+",j="foo"-4              103394659      102902098      -0.48%
BenchmarkPostingsForMatchers/Head/n="1",i=~".+",i!="2",j="foo"-4        482552781      475453903      -1.47%
BenchmarkPostingsForMatchers/Head/n="1",i=~".+",i!~"2.*",j="foo"-4      559257389      589297047      +5.37%
BenchmarkPostingsForMatchers/Block/n="1"-4                              36492          37012          +1.42%
BenchmarkPostingsForMatchers/Block/n="1",j="foo"-4                      557788         611903         +9.70%
BenchmarkPostingsForMatchers/Block/j="foo",n="1"-4                      554443         573814         +3.49%
BenchmarkPostingsForMatchers/Block/n="1",j!="foo"-4                     553227         553826         +0.11%
BenchmarkPostingsForMatchers/Block/i=~".*"-4                            113855090      111707221      -1.89%
BenchmarkPostingsForMatchers/Block/i=~".+"-4                            133994674      136520728      +1.89%
BenchmarkPostingsForMatchers/Block/i=~""-4                              38138091       36299898       -4.82%
BenchmarkPostingsForMatchers/Block/i!=""-4                              28861213       27396723       -5.07%
BenchmarkPostingsForMatchers/Block/n="1",i=~".*",j="foo"-4              112699941      110853868      -1.64%
BenchmarkPostingsForMatchers/Block/n="1",i=~".*",i!="2",j="foo"-4       113198026      111389742      -1.60%
BenchmarkPostingsForMatchers/Block/n="1",i!=""-4                        28994069       27363804       -5.62%
BenchmarkPostingsForMatchers/Block/n="1",i!="",j="foo"-4                29709406       28589223       -3.77%
BenchmarkPostingsForMatchers/Block/n="1",i=~".+",j="foo"-4              134695119      135736971      +0.77%
BenchmarkPostingsForMatchers/Block/n="1",i=~"1.+",j="foo"-4             26783286       25826928       -3.57%
BenchmarkPostingsForMatchers/Block/n="1",i=~".+",i!="2",j="foo"-4       134733254      134116739      -0.46%
BenchmarkPostingsForMatchers/Block/n="1",i=~".+",i!~"2.*",j="foo"-4     160713937      158802768      -1.19%
benchmark                                                               old allocs     new allocs     delta
BenchmarkPostingsForMatchers/Head/n="1"-4                               36             36             +0.00%
BenchmarkPostingsForMatchers/Head/n="1",j="foo"-4                       38             38             +0.00%
BenchmarkPostingsForMatchers/Head/j="foo",n="1"-4                       38             38             +0.00%
BenchmarkPostingsForMatchers/Head/n="1",j!="foo"-4                      42             40             -4.76%
BenchmarkPostingsForMatchers/Head/i=~".*"-4                             61             59             -3.28%
BenchmarkPostingsForMatchers/Head/i=~".+"-4                             100088         100087         -0.00%
BenchmarkPostingsForMatchers/Head/i=~""-4                               100053         100051         -0.00%
BenchmarkPostingsForMatchers/Head/i!=""-4                               100087         100085         -0.00%
BenchmarkPostingsForMatchers/Head/n="1",i=~".*",j="foo"-4               44             42             -4.55%
BenchmarkPostingsForMatchers/Head/n="1",i=~".*",i!="2",j="foo"-4        50             48             -4.00%
BenchmarkPostingsForMatchers/Head/n="1",i!=""-4                         100076         100074         -0.00%
BenchmarkPostingsForMatchers/Head/n="1",i!="",j="foo"-4                 100077         100075         -0.00%
BenchmarkPostingsForMatchers/Head/n="1",i=~".+",j="foo"-4               100077         100074         -0.00%
BenchmarkPostingsForMatchers/Head/n="1",i=~"1.+",j="foo"-4              11167          11165          -0.02%
BenchmarkPostingsForMatchers/Head/n="1",i=~".+",i!="2",j="foo"-4        100082         100080         -0.00%
BenchmarkPostingsForMatchers/Head/n="1",i=~".+",i!~"2.*",j="foo"-4      111265         111261         -0.00%
BenchmarkPostingsForMatchers/Block/n="1"-4                              6              6              +0.00%
BenchmarkPostingsForMatchers/Block/n="1",j="foo"-4                      11             11             +0.00%
BenchmarkPostingsForMatchers/Block/j="foo",n="1"-4                      11             11             +0.00%
BenchmarkPostingsForMatchers/Block/n="1",j!="foo"-4                     15             13             -13.33%
BenchmarkPostingsForMatchers/Block/i=~".*"-4                            12             10             -16.67%
BenchmarkPostingsForMatchers/Block/i=~".+"-4                            100040         100038         -0.00%
BenchmarkPostingsForMatchers/Block/i=~""-4                              100045         100043         -0.00%
BenchmarkPostingsForMatchers/Block/i!=""-4                              100041         100039         -0.00%
BenchmarkPostingsForMatchers/Block/n="1",i=~".*",j="foo"-4              17             15             -11.76%
BenchmarkPostingsForMatchers/Block/n="1",i=~".*",i!="2",j="foo"-4       23             21             -8.70%
BenchmarkPostingsForMatchers/Block/n="1",i!=""-4                        100046         100044         -0.00%
BenchmarkPostingsForMatchers/Block/n="1",i!="",j="foo"-4                100050         100048         -0.00%
BenchmarkPostingsForMatchers/Block/n="1",i=~".+",j="foo"-4              100049         100047         -0.00%
BenchmarkPostingsForMatchers/Block/n="1",i=~"1.+",j="foo"-4             11150          11148          -0.02%
BenchmarkPostingsForMatchers/Block/n="1",i=~".+",i!="2",j="foo"-4       100055         100053         -0.00%
BenchmarkPostingsForMatchers/Block/n="1",i=~".+",i!~"2.*",j="foo"-4     111238         111234         -0.00%
benchmark                                                               old bytes     new bytes     delta
BenchmarkPostingsForMatchers/Head/n="1"-4                               10887816      10887817      +0.00%
BenchmarkPostingsForMatchers/Head/n="1",j="foo"-4                       5456648       5456648       +0.00%
BenchmarkPostingsForMatchers/Head/j="foo",n="1"-4                       5456648       5456648       +0.00%
BenchmarkPostingsForMatchers/Head/n="1",j!="foo"-4                      5456792       5456712       -0.00%
BenchmarkPostingsForMatchers/Head/i=~".*"-4                             258254408     258254328     -0.00%
BenchmarkPostingsForMatchers/Head/i=~".+"-4                             273912888     273912904     +0.00%
BenchmarkPostingsForMatchers/Head/i=~""-4                               17266680      17266600      -0.00%
BenchmarkPostingsForMatchers/Head/i!=""-4                               273912416     273912336     -0.00%
BenchmarkPostingsForMatchers/Head/n="1",i=~".*",j="foo"-4               7062578       7062498       -0.00%
BenchmarkPostingsForMatchers/Head/n="1",i=~".*",i!="2",j="foo"-4        7062770       7062690       -0.00%
BenchmarkPostingsForMatchers/Head/n="1",i!=""-4                         28152346      28152266      -0.00%
BenchmarkPostingsForMatchers/Head/n="1",i!="",j="foo"-4                 22721178      22721098      -0.00%
BenchmarkPostingsForMatchers/Head/n="1",i=~".+",j="foo"-4               22721336      22721224      -0.00%
BenchmarkPostingsForMatchers/Head/n="1",i=~"1.+",j="foo"-4              3623804       3623733       -0.00%
BenchmarkPostingsForMatchers/Head/n="1",i=~".+",i!="2",j="foo"-4        22721480      22721400      -0.00%
BenchmarkPostingsForMatchers/Head/n="1",i=~".+",i!~"2.*",j="foo"-4      24816652      24816444      -0.00%
BenchmarkPostingsForMatchers/Block/n="1"-4                              296           296           +0.00%
BenchmarkPostingsForMatchers/Block/n="1",j="foo"-4                      424           424           +0.00%
BenchmarkPostingsForMatchers/Block/j="foo",n="1"-4                      424           424           +0.00%
BenchmarkPostingsForMatchers/Block/n="1",j!="foo"-4                     1544          1464          -5.18%
BenchmarkPostingsForMatchers/Block/i=~".*"-4                            1606114       1606045       -0.00%
BenchmarkPostingsForMatchers/Block/i=~".+"-4                            17264709      17264629      -0.00%
BenchmarkPostingsForMatchers/Block/i=~""-4                              17264780      17264696      -0.00%
BenchmarkPostingsForMatchers/Block/i!=""-4                              17264680      17264600      -0.00%
BenchmarkPostingsForMatchers/Block/n="1",i=~".*",j="foo"-4              1606253       1606165       -0.01%
BenchmarkPostingsForMatchers/Block/n="1",i=~".*",i!="2",j="foo"-4       1606445       1606348       -0.01%
BenchmarkPostingsForMatchers/Block/n="1",i!=""-4                        17264808      17264728      -0.00%
BenchmarkPostingsForMatchers/Block/n="1",i!="",j="foo"-4                17264936      17264856      -0.00%
BenchmarkPostingsForMatchers/Block/n="1",i=~".+",j="foo"-4              17264965      17264885      -0.00%
BenchmarkPostingsForMatchers/Block/n="1",i=~"1.+",j="foo"-4             3148262       3148182       -0.00%
BenchmarkPostingsForMatchers/Block/n="1",i=~".+",i!="2",j="foo"-4       17265141      17265061      -0.00%
BenchmarkPostingsForMatchers/Block/n="1",i=~".+",i!~"2.*",j="foo"-4     20416944      20416784      -0.00%
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
											
										 
											2020-01-01 19:38:01 +08:00
										 |  |  | 		postingInfos = append(postingInfos, postingInfo{n, uint64(len(lv))}) | 
					
						
							| 
									
										
										
										
											2018-12-29 01:06:12 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	fmt.Printf("\nHighest cardinality labels:\n") | 
					
						
							|  |  |  | 	printInfo(postingInfos) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	postingInfos = postingInfos[:0] | 
					
						
							| 
									
										
										
										
											2023-09-20 18:37:32 +08:00
										 |  |  | 	lv, err := ir.SortedLabelValues(ctx, "__name__", selectors...) | 
					
						
							| 
									
										
										
										
											2018-12-29 01:06:12 +08:00
										 |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2019-07-23 16:04:48 +08:00
										 |  |  | 		return err | 
					
						
							| 
									
										
										
										
											2018-12-29 01:06:12 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
											  
											
												Replace StringTuples with []string
Benchmarks show slight cpu/allocs improvements.
benchmark                                                               old ns/op      new ns/op      delta
BenchmarkPostingsForMatchers/Head/n="1"-4                               269978625      235305110      -12.84%
BenchmarkPostingsForMatchers/Head/n="1",j="foo"-4                       129739974      121646193      -6.24%
BenchmarkPostingsForMatchers/Head/j="foo",n="1"-4                       123826274      122056253      -1.43%
BenchmarkPostingsForMatchers/Head/n="1",j!="foo"-4                      126962188      130038235      +2.42%
BenchmarkPostingsForMatchers/Head/i=~".*"-4                             6423653989     5991126455     -6.73%
BenchmarkPostingsForMatchers/Head/i=~".+"-4                             6934647521     7033370634     +1.42%
BenchmarkPostingsForMatchers/Head/i=~""-4                               1177781285     1121497736     -4.78%
BenchmarkPostingsForMatchers/Head/i!=""-4                               7033680256     7246094991     +3.02%
BenchmarkPostingsForMatchers/Head/n="1",i=~".*",j="foo"-4               293702332      287440212      -2.13%
BenchmarkPostingsForMatchers/Head/n="1",i=~".*",i!="2",j="foo"-4        307628268      307039964      -0.19%
BenchmarkPostingsForMatchers/Head/n="1",i!=""-4                         512247746      480003862      -6.29%
BenchmarkPostingsForMatchers/Head/n="1",i!="",j="foo"-4                 361199794      367066917      +1.62%
BenchmarkPostingsForMatchers/Head/n="1",i=~".+",j="foo"-4               478863761      476037784      -0.59%
BenchmarkPostingsForMatchers/Head/n="1",i=~"1.+",j="foo"-4              103394659      102902098      -0.48%
BenchmarkPostingsForMatchers/Head/n="1",i=~".+",i!="2",j="foo"-4        482552781      475453903      -1.47%
BenchmarkPostingsForMatchers/Head/n="1",i=~".+",i!~"2.*",j="foo"-4      559257389      589297047      +5.37%
BenchmarkPostingsForMatchers/Block/n="1"-4                              36492          37012          +1.42%
BenchmarkPostingsForMatchers/Block/n="1",j="foo"-4                      557788         611903         +9.70%
BenchmarkPostingsForMatchers/Block/j="foo",n="1"-4                      554443         573814         +3.49%
BenchmarkPostingsForMatchers/Block/n="1",j!="foo"-4                     553227         553826         +0.11%
BenchmarkPostingsForMatchers/Block/i=~".*"-4                            113855090      111707221      -1.89%
BenchmarkPostingsForMatchers/Block/i=~".+"-4                            133994674      136520728      +1.89%
BenchmarkPostingsForMatchers/Block/i=~""-4                              38138091       36299898       -4.82%
BenchmarkPostingsForMatchers/Block/i!=""-4                              28861213       27396723       -5.07%
BenchmarkPostingsForMatchers/Block/n="1",i=~".*",j="foo"-4              112699941      110853868      -1.64%
BenchmarkPostingsForMatchers/Block/n="1",i=~".*",i!="2",j="foo"-4       113198026      111389742      -1.60%
BenchmarkPostingsForMatchers/Block/n="1",i!=""-4                        28994069       27363804       -5.62%
BenchmarkPostingsForMatchers/Block/n="1",i!="",j="foo"-4                29709406       28589223       -3.77%
BenchmarkPostingsForMatchers/Block/n="1",i=~".+",j="foo"-4              134695119      135736971      +0.77%
BenchmarkPostingsForMatchers/Block/n="1",i=~"1.+",j="foo"-4             26783286       25826928       -3.57%
BenchmarkPostingsForMatchers/Block/n="1",i=~".+",i!="2",j="foo"-4       134733254      134116739      -0.46%
BenchmarkPostingsForMatchers/Block/n="1",i=~".+",i!~"2.*",j="foo"-4     160713937      158802768      -1.19%
benchmark                                                               old allocs     new allocs     delta
BenchmarkPostingsForMatchers/Head/n="1"-4                               36             36             +0.00%
BenchmarkPostingsForMatchers/Head/n="1",j="foo"-4                       38             38             +0.00%
BenchmarkPostingsForMatchers/Head/j="foo",n="1"-4                       38             38             +0.00%
BenchmarkPostingsForMatchers/Head/n="1",j!="foo"-4                      42             40             -4.76%
BenchmarkPostingsForMatchers/Head/i=~".*"-4                             61             59             -3.28%
BenchmarkPostingsForMatchers/Head/i=~".+"-4                             100088         100087         -0.00%
BenchmarkPostingsForMatchers/Head/i=~""-4                               100053         100051         -0.00%
BenchmarkPostingsForMatchers/Head/i!=""-4                               100087         100085         -0.00%
BenchmarkPostingsForMatchers/Head/n="1",i=~".*",j="foo"-4               44             42             -4.55%
BenchmarkPostingsForMatchers/Head/n="1",i=~".*",i!="2",j="foo"-4        50             48             -4.00%
BenchmarkPostingsForMatchers/Head/n="1",i!=""-4                         100076         100074         -0.00%
BenchmarkPostingsForMatchers/Head/n="1",i!="",j="foo"-4                 100077         100075         -0.00%
BenchmarkPostingsForMatchers/Head/n="1",i=~".+",j="foo"-4               100077         100074         -0.00%
BenchmarkPostingsForMatchers/Head/n="1",i=~"1.+",j="foo"-4              11167          11165          -0.02%
BenchmarkPostingsForMatchers/Head/n="1",i=~".+",i!="2",j="foo"-4        100082         100080         -0.00%
BenchmarkPostingsForMatchers/Head/n="1",i=~".+",i!~"2.*",j="foo"-4      111265         111261         -0.00%
BenchmarkPostingsForMatchers/Block/n="1"-4                              6              6              +0.00%
BenchmarkPostingsForMatchers/Block/n="1",j="foo"-4                      11             11             +0.00%
BenchmarkPostingsForMatchers/Block/j="foo",n="1"-4                      11             11             +0.00%
BenchmarkPostingsForMatchers/Block/n="1",j!="foo"-4                     15             13             -13.33%
BenchmarkPostingsForMatchers/Block/i=~".*"-4                            12             10             -16.67%
BenchmarkPostingsForMatchers/Block/i=~".+"-4                            100040         100038         -0.00%
BenchmarkPostingsForMatchers/Block/i=~""-4                              100045         100043         -0.00%
BenchmarkPostingsForMatchers/Block/i!=""-4                              100041         100039         -0.00%
BenchmarkPostingsForMatchers/Block/n="1",i=~".*",j="foo"-4              17             15             -11.76%
BenchmarkPostingsForMatchers/Block/n="1",i=~".*",i!="2",j="foo"-4       23             21             -8.70%
BenchmarkPostingsForMatchers/Block/n="1",i!=""-4                        100046         100044         -0.00%
BenchmarkPostingsForMatchers/Block/n="1",i!="",j="foo"-4                100050         100048         -0.00%
BenchmarkPostingsForMatchers/Block/n="1",i=~".+",j="foo"-4              100049         100047         -0.00%
BenchmarkPostingsForMatchers/Block/n="1",i=~"1.+",j="foo"-4             11150          11148          -0.02%
BenchmarkPostingsForMatchers/Block/n="1",i=~".+",i!="2",j="foo"-4       100055         100053         -0.00%
BenchmarkPostingsForMatchers/Block/n="1",i=~".+",i!~"2.*",j="foo"-4     111238         111234         -0.00%
benchmark                                                               old bytes     new bytes     delta
BenchmarkPostingsForMatchers/Head/n="1"-4                               10887816      10887817      +0.00%
BenchmarkPostingsForMatchers/Head/n="1",j="foo"-4                       5456648       5456648       +0.00%
BenchmarkPostingsForMatchers/Head/j="foo",n="1"-4                       5456648       5456648       +0.00%
BenchmarkPostingsForMatchers/Head/n="1",j!="foo"-4                      5456792       5456712       -0.00%
BenchmarkPostingsForMatchers/Head/i=~".*"-4                             258254408     258254328     -0.00%
BenchmarkPostingsForMatchers/Head/i=~".+"-4                             273912888     273912904     +0.00%
BenchmarkPostingsForMatchers/Head/i=~""-4                               17266680      17266600      -0.00%
BenchmarkPostingsForMatchers/Head/i!=""-4                               273912416     273912336     -0.00%
BenchmarkPostingsForMatchers/Head/n="1",i=~".*",j="foo"-4               7062578       7062498       -0.00%
BenchmarkPostingsForMatchers/Head/n="1",i=~".*",i!="2",j="foo"-4        7062770       7062690       -0.00%
BenchmarkPostingsForMatchers/Head/n="1",i!=""-4                         28152346      28152266      -0.00%
BenchmarkPostingsForMatchers/Head/n="1",i!="",j="foo"-4                 22721178      22721098      -0.00%
BenchmarkPostingsForMatchers/Head/n="1",i=~".+",j="foo"-4               22721336      22721224      -0.00%
BenchmarkPostingsForMatchers/Head/n="1",i=~"1.+",j="foo"-4              3623804       3623733       -0.00%
BenchmarkPostingsForMatchers/Head/n="1",i=~".+",i!="2",j="foo"-4        22721480      22721400      -0.00%
BenchmarkPostingsForMatchers/Head/n="1",i=~".+",i!~"2.*",j="foo"-4      24816652      24816444      -0.00%
BenchmarkPostingsForMatchers/Block/n="1"-4                              296           296           +0.00%
BenchmarkPostingsForMatchers/Block/n="1",j="foo"-4                      424           424           +0.00%
BenchmarkPostingsForMatchers/Block/j="foo",n="1"-4                      424           424           +0.00%
BenchmarkPostingsForMatchers/Block/n="1",j!="foo"-4                     1544          1464          -5.18%
BenchmarkPostingsForMatchers/Block/i=~".*"-4                            1606114       1606045       -0.00%
BenchmarkPostingsForMatchers/Block/i=~".+"-4                            17264709      17264629      -0.00%
BenchmarkPostingsForMatchers/Block/i=~""-4                              17264780      17264696      -0.00%
BenchmarkPostingsForMatchers/Block/i!=""-4                              17264680      17264600      -0.00%
BenchmarkPostingsForMatchers/Block/n="1",i=~".*",j="foo"-4              1606253       1606165       -0.01%
BenchmarkPostingsForMatchers/Block/n="1",i=~".*",i!="2",j="foo"-4       1606445       1606348       -0.01%
BenchmarkPostingsForMatchers/Block/n="1",i!=""-4                        17264808      17264728      -0.00%
BenchmarkPostingsForMatchers/Block/n="1",i!="",j="foo"-4                17264936      17264856      -0.00%
BenchmarkPostingsForMatchers/Block/n="1",i=~".+",j="foo"-4              17264965      17264885      -0.00%
BenchmarkPostingsForMatchers/Block/n="1",i=~"1.+",j="foo"-4             3148262       3148182       -0.00%
BenchmarkPostingsForMatchers/Block/n="1",i=~".+",i!="2",j="foo"-4       17265141      17265061      -0.00%
BenchmarkPostingsForMatchers/Block/n="1",i=~".+",i!~"2.*",j="foo"-4     20416944      20416784      -0.00%
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
											
										 
											2020-01-01 19:38:01 +08:00
										 |  |  | 	for _, n := range lv { | 
					
						
							| 
									
										
										
										
											2023-09-13 23:45:06 +08:00
										 |  |  | 		postings, err := ir.Postings(ctx, "__name__", n) | 
					
						
							| 
									
										
										
										
											2018-12-29 01:06:12 +08:00
										 |  |  | 		if err != nil { | 
					
						
							| 
									
										
										
										
											2019-07-23 16:04:48 +08:00
										 |  |  | 			return err | 
					
						
							| 
									
										
										
										
											2018-12-29 01:06:12 +08:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2024-11-25 00:30:20 +08:00
										 |  |  | 		// Only intersect postings if matchers are specified.
 | 
					
						
							|  |  |  | 		if len(matchers) > 0 { | 
					
						
							|  |  |  | 			postings = index.Intersect(postings, index.NewListPostings(refs)) | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
											  
											
												Replace StringTuples with []string
Benchmarks show slight cpu/allocs improvements.
benchmark                                                               old ns/op      new ns/op      delta
BenchmarkPostingsForMatchers/Head/n="1"-4                               269978625      235305110      -12.84%
BenchmarkPostingsForMatchers/Head/n="1",j="foo"-4                       129739974      121646193      -6.24%
BenchmarkPostingsForMatchers/Head/j="foo",n="1"-4                       123826274      122056253      -1.43%
BenchmarkPostingsForMatchers/Head/n="1",j!="foo"-4                      126962188      130038235      +2.42%
BenchmarkPostingsForMatchers/Head/i=~".*"-4                             6423653989     5991126455     -6.73%
BenchmarkPostingsForMatchers/Head/i=~".+"-4                             6934647521     7033370634     +1.42%
BenchmarkPostingsForMatchers/Head/i=~""-4                               1177781285     1121497736     -4.78%
BenchmarkPostingsForMatchers/Head/i!=""-4                               7033680256     7246094991     +3.02%
BenchmarkPostingsForMatchers/Head/n="1",i=~".*",j="foo"-4               293702332      287440212      -2.13%
BenchmarkPostingsForMatchers/Head/n="1",i=~".*",i!="2",j="foo"-4        307628268      307039964      -0.19%
BenchmarkPostingsForMatchers/Head/n="1",i!=""-4                         512247746      480003862      -6.29%
BenchmarkPostingsForMatchers/Head/n="1",i!="",j="foo"-4                 361199794      367066917      +1.62%
BenchmarkPostingsForMatchers/Head/n="1",i=~".+",j="foo"-4               478863761      476037784      -0.59%
BenchmarkPostingsForMatchers/Head/n="1",i=~"1.+",j="foo"-4              103394659      102902098      -0.48%
BenchmarkPostingsForMatchers/Head/n="1",i=~".+",i!="2",j="foo"-4        482552781      475453903      -1.47%
BenchmarkPostingsForMatchers/Head/n="1",i=~".+",i!~"2.*",j="foo"-4      559257389      589297047      +5.37%
BenchmarkPostingsForMatchers/Block/n="1"-4                              36492          37012          +1.42%
BenchmarkPostingsForMatchers/Block/n="1",j="foo"-4                      557788         611903         +9.70%
BenchmarkPostingsForMatchers/Block/j="foo",n="1"-4                      554443         573814         +3.49%
BenchmarkPostingsForMatchers/Block/n="1",j!="foo"-4                     553227         553826         +0.11%
BenchmarkPostingsForMatchers/Block/i=~".*"-4                            113855090      111707221      -1.89%
BenchmarkPostingsForMatchers/Block/i=~".+"-4                            133994674      136520728      +1.89%
BenchmarkPostingsForMatchers/Block/i=~""-4                              38138091       36299898       -4.82%
BenchmarkPostingsForMatchers/Block/i!=""-4                              28861213       27396723       -5.07%
BenchmarkPostingsForMatchers/Block/n="1",i=~".*",j="foo"-4              112699941      110853868      -1.64%
BenchmarkPostingsForMatchers/Block/n="1",i=~".*",i!="2",j="foo"-4       113198026      111389742      -1.60%
BenchmarkPostingsForMatchers/Block/n="1",i!=""-4                        28994069       27363804       -5.62%
BenchmarkPostingsForMatchers/Block/n="1",i!="",j="foo"-4                29709406       28589223       -3.77%
BenchmarkPostingsForMatchers/Block/n="1",i=~".+",j="foo"-4              134695119      135736971      +0.77%
BenchmarkPostingsForMatchers/Block/n="1",i=~"1.+",j="foo"-4             26783286       25826928       -3.57%
BenchmarkPostingsForMatchers/Block/n="1",i=~".+",i!="2",j="foo"-4       134733254      134116739      -0.46%
BenchmarkPostingsForMatchers/Block/n="1",i=~".+",i!~"2.*",j="foo"-4     160713937      158802768      -1.19%
benchmark                                                               old allocs     new allocs     delta
BenchmarkPostingsForMatchers/Head/n="1"-4                               36             36             +0.00%
BenchmarkPostingsForMatchers/Head/n="1",j="foo"-4                       38             38             +0.00%
BenchmarkPostingsForMatchers/Head/j="foo",n="1"-4                       38             38             +0.00%
BenchmarkPostingsForMatchers/Head/n="1",j!="foo"-4                      42             40             -4.76%
BenchmarkPostingsForMatchers/Head/i=~".*"-4                             61             59             -3.28%
BenchmarkPostingsForMatchers/Head/i=~".+"-4                             100088         100087         -0.00%
BenchmarkPostingsForMatchers/Head/i=~""-4                               100053         100051         -0.00%
BenchmarkPostingsForMatchers/Head/i!=""-4                               100087         100085         -0.00%
BenchmarkPostingsForMatchers/Head/n="1",i=~".*",j="foo"-4               44             42             -4.55%
BenchmarkPostingsForMatchers/Head/n="1",i=~".*",i!="2",j="foo"-4        50             48             -4.00%
BenchmarkPostingsForMatchers/Head/n="1",i!=""-4                         100076         100074         -0.00%
BenchmarkPostingsForMatchers/Head/n="1",i!="",j="foo"-4                 100077         100075         -0.00%
BenchmarkPostingsForMatchers/Head/n="1",i=~".+",j="foo"-4               100077         100074         -0.00%
BenchmarkPostingsForMatchers/Head/n="1",i=~"1.+",j="foo"-4              11167          11165          -0.02%
BenchmarkPostingsForMatchers/Head/n="1",i=~".+",i!="2",j="foo"-4        100082         100080         -0.00%
BenchmarkPostingsForMatchers/Head/n="1",i=~".+",i!~"2.*",j="foo"-4      111265         111261         -0.00%
BenchmarkPostingsForMatchers/Block/n="1"-4                              6              6              +0.00%
BenchmarkPostingsForMatchers/Block/n="1",j="foo"-4                      11             11             +0.00%
BenchmarkPostingsForMatchers/Block/j="foo",n="1"-4                      11             11             +0.00%
BenchmarkPostingsForMatchers/Block/n="1",j!="foo"-4                     15             13             -13.33%
BenchmarkPostingsForMatchers/Block/i=~".*"-4                            12             10             -16.67%
BenchmarkPostingsForMatchers/Block/i=~".+"-4                            100040         100038         -0.00%
BenchmarkPostingsForMatchers/Block/i=~""-4                              100045         100043         -0.00%
BenchmarkPostingsForMatchers/Block/i!=""-4                              100041         100039         -0.00%
BenchmarkPostingsForMatchers/Block/n="1",i=~".*",j="foo"-4              17             15             -11.76%
BenchmarkPostingsForMatchers/Block/n="1",i=~".*",i!="2",j="foo"-4       23             21             -8.70%
BenchmarkPostingsForMatchers/Block/n="1",i!=""-4                        100046         100044         -0.00%
BenchmarkPostingsForMatchers/Block/n="1",i!="",j="foo"-4                100050         100048         -0.00%
BenchmarkPostingsForMatchers/Block/n="1",i=~".+",j="foo"-4              100049         100047         -0.00%
BenchmarkPostingsForMatchers/Block/n="1",i=~"1.+",j="foo"-4             11150          11148          -0.02%
BenchmarkPostingsForMatchers/Block/n="1",i=~".+",i!="2",j="foo"-4       100055         100053         -0.00%
BenchmarkPostingsForMatchers/Block/n="1",i=~".+",i!~"2.*",j="foo"-4     111238         111234         -0.00%
benchmark                                                               old bytes     new bytes     delta
BenchmarkPostingsForMatchers/Head/n="1"-4                               10887816      10887817      +0.00%
BenchmarkPostingsForMatchers/Head/n="1",j="foo"-4                       5456648       5456648       +0.00%
BenchmarkPostingsForMatchers/Head/j="foo",n="1"-4                       5456648       5456648       +0.00%
BenchmarkPostingsForMatchers/Head/n="1",j!="foo"-4                      5456792       5456712       -0.00%
BenchmarkPostingsForMatchers/Head/i=~".*"-4                             258254408     258254328     -0.00%
BenchmarkPostingsForMatchers/Head/i=~".+"-4                             273912888     273912904     +0.00%
BenchmarkPostingsForMatchers/Head/i=~""-4                               17266680      17266600      -0.00%
BenchmarkPostingsForMatchers/Head/i!=""-4                               273912416     273912336     -0.00%
BenchmarkPostingsForMatchers/Head/n="1",i=~".*",j="foo"-4               7062578       7062498       -0.00%
BenchmarkPostingsForMatchers/Head/n="1",i=~".*",i!="2",j="foo"-4        7062770       7062690       -0.00%
BenchmarkPostingsForMatchers/Head/n="1",i!=""-4                         28152346      28152266      -0.00%
BenchmarkPostingsForMatchers/Head/n="1",i!="",j="foo"-4                 22721178      22721098      -0.00%
BenchmarkPostingsForMatchers/Head/n="1",i=~".+",j="foo"-4               22721336      22721224      -0.00%
BenchmarkPostingsForMatchers/Head/n="1",i=~"1.+",j="foo"-4              3623804       3623733       -0.00%
BenchmarkPostingsForMatchers/Head/n="1",i=~".+",i!="2",j="foo"-4        22721480      22721400      -0.00%
BenchmarkPostingsForMatchers/Head/n="1",i=~".+",i!~"2.*",j="foo"-4      24816652      24816444      -0.00%
BenchmarkPostingsForMatchers/Block/n="1"-4                              296           296           +0.00%
BenchmarkPostingsForMatchers/Block/n="1",j="foo"-4                      424           424           +0.00%
BenchmarkPostingsForMatchers/Block/j="foo",n="1"-4                      424           424           +0.00%
BenchmarkPostingsForMatchers/Block/n="1",j!="foo"-4                     1544          1464          -5.18%
BenchmarkPostingsForMatchers/Block/i=~".*"-4                            1606114       1606045       -0.00%
BenchmarkPostingsForMatchers/Block/i=~".+"-4                            17264709      17264629      -0.00%
BenchmarkPostingsForMatchers/Block/i=~""-4                              17264780      17264696      -0.00%
BenchmarkPostingsForMatchers/Block/i!=""-4                              17264680      17264600      -0.00%
BenchmarkPostingsForMatchers/Block/n="1",i=~".*",j="foo"-4              1606253       1606165       -0.01%
BenchmarkPostingsForMatchers/Block/n="1",i=~".*",i!="2",j="foo"-4       1606445       1606348       -0.01%
BenchmarkPostingsForMatchers/Block/n="1",i!=""-4                        17264808      17264728      -0.00%
BenchmarkPostingsForMatchers/Block/n="1",i!="",j="foo"-4                17264936      17264856      -0.00%
BenchmarkPostingsForMatchers/Block/n="1",i=~".+",j="foo"-4              17264965      17264885      -0.00%
BenchmarkPostingsForMatchers/Block/n="1",i=~"1.+",j="foo"-4             3148262       3148182       -0.00%
BenchmarkPostingsForMatchers/Block/n="1",i=~".+",i!="2",j="foo"-4       17265141      17265061      -0.00%
BenchmarkPostingsForMatchers/Block/n="1",i=~".+",i!~"2.*",j="foo"-4     20416944      20416784      -0.00%
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
											
										 
											2020-01-01 19:38:01 +08:00
										 |  |  | 		count := 0 | 
					
						
							|  |  |  | 		for postings.Next() { | 
					
						
							|  |  |  | 			count++ | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		if postings.Err() != nil { | 
					
						
							|  |  |  | 			return postings.Err() | 
					
						
							| 
									
										
										
										
											2018-12-29 01:06:12 +08:00
										 |  |  | 		} | 
					
						
							| 
									
										
											  
											
												Replace StringTuples with []string
Benchmarks show slight cpu/allocs improvements.
benchmark                                                               old ns/op      new ns/op      delta
BenchmarkPostingsForMatchers/Head/n="1"-4                               269978625      235305110      -12.84%
BenchmarkPostingsForMatchers/Head/n="1",j="foo"-4                       129739974      121646193      -6.24%
BenchmarkPostingsForMatchers/Head/j="foo",n="1"-4                       123826274      122056253      -1.43%
BenchmarkPostingsForMatchers/Head/n="1",j!="foo"-4                      126962188      130038235      +2.42%
BenchmarkPostingsForMatchers/Head/i=~".*"-4                             6423653989     5991126455     -6.73%
BenchmarkPostingsForMatchers/Head/i=~".+"-4                             6934647521     7033370634     +1.42%
BenchmarkPostingsForMatchers/Head/i=~""-4                               1177781285     1121497736     -4.78%
BenchmarkPostingsForMatchers/Head/i!=""-4                               7033680256     7246094991     +3.02%
BenchmarkPostingsForMatchers/Head/n="1",i=~".*",j="foo"-4               293702332      287440212      -2.13%
BenchmarkPostingsForMatchers/Head/n="1",i=~".*",i!="2",j="foo"-4        307628268      307039964      -0.19%
BenchmarkPostingsForMatchers/Head/n="1",i!=""-4                         512247746      480003862      -6.29%
BenchmarkPostingsForMatchers/Head/n="1",i!="",j="foo"-4                 361199794      367066917      +1.62%
BenchmarkPostingsForMatchers/Head/n="1",i=~".+",j="foo"-4               478863761      476037784      -0.59%
BenchmarkPostingsForMatchers/Head/n="1",i=~"1.+",j="foo"-4              103394659      102902098      -0.48%
BenchmarkPostingsForMatchers/Head/n="1",i=~".+",i!="2",j="foo"-4        482552781      475453903      -1.47%
BenchmarkPostingsForMatchers/Head/n="1",i=~".+",i!~"2.*",j="foo"-4      559257389      589297047      +5.37%
BenchmarkPostingsForMatchers/Block/n="1"-4                              36492          37012          +1.42%
BenchmarkPostingsForMatchers/Block/n="1",j="foo"-4                      557788         611903         +9.70%
BenchmarkPostingsForMatchers/Block/j="foo",n="1"-4                      554443         573814         +3.49%
BenchmarkPostingsForMatchers/Block/n="1",j!="foo"-4                     553227         553826         +0.11%
BenchmarkPostingsForMatchers/Block/i=~".*"-4                            113855090      111707221      -1.89%
BenchmarkPostingsForMatchers/Block/i=~".+"-4                            133994674      136520728      +1.89%
BenchmarkPostingsForMatchers/Block/i=~""-4                              38138091       36299898       -4.82%
BenchmarkPostingsForMatchers/Block/i!=""-4                              28861213       27396723       -5.07%
BenchmarkPostingsForMatchers/Block/n="1",i=~".*",j="foo"-4              112699941      110853868      -1.64%
BenchmarkPostingsForMatchers/Block/n="1",i=~".*",i!="2",j="foo"-4       113198026      111389742      -1.60%
BenchmarkPostingsForMatchers/Block/n="1",i!=""-4                        28994069       27363804       -5.62%
BenchmarkPostingsForMatchers/Block/n="1",i!="",j="foo"-4                29709406       28589223       -3.77%
BenchmarkPostingsForMatchers/Block/n="1",i=~".+",j="foo"-4              134695119      135736971      +0.77%
BenchmarkPostingsForMatchers/Block/n="1",i=~"1.+",j="foo"-4             26783286       25826928       -3.57%
BenchmarkPostingsForMatchers/Block/n="1",i=~".+",i!="2",j="foo"-4       134733254      134116739      -0.46%
BenchmarkPostingsForMatchers/Block/n="1",i=~".+",i!~"2.*",j="foo"-4     160713937      158802768      -1.19%
benchmark                                                               old allocs     new allocs     delta
BenchmarkPostingsForMatchers/Head/n="1"-4                               36             36             +0.00%
BenchmarkPostingsForMatchers/Head/n="1",j="foo"-4                       38             38             +0.00%
BenchmarkPostingsForMatchers/Head/j="foo",n="1"-4                       38             38             +0.00%
BenchmarkPostingsForMatchers/Head/n="1",j!="foo"-4                      42             40             -4.76%
BenchmarkPostingsForMatchers/Head/i=~".*"-4                             61             59             -3.28%
BenchmarkPostingsForMatchers/Head/i=~".+"-4                             100088         100087         -0.00%
BenchmarkPostingsForMatchers/Head/i=~""-4                               100053         100051         -0.00%
BenchmarkPostingsForMatchers/Head/i!=""-4                               100087         100085         -0.00%
BenchmarkPostingsForMatchers/Head/n="1",i=~".*",j="foo"-4               44             42             -4.55%
BenchmarkPostingsForMatchers/Head/n="1",i=~".*",i!="2",j="foo"-4        50             48             -4.00%
BenchmarkPostingsForMatchers/Head/n="1",i!=""-4                         100076         100074         -0.00%
BenchmarkPostingsForMatchers/Head/n="1",i!="",j="foo"-4                 100077         100075         -0.00%
BenchmarkPostingsForMatchers/Head/n="1",i=~".+",j="foo"-4               100077         100074         -0.00%
BenchmarkPostingsForMatchers/Head/n="1",i=~"1.+",j="foo"-4              11167          11165          -0.02%
BenchmarkPostingsForMatchers/Head/n="1",i=~".+",i!="2",j="foo"-4        100082         100080         -0.00%
BenchmarkPostingsForMatchers/Head/n="1",i=~".+",i!~"2.*",j="foo"-4      111265         111261         -0.00%
BenchmarkPostingsForMatchers/Block/n="1"-4                              6              6              +0.00%
BenchmarkPostingsForMatchers/Block/n="1",j="foo"-4                      11             11             +0.00%
BenchmarkPostingsForMatchers/Block/j="foo",n="1"-4                      11             11             +0.00%
BenchmarkPostingsForMatchers/Block/n="1",j!="foo"-4                     15             13             -13.33%
BenchmarkPostingsForMatchers/Block/i=~".*"-4                            12             10             -16.67%
BenchmarkPostingsForMatchers/Block/i=~".+"-4                            100040         100038         -0.00%
BenchmarkPostingsForMatchers/Block/i=~""-4                              100045         100043         -0.00%
BenchmarkPostingsForMatchers/Block/i!=""-4                              100041         100039         -0.00%
BenchmarkPostingsForMatchers/Block/n="1",i=~".*",j="foo"-4              17             15             -11.76%
BenchmarkPostingsForMatchers/Block/n="1",i=~".*",i!="2",j="foo"-4       23             21             -8.70%
BenchmarkPostingsForMatchers/Block/n="1",i!=""-4                        100046         100044         -0.00%
BenchmarkPostingsForMatchers/Block/n="1",i!="",j="foo"-4                100050         100048         -0.00%
BenchmarkPostingsForMatchers/Block/n="1",i=~".+",j="foo"-4              100049         100047         -0.00%
BenchmarkPostingsForMatchers/Block/n="1",i=~"1.+",j="foo"-4             11150          11148          -0.02%
BenchmarkPostingsForMatchers/Block/n="1",i=~".+",i!="2",j="foo"-4       100055         100053         -0.00%
BenchmarkPostingsForMatchers/Block/n="1",i=~".+",i!~"2.*",j="foo"-4     111238         111234         -0.00%
benchmark                                                               old bytes     new bytes     delta
BenchmarkPostingsForMatchers/Head/n="1"-4                               10887816      10887817      +0.00%
BenchmarkPostingsForMatchers/Head/n="1",j="foo"-4                       5456648       5456648       +0.00%
BenchmarkPostingsForMatchers/Head/j="foo",n="1"-4                       5456648       5456648       +0.00%
BenchmarkPostingsForMatchers/Head/n="1",j!="foo"-4                      5456792       5456712       -0.00%
BenchmarkPostingsForMatchers/Head/i=~".*"-4                             258254408     258254328     -0.00%
BenchmarkPostingsForMatchers/Head/i=~".+"-4                             273912888     273912904     +0.00%
BenchmarkPostingsForMatchers/Head/i=~""-4                               17266680      17266600      -0.00%
BenchmarkPostingsForMatchers/Head/i!=""-4                               273912416     273912336     -0.00%
BenchmarkPostingsForMatchers/Head/n="1",i=~".*",j="foo"-4               7062578       7062498       -0.00%
BenchmarkPostingsForMatchers/Head/n="1",i=~".*",i!="2",j="foo"-4        7062770       7062690       -0.00%
BenchmarkPostingsForMatchers/Head/n="1",i!=""-4                         28152346      28152266      -0.00%
BenchmarkPostingsForMatchers/Head/n="1",i!="",j="foo"-4                 22721178      22721098      -0.00%
BenchmarkPostingsForMatchers/Head/n="1",i=~".+",j="foo"-4               22721336      22721224      -0.00%
BenchmarkPostingsForMatchers/Head/n="1",i=~"1.+",j="foo"-4              3623804       3623733       -0.00%
BenchmarkPostingsForMatchers/Head/n="1",i=~".+",i!="2",j="foo"-4        22721480      22721400      -0.00%
BenchmarkPostingsForMatchers/Head/n="1",i=~".+",i!~"2.*",j="foo"-4      24816652      24816444      -0.00%
BenchmarkPostingsForMatchers/Block/n="1"-4                              296           296           +0.00%
BenchmarkPostingsForMatchers/Block/n="1",j="foo"-4                      424           424           +0.00%
BenchmarkPostingsForMatchers/Block/j="foo",n="1"-4                      424           424           +0.00%
BenchmarkPostingsForMatchers/Block/n="1",j!="foo"-4                     1544          1464          -5.18%
BenchmarkPostingsForMatchers/Block/i=~".*"-4                            1606114       1606045       -0.00%
BenchmarkPostingsForMatchers/Block/i=~".+"-4                            17264709      17264629      -0.00%
BenchmarkPostingsForMatchers/Block/i=~""-4                              17264780      17264696      -0.00%
BenchmarkPostingsForMatchers/Block/i!=""-4                              17264680      17264600      -0.00%
BenchmarkPostingsForMatchers/Block/n="1",i=~".*",j="foo"-4              1606253       1606165       -0.01%
BenchmarkPostingsForMatchers/Block/n="1",i=~".*",i!="2",j="foo"-4       1606445       1606348       -0.01%
BenchmarkPostingsForMatchers/Block/n="1",i!=""-4                        17264808      17264728      -0.00%
BenchmarkPostingsForMatchers/Block/n="1",i!="",j="foo"-4                17264936      17264856      -0.00%
BenchmarkPostingsForMatchers/Block/n="1",i=~".+",j="foo"-4              17264965      17264885      -0.00%
BenchmarkPostingsForMatchers/Block/n="1",i=~"1.+",j="foo"-4             3148262       3148182       -0.00%
BenchmarkPostingsForMatchers/Block/n="1",i=~".+",i!="2",j="foo"-4       17265141      17265061      -0.00%
BenchmarkPostingsForMatchers/Block/n="1",i=~".+",i!~"2.*",j="foo"-4     20416944      20416784      -0.00%
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
											
										 
											2020-01-01 19:38:01 +08:00
										 |  |  | 		postingInfos = append(postingInfos, postingInfo{n, uint64(count)}) | 
					
						
							| 
									
										
										
										
											2018-12-29 01:06:12 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	fmt.Printf("\nHighest cardinality metric names:\n") | 
					
						
							|  |  |  | 	printInfo(postingInfos) | 
					
						
							| 
									
										
										
										
											2021-07-02 18:08:52 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-08 16:49:00 +08:00
										 |  |  | 	if runExtended { | 
					
						
							| 
									
										
										
										
											2023-09-20 18:37:32 +08:00
										 |  |  | 		return analyzeCompaction(ctx, block, ir, selectors) | 
					
						
							| 
									
										
										
										
											2021-09-08 16:49:00 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return nil | 
					
						
							| 
									
										
										
										
											2021-07-02 18:08:52 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-09-20 18:37:32 +08:00
										 |  |  | func analyzeCompaction(ctx context.Context, block tsdb.BlockReader, indexr tsdb.IndexReader, matchers []*labels.Matcher) (err error) { | 
					
						
							|  |  |  | 	var postingsr index.Postings | 
					
						
							|  |  |  | 	if len(matchers) > 0 { | 
					
						
							| 
									
										
										
										
											2023-09-21 00:34:20 +08:00
										 |  |  | 		postingsr, err = tsdb.PostingsForMatchers(ctx, indexr, matchers...) | 
					
						
							| 
									
										
										
										
											2023-09-20 18:37:32 +08:00
										 |  |  | 	} else { | 
					
						
							|  |  |  | 		n, v := index.AllPostingsKey() | 
					
						
							|  |  |  | 		postingsr, err = indexr.Postings(ctx, n, v) | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2021-07-02 18:08:52 +08:00
										 |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		return err | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2023-09-20 18:37:32 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-07-02 18:08:52 +08:00
										 |  |  | 	chunkr, err := block.Chunks() | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		return err | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	defer func() { | 
					
						
							|  |  |  | 		err = tsdb_errors.NewMulti(err, chunkr.Close()).Err() | 
					
						
							|  |  |  | 	}() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	totalChunks := 0 | 
					
						
							| 
									
										
										
										
											2023-10-15 02:34:50 +08:00
										 |  |  | 	floatChunkSamplesCount := make([]int, 0) | 
					
						
							|  |  |  | 	floatChunkSize := make([]int, 0) | 
					
						
							|  |  |  | 	histogramChunkSamplesCount := make([]int, 0) | 
					
						
							|  |  |  | 	histogramChunkSize := make([]int, 0) | 
					
						
							|  |  |  | 	histogramChunkBucketsCount := make([]int, 0) | 
					
						
							| 
									
										
										
										
											2022-06-28 23:03:26 +08:00
										 |  |  | 	var builder labels.ScratchBuilder | 
					
						
							| 
									
										
										
										
											2021-07-02 18:08:52 +08:00
										 |  |  | 	for postingsr.Next() { | 
					
						
							|  |  |  | 		var chks []chunks.Meta | 
					
						
							| 
									
										
										
										
											2022-12-16 02:19:15 +08:00
										 |  |  | 		if err := indexr.Series(postingsr.At(), &builder, &chks); err != nil { | 
					
						
							| 
									
										
										
										
											2021-07-02 18:08:52 +08:00
										 |  |  | 			return err | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		for _, chk := range chks { | 
					
						
							|  |  |  | 			// Load the actual data of the chunk.
 | 
					
						
							| 
									
										
										
										
											2023-11-28 18:14:29 +08:00
										 |  |  | 			chk, iterable, err := chunkr.ChunkOrIterable(chk) | 
					
						
							| 
									
										
										
										
											2021-07-02 18:08:52 +08:00
										 |  |  | 			if err != nil { | 
					
						
							|  |  |  | 				return err | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2023-11-28 18:14:29 +08:00
										 |  |  | 			// Chunks within blocks should not need to be re-written, so an
 | 
					
						
							|  |  |  | 			// iterable is not expected to be returned from the chunk reader.
 | 
					
						
							|  |  |  | 			if iterable != nil { | 
					
						
							|  |  |  | 				return errors.New("ChunkOrIterable should not return an iterable when reading a block") | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2023-10-15 02:34:50 +08:00
										 |  |  | 			switch chk.Encoding() { | 
					
						
							|  |  |  | 			case chunkenc.EncXOR: | 
					
						
							|  |  |  | 				floatChunkSamplesCount = append(floatChunkSamplesCount, chk.NumSamples()) | 
					
						
							|  |  |  | 				floatChunkSize = append(floatChunkSize, len(chk.Bytes())) | 
					
						
							|  |  |  | 			case chunkenc.EncFloatHistogram: | 
					
						
							|  |  |  | 				histogramChunkSamplesCount = append(histogramChunkSamplesCount, chk.NumSamples()) | 
					
						
							|  |  |  | 				histogramChunkSize = append(histogramChunkSize, len(chk.Bytes())) | 
					
						
							|  |  |  | 				fhchk, ok := chk.(*chunkenc.FloatHistogramChunk) | 
					
						
							|  |  |  | 				if !ok { | 
					
						
							| 
									
										
										
										
											2024-11-03 20:15:51 +08:00
										 |  |  | 					return errors.New("chunk is not FloatHistogramChunk") | 
					
						
							| 
									
										
										
										
											2023-10-15 02:34:50 +08:00
										 |  |  | 				} | 
					
						
							|  |  |  | 				it := fhchk.Iterator(nil) | 
					
						
							|  |  |  | 				bucketCount := 0 | 
					
						
							|  |  |  | 				for it.Next() == chunkenc.ValFloatHistogram { | 
					
						
							| 
									
										
										
										
											2024-01-24 00:02:14 +08:00
										 |  |  | 					_, f := it.AtFloatHistogram(nil) | 
					
						
							| 
									
										
										
										
											2023-10-15 02:34:50 +08:00
										 |  |  | 					bucketCount += len(f.PositiveBuckets) | 
					
						
							|  |  |  | 					bucketCount += len(f.NegativeBuckets) | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 				histogramChunkBucketsCount = append(histogramChunkBucketsCount, bucketCount) | 
					
						
							|  |  |  | 			case chunkenc.EncHistogram: | 
					
						
							|  |  |  | 				histogramChunkSamplesCount = append(histogramChunkSamplesCount, chk.NumSamples()) | 
					
						
							|  |  |  | 				histogramChunkSize = append(histogramChunkSize, len(chk.Bytes())) | 
					
						
							|  |  |  | 				hchk, ok := chk.(*chunkenc.HistogramChunk) | 
					
						
							|  |  |  | 				if !ok { | 
					
						
							| 
									
										
										
										
											2024-11-03 20:15:51 +08:00
										 |  |  | 					return errors.New("chunk is not HistogramChunk") | 
					
						
							| 
									
										
										
										
											2023-10-15 02:34:50 +08:00
										 |  |  | 				} | 
					
						
							|  |  |  | 				it := hchk.Iterator(nil) | 
					
						
							|  |  |  | 				bucketCount := 0 | 
					
						
							|  |  |  | 				for it.Next() == chunkenc.ValHistogram { | 
					
						
							| 
									
										
										
										
											2024-01-24 00:02:14 +08:00
										 |  |  | 					_, f := it.AtHistogram(nil) | 
					
						
							| 
									
										
										
										
											2023-10-15 02:34:50 +08:00
										 |  |  | 					bucketCount += len(f.PositiveBuckets) | 
					
						
							|  |  |  | 					bucketCount += len(f.NegativeBuckets) | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 				histogramChunkBucketsCount = append(histogramChunkBucketsCount, bucketCount) | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2021-07-02 18:08:52 +08:00
										 |  |  | 			totalChunks++ | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	fmt.Printf("\nCompaction analysis:\n") | 
					
						
							| 
									
										
										
										
											2023-10-15 02:34:50 +08:00
										 |  |  | 	fmt.Println() | 
					
						
							|  |  |  | 	displayHistogram("samples per float chunk", floatChunkSamplesCount, totalChunks) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	displayHistogram("bytes per float chunk", floatChunkSize, totalChunks) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	displayHistogram("samples per histogram chunk", histogramChunkSamplesCount, totalChunks) | 
					
						
							| 
									
										
										
										
											2021-07-02 18:08:52 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-10-15 02:34:50 +08:00
										 |  |  | 	displayHistogram("bytes per histogram chunk", histogramChunkSize, totalChunks) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	displayHistogram("buckets per histogram chunk", histogramChunkBucketsCount, totalChunks) | 
					
						
							| 
									
										
										
										
											2019-07-23 16:04:48 +08:00
										 |  |  | 	return nil | 
					
						
							| 
									
										
										
										
											2018-12-29 01:06:12 +08:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2019-02-25 21:51:33 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-25 06:22:32 +08:00
										 |  |  | type SeriesSetFormatter func(series storage.SeriesSet) error | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-11-30 00:49:01 +08:00
										 |  |  | func dumpSamples(ctx context.Context, dbDir, sandboxDirRoot string, mint, maxt int64, match []string, formatter SeriesSetFormatter) (err error) { | 
					
						
							|  |  |  | 	db, err := tsdb.OpenDBReadOnly(dbDir, sandboxDirRoot, nil) | 
					
						
							| 
									
										
										
										
											2019-02-25 21:51:33 +08:00
										 |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2019-07-23 16:04:48 +08:00
										 |  |  | 		return err | 
					
						
							| 
									
										
										
										
											2019-02-25 21:51:33 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2019-07-23 16:04:48 +08:00
										 |  |  | 	defer func() { | 
					
						
							| 
									
										
										
										
											2020-10-28 23:24:58 +08:00
										 |  |  | 		err = tsdb_errors.NewMulti(err, db.Close()).Err() | 
					
						
							| 
									
										
										
										
											2019-07-23 16:04:48 +08:00
										 |  |  | 	}() | 
					
						
							| 
									
										
										
										
											2023-09-12 18:37:38 +08:00
										 |  |  | 	q, err := db.Querier(mint, maxt) | 
					
						
							| 
									
										
										
										
											2020-07-24 02:35:50 +08:00
										 |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		return err | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	defer q.Close() | 
					
						
							| 
									
										
										
										
											2019-02-25 21:51:33 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-15 18:29:53 +08:00
										 |  |  | 	matcherSets, err := parser.ParseMetricSelectors(match) | 
					
						
							| 
									
										
										
										
											2023-01-20 04:03:53 +08:00
										 |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		return err | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2024-01-15 18:29:53 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	var ss storage.SeriesSet | 
					
						
							|  |  |  | 	if len(matcherSets) > 1 { | 
					
						
							|  |  |  | 		var sets []storage.SeriesSet | 
					
						
							|  |  |  | 		for _, mset := range matcherSets { | 
					
						
							|  |  |  | 			sets = append(sets, q.Select(ctx, true, nil, mset...)) | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2024-11-08 00:52:55 +08:00
										 |  |  | 		ss = storage.NewMergeSeriesSet(sets, 0, storage.ChainedSeriesMerge) | 
					
						
							| 
									
										
										
										
											2024-01-15 18:29:53 +08:00
										 |  |  | 	} else { | 
					
						
							|  |  |  | 		ss = q.Select(ctx, false, nil, matcherSets[0]...) | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2020-02-06 23:58:38 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-25 06:22:32 +08:00
										 |  |  | 	err = formatter(ss) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		return err | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if ws := ss.Warnings(); len(ws) > 0 { | 
					
						
							|  |  |  | 		return tsdb_errors.NewMulti(ws.AsErrors()...).Err() | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if ss.Err() != nil { | 
					
						
							|  |  |  | 		return ss.Err() | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return nil | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func formatSeriesSet(ss storage.SeriesSet) error { | 
					
						
							| 
									
										
										
										
											2019-02-25 21:51:33 +08:00
										 |  |  | 	for ss.Next() { | 
					
						
							|  |  |  | 		series := ss.At() | 
					
						
							| 
									
										
										
										
											2020-07-24 02:35:50 +08:00
										 |  |  | 		lbs := series.Labels() | 
					
						
							| 
									
										
										
										
											2022-09-21 01:16:45 +08:00
										 |  |  | 		it := series.Iterator(nil) | 
					
						
							| 
									
										
										
										
											2021-11-29 15:54:23 +08:00
										 |  |  | 		for it.Next() == chunkenc.ValFloat { | 
					
						
							| 
									
										
										
										
											2019-02-25 21:51:33 +08:00
										 |  |  | 			ts, val := it.At() | 
					
						
							| 
									
										
										
										
											2020-07-24 02:35:50 +08:00
										 |  |  | 			fmt.Printf("%s %g %d\n", lbs, val, ts) | 
					
						
							| 
									
										
										
										
											2019-02-25 21:51:33 +08:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2023-09-01 11:21:52 +08:00
										 |  |  | 		for it.Next() == chunkenc.ValFloatHistogram { | 
					
						
							| 
									
										
										
										
											2024-01-24 00:02:14 +08:00
										 |  |  | 			ts, fh := it.AtFloatHistogram(nil) | 
					
						
							| 
									
										
										
										
											2023-09-01 11:21:52 +08:00
										 |  |  | 			fmt.Printf("%s %s %d\n", lbs, fh.String(), ts) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		for it.Next() == chunkenc.ValHistogram { | 
					
						
							| 
									
										
										
										
											2024-01-24 00:02:14 +08:00
										 |  |  | 			ts, h := it.AtHistogram(nil) | 
					
						
							| 
									
										
										
										
											2023-09-01 11:21:52 +08:00
										 |  |  | 			fmt.Printf("%s %s %d\n", lbs, h.String(), ts) | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2019-02-25 21:51:33 +08:00
										 |  |  | 		if it.Err() != nil { | 
					
						
							| 
									
										
										
										
											2019-07-23 16:04:48 +08:00
										 |  |  | 			return ss.Err() | 
					
						
							| 
									
										
										
										
											2019-02-25 21:51:33 +08:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2024-01-25 06:22:32 +08:00
										 |  |  | 	return nil | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2019-02-25 21:51:33 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-25 06:22:32 +08:00
										 |  |  | // CondensedString is labels.Labels.String() without spaces after the commas.
 | 
					
						
							|  |  |  | func CondensedString(ls labels.Labels) string { | 
					
						
							|  |  |  | 	var b bytes.Buffer | 
					
						
							| 
									
										
										
										
											2020-06-10 00:57:31 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-25 06:22:32 +08:00
										 |  |  | 	b.WriteByte('{') | 
					
						
							|  |  |  | 	i := 0 | 
					
						
							|  |  |  | 	ls.Range(func(l labels.Label) { | 
					
						
							|  |  |  | 		if i > 0 { | 
					
						
							|  |  |  | 			b.WriteByte(',') | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		b.WriteString(l.Name) | 
					
						
							|  |  |  | 		b.WriteByte('=') | 
					
						
							|  |  |  | 		b.WriteString(strconv.Quote(l.Value)) | 
					
						
							|  |  |  | 		i++ | 
					
						
							|  |  |  | 	}) | 
					
						
							|  |  |  | 	b.WriteByte('}') | 
					
						
							|  |  |  | 	return b.String() | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func formatSeriesSetOpenMetrics(ss storage.SeriesSet) error { | 
					
						
							|  |  |  | 	for ss.Next() { | 
					
						
							|  |  |  | 		series := ss.At() | 
					
						
							|  |  |  | 		lbs := series.Labels() | 
					
						
							|  |  |  | 		metricName := lbs.Get(labels.MetricName) | 
					
						
							|  |  |  | 		lbs = lbs.DropMetricName() | 
					
						
							|  |  |  | 		it := series.Iterator(nil) | 
					
						
							|  |  |  | 		for it.Next() == chunkenc.ValFloat { | 
					
						
							|  |  |  | 			ts, val := it.At() | 
					
						
							|  |  |  | 			fmt.Printf("%s%s %g %.3f\n", metricName, CondensedString(lbs), val, float64(ts)/1000) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		if it.Err() != nil { | 
					
						
							|  |  |  | 			return ss.Err() | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2019-02-25 21:51:33 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2024-01-25 06:22:32 +08:00
										 |  |  | 	fmt.Println("# EOF") | 
					
						
							| 
									
										
										
										
											2019-07-23 16:04:48 +08:00
										 |  |  | 	return nil | 
					
						
							| 
									
										
										
										
											2019-02-25 21:51:33 +08:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2020-07-24 02:35:50 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | func checkErr(err error) int { | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		fmt.Fprintln(os.Stderr, err) | 
					
						
							|  |  |  | 		return 1 | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return 0 | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2020-11-26 13:07:06 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-08-28 10:12:24 +08:00
										 |  |  | func backfillOpenMetrics(path, outputDir string, humanReadable, quiet bool, maxBlockDuration time.Duration, customLabels map[string]string) int { | 
					
						
							| 
									
										
										
										
											2025-02-13 10:03:31 +08:00
										 |  |  | 	var buf []byte | 
					
						
							|  |  |  | 	info, err := os.Stat(path) | 
					
						
							| 
									
										
										
										
											2020-11-26 13:07:06 +08:00
										 |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2021-02-02 04:18:42 +08:00
										 |  |  | 		return checkErr(err) | 
					
						
							| 
									
										
										
										
											2020-11-26 13:07:06 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2025-02-13 10:03:31 +08:00
										 |  |  | 	if info.Mode()&(os.ModeNamedPipe|os.ModeCharDevice) != 0 { | 
					
						
							|  |  |  | 		// Read the pipe chunks by chunks as it cannot be mmap-ed
 | 
					
						
							|  |  |  | 		buf, err = os.ReadFile(path) | 
					
						
							|  |  |  | 		if err != nil { | 
					
						
							|  |  |  | 			return checkErr(err) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} else { | 
					
						
							|  |  |  | 		inputFile, err := fileutil.OpenMmapFile(path) | 
					
						
							|  |  |  | 		if err != nil { | 
					
						
							|  |  |  | 			return checkErr(err) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		defer inputFile.Close() | 
					
						
							|  |  |  | 		buf = inputFile.Bytes() | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2020-12-25 12:36:36 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-22 16:06:44 +08:00
										 |  |  | 	if err := os.MkdirAll(outputDir, 0o777); err != nil { | 
					
						
							| 
									
										
										
										
											2022-05-24 14:58:59 +08:00
										 |  |  | 		return checkErr(fmt.Errorf("create output dir: %w", err)) | 
					
						
							| 
									
										
										
										
											2020-12-25 12:36:36 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2020-12-28 23:18:00 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-02-13 10:03:31 +08:00
										 |  |  | 	return checkErr(backfill(5000, buf, outputDir, humanReadable, quiet, maxBlockDuration, customLabels)) | 
					
						
							| 
									
										
										
										
											2020-11-26 13:07:06 +08:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2023-10-15 02:34:50 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | func displayHistogram(dataType string, datas []int, total int) { | 
					
						
							| 
									
										
										
										
											2024-04-24 09:35:34 +08:00
										 |  |  | 	if len(datas) == 0 { | 
					
						
							|  |  |  | 		fmt.Printf("%s: N/A\n\n", dataType) | 
					
						
							|  |  |  | 		return | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2023-10-15 02:34:50 +08:00
										 |  |  | 	slices.Sort(datas) | 
					
						
							|  |  |  | 	start, end, step := generateBucket(datas[0], datas[len(datas)-1]) | 
					
						
							|  |  |  | 	sum := 0 | 
					
						
							|  |  |  | 	buckets := make([]int, (end-start)/step+1) | 
					
						
							|  |  |  | 	maxCount := 0 | 
					
						
							|  |  |  | 	for _, c := range datas { | 
					
						
							|  |  |  | 		sum += c | 
					
						
							|  |  |  | 		buckets[(c-start)/step]++ | 
					
						
							|  |  |  | 		if buckets[(c-start)/step] > maxCount { | 
					
						
							|  |  |  | 			maxCount = buckets[(c-start)/step] | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	avg := sum / len(datas) | 
					
						
							|  |  |  | 	fmt.Printf("%s (min/avg/max): %d/%d/%d\n", dataType, datas[0], avg, datas[len(datas)-1]) | 
					
						
							| 
									
										
										
										
											2024-05-13 23:36:19 +08:00
										 |  |  | 	maxLeftLen := strconv.Itoa(len(strconv.Itoa(end))) | 
					
						
							|  |  |  | 	maxRightLen := strconv.Itoa(len(strconv.Itoa(end + step))) | 
					
						
							|  |  |  | 	maxCountLen := strconv.Itoa(len(strconv.Itoa(maxCount))) | 
					
						
							| 
									
										
										
										
											2023-10-15 02:34:50 +08:00
										 |  |  | 	for bucket, count := range buckets { | 
					
						
							|  |  |  | 		percentage := 100.0 * count / total | 
					
						
							|  |  |  | 		fmt.Printf("[%"+maxLeftLen+"d, %"+maxRightLen+"d]: %"+maxCountLen+"d %s\n", bucket*step+start+1, (bucket+1)*step+start, count, strings.Repeat("#", percentage)) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	fmt.Println() | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-08-18 17:27:04 +08:00
										 |  |  | func generateBucket(minVal, maxVal int) (start, end, step int) { | 
					
						
							|  |  |  | 	s := (maxVal - minVal) / 10 | 
					
						
							| 
									
										
										
										
											2023-10-15 02:34:50 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	step = 10 | 
					
						
							|  |  |  | 	for step < s && step <= 10000 { | 
					
						
							|  |  |  | 		step *= 10 | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-08-18 17:27:04 +08:00
										 |  |  | 	start = minVal - minVal%step | 
					
						
							|  |  |  | 	end = maxVal - maxVal%step + step | 
					
						
							| 
									
										
										
										
											2023-10-15 02:34:50 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	return | 
					
						
							|  |  |  | } |