Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support stateless queries. #184

Merged
merged 6 commits into from
Dec 18, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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>
goomrw marked this conversation as resolved.
Show resolved Hide resolved
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);
}
}
Loading