You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@opennlp.apache.org by rz...@apache.org on 2023/12/14 11:46:39 UTC

(opennlp) 01/02: OPENNLP-421 - Adds simple JMH benchmark for "StringList"

This is an automated email from the ASF dual-hosted git repository.

rzo1 pushed a commit to branch OPENNLP-421
in repository https://gitbox.apache.org/repos/asf/opennlp.git

commit 7da0a39f934c76952e477beece52b348354235ad
Author: Richard Zowalla <ri...@hs-heilbronn.de>
AuthorDate: Thu Dec 14 12:45:31 2023 +0100

    OPENNLP-421 - Adds simple JMH benchmark for "StringList"
---
 opennlp-tools/pom.xml                              | 43 ++++++++++++++++++
 .../jmh/java/opennlp/tools/BenchmarkRunner.java    | 25 +++++++++++
 .../java/opennlp/tools/StringListBenchmark.java    | 43 ++++++++++++++++++
 .../jmh/java/opennlp/tools/jmh/ExecutionPlan.java  | 51 ++++++++++++++++++++++
 pom.xml                                            |  1 +
 5 files changed, 163 insertions(+)

diff --git a/opennlp-tools/pom.xml b/opennlp-tools/pom.xml
index 15a203fa..e248af44 100644
--- a/opennlp-tools/pom.xml
+++ b/opennlp-tools/pom.xml
@@ -77,6 +77,7 @@
       <version>1.1.2</version>
       <scope>test</scope>
     </dependency>
+
   </dependencies>
 
   <build>
@@ -153,5 +154,47 @@
         </pluginManagement>
       </build>
     </profile>
+
+    <profile>
+      <id>jmh</id>
+      <dependencies>
+        <dependency>
+          <groupId>org.openjdk.jmh</groupId>
+          <artifactId>jmh-core</artifactId>
+          <version>${jmh.version}</version>
+          <scope>test</scope>
+        </dependency>
+
+        <dependency>
+          <groupId>org.openjdk.jmh</groupId>
+          <artifactId>jmh-generator-annprocess</artifactId>
+          <version>${jmh.version}</version>
+          <scope>test</scope>
+        </dependency>
+      </dependencies>
+      <build>
+        <plugins>
+          <plugin>
+            <groupId>org.codehaus.mojo</groupId>
+            <artifactId>build-helper-maven-plugin</artifactId>
+            <version>3.2.0</version>
+            <executions>
+              <execution>
+                <id>add-test-source</id>
+                <phase>generate-test-sources</phase>
+                <goals>
+                  <goal>add-test-source</goal>
+                </goals>
+                <configuration>
+                  <sources>
+                    <source>src/jmh/</source>
+                  </sources>
+                </configuration>
+              </execution>
+            </executions>
+          </plugin>
+        </plugins>
+      </build>
+    </profile>
   </profiles>
 </project>
diff --git a/opennlp-tools/src/jmh/java/opennlp/tools/BenchmarkRunner.java b/opennlp-tools/src/jmh/java/opennlp/tools/BenchmarkRunner.java
new file mode 100644
index 00000000..cef29c30
--- /dev/null
+++ b/opennlp-tools/src/jmh/java/opennlp/tools/BenchmarkRunner.java
@@ -0,0 +1,25 @@
+/*
+ * 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 opennlp.tools;
+
+public class BenchmarkRunner {
+
+  public static void main(String[] args) throws Exception {
+    org.openjdk.jmh.Main.main(args);
+  }
+
+}
diff --git a/opennlp-tools/src/jmh/java/opennlp/tools/StringListBenchmark.java b/opennlp-tools/src/jmh/java/opennlp/tools/StringListBenchmark.java
new file mode 100644
index 00000000..67212e5e
--- /dev/null
+++ b/opennlp-tools/src/jmh/java/opennlp/tools/StringListBenchmark.java
@@ -0,0 +1,43 @@
+/*
+ * 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 opennlp.tools;
+
+import java.util.concurrent.TimeUnit;
+
+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.Warmup;
+import org.openjdk.jmh.infra.Blackhole;
+
+import opennlp.tools.jmh.ExecutionPlan;
+import opennlp.tools.util.StringList;
+
+@Fork(warmups = 1, value = 1)
+@Warmup(iterations = 5)
+@Measurement(iterations = 10, timeUnit = TimeUnit.MILLISECONDS)
+public class StringListBenchmark {
+
+  @Benchmark
+  @BenchmarkMode(Mode.Throughput)
+  public void newWithArrayConstructor(Blackhole blackhole, ExecutionPlan exec) {
+    blackhole.consume(new StringList(exec.strings));
+  }
+
+}
diff --git a/opennlp-tools/src/jmh/java/opennlp/tools/jmh/ExecutionPlan.java b/opennlp-tools/src/jmh/java/opennlp/tools/jmh/ExecutionPlan.java
new file mode 100644
index 00000000..ca0453c1
--- /dev/null
+++ b/opennlp-tools/src/jmh/java/opennlp/tools/jmh/ExecutionPlan.java
@@ -0,0 +1,51 @@
+/*
+ * 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 opennlp.tools.jmh;
+
+import java.util.Random;
+
+import org.openjdk.jmh.annotations.Level;
+import org.openjdk.jmh.annotations.Scope;
+import org.openjdk.jmh.annotations.Setup;
+import org.openjdk.jmh.annotations.State;
+
+@State(Scope.Benchmark)
+public class ExecutionPlan {
+
+  private static final Random RANDOM = new Random(42);
+
+  public final String[] strings = new String[1_000_000];
+
+  @Setup(Level.Invocation)
+  public void setUp() {
+    for (int i = 0; i < 1_000_000; i++) {
+      strings[i] = generateRandomString(15);
+    }
+  }
+
+  private static String generateRandomString(int length) {
+    final String characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
+    final StringBuilder randomString = new StringBuilder();
+
+    for (int i = 0; i < length; i++) {
+      int index = RANDOM.nextInt(characters.length());
+      randomString.append(characters.charAt(index));
+    }
+
+    return randomString.toString();
+  }
+}
\ No newline at end of file
diff --git a/pom.xml b/pom.xml
index 706d437e..9d469630 100644
--- a/pom.xml
+++ b/pom.xml
@@ -176,6 +176,7 @@
 		<onnxruntime.version>1.15.0</onnxruntime.version>
 		<slf4j.version>1.7.36</slf4j.version>
 		<log4j2.version>2.20.0</log4j2.version>
+		<jmh.version>1.37</jmh.version>
 		<opennlp.forkCount>1.0C</opennlp.forkCount>
 		<coveralls.maven.plugin>4.3.0</coveralls.maven.plugin>
 		<jacoco.maven.plugin>0.7.9</jacoco.maven.plugin>