Skip to content

Commit

Permalink
[CALCITE-6466] Add benchmark for SQL parser instantiation
Browse files Browse the repository at this point in the history
  • Loading branch information
cyrilou242 authored and zabetak committed Jul 15, 2024
1 parent 29c413a commit 95fedab
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 1 deletion.
2 changes: 1 addition & 1 deletion ubenchmark/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ following task:

```kotlin
jmh {
include = listOf("removeAllVertices.*Benchmark")
includes = listOf("removeAllVertices.*Benchmark")
}
```

Expand Down
1 change: 1 addition & 0 deletions ubenchmark/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ plugins {

dependencies {
jmhImplementation(platform(project(":bom")))
jmhImplementation(project(":babel"))
jmhImplementation(project(":core"))
jmhImplementation(project(":linq4j"))
jmhImplementation("com.google.guava:guava")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
/*
* 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.calcite.benchmarks;

import org.apache.calcite.sql.parser.SqlParser;
import org.apache.calcite.sql.parser.babel.SqlBabelParserImpl;

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.Param;
import org.openjdk.jmh.annotations.Scope;
import org.openjdk.jmh.annotations.Setup;
import org.openjdk.jmh.annotations.State;
import org.openjdk.jmh.annotations.Threads;
import org.openjdk.jmh.annotations.Warmup;
import org.openjdk.jmh.runner.Runner;
import org.openjdk.jmh.runner.RunnerException;
import org.openjdk.jmh.runner.options.Options;
import org.openjdk.jmh.runner.options.OptionsBuilder;

import java.util.concurrent.TimeUnit;

/**
* Benchmarks JavaCC-generated SqlBabelParserImpl and ImmutableSqlParser instantiation.
* The instantiation time of parsers should not depend on the call stack depth.
* See https://lists.apache.org/thread/xw35sdy1w1k8lvn1q1lr7xb93bkj0lpq
*/
@Fork(value = 1, jvmArgsPrepend = "-Xmx128m")
@Measurement(iterations = 7, time = 1, timeUnit = TimeUnit.SECONDS)
@Warmup(iterations = 7, time = 1, timeUnit = TimeUnit.SECONDS)
@State(Scope.Thread)
@Threads(1)
@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(TimeUnit.MICROSECONDS)
public class ParserInstantiationBenchmark {

@Param({"0", "100"})
int stackDepth;

@Param({"core", "babel"})
String parser;

SqlParser.Config config;
String sqlExpression;

@Setup
public void setup() {
// not important in this benchmark, see ParserBenchmark for varying string length
sqlExpression = "SELECT 1";
switch (parser) {
case "core":
config = SqlParser.config();
break;
case "babel":
config = SqlParser.config().withParserFactory(SqlBabelParserImpl.FACTORY);
break;
default:
throw new RuntimeException("Unsupported parser: " + parser);
}
}

@Benchmark
public SqlParser instantiateParser() {
return call(stackDepth);
}

// used to increase the stack depth
private SqlParser call(final int depth) {
if (depth == 0) {
return SqlParser.create(sqlExpression, config);
}
return call(depth - 1);
}

public static void main(String[] args) throws RunnerException {
Options opt = new OptionsBuilder().include(ParserInstantiationBenchmark.class.getSimpleName())
.detectJvmArgs()
.build();

new Runner(opt).run();
}
}

0 comments on commit 95fedab

Please sign in to comment.