sis-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From desruisse...@apache.org
Subject [sis] 02/04: Document `QuerySpliteratorsBench`, use some services provide by testing framework and adjust build configuration.
Date Tue, 13 Jul 2021 16:53:25 GMT
This is an automated email from the ASF dual-hosted git repository.

desruisseaux pushed a commit to branch geoapi-4.0
in repository https://gitbox.apache.org/repos/asf/sis.git

commit dda5ef6b7570eefd68c357d53fe489a8ed5f9ae7
Author: Martin Desruisseaux <martin.desruisseaux@geomatys.com>
AuthorDate: Tue Jul 13 15:24:54 2021 +0200

    Document `QuerySpliteratorsBench`, use some services provide by testing framework and
adjust build configuration.
---
 ide-project/NetBeans/nbproject/project.properties  |   3 +-
 pom.xml                                            |   8 ++
 storage/sis-sqlstore/pom.xml                       |   9 --
 .../sql/feature/QuerySpliteratorsBench.java        | 117 ++++++++++++++-------
 4 files changed, 91 insertions(+), 46 deletions(-)

diff --git a/ide-project/NetBeans/nbproject/project.properties b/ide-project/NetBeans/nbproject/project.properties
index c1d4e2c..8b2fc41 100644
--- a/ide-project/NetBeans/nbproject/project.properties
+++ b/ide-project/NetBeans/nbproject/project.properties
@@ -130,7 +130,7 @@ hamcrest.version     = 1.3
 derby.version        = 10.14.2.0
 hsqldb.version       = 2.5.1
 postgresql.version   = 42.2.18
-icons.version        = 3.0.1
+jmh.version          = 1.32
 
 #
 # Classpaths for compilation, execution and tests.
@@ -161,6 +161,7 @@ javac.test.classpath=\
     ${maven.repository}/net/sf/geographiclib/GeographicLib-Java/${geographlib.version}/GeographicLib-Java-${geographlib.version}.jar:\
     ${maven.repository}/junit/junit/${junit.version}/junit-${junit.version}.jar:\
     ${maven.repository}/org/hamcrest/hamcrest-core/${hamcrest.version}/hamcrest-core-${hamcrest.version}.jar:\
+    ${maven.repository}/org/openjdk/jmh/jmh-core/${jmh.version}/jmh-core-${jmh.version}.jar:\
     ${project.GeoAPI}/dist/geoapi-tests.jar:\
     ${build.classes.dir}
 javac.test.processorpath=\
diff --git a/pom.xml b/pom.xml
index cbe0974..eaa7c0c 100644
--- a/pom.xml
+++ b/pom.xml
@@ -520,6 +520,14 @@
         <artifactId>slf4j-jdk14</artifactId>
         <version>1.7.22</version>               <!-- Must matches the version
used by netCDF. -->
       </dependency>
+
+      <!-- Tests and benchmarks -->
+      <dependency>
+        <groupId>org.openjdk.jmh</groupId>
+        <artifactId>jmh-generator-annprocess</artifactId>
+        <version>1.32</version>
+        <scope>test</scope>
+      </dependency>
     </dependencies>
   </dependencyManagement>
 
diff --git a/storage/sis-sqlstore/pom.xml b/storage/sis-sqlstore/pom.xml
index 17ccd49..6b63cdd 100644
--- a/storage/sis-sqlstore/pom.xml
+++ b/storage/sis-sqlstore/pom.xml
@@ -137,20 +137,11 @@
       <artifactId>jts-core</artifactId>
       <scope>test</scope>
     </dependency>
-
-    <dependency>
-      <groupId>org.openjdk.jmh</groupId>
-      <artifactId>jmh-core</artifactId>
-      <version>1.21</version>
-      <scope>test</scope>
-    </dependency>
     <dependency>
       <groupId>org.openjdk.jmh</groupId>
       <artifactId>jmh-generator-annprocess</artifactId>
-      <version>1.21</version>
       <scope>test</scope>
     </dependency>
-
   </dependencies>
 
 </project>
diff --git a/storage/sis-sqlstore/src/test/java/org/apache/sis/internal/sql/feature/QuerySpliteratorsBench.java
b/storage/sis-sqlstore/src/test/java/org/apache/sis/internal/sql/feature/QuerySpliteratorsBench.java
index 8abc26f..a135602 100644
--- a/storage/sis-sqlstore/src/test/java/org/apache/sis/internal/sql/feature/QuerySpliteratorsBench.java
+++ b/storage/sis-sqlstore/src/test/java/org/apache/sis/internal/sql/feature/QuerySpliteratorsBench.java
@@ -16,15 +16,16 @@
  */
 package org.apache.sis.internal.sql.feature;
 
+import java.util.Random;
+import java.util.concurrent.TimeUnit;
+import java.io.IOException;
 import java.nio.charset.StandardCharsets;
 import java.sql.Connection;
 import java.sql.PreparedStatement;
 import java.sql.SQLException;
-import java.util.Random;
-import java.util.concurrent.TimeUnit;
 
-import org.apache.derby.jdbc.EmbeddedDataSource;
-import org.apache.sis.internal.metadata.sql.LocalDataSource;
+import org.apache.sis.test.sql.TestDatabase;
+import org.apache.sis.util.ArgumentChecks;
 
 import org.openjdk.jmh.annotations.Benchmark;
 import org.openjdk.jmh.annotations.Fork;
@@ -37,53 +38,88 @@ import org.openjdk.jmh.annotations.State;
 import org.openjdk.jmh.annotations.TearDown;
 import org.openjdk.jmh.annotations.Warmup;
 
-import static org.apache.sis.util.ArgumentChecks.ensureStrictlyPositive;
+import static org.junit.Assert.assertEquals;
+
 
 /**
+ * Measure the performance of the stream returned by {@link QueryFeatureSet#features(boolean)}.
  *
- * @author Alexis Manin (Geomatys)
- * @version 2.0
- * @since   2.0
+ * @author  Alexis Manin (Geomatys)
+ * @version 1.1
+ * @since   1.1
  * @module
  */
-@Fork(value = 2, jvmArgs = {"-server", "-Xmx2g"} )
+@Fork(value = 2, jvmArgs = {"-server", "-Xmx2G"} )
 @Warmup(iterations = 2, time = 4, timeUnit = TimeUnit.SECONDS)
 @Measurement(iterations = 2, time = 4, timeUnit = TimeUnit.SECONDS)
 public class QuerySpliteratorsBench {
-
+    /**
+     * Provide {@link QueryFeatureSet} instances to be tested.
+     * Various configurations are used (number of rows, parallelism, <i>etc.</i>).
+     *
+     * @see QuerySpliteratorsBench#test(DatabaseInput)
+     */
     @State(Scope.Benchmark)
     public static class DatabaseInput {
-
+        /**
+         * Number of rows to insert in the test database.
+         */
         @Param({"1000", "100000"})
         int numRows;
 
+        /**
+         * Argument to be given in the call to {@link QueryFeatureSet#features(boolean)}.
+         */
         @Param({"true", "false"})
         boolean parallel;
 
+        /**
+         * Value to assign to {@link QueryFeatureSet#allowBatchLoading}.
+         */
         @Param({"true", "false"})
-        boolean prefetch;
+        private boolean prefetch;
 
+        /**
+         * Value to assign to {@link QueryFeatureSet#fetchSize}.
+         */
         @Param({"10", "100", "1000"})
-        int fetchSize;
+        private int fetchSize;
 
+        /**
+         * Value to assign to {@link QueryFeatureSet#fetchRatio}.
+         */
         @Param({"0.5", "1", "2"})
-        float fetchRatio;
+        private float fetchRatio;
+
+        /**
+         * Encapsulate a data source for an in-memory database.
+         */
+        private TestDatabase db;
 
-        EmbeddedDataSource db;
+        /**
+         * The feature set to benchmark.
+         */
         QueryFeatureSet fs;
 
-        public DatabaseInput() {}
+        /**
+         * Creates a new set of {@link QueryFeatureSet} provider.
+         */
+        public DatabaseInput() {
+        }
 
+        /**
+         * Prepares a database populated with arbitrary rows, then creates a {@link QueryFeatureSet}
+         * for querying that database. The {@code QueryFeatureSet} configuration will vary
between
+         * different calls to this method, depending on values injected by JMH in the annotated
fields.
+         *
+         * @throws SQLException if an error occurred while preparing the database.
+         */
         @Setup(Level.Trial)
         public void setup() throws SQLException {
-            ensureStrictlyPositive("Number of rows", numRows);
+            ArgumentChecks.ensureStrictlyPositive("Number of rows", numRows);
 
-            db = new EmbeddedDataSource();
-            db.setDatabaseName("memory:spliterators");
-            db.setDataSourceName("Apache SIS test database");
-            db.setCreateDatabase("create");
-
-            try (Connection c = db.getConnection()) {
+            db = TestDatabase.create("spliterators");
+            try (Connection c = db.source.getConnection()) {
                 c.createStatement().execute(
                         "CREATE TABLE TEST (str CHARACTER VARYING(20), myInt INTEGER, myDouble
DOUBLE)"
                 );
@@ -104,34 +140,43 @@ public class QuerySpliteratorsBench {
                     st.clearBatch();
                 } while (rows < numRows);
 
-                fs = new QueryFeatureSet("SELECT * FROM TEST", db, c);
+                fs = new QueryFeatureSet("SELECT * FROM TEST", db.source, c);
                 fs.allowBatchLoading = prefetch;
-                fs.fetchSize = fetchSize;
+                fs.fetchSize  = fetchSize;
                 fs.fetchRatio = fetchRatio;
             }
         }
 
+        /**
+         * Destroy the in-memory database.
+         *
+         * @throws SQLException in an error occurred.
+         */
         @TearDown
         public void dropDatabase() throws SQLException {
-            db.setCreateDatabase("no");
-            db.setConnectionAttributes("drop=true");
-            try {
-                db.getConnection().close();
-            } catch (SQLException e) {                          // This is the expected exception.
-                if (!LocalDataSource.isSuccessfulShutdown(e)) {
-                    throw e;
-                }
-            }
+            db.close();
         }
     }
 
+    /**
+     * Invoked by JMH for executing the operation that we want to benchmark.
+     *
+     * @param  input  encapsulate the {@link QueryFeatureSet} instance to benchmark.
+     * @throws SQLException if an error occurred during the database query.
+     */
     @Benchmark
     public void test(DatabaseInput input) throws SQLException {
         final int sum = input.fs.features(input.parallel).mapToInt(f -> 1).sum();
-        if (sum != input.numRows) throw new AssertionError("..." + sum + "..." + "WTF ?!");
+        assertEquals(input.numRows, sum);
     }
 
-    public static void main(String... args) throws Exception {
+    /**
+     * Launch the benchmark.
+     *
+     * @param  args  JMH command line argument.
+     * @throws IOException if JMH can not proceed.
+     */
+    public static void main(final String[] args) throws IOException {
         org.openjdk.jmh.Main.main(args);
     }
 }

Mime
View raw message