Debian Codegraph @

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

raw
1 /*
2 * Copyright (C) 2013, 2014 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.newHikariConfig;
20 import static com.zaxxer.hikari.pool.TestElf.newHikariDataSource;
21 import static com.zaxxer.hikari.util.UtilityElf.quietlySleep;
22 import static org.junit.Assert.assertEquals;
23 import static org.junit.Assert.assertTrue;
24 import static org.junit.Assert.fail;
25 import static org.junit.Assume.assumeFalse;
26
27 import java.sql.Connection;
28 import java.sql.SQLException;
29 import java.util.SortedMap;
30 import java.util.concurrent.TimeUnit;
31
32 import org.junit.Test;
33
34 import com.codahale.metrics.Histogram;
35 import com.codahale.metrics.Metric;
36 import com.codahale.metrics.MetricFilter;
37 import com.codahale.metrics.MetricRegistry;
38 import com.codahale.metrics.Timer;
39 import com.codahale.metrics.health.HealthCheck.Result;
40 import com.codahale.metrics.health.HealthCheckRegistry;
41 import com.zaxxer.hikari.HikariConfig;
42 import com.zaxxer.hikari.HikariDataSource;
43 import com.zaxxer.hikari.metrics.MetricsTrackerFactory;
44 import com.zaxxer.hikari.metrics.dropwizard.CodahaleMetricsTrackerFactory;
45 import com.zaxxer.hikari.util.UtilityElf;
46
47 /**
48 * Test HikariCP/CodaHale metrics integration.
49 *
50 * @author Brett Wooldridge
51 */
52 public class TestMetrics
53 {
54 @Test
55 public void testMetricWait() throws SQLException
56 {
57 MetricRegistry metricRegistry = new MetricRegistry();
58
59 HikariConfig config = newHikariConfig();
60 config.setMinimumIdle(1);
61 config.setMaximumPoolSize(1);
62 config.setMetricRegistry(metricRegistry);
63 config.setInitializationFailTimeout(Long.MAX_VALUE);
64 config.setDataSourceClassName("com.zaxxer.hikari.mocks.StubDataSource");
65
66 try (HikariDataSource ds = new HikariDataSource(config)) {
67 ds.getConnection().close();
68
69 Timer timer = metricRegistry.getTimers(new MetricFilter() {
70 /** {@inheritDoc} */
71 @Override
72 public boolean matches(String name, Metric metric)
73 {
74 return "testMetricWait.pool.Wait".equals(MetricRegistry.name("testMetricWait", "pool", "Wait"));
75 }
76 }).values().iterator().next();
77
78 assertEquals(1, timer.getCount());
79 assertTrue(timer.getMeanRate() > 0.0);
80 }
81 }
82
83 @Test
84 public void testMetricUsage() throws SQLException
85 {
86 assumeFalse(System.getProperty("os.name").contains("Windows"));
87 MetricRegistry metricRegistry = new MetricRegistry();
88
89 HikariConfig config = newHikariConfig();
90 config.setMinimumIdle(1);
91 config.setMaximumPoolSize(1);
92 config.setMetricRegistry(metricRegistry);
93 config.setInitializationFailTimeout(0);
94 config.setDataSourceClassName("com.zaxxer.hikari.mocks.StubDataSource");
95
96 try (HikariDataSource ds = new HikariDataSource(config)) {
97 try (Connection connection = ds.getConnection()) {
98 UtilityElf.quietlySleep(250L);
99 }
100
101 Histogram histo = metricRegistry.getHistograms(new MetricFilter() {
102 /** {@inheritDoc} */
103 @Override
104 public boolean matches(String name, Metric metric)
105 {
106 return name.equals(MetricRegistry.name("testMetricUsage", "pool", "Usage"));
107 }
108 }).values().iterator().next();
109
110 assertEquals(1, histo.getCount());
111 double seventyFifth = histo.getSnapshot().get75thPercentile();
112 assertTrue("Seventy-fith percentile less than 250ms: " + seventyFifth, seventyFifth >= 250.0);
113 }
114 }
115
116 @Test
117 public void testHealthChecks() throws Exception
118 {
119 MetricRegistry metricRegistry = new MetricRegistry();
120 HealthCheckRegistry healthRegistry = new HealthCheckRegistry();
121
122 HikariConfig config = newHikariConfig();
123 config.setMaximumPoolSize(10);
124 config.setMetricRegistry(metricRegistry);
125 config.setHealthCheckRegistry(healthRegistry);
126 config.setDataSourceClassName("com.zaxxer.hikari.mocks.StubDataSource");
127 config.addHealthCheckProperty("connectivityCheckTimeoutMs", "1000");
128 config.addHealthCheckProperty("expected99thPercentileMs", "100");
129
130 try (HikariDataSource ds = new HikariDataSource(config)) {
131 quietlySleep(TimeUnit.SECONDS.toMillis(2));
132
133 try (Connection connection = ds.getConnection()) {
134 // close immediately
135 }
136
137 try (Connection connection = ds.getConnection()) {
138 // close immediately
139 }
140
141 SortedMap<String, Result> healthChecks = healthRegistry.runHealthChecks();
142
143 Result connectivityResult = healthChecks.get("testHealthChecks.pool.ConnectivityCheck");
144 assertTrue(connectivityResult.isHealthy());
145
146 Result slaResult = healthChecks.get("testHealthChecks.pool.Connection99Percent");
147 assertTrue(slaResult.isHealthy());
148 }
149 }
150
151 @Test
152 public void testSetters1() throws Exception
153 {
154 try (HikariDataSource ds = newHikariDataSource()) {
155 ds.setMaximumPoolSize(1);
156 ds.setDataSourceClassName("com.zaxxer.hikari.mocks.StubDataSource");
157
158 MetricRegistry metricRegistry = new MetricRegistry();
159 HealthCheckRegistry healthRegistry = new HealthCheckRegistry();
160
161 try {
162 try (Connection connection = ds.getConnection()) {
163 // close immediately
164 }
165
166 // After the pool as started, we can only set them once...
167 ds.setMetricRegistry(metricRegistry);
168 ds.setHealthCheckRegistry(healthRegistry);
169
170 // and never again...
171 ds.setMetricRegistry(metricRegistry);
172 fail("Should not have been allowed to set registry after pool started");
173 }
174 catch (IllegalStateException ise) {
175 // pass
176 try {
177 ds.setHealthCheckRegistry(healthRegistry);
178 fail("Should not have been allowed to set registry after pool started");
179 }
180 catch (IllegalStateException ise2) {
181 // pass
182 }
183 }
184 }
185 }
186
187 @Test
188 public void testSetters2() throws Exception
189 {
190 try (HikariDataSource ds = newHikariDataSource()) {
191 ds.setMaximumPoolSize(1);
192 ds.setDataSourceClassName("com.zaxxer.hikari.mocks.StubDataSource");
193
194 MetricRegistry metricRegistry = new MetricRegistry();
195 HealthCheckRegistry healthRegistry = new HealthCheckRegistry();
196
197 ds.setMetricRegistry(metricRegistry);
198 ds.setHealthCheckRegistry(healthRegistry);
199
200 // before the pool is started, we can set it any number of times...
201 ds.setMetricRegistry(metricRegistry);
202 ds.setHealthCheckRegistry(healthRegistry);
203
204 try (Connection connection = ds.getConnection()) {
205
206 // after the pool is started, we cannot set it any more
207 ds.setMetricRegistry(metricRegistry);
208 fail("Should not have been allowed to set registry after pool started");
209 }
210 catch (IllegalStateException ise) {
211 // pass
212 }
213 }
214 }
215
216 @Test
217 public void testSetters3() throws Exception
218 {
219 try (HikariDataSource ds = newHikariDataSource()) {
220 ds.setMaximumPoolSize(1);
221 ds.setDataSourceClassName("com.zaxxer.hikari.mocks.StubDataSource");
222
223 MetricRegistry metricRegistry = new MetricRegistry();
224 MetricsTrackerFactory metricsTrackerFactory = new CodahaleMetricsTrackerFactory(metricRegistry);
225
226 try (Connection connection = ds.getConnection()) {
227
228 // After the pool as started, we can only set them once...
229 ds.setMetricsTrackerFactory(metricsTrackerFactory);
230
231 // and never again...
232 ds.setMetricsTrackerFactory(metricsTrackerFactory);
233 fail("Should not have been allowed to set metricsTrackerFactory after pool started");
234 }
235 catch (IllegalStateException ise) {
236 // pass
237 try {
238 // and never again... (even when calling another method)
239 ds.setMetricRegistry(metricRegistry);
240 fail("Should not have been allowed to set registry after pool started");
241 }
242 catch (IllegalStateException ise2) {
243 // pass
244 }
245 }
246 }
247 }
248
249 @Test
250 public void testSetters4() throws Exception
251 {
252 try (HikariDataSource ds = newHikariDataSource()) {
253 ds.setMaximumPoolSize(1);
254 ds.setDataSourceClassName("com.zaxxer.hikari.mocks.StubDataSource");
255
256 MetricRegistry metricRegistry = new MetricRegistry();
257
258 // before the pool is started, we can set it any number of times using either setter
259 ds.setMetricRegistry(metricRegistry);
260 ds.setMetricRegistry(metricRegistry);
261 ds.setMetricRegistry(metricRegistry);
262
263 try (Connection connection = ds.getConnection()) {
264
265 // after the pool is started, we cannot set it any more
266 ds.setMetricRegistry(metricRegistry);
267 fail("Should not have been allowed to set registry after pool started");
268 }
269 catch (IllegalStateException ise) {
270 // pass
271 }
272 }
273 }
274
275 @Test
276 public void testSetters5() throws Exception
277 {
278 try (HikariDataSource ds = newHikariDataSource()) {
279 ds.setMaximumPoolSize(1);
280 ds.setDataSourceClassName("com.zaxxer.hikari.mocks.StubDataSource");
281
282 MetricRegistry metricRegistry = new MetricRegistry();
283 MetricsTrackerFactory metricsTrackerFactory = new CodahaleMetricsTrackerFactory(metricRegistry);
284
285 // before the pool is started, we can set it any number of times using either setter
286 ds.setMetricsTrackerFactory(metricsTrackerFactory);
287 ds.setMetricsTrackerFactory(metricsTrackerFactory);
288 ds.setMetricsTrackerFactory(metricsTrackerFactory);
289
290 try (Connection connection = ds.getConnection()) {
291
292 // after the pool is started, we cannot set it any more
293 ds.setMetricsTrackerFactory(metricsTrackerFactory);
294 fail("Should not have been allowed to set registry factory after pool started");
295 }
296 catch (IllegalStateException ise) {
297 // pass
298 }
299 }
300 }
301
302 @Test(expected = IllegalArgumentException.class)
303 public void testFakeMetricRegistryThrowsIllegalArgumentException()
304 {
305 try (HikariDataSource ds = newHikariDataSource()) {
306 ds.setMaximumPoolSize(1);
307 ds.setDataSourceClassName("com.zaxxer.hikari.mocks.StubDataSource");
308
309 FakeMetricRegistry metricRegistry = new FakeMetricRegistry();
310
311 ds.setMetricRegistry(metricRegistry);
312 }
313 }
314
315 private static class FakeMetricRegistry {}
316
317 @Test
318 public void testMetricRegistrySubclassIsAllowed()
319 {
320 try (HikariDataSource ds = newHikariDataSource()) {
321 ds.setMaximumPoolSize(1);
322 ds.setDataSourceClassName("com.zaxxer.hikari.mocks.StubDataSource");
323
324 ds.setMetricRegistry(new MetricRegistry() {
325 @Override
326 public Timer timer(String name) {
327 return super.timer(name);
328 }
329 });
330 }
331 }
332 }