package test.performance.timer;
import java.util.Date;
import javax.management.MBeanServer;
import javax.management.MBeanServerFactory;
import javax.management.Notification;
import javax.management.NotificationListener;
import javax.management.ObjectName;
import javax.management.timer.Timer;
import javax.management.timer.TimerNotification;
import junit.framework.TestCase;
import test.performance.PerformanceSUITE;
public class TimerTortureTestCase
extends TestCase
implements NotificationListener
{
String TIMER_TYPE = "TimerType";
String MESSAGE = "Message";
String USER_DATA = "UserData";
MBeanServer server;
ObjectName timerName;
Timer timer;
Integer id;
int target = 0;
int notifications = 0;
public TimerTortureTestCase(String s)
{
super(s);
}
public void testTortureOne()
{
System.err.println("\nTimer iterations " + PerformanceSUITE.TIMER_ITERATION_COUNT);
System.err.println("One notification at 1 millsecond intervals.");
initTest();
try
{
initTimer();
startTimer();
target = PerformanceSUITE.TIMER_ITERATION_COUNT;
long start = timeOffset(0).getTime();
addNotification(1000, 1, PerformanceSUITE.TIMER_ITERATION_COUNT);
sync();
sleep(1000);
long end = timeOffset(0).getTime();
stopTimer();
System.err.println("Time (ms): " + (end-start));
}
finally
{
MBeanServerFactory.releaseMBeanServer(server);
}
}
public void testTortureTen()
{
System.err.println("\nTimer iterations " + PerformanceSUITE.TIMER_ITERATION_COUNT);
System.err.println("Ten notifications at 1 millsecond intervals.");
initTest();
try
{
initTimer();
startTimer();
target = 10 * PerformanceSUITE.TIMER_ITERATION_COUNT;
long start = timeOffset(0).getTime();
for (int i=0; i<10; i++)
addNotification(1000, 1, PerformanceSUITE.TIMER_ITERATION_COUNT);
sync();
sleep(1000);
long end = timeOffset(0).getTime();
stopTimer();
System.err.println("Time (ms): " + (end-start));
}
finally
{
MBeanServerFactory.releaseMBeanServer(server);
}
}
private void initTest()
{
notifications = 0;
server = MBeanServerFactory.createMBeanServer();
}
private void initTimer()
{
try
{
timer = new Timer();
timerName = new ObjectName("test:type=timer");
server.registerMBean(timer, timerName);
server.addNotificationListener(timerName, this, null, null);
}
catch (Exception e)
{
fail(e.toString());
}
}
private void startTimer()
{
timer.start();
}
private void stopTimer()
{
timer.stop();
}
private void addNotification(long offset, long period, long occurs)
{
id = timer.addNotification(TIMER_TYPE, MESSAGE, USER_DATA,
timeOffset(offset), period, occurs);
}
public void handleNotification(Notification n, Object ignored)
{
notifications++;
TimerNotification tn = (TimerNotification) n;
if (timer.getNbOccurences(tn.getNotificationID()).longValue() == 1)
synchronized(timerName)
{
timerName.notifyAll();
}
}
private void sync()
{
synchronized(timerName)
{
try
{
timerName.wait(60000);
}
catch (InterruptedException ignored)
{
}
}
}
private Date timeOffset(long offset)
{
return new Date(System.currentTimeMillis() + offset);
}
private void sleep(long time)
{
try
{
Thread.sleep(time);
}
catch (InterruptedException ignored)
{
}
}
}