com.zaxxer.hikari.benchmark.BenchBase.java Source code

Java tutorial

Introduction

Here is the source code for com.zaxxer.hikari.benchmark.BenchBase.java

Source

/*
 * Copyright (C) 2014 Brett Wooldridge
 *
 * 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.Connection;
import java.util.concurrent.TimeUnit;

import javax.sql.DataSource;

import org.apache.tomcat.jdbc.pool.PoolProperties;
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.vibur.dbcp.ViburDBCPDataSource;

import com.jolbox.bonecp.BoneCPConfig;
import com.jolbox.bonecp.BoneCPDataSource;
import com.mchange.v2.c3p0.ComboPooledDataSource;
import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
import java.sql.SQLException;
import org.apache.commons.dbcp2.BasicDataSource;
import org.apache.commons.dbcp2.DriverManagerConnectionFactory;
import org.apache.commons.dbcp2.PoolingDataSource;

@State(Scope.Benchmark)
public class BenchBase {
    protected static final int MIN_POOL_SIZE = 0;

    @Param({ "hikari", "bone", "tomcat", "c3p0", "vibur", "dbcp2", "dbcp2-basic" })
    public String pool;

    @Param({ "32", "100", "500", "1000" })
    public int maxPoolSize;

    public static volatile DataSource DS;

    @Setup
    public void setup() throws SQLException {
        try {
            Class.forName(dbDriver);
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }

        switch (pool) {
        case "hikari":
            setupHikari();
            break;
        case "bone":
            setupBone();
            break;
        case "tomcat":
            setupTomcat();
            break;
        case "c3p0":
            setupC3P0();
            break;
        case "vibur":
            setupVibur();
            break;
        case "dbcp2":
            setupDBCP2();
            break;
        case "dbcp2-basic":
            setupDBCP2basic();
            break;
        }
    }

    @TearDown
    public void teardown() throws SQLException {
        switch (pool) {
        case "hikari":
            ((HikariDataSource) DS).shutdown();
            break;
        case "bone":
            ((BoneCPDataSource) DS).close();
            break;
        case "tomcat":
            ((org.apache.tomcat.jdbc.pool.DataSource) DS).close();
            break;
        case "c3p0":
            ((ComboPooledDataSource) DS).close();
            break;
        case "vibur":
            ((ViburDBCPDataSource) DS).terminate();
        case "dbcp2-basic":
            ((BasicDataSource) DS).close();
        case "dbcp2":
            // do nothing 
        }

    }

    protected void setupTomcat() {
        PoolProperties props = new PoolProperties();
        props.setUrl(jdbcURL);
        props.setDriverClassName(dbDriver);
        props.setUsername("sa");
        props.setPassword("");
        props.setInitialSize(MIN_POOL_SIZE);
        props.setMinIdle(MIN_POOL_SIZE);
        props.setMaxIdle(maxPoolSize);
        props.setMaxActive(maxPoolSize);
        props.setMaxWait(8000);
        props.setDefaultAutoCommit(false);
        props.setRollbackOnReturn(true);
        props.setMinEvictableIdleTimeMillis((int) TimeUnit.MINUTES.toMillis(30));
        props.setTestOnBorrow(true);
        props.setDefaultTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED);
        props.setValidationQuery("VALUES 1");
        props.setJdbcInterceptors("ConnectionState");

        DS = new org.apache.tomcat.jdbc.pool.DataSource(props);
    }

    private final String jdbcURL = "jdbc:stub";
    private final String dbDriver = "com.zaxxer.hikari.benchmark.stubs.StubDriver";

    //    private final String jdbcURL = "jdbc:h2:~/test";
    //    private final String dbDriver = "org.h2.Driver";

    protected void setupBone() {
        BoneCPConfig config = new BoneCPConfig();
        config.setAcquireIncrement(1);
        config.setMinConnectionsPerPartition(MIN_POOL_SIZE);
        config.setMaxConnectionsPerPartition(maxPoolSize);
        config.setConnectionTimeoutInMs(8000);
        config.setIdleMaxAgeInMinutes(30);
        config.setConnectionTestStatement("VALUES 1");
        config.setCloseOpenStatements(true);
        config.setDisableConnectionTracking(true);
        config.setDefaultAutoCommit(false);
        config.setResetConnectionOnClose(true);
        config.setDefaultTransactionIsolation("READ_COMMITTED");
        config.setDisableJMX(true);
        config.setJdbcUrl(jdbcURL);
        config.setUsername("nobody");
        config.setPassword("nopass");
        config.setPoolStrategy("CACHED");

        DS = new BoneCPDataSource(config);
    }

    protected void setupHikari() {
        HikariConfig config = new HikariConfig();
        config.setMinimumIdle(MIN_POOL_SIZE);
        config.setMaximumPoolSize(maxPoolSize);
        config.setConnectionTimeout(8000);
        config.setIdleTimeout(TimeUnit.MINUTES.toMillis(30));
        config.setJdbc4ConnectionTest(true);
        config.setAutoCommit(false);
        config.setTransactionIsolation("TRANSACTION_READ_COMMITTED");
        config.setDataSourceClassName("com.zaxxer.hikari.benchmark.stubs.StubDataSource");

        DS = new HikariDataSource(config);
    }

    protected void setupC3P0() {
        try {
            ComboPooledDataSource cpds = new ComboPooledDataSource();
            cpds.setDriverClass(dbDriver);
            cpds.setJdbcUrl(jdbcURL);
            cpds.setAcquireIncrement(1);
            cpds.setInitialPoolSize(MIN_POOL_SIZE);
            cpds.setMinPoolSize(MIN_POOL_SIZE);
            cpds.setMaxPoolSize(maxPoolSize);
            cpds.setCheckoutTimeout(8000);
            cpds.setLoginTimeout(8);
            cpds.setTestConnectionOnCheckout(true);
            cpds.setPreferredTestQuery("VALUES 1");

            DS = cpds;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    private void setupVibur() {
        ViburDBCPDataSource vibur = new ViburDBCPDataSource();
        vibur.setJdbcUrl(jdbcURL);
        vibur.setPoolInitialSize(MIN_POOL_SIZE);
        vibur.setPoolMaxSize(maxPoolSize);
        vibur.setTestConnectionQuery("VALUES 1");
        vibur.setDefaultAutoCommit(false);
        vibur.setResetDefaultsAfterUse(true);
        vibur.setConnectionIdleLimitInSeconds(1);
        vibur.setDefaultTransactionIsolation("READ_COMMITTED");
        vibur.start();

        DS = vibur;
    }

    private void setupDBCP2() {
        org.apache.commons.pool2.impl.GenericObjectPool<org.apache.commons.dbcp2.PoolableConnection> connectionPool;
        DriverManagerConnectionFactory connectionFactory = new DriverManagerConnectionFactory(jdbcURL, "sa", "");

        // Wrap the connections and statements with pooled variants
        org.apache.commons.dbcp2.PoolableConnectionFactory poolableCF = null;
        poolableCF = new org.apache.commons.dbcp2.PoolableConnectionFactory(connectionFactory, null);

        poolableCF.setValidationQuery("VALUES 1");
        poolableCF.setDefaultTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED);
        poolableCF.setDefaultAutoCommit(false);
        poolableCF.setRollbackOnReturn(true);

        // Create the actual pool of connections, and apply any properties
        connectionPool = new org.apache.commons.pool2.impl.GenericObjectPool(poolableCF);
        connectionPool.setTestOnBorrow(true);
        connectionPool.setMaxIdle(maxPoolSize);

        connectionPool.setMinIdle(MIN_POOL_SIZE);
        connectionPool.setMaxTotal(maxPoolSize);
        connectionPool.setMaxWaitMillis(8000);
        connectionPool.setMinEvictableIdleTimeMillis((int) TimeUnit.MINUTES.toMillis(30));
        poolableCF.setPool(connectionPool);
        DS = new org.apache.commons.dbcp2.PoolingDataSource(connectionPool);
    }

    private void setupDBCP2basic() throws SQLException {
        BasicDataSource ds = new BasicDataSource();
        ds.setDriverClassName(dbDriver);
        ds.setUsername("sa");
        ds.setPassword("");
        ds.setUrl(jdbcURL);
        ds.setMaxTotal(maxPoolSize);
        ds.setDefaultAutoCommit(false);
        ds.setDefaultTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED);

        ds.getConnection().createStatement().execute("CREATE TABLE IF NOT EXISTS test (column varchar);");
        DS = ds;
    }
}