KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > test > performance > timer > TimerTortureTestCase


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2005, JBoss Inc., and individual contributors as indicated
4 * by the @authors tag. See the copyright.txt in the distribution for a
5 * full listing of individual contributors.
6 *
7 * This is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * This software is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this software; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21 */

22 package test.performance.timer;
23
24 import java.util.Date JavaDoc;
25
26 import javax.management.MBeanServer JavaDoc;
27 import javax.management.MBeanServerFactory JavaDoc;
28 import javax.management.Notification JavaDoc;
29 import javax.management.NotificationListener JavaDoc;
30 import javax.management.ObjectName JavaDoc;
31 import javax.management.timer.Timer JavaDoc;
32 import javax.management.timer.TimerNotification JavaDoc;
33
34 import junit.framework.TestCase;
35 import test.performance.PerformanceSUITE;
36
37 /**
38  * Timer Performance Tests
39  *
40  * @author <a HREF="mailto:Adrian.Brock@HappeningTimes.com">Adrian Brock</a>.
41  */

42 public class TimerTortureTestCase
43    extends TestCase
44    implements NotificationListener JavaDoc
45 {
46    // Constants ---------------------------------------------------------------
47

48    String JavaDoc TIMER_TYPE = "TimerType";
49    String JavaDoc MESSAGE = "Message";
50    String JavaDoc USER_DATA = "UserData";
51
52    // Attributes --------------------------------------------------------------
53

54    /**
55     * The MBeanServer
56     */

57    MBeanServer JavaDoc server;
58
59    /**
60     * The object name of the timer
61     */

62    ObjectName JavaDoc timerName;
63
64    /**
65     * The timer
66     */

67    Timer JavaDoc timer;
68
69    /**
70     * The timer notification id
71     */

72    Integer JavaDoc id;
73
74    /**
75     * The target notifications
76     */

77    int target = 0;
78
79    /**
80     * The number of notifications
81     */

82    int notifications = 0;
83
84    // Constructor -------------------------------------------------------------
85

86    /**
87     * Construct the test
88     */

89    public TimerTortureTestCase(String JavaDoc s)
90    {
91       super(s);
92    }
93
94    // Tests -------------------------------------------------------------------
95

96    /**
97     * See how long to do many notifications one notification,
98     * this tests the overhead
99     */

100    public void testTortureOne()
101    {
102       System.err.println("\nTimer iterations " + PerformanceSUITE.TIMER_ITERATION_COUNT);
103       System.err.println("One notification at 1 millsecond intervals.");
104       initTest();
105       try
106       {
107          initTimer();
108          startTimer();
109          target = PerformanceSUITE.TIMER_ITERATION_COUNT;
110          long start = timeOffset(0).getTime();
111          addNotification(1000, 1, PerformanceSUITE.TIMER_ITERATION_COUNT);
112          sync();
113          sleep(1000);
114          long end = timeOffset(0).getTime();
115          stopTimer();
116
117          System.err.println("Time (ms): " + (end-start));
118       }
119       finally
120       {
121          MBeanServerFactory.releaseMBeanServer(server);
122       }
123    }
124
125    /**
126     * See how long to many notifications one notification, this tests the overhead
127     */

128    public void testTortureTen()
129    {
130       System.err.println("\nTimer iterations " + PerformanceSUITE.TIMER_ITERATION_COUNT);
131       System.err.println("Ten notifications at 1 millsecond intervals.");
132       initTest();
133       try
134       {
135          initTimer();
136          startTimer();
137          target = 10 * PerformanceSUITE.TIMER_ITERATION_COUNT;
138          long start = timeOffset(0).getTime();
139          for (int i=0; i<10; i++)
140             addNotification(1000, 1, PerformanceSUITE.TIMER_ITERATION_COUNT);
141          sync();
142          sleep(1000);
143          long end = timeOffset(0).getTime();
144          stopTimer();
145
146          System.err.println("Time (ms): " + (end-start));
147       }
148       finally
149       {
150          MBeanServerFactory.releaseMBeanServer(server);
151       }
152    }
153
154    // Support -----------------------------------------------------------------
155

156    /**
157     * Start a new test
158     */

159    private void initTest()
160    {
161       notifications = 0;
162       server = MBeanServerFactory.createMBeanServer();
163    }
164
165    /**
166     * Create a timer and register ourselves as a listener
167     */

168    private void initTimer()
169    {
170       try
171       {
172          timer = new Timer JavaDoc();
173          timerName = new ObjectName JavaDoc("test:type=timer");
174          server.registerMBean(timer, timerName);
175          server.addNotificationListener(timerName, this, null, null);
176       }
177       catch (Exception JavaDoc e)
178       {
179          fail(e.toString());
180       }
181    }
182
183    /**
184     * Start the timer
185     */

186    private void startTimer()
187    {
188       timer.start();
189    }
190
191    /**
192     * Stop the timer, does a small wait to avoid problems with the RI
193     */

194    private void stopTimer()
195    {
196       timer.stop();
197    }
198
199    /**
200     * Add a notification
201     */

202    private void addNotification(long offset, long period, long occurs)
203    {
204       id = timer.addNotification(TIMER_TYPE, MESSAGE, USER_DATA,
205                                  timeOffset(offset), period, occurs);
206    }
207
208    /**
209     * Handle the notification, just add it to the list
210     */

211    public void handleNotification(Notification JavaDoc n, Object JavaDoc ignored)
212    {
213       notifications++;
214       TimerNotification JavaDoc tn = (TimerNotification JavaDoc) n;
215       if (timer.getNbOccurences(tn.getNotificationID()).longValue() == 1)
216          synchronized(timerName)
217          {
218             timerName.notifyAll();
219          }
220    }
221
222    /**
223     * Sync with the notification handler
224     */

225    private void sync()
226    {
227       synchronized(timerName)
228       {
229          try
230          {
231             timerName.wait(60000);
232          }
233          catch (InterruptedException JavaDoc ignored)
234          {
235          }
236       }
237    }
238
239    /**
240     * Get the time using and offset
241     */

242    private Date JavaDoc timeOffset(long offset)
243    {
244       return new Date JavaDoc(System.currentTimeMillis() + offset);
245    }
246
247    /**
248     * Sleep for a bit
249     */

250    private void sleep(long time)
251    {
252       try
253       {
254          Thread.sleep(time);
255       }
256       catch (InterruptedException JavaDoc ignored)
257       {
258       }
259    }
260 }
261
Popular Tags