use of com.zaxxer.hikari.HikariDataSource in project HikariCP by brettwooldridge.
the class PostgresTest method testCase4.
// @Test
public void testCase4() throws Exception {
HikariConfig config = newHikariConfig();
config.setMinimumIdle(0);
config.setMaximumPoolSize(15);
config.setConnectionTimeout(10000);
config.setIdleTimeout(TimeUnit.MINUTES.toMillis(1));
config.setMaxLifetime(TimeUnit.MINUTES.toMillis(2));
config.setRegisterMbeans(true);
config.setJdbcUrl("jdbc:postgresql://localhost:5432/netld");
config.setUsername("brettw");
try (final HikariDataSource ds = new HikariDataSource(config)) {
countdown(20);
List<Thread> threads = new ArrayList<>();
for (int i = 0; i < 20; i++) {
threads.add(new Thread() {
public void run() {
UtilityElf.quietlySleep((long) (Math.random() * 2500L));
final long start = currentTime();
do {
try (Connection conn = ds.getConnection();
Statement stmt = conn.createStatement()) {
try (ResultSet rs = stmt.executeQuery("SELECT * FROM device WHERE device_id=0 ORDER BY device_id LIMIT 1 OFFSET 0")) {
rs.next();
}
//Math.max(50L, (long)(Math.random() * 250L)));
UtilityElf.quietlySleep(100L);
} catch (SQLException e) {
e.printStackTrace();
// throw new RuntimeException(e);
}
// UtilityElf.quietlySleep(10L); //Math.max(50L, (long)(Math.random() * 250L)));
} while (elapsedMillis(start) < TimeUnit.MINUTES.toMillis(5));
}
});
}
// threads.forEach(t -> t.start());
// threads.forEach(t -> { try { t.join(); } catch (InterruptedException e) {} });
}
}
use of com.zaxxer.hikari.HikariDataSource in project HikariCP by brettwooldridge.
the class RampUpDown method rampUpDownTest.
@Test
public void rampUpDownTest() throws SQLException {
HikariConfig config = newHikariConfig();
config.setMinimumIdle(5);
config.setMaximumPoolSize(60);
config.setInitializationFailTimeout(0);
config.setConnectionTestQuery("VALUES 1");
config.setDataSourceClassName("com.zaxxer.hikari.mocks.StubDataSource");
System.setProperty("com.zaxxer.hikari.housekeeping.periodMs", "250");
try (HikariDataSource ds = new HikariDataSource(config)) {
ds.setIdleTimeout(1000);
HikariPool pool = getPool(ds);
// wait two housekeeping periods so we don't fail if this part of test runs too quickly
quietlySleep(500);
Assert.assertSame("Total connections not as expected", 5, pool.getTotalConnections());
Connection[] connections = new Connection[ds.getMaximumPoolSize()];
for (int i = 0; i < connections.length; i++) {
connections[i] = ds.getConnection();
}
assertSame("Total connections not as expected", 60, pool.getTotalConnections());
for (Connection connection : connections) {
connection.close();
}
quietlySleep(500);
assertSame("Total connections not as expected", 5, pool.getTotalConnections());
}
}
use of com.zaxxer.hikari.HikariDataSource in project HikariCP by brettwooldridge.
the class SaturatedPoolTest830 method saturatedPoolTest.
@Test
public void saturatedPoolTest() throws Exception {
HikariConfig config = newHikariConfig();
config.setMinimumIdle(5);
config.setMaximumPoolSize(MAX_POOL_SIZE);
config.setInitializationFailTimeout(Long.MAX_VALUE);
config.setConnectionTimeout(1000);
config.setDataSourceClassName("com.zaxxer.hikari.mocks.StubDataSource");
StubConnection.slowCreate = true;
StubStatement.setSimulatedQueryTime(1000);
setSlf4jLogLevel(HikariPool.class, Level.DEBUG);
System.setProperty("com.zaxxer.hikari.housekeeping.periodMs", "5000");
final long start = currentTime();
try (final HikariDataSource ds = new HikariDataSource(config)) {
LinkedBlockingQueue<Runnable> queue = new LinkedBlockingQueue<>();
ThreadPoolExecutor threadPool = new ThreadPoolExecutor(50, /*core*/
50, /*max*/
2, /*keepalive*/
SECONDS, queue, new ThreadPoolExecutor.CallerRunsPolicy());
threadPool.allowCoreThreadTimeOut(true);
AtomicInteger windowIndex = new AtomicInteger();
boolean[] failureWindow = new boolean[100];
Arrays.fill(failureWindow, true);
// Initial saturation
for (int i = 0; i < 50; i++) {
threadPool.execute(() -> {
try (Connection conn = ds.getConnection();
Statement stmt = conn.createStatement()) {
stmt.execute("SELECT bogus FROM imaginary");
} catch (SQLException e) {
LOGGER.info(e.getMessage());
}
});
}
long sleep = 80;
outer: while (true) {
quietlySleep(sleep);
if (elapsedMillis(start) > SECONDS.toMillis(12) && sleep < 100) {
sleep = 100;
LOGGER.warn("Switching to 100ms sleep");
} else if (elapsedMillis(start) > SECONDS.toMillis(6) && sleep < 90) {
sleep = 90;
LOGGER.warn("Switching to 90ms sleep");
}
threadPool.execute(() -> {
int ndx = windowIndex.incrementAndGet() % failureWindow.length;
try (Connection conn = ds.getConnection();
Statement stmt = conn.createStatement()) {
stmt.execute("SELECT bogus FROM imaginary");
failureWindow[ndx] = false;
} catch (SQLException e) {
LOGGER.info(e.getMessage());
failureWindow[ndx] = true;
}
});
for (int i = 0; i < failureWindow.length; i++) {
if (failureWindow[i]) {
if (elapsedMillis(start) % (SECONDS.toMillis(1) - sleep) < sleep) {
LOGGER.info("Active threads {}, submissions per second {}, waiting threads {}", threadPool.getActiveCount(), SECONDS.toMillis(1) / sleep, getPool(ds).getThreadsAwaitingConnection());
}
continue outer;
}
}
LOGGER.info("Timeouts have subsided.");
LOGGER.info("Active threads {}, submissions per second {}, waiting threads {}", threadPool.getActiveCount(), SECONDS.toMillis(1) / sleep, getPool(ds).getThreadsAwaitingConnection());
break;
}
LOGGER.info("Waiting for completion of {} active tasks.", threadPool.getActiveCount());
while (getPool(ds).getActiveConnections() > 0) {
quietlySleep(50);
}
assertEquals("Rate not in balance at 10req/s", SECONDS.toMillis(1) / sleep, 10L);
} finally {
StubStatement.setSimulatedQueryTime(0);
StubConnection.slowCreate = false;
System.clearProperty("com.zaxxer.hikari.housekeeping.periodMs");
setSlf4jLogLevel(HikariPool.class, Level.INFO);
}
}
use of com.zaxxer.hikari.HikariDataSource in project HikariCP by brettwooldridge.
the class ShutdownTest method testShutdown1.
@Test
public void testShutdown1() throws SQLException {
Assert.assertSame("StubConnection count not as expected", 0, StubConnection.count.get());
StubConnection.slowCreate = true;
HikariConfig config = newHikariConfig();
config.setMinimumIdle(0);
config.setMaximumPoolSize(10);
config.setInitializationFailTimeout(Long.MAX_VALUE);
config.setConnectionTestQuery("VALUES 1");
config.setDataSourceClassName("com.zaxxer.hikari.mocks.StubDataSource");
try (HikariDataSource ds = new HikariDataSource(config)) {
HikariPool pool = getPool(ds);
Thread[] threads = new Thread[10];
for (int i = 0; i < 10; i++) {
threads[i] = new Thread() {
@Override
public void run() {
try {
if (ds.getConnection() != null) {
quietlySleep(SECONDS.toMillis(1));
}
} catch (SQLException e) {
}
}
};
threads[i].setDaemon(true);
}
for (int i = 0; i < 10; i++) {
threads[i].start();
}
quietlySleep(1800L);
assertTrue("Total connection count not as expected, ", pool.getTotalConnections() > 0);
ds.close();
assertSame("Active connection count not as expected, ", 0, pool.getActiveConnections());
assertSame("Idle connection count not as expected, ", 0, pool.getIdleConnections());
assertSame("Total connection count not as expected, ", 0, pool.getTotalConnections());
assertTrue(ds.isClosed());
}
}
use of com.zaxxer.hikari.HikariDataSource in project HikariCP by brettwooldridge.
the class ShutdownTest method testShutdown3.
@Test
public void testShutdown3() throws SQLException {
assertSame("StubConnection count not as expected", 0, StubConnection.count.get());
StubConnection.slowCreate = false;
HikariConfig config = newHikariConfig();
config.setMinimumIdle(5);
config.setMaximumPoolSize(5);
config.setInitializationFailTimeout(0);
config.setConnectionTestQuery("VALUES 1");
config.setDataSourceClassName("com.zaxxer.hikari.mocks.StubDataSource");
try (HikariDataSource ds = new HikariDataSource(config)) {
HikariPool pool = getPool(ds);
quietlySleep(1200L);
assertTrue("Total connection count not as expected, ", pool.getTotalConnections() == 5);
ds.close();
assertSame("Active connection count not as expected, ", 0, pool.getActiveConnections());
assertSame("Idle connection count not as expected, ", 0, pool.getIdleConnections());
assertSame("Total connection count not as expected, ", 0, pool.getTotalConnections());
}
}
Aggregations