|
| 1 | +/* |
| 2 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 | + * you may not use this file except in compliance with the License. |
| 4 | + * You may obtain a copy of the License at |
| 5 | + * |
| 6 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | + * |
| 8 | + * Unless required by applicable law or agreed to in writing, software |
| 9 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | + * See the License for the specific language governing permissions and |
| 12 | + * limitations under the License. |
| 13 | + */ |
| 14 | +package com.facebook.presto.sql.planner.iterative.rule; |
| 15 | + |
| 16 | +import com.facebook.presto.Session; |
| 17 | +import com.facebook.presto.testing.LocalQueryRunner; |
| 18 | +import com.facebook.presto.testing.MaterializedResult; |
| 19 | +import com.facebook.presto.testing.QueryRunner; |
| 20 | +import com.facebook.presto.tpch.TpchConnectorFactory; |
| 21 | +import com.google.common.collect.ImmutableMap; |
| 22 | +import org.openjdk.jmh.annotations.Benchmark; |
| 23 | +import org.openjdk.jmh.annotations.BenchmarkMode; |
| 24 | +import org.openjdk.jmh.annotations.Fork; |
| 25 | +import org.openjdk.jmh.annotations.Measurement; |
| 26 | +import org.openjdk.jmh.annotations.OutputTimeUnit; |
| 27 | +import org.openjdk.jmh.annotations.Param; |
| 28 | +import org.openjdk.jmh.annotations.Setup; |
| 29 | +import org.openjdk.jmh.annotations.State; |
| 30 | +import org.openjdk.jmh.annotations.TearDown; |
| 31 | +import org.openjdk.jmh.annotations.Warmup; |
| 32 | +import org.openjdk.jmh.runner.Runner; |
| 33 | +import org.openjdk.jmh.runner.RunnerException; |
| 34 | +import org.openjdk.jmh.runner.options.Options; |
| 35 | +import org.openjdk.jmh.runner.options.OptionsBuilder; |
| 36 | +import org.openjdk.jmh.runner.options.VerboseMode; |
| 37 | + |
| 38 | +import static com.facebook.presto.testing.TestingSession.testSessionBuilder; |
| 39 | +import static com.google.common.base.Preconditions.checkState; |
| 40 | +import static java.lang.String.format; |
| 41 | +import static java.util.concurrent.TimeUnit.MILLISECONDS; |
| 42 | +import static org.openjdk.jmh.annotations.Mode.AverageTime; |
| 43 | +import static org.openjdk.jmh.annotations.Scope.Thread; |
| 44 | + |
| 45 | +@State(Thread) |
| 46 | +@OutputTimeUnit(MILLISECONDS) |
| 47 | +@BenchmarkMode(AverageTime) |
| 48 | +@Fork(3) |
| 49 | +@Warmup(iterations = 10) |
| 50 | +@Measurement(iterations = 10) |
| 51 | +public class BenchmarkReorderJoinsConnectedGraph |
| 52 | +{ |
| 53 | + @Benchmark |
| 54 | + public MaterializedResult benchmarkReorderJoins(BenchmarkInfo benchmarkInfo) |
| 55 | + { |
| 56 | + return benchmarkInfo.getQueryRunner().execute(benchmarkInfo.getQuery()); |
| 57 | + } |
| 58 | + |
| 59 | + @State(Thread) |
| 60 | + public static class BenchmarkInfo |
| 61 | + { |
| 62 | + @Param({"ELIMINATE_CROSS_JOINS", "COST_BASED"}) |
| 63 | + private String joinReorderingStrategy; |
| 64 | + |
| 65 | + @Param({"2", "4", "6", "8", "10"}) |
| 66 | + private int numberOfTables; |
| 67 | + |
| 68 | + private String query; |
| 69 | + private LocalQueryRunner queryRunner; |
| 70 | + |
| 71 | + @Setup |
| 72 | + public void setup() |
| 73 | + { |
| 74 | + checkState(numberOfTables >= 2, "numberOfTables must be >= 2"); |
| 75 | + Session session = testSessionBuilder() |
| 76 | + .setSystemProperty("join_reordering_strategy", joinReorderingStrategy) |
| 77 | + .setSystemProperty("join_distribution_type", "AUTOMATIC") |
| 78 | + .setCatalog("tpch") |
| 79 | + .setSchema("tiny") |
| 80 | + .build(); |
| 81 | + queryRunner = new LocalQueryRunner(session); |
| 82 | + queryRunner.createCatalog("tpch", new TpchConnectorFactory(1), ImmutableMap.of()); |
| 83 | + StringBuilder stringBuilder = new StringBuilder(); |
| 84 | + stringBuilder.append("EXPLAIN SELECT * FROM NATION n1"); |
| 85 | + for (int i = 2; i <= numberOfTables; i++) { |
| 86 | + stringBuilder.append(format(" JOIN nation n%s on n%s.nationkey = n%s.nationkey", i, i - 1, i)); |
| 87 | + } |
| 88 | + query = stringBuilder.toString(); |
| 89 | + } |
| 90 | + |
| 91 | + public String getQuery() |
| 92 | + { |
| 93 | + return query; |
| 94 | + } |
| 95 | + |
| 96 | + public QueryRunner getQueryRunner() |
| 97 | + { |
| 98 | + return queryRunner; |
| 99 | + } |
| 100 | + |
| 101 | + @TearDown |
| 102 | + public void tearDown() |
| 103 | + { |
| 104 | + queryRunner.close(); |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + public static void main(String[] args) |
| 109 | + throws RunnerException |
| 110 | + { |
| 111 | + Options options = new OptionsBuilder() |
| 112 | + .verbosity(VerboseMode.NORMAL) |
| 113 | + .include(".*" + BenchmarkReorderJoinsConnectedGraph.class.getSimpleName() + ".*") |
| 114 | + .build(); |
| 115 | + |
| 116 | + new Runner(options).run(); |
| 117 | + } |
| 118 | +} |
0 commit comments