|
| 1 | +/* |
| 2 | + * Copyright OpenSearch Contributors |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +package org.opensearch.sql.api; |
| 7 | + |
| 8 | +import java.sql.PreparedStatement; |
| 9 | +import java.util.concurrent.TimeUnit; |
| 10 | +import org.apache.calcite.rel.RelNode; |
| 11 | +import org.apache.calcite.sql.dialect.SparkSqlDialect; |
| 12 | +import org.openjdk.jmh.annotations.Benchmark; |
| 13 | +import org.openjdk.jmh.annotations.BenchmarkMode; |
| 14 | +import org.openjdk.jmh.annotations.Fork; |
| 15 | +import org.openjdk.jmh.annotations.Level; |
| 16 | +import org.openjdk.jmh.annotations.Measurement; |
| 17 | +import org.openjdk.jmh.annotations.Mode; |
| 18 | +import org.openjdk.jmh.annotations.OutputTimeUnit; |
| 19 | +import org.openjdk.jmh.annotations.Param; |
| 20 | +import org.openjdk.jmh.annotations.Scope; |
| 21 | +import org.openjdk.jmh.annotations.Setup; |
| 22 | +import org.openjdk.jmh.annotations.State; |
| 23 | +import org.openjdk.jmh.annotations.TearDown; |
| 24 | +import org.openjdk.jmh.annotations.Warmup; |
| 25 | +import org.opensearch.sql.api.compiler.UnifiedQueryCompiler; |
| 26 | +import org.opensearch.sql.api.transpiler.UnifiedQueryTranspiler; |
| 27 | +import org.opensearch.sql.executor.QueryType; |
| 28 | + |
| 29 | +/** |
| 30 | + * JMH benchmark for measuring the overhead of the Calcite native SQL planning path. Provides |
| 31 | + * baseline metrics comparable to the PPL benchmarks in {@link UnifiedQueryBenchmark}. |
| 32 | + */ |
| 33 | +@Warmup(iterations = 2, time = 1) |
| 34 | +@Measurement(iterations = 5, time = 1) |
| 35 | +@BenchmarkMode(Mode.AverageTime) |
| 36 | +@OutputTimeUnit(TimeUnit.MILLISECONDS) |
| 37 | +@State(Scope.Thread) |
| 38 | +@Fork(value = 1) |
| 39 | +public class UnifiedSqlQueryBenchmark extends UnifiedQueryTestBase { |
| 40 | + |
| 41 | + @Param({ |
| 42 | + "SELECT * FROM catalog.employees", |
| 43 | + "SELECT * FROM catalog.employees WHERE age > 30", |
| 44 | + "SELECT department, count(*) FROM catalog.employees GROUP BY department", |
| 45 | + "SELECT * FROM catalog.employees ORDER BY age DESC", |
| 46 | + "SELECT * FROM catalog.employees WHERE age > 25" |
| 47 | + + " GROUP BY department ORDER BY department" |
| 48 | + }) |
| 49 | + private String query; |
| 50 | + |
| 51 | + private UnifiedQueryTranspiler transpiler; |
| 52 | + private UnifiedQueryCompiler compiler; |
| 53 | + |
| 54 | + @Override |
| 55 | + protected QueryType queryType() { |
| 56 | + return QueryType.SQL; |
| 57 | + } |
| 58 | + |
| 59 | + @Setup(Level.Trial) |
| 60 | + public void setUpBenchmark() { |
| 61 | + super.setUp(); |
| 62 | + transpiler = UnifiedQueryTranspiler.builder().dialect(SparkSqlDialect.DEFAULT).build(); |
| 63 | + compiler = new UnifiedQueryCompiler(context); |
| 64 | + } |
| 65 | + |
| 66 | + @TearDown(Level.Trial) |
| 67 | + public void tearDownBenchmark() throws Exception { |
| 68 | + super.tearDown(); |
| 69 | + } |
| 70 | + |
| 71 | + @Benchmark |
| 72 | + public RelNode planQuery() { |
| 73 | + return planner.plan(query); |
| 74 | + } |
| 75 | + |
| 76 | + @Benchmark |
| 77 | + public String transpileToSql() { |
| 78 | + RelNode plan = planner.plan(query); |
| 79 | + return transpiler.toSql(plan); |
| 80 | + } |
| 81 | + |
| 82 | + @Benchmark |
| 83 | + public void compileQuery() throws Exception { |
| 84 | + RelNode plan = planner.plan(query); |
| 85 | + try (PreparedStatement stmt = compiler.compile(plan)) { |
| 86 | + // Statement is auto-closed after benchmark iteration |
| 87 | + } |
| 88 | + } |
| 89 | +} |
0 commit comments