001: /*
002: * This software is released under a licence similar to the Apache Software Licence.
003: * See org.logicalcobwebs.proxool.package.html for details.
004: * The latest version is available at http://proxool.sourceforge.net
005: */
006: package org.logicalcobwebs.proxool.admin;
007:
008: import org.apache.commons.logging.Log;
009: import org.apache.commons.logging.LogFactory;
010: import org.logicalcobwebs.proxool.AbstractProxoolTest;
011: import org.logicalcobwebs.proxool.ConnectionPoolDefinitionIF;
012: import org.logicalcobwebs.proxool.ProxoolConstants;
013: import org.logicalcobwebs.proxool.ProxoolFacade;
014: import org.logicalcobwebs.proxool.ResultMonitor;
015: import org.logicalcobwebs.proxool.TestConstants;
016: import org.logicalcobwebs.proxool.TestHelper;
017:
018: import java.sql.DriverManager;
019: import java.text.DecimalFormat;
020: import java.util.Properties;
021:
022: /**
023: * Test {@link StatisticsIF}
024: *
025: * @version $Revision: 1.22 $, $Date: 2006/01/18 14:40:06 $
026: * @author Bill Horsman (bill@logicalcobwebs.co.uk)
027: * @author $Author: billhorsman $ (current maintainer)
028: * @since Proxool 0.7
029: */
030: public class StatisticsTest extends AbstractProxoolTest {
031:
032: private static final Log LOG = LogFactory
033: .getLog(StatisticsTest.class);
034:
035: private static final DecimalFormat DECIMAL_FORMAT = new DecimalFormat(
036: "0.00");
037:
038: /**
039: * @see junit.framework.TestCase#TestCase
040: */
041: public StatisticsTest(String s) {
042: super (s);
043: }
044:
045: /**
046: * Test whether the statistics we get back are roughly right.
047: */
048: public void testStatistics() throws Exception {
049:
050: String testName = "statistics";
051: String alias = testName;
052:
053: String url = TestHelper.buildProxoolUrl(alias,
054: TestConstants.HYPERSONIC_DRIVER,
055: TestConstants.HYPERSONIC_TEST_URL);
056: Properties info = new Properties();
057: info.setProperty(ProxoolConstants.USER_PROPERTY,
058: TestConstants.HYPERSONIC_USER);
059: info.setProperty(ProxoolConstants.PASSWORD_PROPERTY,
060: TestConstants.HYPERSONIC_PASSWORD);
061: info.setProperty(ProxoolConstants.STATISTICS_PROPERTY,
062: "10s,15s");
063: info
064: .setProperty(
065: ProxoolConstants.MINIMUM_CONNECTION_COUNT_PROPERTY,
066: "1");
067: info.setProperty(ProxoolConstants.VERBOSE_PROPERTY, "true");
068:
069: // We don't test whether anything is logged, but this line should make something appear
070: info.setProperty(
071: ProxoolConstants.STATISTICS_LOG_LEVEL_PROPERTY,
072: ProxoolConstants.STATISTICS_LOG_LEVEL_DEBUG);
073:
074: // Register pool
075: ProxoolFacade.registerConnectionPool(url, info);
076:
077: // Skip past the first set because they will probably be for only part
078: // of the 10s period.
079: StatisticsResultMonitor srm = new StatisticsResultMonitor(
080: alias, "10s");
081: assertEquals("Timeout", ResultMonitor.SUCCESS, srm.getResult());
082: srm.getStatistics();
083:
084: DriverManager.getConnection(url).close();
085:
086: srm = new StatisticsResultMonitor(alias, "10s") {
087: protected boolean check(StatisticsIF statistics) {
088: return (statistics.getServedCount() == 1);
089: }
090: };
091: assertEquals("Timeout", ResultMonitor.SUCCESS, srm.getResult());
092: StatisticsIF statistics = srm.getStatistics();
093:
094: assertEquals("servedCount", 1L, statistics.getServedCount());
095: assertEquals("servedPerSecond", 0.09, 0.11, statistics
096: .getServedPerSecond());
097: assertEquals("refusedCount", 0L, statistics.getRefusedCount());
098:
099: }
100:
101: public void testOverhead() throws Exception {
102: String testName = "overhead";
103: String alias = testName;
104: String url = TestHelper.buildProxoolUrl(alias,
105: TestConstants.HYPERSONIC_DRIVER,
106: TestConstants.HYPERSONIC_TEST_URL);
107: Properties info = new Properties();
108: info.setProperty(ProxoolConstants.USER_PROPERTY,
109: TestConstants.HYPERSONIC_USER);
110: info.setProperty(ProxoolConstants.PASSWORD_PROPERTY,
111: TestConstants.HYPERSONIC_PASSWORD);
112: info.setProperty(ProxoolConstants.STATISTICS_PROPERTY, "10s");
113: info
114: .setProperty(
115: ProxoolConstants.MINIMUM_CONNECTION_COUNT_PROPERTY,
116: "1");
117:
118: // We don't test whether anything is logged, but this line should make something appear
119: info.setProperty(
120: ProxoolConstants.STATISTICS_LOG_LEVEL_PROPERTY,
121: ProxoolConstants.STATISTICS_LOG_LEVEL_DEBUG);
122:
123: // Register pool
124: ProxoolFacade.registerConnectionPool(url, info);
125:
126: ConnectionPoolDefinitionIF cpd = ProxoolFacade
127: .getConnectionPoolDefinition(alias);
128: Admin admin = new Admin(cpd);
129:
130: final int loops = 100000;
131: long start = System.currentTimeMillis();
132: for (int i = 0; i < loops; i++) {
133: admin.connectionReturned(10);
134: }
135: double avg = (double) (System.currentTimeMillis() - start)
136: / (double) loops;
137: LOG.info("Statistics take " + DECIMAL_FORMAT.format(avg * 1000)
138: + " microseconds");
139:
140: }
141:
142: }
143:
144: /*
145: Revision history:
146: $Log: StatisticsTest.java,v $
147: Revision 1.22 2006/01/18 14:40:06 billhorsman
148: Unbundled Jakarta's Commons Logging.
149:
150: Revision 1.21 2003/03/06 22:28:31 billhorsman
151: another go at statistics threading (in tests)
152:
153: Revision 1.20 2003/03/06 12:45:06 billhorsman
154: switch on verbose logging
155:
156: Revision 1.19 2003/03/06 10:37:41 billhorsman
157: better timeout assertion
158:
159: Revision 1.18 2003/03/04 10:58:44 billhorsman
160: checkstyle
161:
162: Revision 1.17 2003/03/04 10:24:41 billhorsman
163: removed try blocks around each test
164:
165: Revision 1.16 2003/03/03 17:09:09 billhorsman
166: all tests now extend AbstractProxoolTest
167:
168: Revision 1.15 2003/03/03 11:12:06 billhorsman
169: fixed licence
170:
171: Revision 1.14 2003/03/03 09:10:41 billhorsman
172: removed debug
173:
174: Revision 1.13 2003/03/02 01:16:37 billhorsman
175: removed flakey average active time test
176:
177: Revision 1.12 2003/03/01 18:25:53 billhorsman
178: *** empty log message ***
179:
180: Revision 1.11 2003/03/01 16:46:08 billhorsman
181: debug
182:
183: Revision 1.10 2003/03/01 16:14:32 billhorsman
184: debug
185:
186: Revision 1.9 2003/03/01 16:04:45 billhorsman
187: fix
188:
189: Revision 1.8 2003/03/01 15:27:25 billhorsman
190: checkstyle
191:
192: Revision 1.7 2003/02/28 12:36:33 billhorsman
193: more robust waiting for statistics
194:
195: Revision 1.6 2003/02/28 12:23:59 billhorsman
196: more robust waiting for statistics
197:
198: Revision 1.5 2003/02/27 18:01:49 billhorsman
199: completely rethought the test structure. it's now
200: more obvious. no new tests yet though.
201:
202: Revision 1.4 2003/02/26 23:45:18 billhorsman
203: add some sleep
204:
205: Revision 1.3 2003/02/26 18:30:02 billhorsman
206: test for stats overhead
207:
208: Revision 1.2 2003/02/26 16:05:51 billhorsman
209: widespread changes caused by refactoring the way we
210: update and redefine pool definitions.
211:
212: Revision 1.1 2003/02/20 00:33:15 billhorsman
213: renamed monitor package -> admin
214:
215: Revision 1.3 2003/02/19 23:36:50 billhorsman
216: renamed monitor package to admin
217:
218: Revision 1.2 2003/02/19 15:14:31 billhorsman
219: fixed copyright (copy and paste error,
220: not copyright change)
221:
222: Revision 1.1 2003/02/07 17:28:36 billhorsman
223: *** empty log message ***
224:
225: */
|