Skip to content

Commit

Permalink
Add microbenchmark for stateless queries.
Browse files Browse the repository at this point in the history
This adds a Java Microbenchmark Harness (JMH) benchmark that compares
a small query with and without job creation.

Documentation on running the microbenchmark can be found in its
class' Javadoc.

A small query with job creation takes almost twice as long as one
with optional job creation.
  • Loading branch information
goomrw committed Dec 13, 2023
1 parent efef4cc commit 71bc3a9
Show file tree
Hide file tree
Showing 3 changed files with 163 additions and 2 deletions.
37 changes: 35 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,18 @@
<version>1.19.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.openjdk.jmh</groupId>
<artifactId>jmh-core</artifactId>
<version>1.37</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.openjdk.jmh</groupId>
<artifactId>jmh-generator-annprocess</artifactId>
<version>1.37</version>
<scope>test</scope>
</dependency>
<!-- END TEST DEPENDENCIES -->
</dependencies>
<build>
Expand Down Expand Up @@ -221,8 +233,29 @@
</java>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>3.1.0</version>
<executions>
<execution>
<id>benchmark-stateless-small</id>
<goals><goal>exec</goal></goals>
<configuration>
<classpathScope>test</classpathScope>
<executable>java</executable>
<arguments>
<argument>-classpath</argument>
<classpath/>
<argument>org.openjdk.jmh.Main</argument>
<argument>
net.starschema.clouddb.jdbc.StatelessSmallQueryBenchmark
</argument>
</arguments>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>


</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package net.starschema.clouddb.jdbc;

import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/** Utility class to enable BigQuery connections from properties resources. */
public class ConnectionFromResources {
private static final Logger logger = LoggerFactory.getLogger(ConnectionFromResources.class);

/**
* Connect to a BigQuery project as described in a properties file
*
* @param propertiesFilePath the path to the properties in file in src/test/resources
* @param extraUrl extra URL arguments to add; must start with &
* @return A {@link BQConnection} connected to the given database
* @throws IOException if the properties file cannot be read
* @throws SQLException if the configuration can't connect to BigQuery
*/
public static BQConnection connect(String propertiesFilePath, String extraUrl)
throws IOException, SQLException {
final Properties properties = new Properties();
final ClassLoader loader = ConnectionFromResources.class.getClassLoader();
try (InputStream stream = loader.getResourceAsStream(propertiesFilePath)) {
properties.load(stream);
}
final StringBuilder jdcbUrlBuilder =
new StringBuilder(BQSupportFuncts.constructUrlFromPropertiesFile(properties));
if (extraUrl != null) {
jdcbUrlBuilder.append(extraUrl);
}
final String jdbcUrl = jdcbUrlBuilder.toString();

final Connection connection = DriverManager.getConnection(jdbcUrl, properties);
final BQConnection bqConnection = (BQConnection) connection;
logger.info("Created connection from {} to {}", propertiesFilePath, bqConnection.getURLPART());
return bqConnection;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package net.starschema.clouddb.jdbc;

import java.io.IOException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import org.assertj.core.api.Assertions;
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.Mode;
import org.openjdk.jmh.annotations.Scope;
import org.openjdk.jmh.annotations.Setup;
import org.openjdk.jmh.annotations.State;
import org.openjdk.jmh.annotations.TearDown;
import org.openjdk.jmh.annotations.Threads;

/**
* Performance microbenchmark for stateless queries. This uses the example query from {@link
* StatelessQuery}, same as the tests.
*
* <p>Run with mvn exec:exec@benchmark-stateless - note that mvn install must have been run at least
* once before.
*/
@Fork(1)
@Threads(10)
@BenchmarkMode(Mode.Throughput)
public class StatelessSmallQueryBenchmark {
private static final String CONNECTION_PROPERTIES = "installedaccount1.properties";

@State(Scope.Thread)
public static class RequiredJob {
private BQConnection connection;

@Setup(Level.Trial)
public void connect() throws SQLException, IOException {
connection = ConnectionFromResources.connect(CONNECTION_PROPERTIES, null);
}

@TearDown
public void disconnect() throws SQLException {
connection.close();
}
}

@State(Scope.Thread)
public static class OptionalJob {
private BQConnection connection;

@Setup(Level.Trial)
public void connect() throws SQLException, IOException {
connection =
ConnectionFromResources.connect(
CONNECTION_PROPERTIES, "&jobcreationmode=JOB_CREATION_OPTIONAL");
}

@TearDown
public void disconnect() throws SQLException {
connection.close();
}
}

private String[][] benchmarkSmallQuery(final Connection connection) throws SQLException {
final Statement statement = connection.createStatement();
final ResultSet results = statement.executeQuery(StatelessQuery.exampleQuery());
final String[][] rows = BQSupportMethods.GetQueryResult(results);
Assertions.assertThat(rows).isEqualTo(StatelessQuery.exampleValues());
return rows;
}

@Benchmark
public String[][] benchmarkSmallQueryRequiredJob(final RequiredJob requiredJob)
throws SQLException {
return benchmarkSmallQuery(requiredJob.connection);
}

@Benchmark
public String[][] benchmarkSmallQueryOptionalJob(final OptionalJob optionalJob)
throws SQLException {
return benchmarkSmallQuery(optionalJob.connection);
}
}

0 comments on commit 71bc3a9

Please sign in to comment.