Skip to content

Instantly share code, notes, and snippets.

@apangin
Created October 18, 2015 22:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save apangin/436220e9a462a7d65810 to your computer and use it in GitHub Desktop.
Save apangin/436220e9a462a7d65810 to your computer and use it in GitHub Desktop.
DB connection pool benchmark
Benchmark (jdbcUrl) (maxPoolSize) (pool) Mode Cnt Score Error Units
ConnectionBench.cycleConnection jdbc:stub 32 hikari thrpt 5 11,149 ± 0,674 ops/us
ConnectionBench.cycleConnection jdbc:stub 32 odkl thrpt 5 39,349 ± 2,714 ops/us
StatementBench.cycleStatement jdbc:stub 32 hikari thrpt 5 48,113 ± 0,488 ops/us
StatementBench.cycleStatement jdbc:stub 32 odkl thrpt 5 74,588 ± 0,814 ops/us
/*
* Copyright (C) 2014 Brett Wooldridge, Andrei Pangin
*
* Licensed 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 com.zaxxer.hikari.benchmark;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;
import javax.sql.DataSource;
import one.datasource.DataSourceImpl;
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.TearDown;
import org.openjdk.jmh.infra.BenchmarkParams;
import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
@State(Scope.Benchmark)
public class BenchBase
{
protected static final int MIN_POOL_SIZE = 0;
@Param({ "hikari", "odkl" })
public String pool;
@Param({ "32" })
public int maxPoolSize;
@Param({ "jdbc:stub" })
public String jdbcUrl;
public static volatile DataSource DS;
@Setup
public void setup(BenchmarkParams params)
{
try
{
Class.forName("com.zaxxer.hikari.benchmark.stubs.StubDriver");
System.err.printf("Using driver (%s): %s", jdbcUrl, DriverManager.getDriver(jdbcUrl));
}
catch (Exception e)
{
throw new RuntimeException(e);
}
if (this.getClass().getName().contains("Statement")) {
System.err.println("# Overriding maxPoolSize paramter for StatementBench: maxPoolSize=" + params.getThreads());
maxPoolSize = params.getThreads();
}
switch (pool)
{
case "hikari":
setupHikari();
break;
case "odkl":
setupOdkl();
break;
}
}
@TearDown
public void teardown() throws SQLException
{
switch (pool)
{
case "hikari":
((HikariDataSource) DS).close();
break;
case "odkl":
((DataSourceImpl) DS).close();
break;
}
}
protected void setupHikari()
{
HikariConfig config = new HikariConfig();
config.setJdbcUrl(jdbcUrl);
config.setUsername("brettw");
config.setPassword("");
config.setMinimumIdle(MIN_POOL_SIZE);
config.setMaximumPoolSize(maxPoolSize);
config.setConnectionTimeout(8000);
config.setAutoCommit(false);
DS = new HikariDataSource(config);
}
protected void setupOdkl()
{
Properties props = new Properties();
props.setProperty("driver", "com.zaxxer.hikari.benchmark.stubs.StubDriver");
props.setProperty("pool-size", Integer.toString(maxPoolSize));
DS = new DataSourceImpl("Stub", props);
}
}
/*
* Copyright (C) 2014 Brett Wooldridge, Andrei Pangin
*
* Licensed 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 com.zaxxer.hikari.benchmark;
import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.Scope;
import org.openjdk.jmh.annotations.State;
import java.sql.Connection;
import java.sql.SQLException;
@State(Scope.Thread)
public class ConnectionBench extends BenchBase {
@Benchmark
public Connection cycleConnection() throws SQLException {
Connection connection = DS.getConnection();
connection.close();
return connection;
}
}
/*
* Copyright (C) 2014 Brett Wooldridge, Andrei Pangin
*
* Licensed 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 com.zaxxer.hikari.benchmark;
import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.Level;
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.infra.Blackhole;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
@State(Scope.Thread)
public class StatementBench extends BenchBase {
@Benchmark
public Statement cycleStatement(ConnectionState state) throws SQLException {
Statement statement = state.connection.createStatement();
state.consume(statement.execute("INSERT INTO test (column) VALUES (?)"));
statement.close();
return statement;
}
@State(Scope.Thread)
public static class ConnectionState extends Blackhole {
Connection connection;
@Setup(Level.Iteration)
public void setup() throws SQLException {
connection = DS.getConnection();
}
@TearDown(Level.Iteration)
public void teardown() throws SQLException {
connection.close();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment