[jboss-cvs] JBoss Messaging SVN: r3780 - projects/jaio/trunk/jaio/java/tests/org/jboss/jaio/libaioimpl/test.
jboss-cvs-commits at lists.jboss.org
jboss-cvs-commits at lists.jboss.org
Sun Feb 24 21:56:04 EST 2008
Author: clebert.suconic at jboss.com
Date: 2008-02-24 21:56:03 -0500 (Sun, 24 Feb 2008)
New Revision: 3780
Modified:
projects/jaio/trunk/jaio/java/tests/org/jboss/jaio/libaioimpl/test/MultiThreadTests.java
Log:
Testcase changes
Modified: projects/jaio/trunk/jaio/java/tests/org/jboss/jaio/libaioimpl/test/MultiThreadTests.java
===================================================================
--- projects/jaio/trunk/jaio/java/tests/org/jboss/jaio/libaioimpl/test/MultiThreadTests.java 2008-02-24 12:38:54 UTC (rev 3779)
+++ projects/jaio/trunk/jaio/java/tests/org/jboss/jaio/libaioimpl/test/MultiThreadTests.java 2008-02-25 02:56:03 UTC (rev 3780)
@@ -12,21 +12,21 @@
import org.jboss.jaio.api.AIOCallback;
import org.jboss.jaio.libaioimpl.JLibAIO;
+import EDU.oswego.cs.dl.util.concurrent.SynchronizedInt;
+
import junit.framework.TestCase;
public class MultiThreadTests extends TestCase
{
- private static CharsetEncoder UTF_8_ENCODER = Charset.forName("UTF-8").newEncoder();
-
- static int position = 0;
+ static SynchronizedInt position = new SynchronizedInt(0);
String FILE_NAME="/tmp/libaio.log";
static final int SIZE = 1024;
- static final int NUMBER_OF_THREADS = 5;
- static final int NUMBER_OF_LINES = 50000;
+ static final int NUMBER_OF_THREADS = 20;
+ static final int NUMBER_OF_LINES = 5000;
private static void addData(JLibAIO aio, ByteBuffer buffer, AIOCallback callback)
@@ -42,21 +42,33 @@
File file = new File(FILE_NAME);
file.delete();
- position = 0;
+ position.set(0);
}
public void testMultipleASynchronousWrites() throws Throwable
{
+ executeTest(false);
+ }
+
+ public void testMultipleSynchronousWrites() throws Throwable
+ {
+ executeTest(true);
+ }
+
+ private void executeTest(boolean sync) throws Throwable
+ {
JLibAIO jlibAIO = new JLibAIO();
- jlibAIO.open(FILE_NAME, 10000);
+ jlibAIO.open(FILE_NAME, 50000);
System.out.println("Preallocating file");
- jlibAIO.preAllocate(NUMBER_OF_THREADS, SIZE * NUMBER_OF_LINES);
+ jlibAIO.preAllocate(1, NUMBER_OF_THREADS * SIZE * NUMBER_OF_LINES);
+ System.out.println("Done Preallocating file");
CountDownLatch latchStart = new CountDownLatch (NUMBER_OF_THREADS + 1);
+ CountDownLatch latchEnd = new CountDownLatch (NUMBER_OF_THREADS);
ArrayList<ThreadProducer> list = new ArrayList<ThreadProducer>(NUMBER_OF_THREADS);
for(int i=0;i<NUMBER_OF_THREADS;i++)
{
- ThreadProducer producer = new ThreadProducer("Thread " + i, latchStart, jlibAIO);
+ ThreadProducer producer = new ThreadProducer("Thread " + i, latchStart, latchEnd, jlibAIO, sync);
list.add(producer);
producer.start();
}
@@ -64,8 +76,18 @@
latchStart.countDown();
latchStart.await();
+
long startTime = System.currentTimeMillis();
+ System.out.println("Every started");
+
+ latchEnd.await();
+
+ long endTime = System.currentTimeMillis();
+
+ System.out.println("Records/Second = " + (NUMBER_OF_THREADS * NUMBER_OF_LINES * 1000 / (endTime - startTime)));
+
+
for (ThreadProducer producer: list)
{
producer.join();
@@ -74,34 +96,46 @@
throw producer.failed;
}
}
+ jlibAIO.close();
- long endTime = System.currentTimeMillis();
-
- System.out.println("Records/Second = " + (NUMBER_OF_THREADS * NUMBER_OF_LINES * 1000 / (endTime - startTime)));
}
- private static synchronized int getNewPosition()
+ private static int getNewPosition()
{
- return position++;
+ return position.add(1);
}
static class ThreadProducer extends Thread
{
+ //private CharsetEncoder UTF_8_ENCODER = Charset.forName("UTF-8").newEncoder();
+
Throwable failed = null;
CountDownLatch latchStart;
+ CountDownLatch latchEnd;
+ boolean sync;
JLibAIO libaio;
- public ThreadProducer(String name, CountDownLatch latchStart, JLibAIO libaio)
+ public ThreadProducer(String name, CountDownLatch latchStart, CountDownLatch latchEnd, JLibAIO libaio, boolean sync)
{
super(name);
this.latchStart = latchStart;
this.libaio = libaio;
+ this.latchEnd = latchEnd;
+ this.sync = sync;
}
+ /*private void addString(String str, ByteBuffer buffer)
+ {
+ CharBuffer charBuffer = CharBuffer.wrap(str);
+ UTF_8_ENCODER.encode(charBuffer, buffer, true);
+
+ }*/
+
+
public void run()
{
super.run();
@@ -113,29 +147,44 @@
latchStart.countDown();
latchStart.await();
+ long startTime = System.currentTimeMillis();
- CountDownLatch latchFinishThread = new CountDownLatch(NUMBER_OF_LINES);
+
+ CountDownLatch latchFinishThread = null;
+
+ if (!sync) latchFinishThread = new CountDownLatch(NUMBER_OF_LINES);
LinkedList<LocalCallback> list = new LinkedList<LocalCallback>();
for (int i=0;i<NUMBER_OF_LINES;i++)
{
+ ByteBuffer buffer = libaio.newBuffer(SIZE);
+
//System.out.println("Thread " + Thread.currentThread().getName() + " Adding a line");
- ByteBuffer buffer = libaio.newBuffer(SIZE);
addString ("Thread name=" + Thread.currentThread().getName() + ";" + i + "\n", buffer);
for (int local = buffer.position(); local < buffer.capacity() - 1; local++)
{
buffer.put((byte)' ');
}
buffer.put((byte)'\n');
-
+
+ if (sync) latchFinishThread = new CountDownLatch(1);
LocalCallback callback = new LocalCallback(latchFinishThread, buffer, libaio);
list.add(callback);
addData(libaio, buffer,callback);
+ //Thread.yield();
+ if (sync) latchFinishThread.await();
//libaio.write(getNewPosition() * SIZE, SIZE, buffer, callback);
}
- latchFinishThread.await();
+ if (!sync) latchFinishThread.await();
+ long endtime = System.currentTimeMillis();
+
+ System.out.println(Thread.currentThread().getName() + " Rec/Sec= " + (NUMBER_OF_LINES * 1000 / (endtime-startTime)));
+
+ latchEnd.countDown();
+
+
for (LocalCallback callback: list)
{
assertTrue (callback.doneCalled);
@@ -154,14 +203,16 @@
private static void addString(String str, ByteBuffer buffer)
{
- CharBuffer charBuffer = CharBuffer.wrap(str);
- UTF_8_ENCODER.encode(charBuffer, buffer, true);
+ byte bytes[] = str.getBytes();
+ //buffer.putInt(bytes.length);
+ buffer.put(bytes);
+ //CharBuffer charBuffer = CharBuffer.wrap(str);
+ //UTF_8_ENCODER.encode(charBuffer, buffer, true);
}
static class LocalCallback implements AIOCallback
{
-
boolean doneCalled = false;
boolean errorCalled = false;
CountDownLatch latchDone;
More information about the jboss-cvs-commits
mailing list