1 22 package test.performance.timer; 23 24 import java.util.Date ; 25 26 import javax.management.MBeanServer ; 27 import javax.management.MBeanServerFactory ; 28 import javax.management.Notification ; 29 import javax.management.NotificationListener ; 30 import javax.management.ObjectName ; 31 import javax.management.timer.Timer ; 32 import javax.management.timer.TimerNotification ; 33 34 import junit.framework.TestCase; 35 import test.performance.PerformanceSUITE; 36 37 42 public class TimerTortureTestCase 43 extends TestCase 44 implements NotificationListener 45 { 46 48 String TIMER_TYPE = "TimerType"; 49 String MESSAGE = "Message"; 50 String USER_DATA = "UserData"; 51 52 54 57 MBeanServer server; 58 59 62 ObjectName timerName; 63 64 67 Timer timer; 68 69 72 Integer id; 73 74 77 int target = 0; 78 79 82 int notifications = 0; 83 84 86 89 public TimerTortureTestCase(String s) 90 { 91 super(s); 92 } 93 94 96 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 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 156 159 private void initTest() 160 { 161 notifications = 0; 162 server = MBeanServerFactory.createMBeanServer(); 163 } 164 165 168 private void initTimer() 169 { 170 try 171 { 172 timer = new Timer (); 173 timerName = new ObjectName ("test:type=timer"); 174 server.registerMBean(timer, timerName); 175 server.addNotificationListener(timerName, this, null, null); 176 } 177 catch (Exception e) 178 { 179 fail(e.toString()); 180 } 181 } 182 183 186 private void startTimer() 187 { 188 timer.start(); 189 } 190 191 194 private void stopTimer() 195 { 196 timer.stop(); 197 } 198 199 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 211 public void handleNotification(Notification n, Object ignored) 212 { 213 notifications++; 214 TimerNotification tn = (TimerNotification ) n; 215 if (timer.getNbOccurences(tn.getNotificationID()).longValue() == 1) 216 synchronized(timerName) 217 { 218 timerName.notifyAll(); 219 } 220 } 221 222 225 private void sync() 226 { 227 synchronized(timerName) 228 { 229 try 230 { 231 timerName.wait(60000); 232 } 233 catch (InterruptedException ignored) 234 { 235 } 236 } 237 } 238 239 242 private Date timeOffset(long offset) 243 { 244 return new Date (System.currentTimeMillis() + offset); 245 } 246 247 250 private void sleep(long time) 251 { 252 try 253 { 254 Thread.sleep(time); 255 } 256 catch (InterruptedException ignored) 257 { 258 } 259 } 260 } 261 | Popular Tags |