You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@calcite.apache.org by GitBox <gi...@apache.org> on 2019/03/04 13:46:27 UTC

[GitHub] [calcite] zabetak commented on a change in pull request #1080: [CALCITE-2887] Improve performance of RelLiteral#toJavaString

zabetak commented on a change in pull request #1080: [CALCITE-2887] Improve performance of RelLiteral#toJavaString
URL: https://github.com/apache/calcite/pull/1080#discussion_r262063337
 
 

 ##########
 File path: ubenchmark/src/main/java/org/apache/calcite/benchmarks/StringConstructBenchmark.java
 ##########
 @@ -0,0 +1,133 @@
+/*
+ * 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.openjdk.jmh.annotations.Benchmark;
+import org.openjdk.jmh.annotations.BenchmarkMode;
+import org.openjdk.jmh.annotations.Fork;
+import org.openjdk.jmh.annotations.Level;
+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.infra.Blackhole;
+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.io.IOException;
+import java.io.PrintWriter;
+import java.io.StringWriter;
+import java.io.Writer;
+import java.util.concurrent.TimeUnit;
+
+/**
+ * A benchmark of the most common patterns that are used to construct gradually String objects.
+ *
+ * The benchmark emphasizes on the build patterns that appear in the Calcite project.
+ */
+@Fork(value = 1, jvmArgsPrepend = "-Xmx2048m")
+@Measurement(iterations = 10, time = 100, timeUnit = TimeUnit.MILLISECONDS)
+@Warmup(iterations = 10, time = 100, timeUnit = TimeUnit.MILLISECONDS)
+@Threads(1)
+@OutputTimeUnit(TimeUnit.MILLISECONDS)
+@BenchmarkMode(Mode.Throughput)
+public class StringConstructBenchmark {
+
+  /**
+   * A state holding a Writer object which is initialized only once at the beginning of the
+   * benchmark.
+   */
+  @State(Scope.Thread)
+  public static class WriterState {
+    public Writer writer;
+
+    @Setup(Level.Trial)
+    public void setup() {
+      this.writer = new StringWriter();
+    }
+  }
+
+  /**
+   * A state holding an Appendable object which is initialized once at the beginning of every
+   * iteration.
+   */
+  @State(Scope.Thread)
+  public static class AppenderState {
+    /**
+     * The type of the appender to be initialised.
+     */
+    @Param({"StringBuilder", "StringWriter", "PrintWriter"})
+    public String appenderType;
+
+    public Appendable appender;
+
+    @Setup(Level.Iteration)
+    public void setup() {
+      if (appenderType.equals("StringBuilder")) {
+        this.appender = new StringBuilder();
+      }
+      if (appenderType.equals("StringWriter")) {
+        this.appender = new StringWriter();
+      }
+      if (appenderType.equals("PrintWriter")) {
+        this.appender = new PrintWriter(new StringWriter());
+      }
+      throw new IllegalStateException("No correct appender type is specified.");
+    }
+  }
+
+  @Benchmark
+  public StringBuilder initStringBuilder() {
+    return new StringBuilder();
+  }
+
+  @Benchmark
+  public StringWriter initStringWriter() {
+    return new StringWriter();
+  }
+
+  @Benchmark
+  public PrintWriter initPrintWriter(WriterState writerState) {
+    return new PrintWriter(writerState.writer);
+  }
+
+
+  @Benchmark
+  public void appendString(Blackhole bh, AppenderState appenderState) throws IOException {
+    bh.consume(appenderState.appender.append("A small string"));
 
 Review comment:
   @vlsi : I changed a bit the `appendString` benchmark to include `@Param int maxAppends` so now it also includes the time to instantiate the Appendable. This is a more realistic representation of what is happening in `RexLiteral#toJavaString`.

----------------------------------------------------------------
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.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services