richardstartin commented on a change in pull request #7916: URL: https://github.com/apache/pinot/pull/7916#discussion_r770692340
########## File path: pinot-perf/src/main/java/org/apache/pinot/perf/BenchmarkFilteredAggregations.java ########## @@ -0,0 +1,192 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.pinot.perf; + +import java.io.File; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashSet; +import java.util.List; +import java.util.Set; +import java.util.concurrent.TimeUnit; +import org.apache.commons.io.FileUtils; +import org.apache.pinot.queries.BaseQueriesTest; +import org.apache.pinot.segment.local.indexsegment.immutable.ImmutableSegmentLoader; +import org.apache.pinot.segment.local.segment.creator.impl.SegmentIndexCreationDriverImpl; +import org.apache.pinot.segment.local.segment.index.loader.IndexLoadingConfig; +import org.apache.pinot.segment.local.segment.readers.GenericRowRecordReader; +import org.apache.pinot.segment.spi.ImmutableSegment; +import org.apache.pinot.segment.spi.IndexSegment; +import org.apache.pinot.segment.spi.creator.SegmentGeneratorConfig; +import org.apache.pinot.spi.config.table.FieldConfig; +import org.apache.pinot.spi.config.table.TableConfig; +import org.apache.pinot.spi.config.table.TableType; +import org.apache.pinot.spi.data.FieldSpec; +import org.apache.pinot.spi.data.Schema; +import org.apache.pinot.spi.data.readers.GenericRow; +import org.apache.pinot.spi.data.readers.RecordReader; +import org.apache.pinot.spi.utils.builder.TableConfigBuilder; +import org.openjdk.jmh.annotations.Benchmark; +import org.openjdk.jmh.annotations.BenchmarkMode; +import org.openjdk.jmh.annotations.Fork; +import org.openjdk.jmh.annotations.Measurement; +import org.openjdk.jmh.annotations.Mode; +import org.openjdk.jmh.annotations.OutputTimeUnit; +import org.openjdk.jmh.annotations.Scope; +import org.openjdk.jmh.annotations.Setup; +import org.openjdk.jmh.annotations.State; +import org.openjdk.jmh.annotations.TearDown; +import org.openjdk.jmh.annotations.Warmup; +import org.openjdk.jmh.infra.Blackhole; +import org.openjdk.jmh.runner.Runner; +import org.openjdk.jmh.runner.options.OptionsBuilder; + +@BenchmarkMode(Mode.AverageTime) +@OutputTimeUnit(TimeUnit.MILLISECONDS) +@Fork(1) +@Warmup(iterations = 3, time = 10) +@Measurement(iterations = 5, time = 10) +@State(Scope.Benchmark) +public class BenchmarkFilteredAggregations extends BaseQueriesTest { + + private static final File INDEX_DIR = new File(FileUtils.getTempDirectory(), "FilteredAggregationsTest"); + private static final String TABLE_NAME = "MyTable"; + private static final String FIRST_SEGMENT_NAME = "firstTestSegment"; + private static final String SECOND_SEGMENT_NAME = "secondTestSegment"; + private static final String INT_COL_NAME = "INT_COL"; + private static final String NO_INDEX_INT_COL_NAME = "NO_INDEX_INT_COL"; + private static final Integer INT_BASE_VALUE = 0; + private static final Integer NUM_ROWS = 1500000; + + private IndexSegment _indexSegment; + private List<IndexSegment> _indexSegments; + + public String _filteredQuery = "SELECT SUM(INT_COL) FILTER(WHERE INT_COL > 123 AND INT_COL < 599999)," + + "MAX(INT_COL) FILTER(WHERE INT_COL > 123 AND INT_COL < 599999) " + + "FROM MyTable WHERE NO_INDEX_INT_COL > 5 AND NO_INDEX_INT_COL < 1499999"; + + public String _nonFilteredQuery = "SELECT SUM(" + + "CASE " + + "WHEN (INT_COL > 123 AND INT_COL < 599999) THEN INT_COL " + + "ELSE 0 " + + "END) AS total_sum," + + "MAX(" + + "CASE " + + "WHEN (INT_COL > 123 AND INT_COL < 599999) THEN INT_COL " + + "ELSE 0 " + + "END) AS total_avg " + + "FROM MyTable WHERE NO_INDEX_INT_COL > 5 AND NO_INDEX_INT_COL < 1499999"; + + @Setup + public void setUp() + throws Exception { + FileUtils.deleteQuietly(INDEX_DIR); + + buildSegment(FIRST_SEGMENT_NAME); + buildSegment(SECOND_SEGMENT_NAME); + IndexLoadingConfig indexLoadingConfig = new IndexLoadingConfig(); + + Set<String> invertedIndexCols = new HashSet<>(); + invertedIndexCols.add(INT_COL_NAME); + + indexLoadingConfig.setRangeIndexColumns(invertedIndexCols); + indexLoadingConfig.setInvertedIndexColumns(invertedIndexCols); + + ImmutableSegment firstImmutableSegment = + ImmutableSegmentLoader.load(new File(INDEX_DIR, FIRST_SEGMENT_NAME), indexLoadingConfig); + ImmutableSegment secondImmutableSegment = + ImmutableSegmentLoader.load(new File(INDEX_DIR, SECOND_SEGMENT_NAME), indexLoadingConfig); + _indexSegment = firstImmutableSegment; + _indexSegments = Arrays.asList(firstImmutableSegment, secondImmutableSegment); + } + + @TearDown + public void tearDown() { + for (IndexSegment indexSegment : _indexSegments) { + indexSegment.destroy(); + } + + FileUtils.deleteQuietly(INDEX_DIR); + } + + private List<GenericRow> createTestData(int numRows) { + List<GenericRow> rows = new ArrayList<>(); + + for (int i = 0; i < numRows; i++) { + GenericRow row = new GenericRow(); + row.putField(INT_COL_NAME, INT_BASE_VALUE + i); + row.putField(NO_INDEX_INT_COL_NAME, INT_BASE_VALUE + i); + + rows.add(row); + } + return rows; + } + + private void buildSegment(String segmentName) + throws Exception { + List<GenericRow> rows = createTestData(NUM_ROWS); + List<FieldConfig> fieldConfigs = new ArrayList<>(); + + TableConfig tableConfig = new TableConfigBuilder(TableType.OFFLINE).setTableName(TABLE_NAME) + .setInvertedIndexColumns(Arrays.asList(INT_COL_NAME)).setFieldConfigList(fieldConfigs).build(); + Schema schema = new Schema.SchemaBuilder().setSchemaName(TABLE_NAME) + .addSingleValueDimension(NO_INDEX_INT_COL_NAME, FieldSpec.DataType.INT) + .addSingleValueDimension(INT_COL_NAME, FieldSpec.DataType.INT).build(); + SegmentGeneratorConfig config = new SegmentGeneratorConfig(tableConfig, schema); + config.setOutDir(INDEX_DIR.getPath()); + config.setTableName(TABLE_NAME); + config.setSegmentName(segmentName); + + SegmentIndexCreationDriverImpl driver = new SegmentIndexCreationDriverImpl(); + try (RecordReader recordReader = new GenericRowRecordReader(rows)) { + driver.init(config, recordReader); + driver.build(); + } + } + + @Benchmark + public void testFilteredAggregations(Blackhole blackhole) { + blackhole.consume(getBrokerResponseForSqlQuery(_filteredQuery)); + } + + @Benchmark + public void testNonFilteredAggregations(Blackhole blackhole) { + blackhole.consume(getBrokerResponseForSqlQuery(_nonFilteredQuery)); + } Review comment: Just FYI, it's ok and encouraged to just return the value here, no need to blackhole the value yourself. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: commits-unsubscr...@pinot.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: commits-unsubscr...@pinot.apache.org For additional commands, e-mail: commits-h...@pinot.apache.org