You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@calcite.apache.org by "cyrilou242 (via GitHub)" <gi...@apache.org> on 2024/07/11 08:09:17 UTC

[PR] [CALCITE-1012] add benchmark for SQL parser instantiation [calcite]

cyrilou242 opened a new pull request, #3853:
URL: https://github.com/apache/calcite/pull/3853

   This benchmark will help to measure the progress on the discussion here:
   https://lists.apache.org/thread/xw35sdy1w1k8lvn1q1lr7xb93bkj0lpq
   As of today the parsers instantiation is pretty slow and the speed depends on the call stack depth. This is an issue caused by JavaCC. The issue is fixed in newer version of JavaCC.
   
   Once JavaCC is upgraded to 7.0.13 (https://issues.apache.org/jira/browse/CALCITE-5541), this benchmark can be re-run. 
   Expected improvements are: 
   - a minor speed improvement of ~10% for `instantiateBabelParser` and `instantiateCoreParser`
   - the instantiation of parsers should not depend on the call stack depth anymore: 
     - `instantiateBabelParser` ~= `instantiateBabelParserInDeepCallStack`
     - `instantiateCoreParser` ~= `instantiateCoreParserInDeepCallStack`
   
   ## Current Results: 
   ```
   Benchmark                                                           (comments)  (length)  Mode  Cnt    Score   Error  Units
   ParserBenchmark.parseCached                                               true      1000  avgt    7  126.512 ± 0.299  us/op
   ParserBenchmark.parseNonCached                                            true      1000  avgt    7   93.536 ± 0.278  us/op
   ParserInstantiationBenchmark.instantiateBabelParser                        N/A       N/A  avgt    7   14.716 ± 0.174  us/op
   ParserInstantiationBenchmark.instantiateBabelParserInDeepCallStack         N/A       N/A  avgt    7   21.592 ± 0.614  us/op
   ParserInstantiationBenchmark.instantiateCoreParser                         N/A       N/A  avgt    7    6.523 ± 0.114  us/op
   ParserInstantiationBenchmark.instantiateCoreParserInDeepCallStack          N/A       N/A  avgt    7   21.586 ± 0.187  us/op
   ```
   
   (temurin 17, intel macbook)
   
   ## How to run: 
   Add: 
   ```
   jmh {
       includes = listOf("Parser.*Benchmark")
   }
   ```
   To `ubenchmark/build.gradle.kts`  
   Then `./gradlew :ubenchmark:jmh`
   This will run the new `ParserInstantiationBenchmark` and the existing `ParserBenchmark`.


-- 
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-unsubscribe@calcite.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [CALCITE-1012] add benchmark for SQL parser instantiation [calcite]

Posted by "zabetak (via GitHub)" <gi...@apache.org>.
zabetak commented on code in PR #3853:
URL: https://github.com/apache/calcite/pull/3853#discussion_r1673961753


##########
ubenchmark/src/jmh/java/org/apache/calcite/benchmarks/ParserInstantiationBenchmark.java:
##########
@@ -0,0 +1,115 @@
+/*
+ * 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 java.util.concurrent.TimeUnit;
+
+import org.apache.calcite.sql.parser.SqlParseException;
+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.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.infra.Blackhole;
+import org.openjdk.jmh.profile.GCProfiler;
+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;
+
+/**
+ * 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 {
+
+  int callStackDepth;
+  SqlParser.Config babelConfig;
+  SqlParser.Config coreConfig;
+  String sqlExpression;
+
+  @Setup
+  public void setup() throws SqlParseException {
+    babelConfig = SqlParser.config().withParserFactory(SqlBabelParserImpl.FACTORY);
+    coreConfig = SqlParser.config();
+    // not important in this benchmark, see ParserBenchmark for varying string length
+    sqlExpression = "SELECT 1";
+    callStackDepth = 100;
+  }
+
+  @Benchmark
+  public void instantiateBabelParser(Blackhole bh) {
+    bh.consume(SqlParser.create(sqlExpression, babelConfig));

Review Comment:
   Blackholes are not needed. If you just do `return SqlParser.create` dead-code elimination will not kick in.



##########
ubenchmark/src/jmh/java/org/apache/calcite/benchmarks/ParserInstantiationBenchmark.java:
##########
@@ -0,0 +1,115 @@
+/*
+ * 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 java.util.concurrent.TimeUnit;
+
+import org.apache.calcite.sql.parser.SqlParseException;
+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.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.infra.Blackhole;
+import org.openjdk.jmh.profile.GCProfiler;
+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;
+
+/**
+ * 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 {

Review Comment:
   Maybe rename to `ParserStackDepthBenchmark`?



##########
ubenchmark/src/jmh/java/org/apache/calcite/benchmarks/ParserInstantiationBenchmark.java:
##########
@@ -0,0 +1,115 @@
+/*
+ * 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 java.util.concurrent.TimeUnit;
+
+import org.apache.calcite.sql.parser.SqlParseException;
+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.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.infra.Blackhole;
+import org.openjdk.jmh.profile.GCProfiler;
+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;
+
+/**
+ * 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 {
+
+  int callStackDepth;
+  SqlParser.Config babelConfig;
+  SqlParser.Config coreConfig;
+  String sqlExpression;
+
+  @Setup
+  public void setup() throws SqlParseException {
+    babelConfig = SqlParser.config().withParserFactory(SqlBabelParserImpl.FACTORY);
+    coreConfig = SqlParser.config();
+    // not important in this benchmark, see ParserBenchmark for varying string length
+    sqlExpression = "SELECT 1";
+    callStackDepth = 100;
+  }
+
+  @Benchmark
+  public void instantiateBabelParser(Blackhole bh) {

Review Comment:
   Since this benchmark aims mostly to verify changes in JavaCC we may not need to benchmark different parser factories since all parsers are using JavaCC.



##########
ubenchmark/src/jmh/java/org/apache/calcite/benchmarks/ParserInstantiationBenchmark.java:
##########
@@ -0,0 +1,115 @@
+/*
+ * 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 java.util.concurrent.TimeUnit;
+
+import org.apache.calcite.sql.parser.SqlParseException;
+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.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.infra.Blackhole;
+import org.openjdk.jmh.profile.GCProfiler;
+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;
+
+/**
+ * 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 {
+
+  int callStackDepth;
+  SqlParser.Config babelConfig;
+  SqlParser.Config coreConfig;
+  String sqlExpression;
+
+  @Setup
+  public void setup() throws SqlParseException {
+    babelConfig = SqlParser.config().withParserFactory(SqlBabelParserImpl.FACTORY);
+    coreConfig = SqlParser.config();
+    // not important in this benchmark, see ParserBenchmark for varying string length
+    sqlExpression = "SELECT 1";
+    callStackDepth = 100;
+  }
+
+  @Benchmark
+  public void instantiateBabelParser(Blackhole bh) {
+    bh.consume(SqlParser.create(sqlExpression, babelConfig));
+  }
+
+  @Benchmark
+  public void instantiateBabelParserInDeepCallStack(Blackhole bh) {
+    recursiveCall(
+        () -> bh.consume(SqlParser.create(sqlExpression, babelConfig)),
+        callStackDepth
+    );
+  }
+
+  @Benchmark
+  public void instantiateCoreParser(Blackhole bh) {
+    bh.consume(SqlParser.create(sqlExpression, coreConfig));
+  }
+
+  @Benchmark
+  public void instantiateCoreParserInDeepCallStack(Blackhole bh) {
+    recursiveCall(
+        () -> bh.consume(SqlParser.create(sqlExpression, babelConfig)),
+        callStackDepth
+    );
+  }
+
+  // used to increase the stack depth
+  private void recursiveCall(final Runnable r, final int callStackSize) {
+    if (callStackSize == 0) {
+      r.run();
+      return;

Review Comment:
   This is subject to dead-code elimination. How about the following:
   ```java
   private SqlParser call(int depth) {
     if (depth == 0) {
       return SqlParser.create(..)
     } else {
       return call(depth-1);
     }
   }
   ```



##########
ubenchmark/src/jmh/java/org/apache/calcite/benchmarks/ParserInstantiationBenchmark.java:
##########
@@ -0,0 +1,115 @@
+/*
+ * 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 java.util.concurrent.TimeUnit;
+
+import org.apache.calcite.sql.parser.SqlParseException;
+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.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.infra.Blackhole;
+import org.openjdk.jmh.profile.GCProfiler;
+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;
+
+/**
+ * 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 {
+
+  int callStackDepth;
+  SqlParser.Config babelConfig;
+  SqlParser.Config coreConfig;
+  String sqlExpression;
+
+  @Setup
+  public void setup() throws SqlParseException {
+    babelConfig = SqlParser.config().withParserFactory(SqlBabelParserImpl.FACTORY);
+    coreConfig = SqlParser.config();
+    // not important in this benchmark, see ParserBenchmark for varying string length
+    sqlExpression = "SELECT 1";
+    callStackDepth = 100;
+  }
+
+  @Benchmark
+  public void instantiateBabelParser(Blackhole bh) {
+    bh.consume(SqlParser.create(sqlExpression, babelConfig));
+  }
+
+  @Benchmark
+  public void instantiateBabelParserInDeepCallStack(Blackhole bh) {
+    recursiveCall(
+        () -> bh.consume(SqlParser.create(sqlExpression, babelConfig)),
+        callStackDepth
+    );
+  }
+
+  @Benchmark
+  public void instantiateCoreParser(Blackhole bh) {
+    bh.consume(SqlParser.create(sqlExpression, coreConfig));
+  }
+
+  @Benchmark
+  public void instantiateCoreParserInDeepCallStack(Blackhole bh) {
+    recursiveCall(
+        () -> bh.consume(SqlParser.create(sqlExpression, babelConfig)),
+        callStackDepth
+    );
+  }
+
+  // used to increase the stack depth
+  private void recursiveCall(final Runnable r, final int callStackSize) {
+    if (callStackSize == 0) {
+      r.run();
+      return;
+    }
+    recursiveCall(r, callStackSize - 1);
+  }
+
+  public static void main(String[] args) throws RunnerException {
+    Options opt = new OptionsBuilder().include(ParserInstantiationBenchmark.class.getSimpleName())
+        .addProfiler(GCProfiler.class)
+        .addProfiler(FlightRecorderProfiler.class)

Review Comment:
   Do we need/use these profilers?



##########
ubenchmark/src/jmh/java/org/apache/calcite/benchmarks/ParserInstantiationBenchmark.java:
##########
@@ -0,0 +1,115 @@
+/*
+ * 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 java.util.concurrent.TimeUnit;
+
+import org.apache.calcite.sql.parser.SqlParseException;
+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.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.infra.Blackhole;
+import org.openjdk.jmh.profile.GCProfiler;
+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;
+
+/**
+ * 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 {
+
+  int callStackDepth;
+  SqlParser.Config babelConfig;
+  SqlParser.Config coreConfig;
+  String sqlExpression;
+
+  @Setup
+  public void setup() throws SqlParseException {
+    babelConfig = SqlParser.config().withParserFactory(SqlBabelParserImpl.FACTORY);
+    coreConfig = SqlParser.config();
+    // not important in this benchmark, see ParserBenchmark for varying string length
+    sqlExpression = "SELECT 1";
+    callStackDepth = 100;
+  }
+
+  @Benchmark
+  public void instantiateBabelParser(Blackhole bh) {

Review Comment:
   You can refactor the multiple benchmark methods into one by using JMH `@Param` annotations. One parameter is the stack depth and the other the parser factory. That way it would be also convenient to run the benchmark with slightly different parameters.



-- 
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-unsubscribe@calcite.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [CALCITE-6466] Add benchmark for SQL parser instantiation [calcite]

Posted by "zabetak (via GitHub)" <gi...@apache.org>.
zabetak closed pull request #3853: [CALCITE-6466] Add benchmark for SQL parser instantiation
URL: https://github.com/apache/calcite/pull/3853


-- 
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-unsubscribe@calcite.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [CALCITE-6466] add benchmark for SQL parser instantiation [calcite]

Posted by "cyrilou242 (via GitHub)" <gi...@apache.org>.
cyrilou242 commented on PR #3853:
URL: https://github.com/apache/calcite/pull/3853#issuecomment-2223972517

   > Thanks for the benchmark @cyrilou242 ! I left some small comments but looks good.
   > 
   > The purpose of this benchmark is different from that in [CALCITE-1012](https://issues.apache.org/jira/browse/CALCITE-1012) so I would suggest creating a new ticket for this purpose and link it with [CALCITE-5541](https://issues.apache.org/jira/browse/CALCITE-5541).
   
   Thanks for the quick review @zabetak! 
   I created the following ticket: [CALCITE-6466](https://issues.apache.org/jira/browse/CALCITE-6466)
   Will go through your points in the following days.


-- 
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-unsubscribe@calcite.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [CALCITE-6466] Add benchmark for SQL parser instantiation [calcite]

Posted by "zabetak (via GitHub)" <gi...@apache.org>.
zabetak commented on code in PR #3853:
URL: https://github.com/apache/calcite/pull/3853#discussion_r1677728020


##########
ubenchmark/src/jmh/java/org/apache/calcite/benchmarks/ParserInstantiationBenchmark.java:
##########
@@ -0,0 +1,115 @@
+/*
+ * 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 java.util.concurrent.TimeUnit;
+
+import org.apache.calcite.sql.parser.SqlParseException;
+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.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.infra.Blackhole;
+import org.openjdk.jmh.profile.GCProfiler;
+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;
+
+/**
+ * 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 {

Review Comment:
   I don't have a strong preference so let's keep it as is.



##########
ubenchmark/src/jmh/java/org/apache/calcite/benchmarks/ParserInstantiationBenchmark.java:
##########
@@ -0,0 +1,115 @@
+/*
+ * 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 java.util.concurrent.TimeUnit;
+
+import org.apache.calcite.sql.parser.SqlParseException;
+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.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.infra.Blackhole;
+import org.openjdk.jmh.profile.GCProfiler;
+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;
+
+/**
+ * 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 {
+
+  int callStackDepth;
+  SqlParser.Config babelConfig;
+  SqlParser.Config coreConfig;
+  String sqlExpression;
+
+  @Setup
+  public void setup() throws SqlParseException {
+    babelConfig = SqlParser.config().withParserFactory(SqlBabelParserImpl.FACTORY);
+    coreConfig = SqlParser.config();
+    // not important in this benchmark, see ParserBenchmark for varying string length
+    sqlExpression = "SELECT 1";
+    callStackDepth = 100;
+  }
+
+  @Benchmark
+  public void instantiateBabelParser(Blackhole bh) {

Review Comment:
   If there is interest in keeping the additional benchmark then that's fine with me!



-- 
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-unsubscribe@calcite.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [CALCITE-6466] Add benchmark for SQL parser instantiation [calcite]

Posted by "sonarcloud[bot] (via GitHub)" <gi...@apache.org>.
sonarcloud[bot] commented on PR #3853:
URL: https://github.com/apache/calcite/pull/3853#issuecomment-2228411867

   ## [![Quality Gate Passed](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/QualityGateBadge/qg-passed-20px.png 'Quality Gate Passed')](https://sonarcloud.io/dashboard?id=apache_calcite&pullRequest=3853) **Quality Gate passed**  
   Issues  
   ![](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/passed-16px.png '') [0 New issues](https://sonarcloud.io/project/issues?id=apache_calcite&pullRequest=3853&issueStatuses=OPEN,CONFIRMED&sinceLeakPeriod=true)  
   ![](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/accepted-16px.png '') [0 Accepted issues](https://sonarcloud.io/project/issues?id=apache_calcite&pullRequest=3853&issueStatuses=ACCEPTED)
   
   Measures  
   ![](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/passed-16px.png '') [0 Security Hotspots](https://sonarcloud.io/project/security_hotspots?id=apache_calcite&pullRequest=3853&issueStatuses=OPEN,CONFIRMED&sinceLeakPeriod=true)  
   ![](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/passed-16px.png '') [0.0% Coverage on New Code](https://sonarcloud.io/component_measures?id=apache_calcite&pullRequest=3853&metric=new_coverage&view=list)  
   ![](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/passed-16px.png '') [0.0% Duplication on New Code](https://sonarcloud.io/component_measures?id=apache_calcite&pullRequest=3853&metric=new_duplicated_lines_density&view=list)  
     
   [See analysis details on SonarCloud](https://sonarcloud.io/dashboard?id=apache_calcite&pullRequest=3853)
   
   


-- 
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-unsubscribe@calcite.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [CALCITE-6466] add benchmark for SQL parser instantiation [calcite]

Posted by "cyrilou242 (via GitHub)" <gi...@apache.org>.
cyrilou242 commented on code in PR #3853:
URL: https://github.com/apache/calcite/pull/3853#discussion_r1674756688


##########
ubenchmark/src/jmh/java/org/apache/calcite/benchmarks/ParserInstantiationBenchmark.java:
##########
@@ -0,0 +1,115 @@
+/*
+ * 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 java.util.concurrent.TimeUnit;
+
+import org.apache.calcite.sql.parser.SqlParseException;
+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.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.infra.Blackhole;
+import org.openjdk.jmh.profile.GCProfiler;
+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;
+
+/**
+ * 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 {

Review Comment:
   Longer term I was thinking the interest of this benchmark is to measure how fast it is to instantiate a parser, whatever the stack depth. 
   Once the issue of the stacktrace is fixed the stack depth will have no impact on the speed of instantiation, but maybe some other things in javacc or the parser implem will?
   What do you think? 
   



-- 
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-unsubscribe@calcite.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [CALCITE-6466] add benchmark for SQL parser instantiation [calcite]

Posted by "cyrilou242 (via GitHub)" <gi...@apache.org>.
cyrilou242 commented on code in PR #3853:
URL: https://github.com/apache/calcite/pull/3853#discussion_r1674746023


##########
ubenchmark/src/jmh/java/org/apache/calcite/benchmarks/ParserInstantiationBenchmark.java:
##########
@@ -0,0 +1,115 @@
+/*
+ * 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 java.util.concurrent.TimeUnit;
+
+import org.apache.calcite.sql.parser.SqlParseException;
+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.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.infra.Blackhole;
+import org.openjdk.jmh.profile.GCProfiler;
+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;
+
+/**
+ * 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 {
+
+  int callStackDepth;
+  SqlParser.Config babelConfig;
+  SqlParser.Config coreConfig;
+  String sqlExpression;
+
+  @Setup
+  public void setup() throws SqlParseException {
+    babelConfig = SqlParser.config().withParserFactory(SqlBabelParserImpl.FACTORY);
+    coreConfig = SqlParser.config();
+    // not important in this benchmark, see ParserBenchmark for varying string length
+    sqlExpression = "SELECT 1";
+    callStackDepth = 100;
+  }
+
+  @Benchmark
+  public void instantiateBabelParser(Blackhole bh) {
+    bh.consume(SqlParser.create(sqlExpression, babelConfig));
+  }
+
+  @Benchmark
+  public void instantiateBabelParserInDeepCallStack(Blackhole bh) {
+    recursiveCall(
+        () -> bh.consume(SqlParser.create(sqlExpression, babelConfig)),
+        callStackDepth
+    );
+  }
+
+  @Benchmark
+  public void instantiateCoreParser(Blackhole bh) {
+    bh.consume(SqlParser.create(sqlExpression, coreConfig));
+  }
+
+  @Benchmark
+  public void instantiateCoreParserInDeepCallStack(Blackhole bh) {
+    recursiveCall(
+        () -> bh.consume(SqlParser.create(sqlExpression, babelConfig)),
+        callStackDepth
+    );
+  }
+
+  // used to increase the stack depth
+  private void recursiveCall(final Runnable r, final int callStackSize) {
+    if (callStackSize == 0) {
+      r.run();
+      return;

Review Comment:
   oh right thanks



-- 
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-unsubscribe@calcite.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [CALCITE-6466] add benchmark for SQL parser instantiation [calcite]

Posted by "cyrilou242 (via GitHub)" <gi...@apache.org>.
cyrilou242 commented on code in PR #3853:
URL: https://github.com/apache/calcite/pull/3853#discussion_r1674753689


##########
ubenchmark/src/jmh/java/org/apache/calcite/benchmarks/ParserInstantiationBenchmark.java:
##########
@@ -0,0 +1,115 @@
+/*
+ * 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 java.util.concurrent.TimeUnit;
+
+import org.apache.calcite.sql.parser.SqlParseException;
+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.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.infra.Blackhole;
+import org.openjdk.jmh.profile.GCProfiler;
+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;
+
+/**
+ * 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 {
+
+  int callStackDepth;
+  SqlParser.Config babelConfig;
+  SqlParser.Config coreConfig;
+  String sqlExpression;
+
+  @Setup
+  public void setup() throws SqlParseException {
+    babelConfig = SqlParser.config().withParserFactory(SqlBabelParserImpl.FACTORY);
+    coreConfig = SqlParser.config();
+    // not important in this benchmark, see ParserBenchmark for varying string length
+    sqlExpression = "SELECT 1";
+    callStackDepth = 100;
+  }
+
+  @Benchmark
+  public void instantiateBabelParser(Blackhole bh) {

Review Comment:
   > Since this benchmark aims mostly to verify changes in JavaCC we may not need to benchmark different parser factories since all parsers are using JavaCC.
   
   I don't have much knowledge about Calcite but I was thinking the generated code could be very different. 
   From the benchmark we can see that core is ~2x faster to instantiate than babel.
   
   As you like. For my company we'd really like to keep a tab on the babel parser.



-- 
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-unsubscribe@calcite.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [CALCITE-6466] add benchmark for SQL parser instantiation [calcite]

Posted by "cyrilou242 (via GitHub)" <gi...@apache.org>.
cyrilou242 commented on code in PR #3853:
URL: https://github.com/apache/calcite/pull/3853#discussion_r1674747392


##########
ubenchmark/src/jmh/java/org/apache/calcite/benchmarks/ParserInstantiationBenchmark.java:
##########
@@ -0,0 +1,115 @@
+/*
+ * 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 java.util.concurrent.TimeUnit;
+
+import org.apache.calcite.sql.parser.SqlParseException;
+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.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.infra.Blackhole;
+import org.openjdk.jmh.profile.GCProfiler;
+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;
+
+/**
+ * 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 {
+
+  int callStackDepth;
+  SqlParser.Config babelConfig;
+  SqlParser.Config coreConfig;
+  String sqlExpression;
+
+  @Setup
+  public void setup() throws SqlParseException {
+    babelConfig = SqlParser.config().withParserFactory(SqlBabelParserImpl.FACTORY);
+    coreConfig = SqlParser.config();
+    // not important in this benchmark, see ParserBenchmark for varying string length
+    sqlExpression = "SELECT 1";
+    callStackDepth = 100;
+  }
+
+  @Benchmark
+  public void instantiateBabelParser(Blackhole bh) {
+    bh.consume(SqlParser.create(sqlExpression, babelConfig));

Review Comment:
   nice thanks.



-- 
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-unsubscribe@calcite.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [CALCITE-6466] add benchmark for SQL parser instantiation [calcite]

Posted by "cyrilou242 (via GitHub)" <gi...@apache.org>.
cyrilou242 commented on code in PR #3853:
URL: https://github.com/apache/calcite/pull/3853#discussion_r1674719255


##########
ubenchmark/src/jmh/java/org/apache/calcite/benchmarks/ParserInstantiationBenchmark.java:
##########
@@ -0,0 +1,115 @@
+/*
+ * 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 java.util.concurrent.TimeUnit;
+
+import org.apache.calcite.sql.parser.SqlParseException;
+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.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.infra.Blackhole;
+import org.openjdk.jmh.profile.GCProfiler;
+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;
+
+/**
+ * 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 {
+
+  int callStackDepth;
+  SqlParser.Config babelConfig;
+  SqlParser.Config coreConfig;
+  String sqlExpression;
+
+  @Setup
+  public void setup() throws SqlParseException {
+    babelConfig = SqlParser.config().withParserFactory(SqlBabelParserImpl.FACTORY);
+    coreConfig = SqlParser.config();
+    // not important in this benchmark, see ParserBenchmark for varying string length
+    sqlExpression = "SELECT 1";
+    callStackDepth = 100;
+  }
+
+  @Benchmark
+  public void instantiateBabelParser(Blackhole bh) {
+    bh.consume(SqlParser.create(sqlExpression, babelConfig));
+  }
+
+  @Benchmark
+  public void instantiateBabelParserInDeepCallStack(Blackhole bh) {
+    recursiveCall(
+        () -> bh.consume(SqlParser.create(sqlExpression, babelConfig)),
+        callStackDepth
+    );
+  }
+
+  @Benchmark
+  public void instantiateCoreParser(Blackhole bh) {
+    bh.consume(SqlParser.create(sqlExpression, coreConfig));
+  }
+
+  @Benchmark
+  public void instantiateCoreParserInDeepCallStack(Blackhole bh) {
+    recursiveCall(
+        () -> bh.consume(SqlParser.create(sqlExpression, babelConfig)),
+        callStackDepth
+    );
+  }
+
+  // used to increase the stack depth
+  private void recursiveCall(final Runnable r, final int callStackSize) {
+    if (callStackSize == 0) {
+      r.run();
+      return;
+    }
+    recursiveCall(r, callStackSize - 1);
+  }
+
+  public static void main(String[] args) throws RunnerException {
+    Options opt = new OptionsBuilder().include(ParserInstantiationBenchmark.class.getSimpleName())
+        .addProfiler(GCProfiler.class)
+        .addProfiler(FlightRecorderProfiler.class)

Review Comment:
   no.
   thanks



-- 
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-unsubscribe@calcite.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org