Debian Codegraph @

src/test/java/com/zaxxer/hikari/pool/ConnectionPoolSizeVsThreadsTest.java

raw
1 /*
2 * Copyright (C) 2013, 2017 Brett Wooldridge
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 package com.zaxxer.hikari.pool;
18
19 import static com.zaxxer.hikari.pool.TestElf.getPool;
20 import static com.zaxxer.hikari.pool.TestElf.newHikariConfig;
21 import static com.zaxxer.hikari.util.ClockSource.currentTime;
22 import static com.zaxxer.hikari.util.ClockSource.elapsedMillis;
23 import static com.zaxxer.hikari.util.UtilityElf.quietlySleep;
24 import static java.util.concurrent.Executors.newFixedThreadPool;
25 import static java.util.concurrent.TimeUnit.SECONDS;
26 import static org.junit.Assert.assertEquals;
27 import static org.junit.Assert.assertTrue;
28 import static org.junit.Assert.fail;
29
30 import java.sql.Connection;
31 import java.util.concurrent.CountDownLatch;
32 import java.util.concurrent.ExecutorService;
33 import java.util.concurrent.atomic.AtomicReference;
34
35 import org.junit.Test;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
38
39 import com.zaxxer.hikari.HikariConfig;
40 import com.zaxxer.hikari.HikariDataSource;
41 import com.zaxxer.hikari.mocks.StubDataSource;
42
43 /**
44 * @author Matthew Tambara ([email protected])
45 */
46 public class ConnectionPoolSizeVsThreadsTest {
47
48 private static final Logger LOGGER = LoggerFactory.getLogger(ConnectionPoolSizeVsThreadsTest.class);
49
50 private static final int ITERATIONS = 50_000;
51
52 @Test
53 public void testPoolSizeAboutSameSizeAsThreadCount() throws Exception {
54 final int threadCount = 50;
55 final Counts counts = testPoolSize(2 /*minIdle*/,
56 100 /*maxPoolSize*/,
57 threadCount,
58 1 /*workTimeMs*/,
59 0 /*restTimeMs*/,
60 20 /*connectionAcquisitionTimeMs*/,
61 ITERATIONS,
62 SECONDS.toMillis(2) /*postTestTimeMs*/);
63
64 // maxActive may never make it to threadCount but it shouldn't be any higher
65 assertEquals(threadCount, counts.maxActive, 15 /*delta*/);
66 assertEquals(threadCount, counts.maxTotal, 5 /*delta*/);
67 }
68
69 @Test
70 public void testSlowConnectionTimeBurstyWork() throws Exception {
71 // setup a bursty work load, 50 threads all needing to do around 100 units of work.
72 // Using a more realistic time for connection startup of 250 ms and only 5 seconds worth of work will mean that we end up finishing
73 // all of the work before we actually have setup 50 connections even though we have requested 50 connections
74 final int threadCount = 50;
75 final int workItems = threadCount * 100;
76 final int workTimeMs = 0;
77 final int connectionAcquisitionTimeMs = 250;
78 final Counts counts = testPoolSize(2 /*minIdle*/,
79 100 /*maxPoolSize*/,
80 threadCount,
81 workTimeMs,
82 0 /*restTimeMs*/,
83 connectionAcquisitionTimeMs,
84 workItems /*iterations*/,
85 SECONDS.toMillis(3) /*postTestTimeMs*/);
86
87 // hard to put exact bounds on how many thread we will use but we can put an upper bound on usage (if there was only one thread)
88 final long totalWorkTime = workItems * workTimeMs;
89 final long connectionMax = totalWorkTime / connectionAcquisitionTimeMs;
90 assertTrue(connectionMax <= counts.maxActive);
91 assertEquals(connectionMax, counts.maxTotal, 2 + 2 /*delta*/);
92 }
93
94 private Counts testPoolSize(final int minIdle, final int maxPoolSize, final int threadCount,
95 final long workTimeMs, final long restTimeMs, final long connectionAcquisitionTimeMs,
96 final int iterations, final long postTestTimeMs) throws Exception {
97
98 LOGGER.info("Starting test (minIdle={}, maxPoolSize={}, threadCount={}, workTimeMs={}, restTimeMs={}, connectionAcquisitionTimeMs={}, iterations={}, postTestTimeMs={})",
99 minIdle, maxPoolSize, threadCount, workTimeMs, restTimeMs, connectionAcquisitionTimeMs, iterations, postTestTimeMs);
100
101 final HikariConfig config = newHikariConfig();
102 config.setMinimumIdle(minIdle);
103 config.setMaximumPoolSize(maxPoolSize);
104 config.setInitializationFailTimeout(Long.MAX_VALUE);
105 config.setConnectionTimeout(2500);
106 config.setDataSourceClassName("com.zaxxer.hikari.mocks.StubDataSource");
107
108 final AtomicReference<Exception> ref = new AtomicReference<>(null);
109
110 // Initialize HikariPool with no initial connections and room to grow
111 try (final HikariDataSource ds = new HikariDataSource(config)) {
112 final StubDataSource stubDataSource = ds.unwrap(StubDataSource.class);
113 // connection acquisition takes more than 0 ms in a real system
114 stubDataSource.setConnectionAcquistionTime(connectionAcquisitionTimeMs);
115
116 final ExecutorService threadPool = newFixedThreadPool(threadCount);
117 final CountDownLatch allThreadsDone = new CountDownLatch(iterations);
118 for (int i = 0; i < iterations; i++) {
119 threadPool.submit(() -> {
120 if (ref.get() == null) {
121 quietlySleep(restTimeMs);
122 try (Connection c2 = ds.getConnection()) {
123 quietlySleep(workTimeMs);
124 }
125 catch (Exception e) {
126 ref.set(e);
127 }
128 }
129 allThreadsDone.countDown();
130 });
131 }
132
133 final HikariPool pool = getPool(ds);
134
135 // collect pool usage data while work is still being done
136 final Counts underLoad = new Counts();
137 while (allThreadsDone.getCount() > 0 || pool.getTotalConnections() < minIdle) {
138 quietlySleep(50);
139 underLoad.updateMaxCounts(pool);
140 }
141
142 // wait for long enough any pending acquisitions have already been done
143 LOGGER.info("Test Over, waiting for post delay time {}ms ", postTestTimeMs);
144 quietlySleep(connectionAcquisitionTimeMs + workTimeMs + restTimeMs);
145
146 // collect pool data while there is no work to do.
147 final Counts postLoad = new Counts();
148 final long start = currentTime();
149 while (elapsedMillis(start) < postTestTimeMs) {
150 quietlySleep(50);
151 postLoad.updateMaxCounts(pool);
152 }
153
154 allThreadsDone.await();
155
156 threadPool.shutdown();
157 threadPool.awaitTermination(30, SECONDS);
158
159 if (ref.get() != null) {
160 LOGGER.error("Task failed", ref.get());
161 fail("Task failed");
162 }
163
164 LOGGER.info("Under Load... {}", underLoad);
165 LOGGER.info("Post Load.... {}", postLoad);
166
167 // verify that the no connections created after the work has stopped
168 if (postTestTimeMs > 0) {
169 if (postLoad.maxActive != 0) {
170 fail("Max Active was greater than 0 after test was done");
171 }
172
173 final int createdAfterWorkAllFinished = postLoad.maxTotal - underLoad.maxTotal;
174 assertEquals("Connections were created when there was no waiting consumers", 0, createdAfterWorkAllFinished, 1 /*delta*/);
175 }
176
177 return underLoad;
178 }
179 }
180
181 private static class Counts {
182 int maxTotal = 0;
183 int maxActive = 0;
184
185 void updateMaxCounts(final HikariPool pool) {
186 maxTotal = Math.max(pool.getTotalConnections(), maxTotal);
187 maxActive = Math.max(pool.getActiveConnections(), maxActive);
188 }
189
190 @Override
191 public String toString() {
192 return "Counts{" +
193 "maxTotal=" + maxTotal +
194 ", maxActive=" + maxActive +
195 '}';
196 }
197 }
198 }