KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > logicalcobwebs > proxool > admin > StatisticsTest


1 /*
2  * This software is released under a licence similar to the Apache Software Licence.
3  * See org.logicalcobwebs.proxool.package.html for details.
4  * The latest version is available at http://proxool.sourceforge.net
5  */

6 package org.logicalcobwebs.proxool.admin;
7
8 import org.apache.commons.logging.Log;
9 import org.apache.commons.logging.LogFactory;
10 import org.logicalcobwebs.proxool.AbstractProxoolTest;
11 import org.logicalcobwebs.proxool.ConnectionPoolDefinitionIF;
12 import org.logicalcobwebs.proxool.ProxoolConstants;
13 import org.logicalcobwebs.proxool.ProxoolFacade;
14 import org.logicalcobwebs.proxool.ResultMonitor;
15 import org.logicalcobwebs.proxool.TestConstants;
16 import org.logicalcobwebs.proxool.TestHelper;
17
18 import java.sql.DriverManager JavaDoc;
19 import java.text.DecimalFormat JavaDoc;
20 import java.util.Properties JavaDoc;
21
22 /**
23  * Test {@link StatisticsIF}
24  *
25  * @version $Revision: 1.22 $, $Date: 2006/01/18 14:40:06 $
26  * @author Bill Horsman (bill@logicalcobwebs.co.uk)
27  * @author $Author: billhorsman $ (current maintainer)
28  * @since Proxool 0.7
29  */

30 public class StatisticsTest extends AbstractProxoolTest {
31
32     private static final Log LOG = LogFactory.getLog(StatisticsTest.class);
33
34     private static final DecimalFormat JavaDoc DECIMAL_FORMAT = new DecimalFormat JavaDoc("0.00");
35
36     /**
37      * @see junit.framework.TestCase#TestCase
38      */

39     public StatisticsTest(String JavaDoc s) {
40         super(s);
41     }
42
43     /**
44      * Test whether the statistics we get back are roughly right.
45      */

46     public void testStatistics() throws Exception JavaDoc {
47
48         String JavaDoc testName = "statistics";
49         String JavaDoc alias = testName;
50
51         String JavaDoc url = TestHelper.buildProxoolUrl(alias,
52                 TestConstants.HYPERSONIC_DRIVER,
53                 TestConstants.HYPERSONIC_TEST_URL);
54         Properties JavaDoc info = new Properties JavaDoc();
55         info.setProperty(ProxoolConstants.USER_PROPERTY, TestConstants.HYPERSONIC_USER);
56         info.setProperty(ProxoolConstants.PASSWORD_PROPERTY, TestConstants.HYPERSONIC_PASSWORD);
57         info.setProperty(ProxoolConstants.STATISTICS_PROPERTY, "10s,15s");
58         info.setProperty(ProxoolConstants.MINIMUM_CONNECTION_COUNT_PROPERTY, "1");
59         info.setProperty(ProxoolConstants.VERBOSE_PROPERTY, "true");
60
61         // We don't test whether anything is logged, but this line should make something appear
62
info.setProperty(ProxoolConstants.STATISTICS_LOG_LEVEL_PROPERTY, ProxoolConstants.STATISTICS_LOG_LEVEL_DEBUG);
63
64         // Register pool
65
ProxoolFacade.registerConnectionPool(url, info);
66
67         // Skip past the first set because they will probably be for only part
68
// of the 10s period.
69
StatisticsResultMonitor srm = new StatisticsResultMonitor(alias, "10s");
70         assertEquals("Timeout", ResultMonitor.SUCCESS, srm.getResult());
71         srm.getStatistics();
72
73
74         DriverManager.getConnection(url).close();
75
76         srm = new StatisticsResultMonitor(alias, "10s") {
77             protected boolean check(StatisticsIF statistics) {
78                 return (statistics.getServedCount() == 1);
79             }
80         };
81         assertEquals("Timeout", ResultMonitor.SUCCESS, srm.getResult());
82         StatisticsIF statistics = srm.getStatistics();
83
84         assertEquals("servedCount", 1L, statistics.getServedCount());
85         assertEquals("servedPerSecond", 0.09, 0.11, statistics.getServedPerSecond());
86         assertEquals("refusedCount", 0L, statistics.getRefusedCount());
87
88     }
89
90     public void testOverhead() throws Exception JavaDoc {
91         String JavaDoc testName = "overhead";
92         String JavaDoc alias = testName;
93         String JavaDoc url = TestHelper.buildProxoolUrl(alias,
94                 TestConstants.HYPERSONIC_DRIVER,
95                 TestConstants.HYPERSONIC_TEST_URL);
96         Properties JavaDoc info = new Properties JavaDoc();
97         info.setProperty(ProxoolConstants.USER_PROPERTY, TestConstants.HYPERSONIC_USER);
98         info.setProperty(ProxoolConstants.PASSWORD_PROPERTY, TestConstants.HYPERSONIC_PASSWORD);
99         info.setProperty(ProxoolConstants.STATISTICS_PROPERTY, "10s");
100         info.setProperty(ProxoolConstants.MINIMUM_CONNECTION_COUNT_PROPERTY, "1");
101
102         // We don't test whether anything is logged, but this line should make something appear
103
info.setProperty(ProxoolConstants.STATISTICS_LOG_LEVEL_PROPERTY, ProxoolConstants.STATISTICS_LOG_LEVEL_DEBUG);
104
105         // Register pool
106
ProxoolFacade.registerConnectionPool(url, info);
107
108         ConnectionPoolDefinitionIF cpd = ProxoolFacade.getConnectionPoolDefinition(alias);
109         Admin admin = new Admin(cpd);
110
111         final int loops = 100000;
112         long start = System.currentTimeMillis();
113         for (int i = 0; i < loops; i++) {
114             admin.connectionReturned(10);
115         }
116         double avg = (double) (System.currentTimeMillis() - start) / (double) loops;
117         LOG.info("Statistics take " + DECIMAL_FORMAT.format(avg * 1000) + " microseconds");
118
119     }
120
121 }
122
123 /*
124  Revision history:
125  $Log: StatisticsTest.java,v $
126  Revision 1.22 2006/01/18 14:40:06 billhorsman
127  Unbundled Jakarta's Commons Logging.
128
129  Revision 1.21 2003/03/06 22:28:31 billhorsman
130  another go at statistics threading (in tests)
131
132  Revision 1.20 2003/03/06 12:45:06 billhorsman
133  switch on verbose logging
134
135  Revision 1.19 2003/03/06 10:37:41 billhorsman
136  better timeout assertion
137
138  Revision 1.18 2003/03/04 10:58:44 billhorsman
139  checkstyle
140
141  Revision 1.17 2003/03/04 10:24:41 billhorsman
142  removed try blocks around each test
143
144  Revision 1.16 2003/03/03 17:09:09 billhorsman
145  all tests now extend AbstractProxoolTest
146
147  Revision 1.15 2003/03/03 11:12:06 billhorsman
148  fixed licence
149
150  Revision 1.14 2003/03/03 09:10:41 billhorsman
151  removed debug
152
153  Revision 1.13 2003/03/02 01:16:37 billhorsman
154  removed flakey average active time test
155
156  Revision 1.12 2003/03/01 18:25:53 billhorsman
157  *** empty log message ***
158
159  Revision 1.11 2003/03/01 16:46:08 billhorsman
160  debug
161
162  Revision 1.10 2003/03/01 16:14:32 billhorsman
163  debug
164
165  Revision 1.9 2003/03/01 16:04:45 billhorsman
166  fix
167
168  Revision 1.8 2003/03/01 15:27:25 billhorsman
169  checkstyle
170
171  Revision 1.7 2003/02/28 12:36:33 billhorsman
172  more robust waiting for statistics
173
174  Revision 1.6 2003/02/28 12:23:59 billhorsman
175  more robust waiting for statistics
176
177  Revision 1.5 2003/02/27 18:01:49 billhorsman
178  completely rethought the test structure. it's now
179  more obvious. no new tests yet though.
180
181  Revision 1.4 2003/02/26 23:45:18 billhorsman
182  add some sleep
183
184  Revision 1.3 2003/02/26 18:30:02 billhorsman
185  test for stats overhead
186
187  Revision 1.2 2003/02/26 16:05:51 billhorsman
188  widespread changes caused by refactoring the way we
189  update and redefine pool definitions.
190
191  Revision 1.1 2003/02/20 00:33:15 billhorsman
192  renamed monitor package -> admin
193
194  Revision 1.3 2003/02/19 23:36:50 billhorsman
195  renamed monitor package to admin
196
197  Revision 1.2 2003/02/19 15:14:31 billhorsman
198  fixed copyright (copy and paste error,
199  not copyright change)
200
201  Revision 1.1 2003/02/07 17:28:36 billhorsman
202  *** empty log message ***
203
204   */

205
Popular Tags