diff --git a/pom.xml b/pom.xml index 8c9929996..6b36f4683 100644 --- a/pom.xml +++ b/pom.xml @@ -236,6 +236,11 @@ jackson-dataformat-yaml 2.13.4 + + com.zaxxer + HikariCP + 5.0.1 + org.hsqldb hsqldb diff --git a/src/main/java/com/oltpbenchmark/ThreadBench.java b/src/main/java/com/oltpbenchmark/ThreadBench.java index 568902fec..96409d4e1 100644 --- a/src/main/java/com/oltpbenchmark/ThreadBench.java +++ b/src/main/java/com/oltpbenchmark/ThreadBench.java @@ -97,7 +97,7 @@ private int finalizeWorkers(ArrayList workerThreads) throws InterruptedE workers.get(i).tearDown(); } - + this.workers.get(0).getBenchmark().closeDataSource(); return requests; } diff --git a/src/main/java/com/oltpbenchmark/api/BenchmarkModule.java b/src/main/java/com/oltpbenchmark/api/BenchmarkModule.java index 5c4762d71..2751c185d 100644 --- a/src/main/java/com/oltpbenchmark/api/BenchmarkModule.java +++ b/src/main/java/com/oltpbenchmark/api/BenchmarkModule.java @@ -28,6 +28,8 @@ import org.apache.commons.lang3.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import com.zaxxer.hikari.HikariConfig; +import com.zaxxer.hikari.HikariDataSource; import java.io.IOException; import java.io.InputStream; @@ -73,6 +75,14 @@ public abstract class BenchmarkModule { public BenchmarkModule(WorkloadConfiguration workConf) { this.workConf = workConf; this.dialects = new StatementDialects(workConf); + if (workConf.getXmlConfig().getBoolean("use_hikari_pool", false)) { + try { + createDataSource(); + } catch (Exception e) { + LOG.error("Failed to create Data Source", e); + throw e; + } + } } // -------------------------------------------------------------------------- @@ -80,8 +90,9 @@ public BenchmarkModule(WorkloadConfiguration workConf) { // -------------------------------------------------------------------------- public final Connection makeConnection() throws SQLException { - - if (StringUtils.isEmpty(workConf.getUsername())) { + if (workConf.getXmlConfig().getBoolean("use_hikari_pool", false)) { + return hikariDataSource.getConnection(); + } else if (StringUtils.isEmpty(workConf.getUsername())) { return DriverManager.getConnection(workConf.getUrl()); } else { return DriverManager.getConnection( @@ -91,6 +102,28 @@ public final Connection makeConnection() throws SQLException { } } + protected HikariDataSource hikariDataSource; + + public void createDataSource() { + HikariConfig config = new HikariConfig(); + config.setJdbcUrl(workConf.getUrl()); + if (!StringUtils.isEmpty(workConf.getUsername())) { + config.setUsername(workConf.getUsername()); + config.setPassword(workConf.getPassword()); + /* take max_pool_size from xmlConfig, default is = number of terminals*/ + config.setMaximumPoolSize(workConf.getXmlConfig().getInt("max_pool_size", workConf.getTerminals())); + config.setMinimumIdle(5); + } + config.setTransactionIsolation(workConf.getIsolationString()); + hikariDataSource = new HikariDataSource(config); + } + + public void closeDataSource() { + if (workConf.getXmlConfig().getBoolean("use_hikari_pool", false)) { + this.hikariDataSource.close(); + } + } + // -------------------------------------------------------------------------- // IMPLEMENTING CLASS INTERFACE // -------------------------------------------------------------------------- diff --git a/src/main/java/com/oltpbenchmark/api/Worker.java b/src/main/java/com/oltpbenchmark/api/Worker.java index de19b6a92..4ea5e15da 100644 --- a/src/main/java/com/oltpbenchmark/api/Worker.java +++ b/src/main/java/com/oltpbenchmark/api/Worker.java @@ -65,6 +65,9 @@ public abstract class Worker implements Runnable { private boolean seenDone = false; public final FeaturebenchAdditionalResults featurebenchAdditionalResults = new FeaturebenchAdditionalResults(); + public boolean usingHikari = false; + public boolean autoCommitVal = false; + public Worker(T benchmark, int id) { this.id = id; this.benchmark = benchmark; @@ -72,13 +75,14 @@ public Worker(T benchmark, int id) { this.workloadState = this.configuration.getWorkloadState(); this.currStatement = null; this.transactionTypes = this.configuration.getTransTypes(); - boolean autoCommitVal = false; + this.autoCommitVal = false; + this.usingHikari = this.getWorkloadConfiguration().getXmlConfig().getBoolean("use_hikari_pool", false); if (this.benchmark.getBenchmarkName().equalsIgnoreCase("featurebench") && this.benchmark.getWorkloadConfiguration().getXmlConfig().containsKey("microbenchmark/properties/setAutoCommit")) { autoCommitVal = this.benchmark.getWorkloadConfiguration().getXmlConfig().getBoolean("microbenchmark/properties/setAutoCommit"); this.isFeaturebenchWorkload = true; } - if (!this.configuration.getNewConnectionPerTxn()) { + if (!this.configuration.getNewConnectionPerTxn() && !usingHikari) { try { this.conn = this.benchmark.makeConnection(); this.conn.setAutoCommit(autoCommitVal); @@ -198,7 +202,16 @@ public final void run() { // Invoke initialize callback try { - this.initialize(); + if (!this.usingHikari) + this.initialize(); + else { + if(this.conn == null || this.conn.isClosed()) { + this.conn = this.getBenchmark().makeConnection(); + this.conn.setAutoCommit(this.autoCommitVal); + this.initialize(); + this.conn.close(); + } + } } catch (Throwable ex) { throw new RuntimeException("Unexpected error when initializing " + this, ex); } @@ -282,11 +295,27 @@ public final void run() { } } - long start = System.nanoTime(); - - doWork(configuration.getDatabaseType(), transactionType); - - long end = System.nanoTime(); + long start = 0; + long end = 0; + if (!this.usingHikari) { + start = System.nanoTime(); + doWork(configuration.getDatabaseType(), transactionType); + end = System.nanoTime(); + } + else { + try { + if (this.conn == null || this.conn.isClosed()) { + this.conn = this.getBenchmark().makeConnection(); + this.conn.setAutoCommit(this.autoCommitVal); + start = System.nanoTime(); + doWork(configuration.getDatabaseType(), transactionType); + end = System.nanoTime(); + this.conn.close(); + } + } catch(SQLException ex) { + throw new RuntimeException("Failed to connect to database", ex); + } + } // PART 4: Record results diff --git a/src/main/java/com/oltpbenchmark/benchmarks/featurebench/FeatureBenchWorker.java b/src/main/java/com/oltpbenchmark/benchmarks/featurebench/FeatureBenchWorker.java index b6c8ae1c9..0db3ed014 100644 --- a/src/main/java/com/oltpbenchmark/benchmarks/featurebench/FeatureBenchWorker.java +++ b/src/main/java/com/oltpbenchmark/benchmarks/featurebench/FeatureBenchWorker.java @@ -284,6 +284,16 @@ protected TransactionStatus executeWork(Connection conn, TransactionType txnType @Override public void tearDown() { + if (this.usingHikari) { + try { + if (this.conn == null || this.conn.isClosed()) { + this.conn = this.getBenchmark().makeConnection(); + this.conn.setAutoCommit(true); + } + } catch (SQLException ex) { + throw new RuntimeException("Failed to connect to database", ex); + } + } synchronized (FeatureBenchWorker.class) { if (!this.configuration.getNewConnectionPerTxn() && this.configuration.getWorkloadState().getGlobalState() == State.EXIT && !isPGStatStatementCollected.get()) { @@ -349,6 +359,15 @@ public void tearDown() { } } } + if(usingHikari) { + try { + if (this.conn != null && !this.conn.isClosed()) { + this.conn.close(); + } + } catch (SQLException ex) { + throw new RuntimeException("Failed to connect to database", ex); + } + } } private JSONObject callPGStats() throws SQLException{