001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package test.performance.timer;
023:
024: import java.util.Date;
025:
026: import javax.management.MBeanServer;
027: import javax.management.MBeanServerFactory;
028: import javax.management.Notification;
029: import javax.management.NotificationListener;
030: import javax.management.ObjectName;
031: import javax.management.timer.Timer;
032: import javax.management.timer.TimerNotification;
033:
034: import junit.framework.TestCase;
035: import test.performance.PerformanceSUITE;
036:
037: /**
038: * Timer Performance Tests
039: *
040: * @author <a href="mailto:Adrian.Brock@HappeningTimes.com">Adrian Brock</a>.
041: */
042: public class TimerTortureTestCase extends TestCase implements
043: NotificationListener {
044: // Constants ---------------------------------------------------------------
045:
046: String TIMER_TYPE = "TimerType";
047: String MESSAGE = "Message";
048: String USER_DATA = "UserData";
049:
050: // Attributes --------------------------------------------------------------
051:
052: /**
053: * The MBeanServer
054: */
055: MBeanServer server;
056:
057: /**
058: * The object name of the timer
059: */
060: ObjectName timerName;
061:
062: /**
063: * The timer
064: */
065: Timer timer;
066:
067: /**
068: * The timer notification id
069: */
070: Integer id;
071:
072: /**
073: * The target notifications
074: */
075: int target = 0;
076:
077: /**
078: * The number of notifications
079: */
080: int notifications = 0;
081:
082: // Constructor -------------------------------------------------------------
083:
084: /**
085: * Construct the test
086: */
087: public TimerTortureTestCase(String s) {
088: super (s);
089: }
090:
091: // Tests -------------------------------------------------------------------
092:
093: /**
094: * See how long to do many notifications one notification,
095: * this tests the overhead
096: */
097: public void testTortureOne() {
098: System.err.println("\nTimer iterations "
099: + PerformanceSUITE.TIMER_ITERATION_COUNT);
100: System.err
101: .println("One notification at 1 millsecond intervals.");
102: initTest();
103: try {
104: initTimer();
105: startTimer();
106: target = PerformanceSUITE.TIMER_ITERATION_COUNT;
107: long start = timeOffset(0).getTime();
108: addNotification(1000, 1,
109: PerformanceSUITE.TIMER_ITERATION_COUNT);
110: sync();
111: sleep(1000);
112: long end = timeOffset(0).getTime();
113: stopTimer();
114:
115: System.err.println("Time (ms): " + (end - start));
116: } finally {
117: MBeanServerFactory.releaseMBeanServer(server);
118: }
119: }
120:
121: /**
122: * See how long to many notifications one notification, this tests the overhead
123: */
124: public void testTortureTen() {
125: System.err.println("\nTimer iterations "
126: + PerformanceSUITE.TIMER_ITERATION_COUNT);
127: System.err
128: .println("Ten notifications at 1 millsecond intervals.");
129: initTest();
130: try {
131: initTimer();
132: startTimer();
133: target = 10 * PerformanceSUITE.TIMER_ITERATION_COUNT;
134: long start = timeOffset(0).getTime();
135: for (int i = 0; i < 10; i++)
136: addNotification(1000, 1,
137: PerformanceSUITE.TIMER_ITERATION_COUNT);
138: sync();
139: sleep(1000);
140: long end = timeOffset(0).getTime();
141: stopTimer();
142:
143: System.err.println("Time (ms): " + (end - start));
144: } finally {
145: MBeanServerFactory.releaseMBeanServer(server);
146: }
147: }
148:
149: // Support -----------------------------------------------------------------
150:
151: /**
152: * Start a new test
153: */
154: private void initTest() {
155: notifications = 0;
156: server = MBeanServerFactory.createMBeanServer();
157: }
158:
159: /**
160: * Create a timer and register ourselves as a listener
161: */
162: private void initTimer() {
163: try {
164: timer = new Timer();
165: timerName = new ObjectName("test:type=timer");
166: server.registerMBean(timer, timerName);
167: server.addNotificationListener(timerName, this , null, null);
168: } catch (Exception e) {
169: fail(e.toString());
170: }
171: }
172:
173: /**
174: * Start the timer
175: */
176: private void startTimer() {
177: timer.start();
178: }
179:
180: /**
181: * Stop the timer, does a small wait to avoid problems with the RI
182: */
183: private void stopTimer() {
184: timer.stop();
185: }
186:
187: /**
188: * Add a notification
189: */
190: private void addNotification(long offset, long period, long occurs) {
191: id = timer.addNotification(TIMER_TYPE, MESSAGE, USER_DATA,
192: timeOffset(offset), period, occurs);
193: }
194:
195: /**
196: * Handle the notification, just add it to the list
197: */
198: public void handleNotification(Notification n, Object ignored) {
199: notifications++;
200: TimerNotification tn = (TimerNotification) n;
201: if (timer.getNbOccurences(tn.getNotificationID()).longValue() == 1)
202: synchronized (timerName) {
203: timerName.notifyAll();
204: }
205: }
206:
207: /**
208: * Sync with the notification handler
209: */
210: private void sync() {
211: synchronized (timerName) {
212: try {
213: timerName.wait(60000);
214: } catch (InterruptedException ignored) {
215: }
216: }
217: }
218:
219: /**
220: * Get the time using and offset
221: */
222: private Date timeOffset(long offset) {
223: return new Date(System.currentTimeMillis() + offset);
224: }
225:
226: /**
227: * Sleep for a bit
228: */
229: private void sleep(long time) {
230: try {
231: Thread.sleep(time);
232: } catch (InterruptedException ignored) {
233: }
234: }
235: }
|