Revision: 15180
Author: vladimir.ralev
Date: Wed Nov 10 20:22:10 2010
Log: Update Issue 2099
Testcases for single vs multi-thread task
http://code.google.com/p/mobicents/source/detail?r=15180
Added:
/trunk/servers/media/core/server-impl/src/test/java/org/mobicents/media/server/impl/clock/SchedulerTest.java
=======================================
--- /dev/null
+++
/trunk/servers/media/core/server-impl/src/test/java/org/mobicents/media/server/impl/clock/SchedulerTest.java
Wed Nov 10 20:22:10 2010
@@ -0,0 +1,86 @@
+package org.mobicents.media.server.impl.clock;
+
+import junit.framework.TestCase;
+
+import org.mobicents.media.server.spi.clock.Task;
+
+public class SchedulerTest extends TestCase{
+ public boolean samethread;
+
+ static long sum;
+ static long count;
+
+ static final int INTERVAL=40;
+
+ public void testClockSingleThread() {
+ samethread = true;
+ _runTestWithOption();
+ }
+
+ public void testClockSeparateThreads() {
+ samethread = false;
+ _runTestWithOption();
+ }
+
+ private void _runTestWithOption() {
+ Scheduler s = new Scheduler();
+ sum=count=0;
+
+ Task t = new Task() {
+ boolean active = true;
+ long last=0;
+ @Override
+ public int perform() {
+
+ doTask(samethread);
+ long delta = System.currentTimeMillis()-last;
+ if(delta<10000) {sum+=delta;count++;}
+ System.out.print(delta + ", ");
+ last = System.currentTimeMillis();
+ return INTERVAL;
+ }
+
+ @Override
+ public boolean isActive() {
+ // TODO Auto-generated method stub
+ return active;
+ }
+
+ @Override
+ public void cancel() {
+ active = false;
+ }
+ };
+ s.start();
+ s.execute(t);
+ try {
+ Thread.sleep(10000);
+ } catch (InterruptedException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ }
+ s.stop();
+ System.out.println("\nAverage delta:" + sum/count);
+ }
+ public void doTask(boolean sameThread) {
+ Thread tt = new Thread() {
+
+ public void run() {
+ try {/*
+ int[] d = new
int[1000000];d[9999]=38432;
+ for(int q=0;q<100000;q++) {
+
d[q]*=d[q+5*2]+d[q]*3*q-5*d[2+q];
+ }*/
+ Thread.sleep(INTERVAL+2);
+ } catch (InterruptedException e) {
+ }
+
+ }
+ };
+ if(sameThread) {
+ tt.run();
+ } else {
+ tt.start();
+ }
+ }
+}