aboutsummaryrefslogtreecommitdiffstats
path: root/src/junit/com/jogamp/common/nio/CachedBufferFactoryTest.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/junit/com/jogamp/common/nio/CachedBufferFactoryTest.java')
-rw-r--r--src/junit/com/jogamp/common/nio/CachedBufferFactoryTest.java136
1 files changed, 68 insertions, 68 deletions
diff --git a/src/junit/com/jogamp/common/nio/CachedBufferFactoryTest.java b/src/junit/com/jogamp/common/nio/CachedBufferFactoryTest.java
index b0cd49e..b0f3cfb 100644
--- a/src/junit/com/jogamp/common/nio/CachedBufferFactoryTest.java
+++ b/src/junit/com/jogamp/common/nio/CachedBufferFactoryTest.java
@@ -3,14 +3,14 @@
*
* Redistribution and use in source and binary forms, with or without modification, are
* permitted provided that the following conditions are met:
- *
+ *
* 1. Redistributions of source code must retain the above copyright notice, this list of
* conditions and the following disclaimer.
- *
+ *
* 2. Redistributions in binary form must reproduce the above copyright notice, this list
* of conditions and the following disclaimer in the documentation and/or other materials
* provided with the distribution.
- *
+ *
* THIS SOFTWARE IS PROVIDED BY JogAmp Community ``AS IS'' AND ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
* FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JogAmp Community OR
@@ -20,7 +20,7 @@
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- *
+ *
* The views and conclusions contained in the software and documentation are those of the
* authors and should not be interpreted as representing official policies, either expressed
* or implied, of JogAmp Community.
@@ -56,30 +56,30 @@ import org.junit.runners.MethodSorters;
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class CachedBufferFactoryTest extends JunitTracer {
-
+
private final int BUFFERCOUNT = 120;
-
+
private static int[] sizes;
private static int[] values;
private static IntBuffer[] buffers;
-
+
@Before
public void setup() {
-
+
sizes = new int[BUFFERCOUNT];
values = new int[sizes.length];
buffers = new IntBuffer[sizes.length];
-
- Random rnd = new Random(7);
-
+
+ final Random rnd = new Random(7);
+
// setup
for (int i = 0; i < sizes.length; i++) {
sizes[i] = rnd.nextInt(80)+1;
values[i] = rnd.nextInt();
}
-
+
}
-
+
@After
public void teardown() {
sizes = null;
@@ -89,34 +89,34 @@ public class CachedBufferFactoryTest extends JunitTracer {
@Test
public void dynamicTest() {
-
- CachedBufferFactory factory = CachedBufferFactory.create(64);
-
+
+ final CachedBufferFactory factory = CachedBufferFactory.create(64);
+
// create
for (int i = 0; i < sizes.length; i++) {
buffers[i] = factory.newDirectIntBuffer(sizes[i]);
assertEquals(ByteOrder.nativeOrder(), buffers[i].order());
fill(buffers[i], values[i]);
}
-
+
// check
checkBuffers(buffers, sizes, values);
-
+
}
-
+
@Test
public void dynamicConcurrentTest() throws InterruptedException, ExecutionException {
-
+
final CachedBufferFactory factory = CachedBufferFactory.createSynchronized(24);
-
- List<Callable<Object>> callables = new ArrayList<Callable<Object>>();
-
+
+ final List<Callable<Object>> callables = new ArrayList<Callable<Object>>();
+
final CountDownLatch latch = new CountDownLatch(10);
-
+
// create
for (int i = 0; i < sizes.length; i++) {
final int n = i;
- Callable<Object> c = new Callable<Object>() {
+ final Callable<Object> c = new Callable<Object>() {
public Object call() throws Exception {
latch.countDown();
latch.await();
@@ -127,52 +127,52 @@ public class CachedBufferFactoryTest extends JunitTracer {
};
callables.add(c);
}
-
- ExecutorService dathVader = Executors.newFixedThreadPool(10);
+
+ final ExecutorService dathVader = Executors.newFixedThreadPool(10);
dathVader.invokeAll(callables);
-
+
dathVader.shutdown();
-
+
// check
checkBuffers(buffers, sizes, values);
-
+
}
- private void checkBuffers(IntBuffer[] buffers, int[] sizes, int[] values) {
+ private void checkBuffers(final IntBuffer[] buffers, final int[] sizes, final int[] values) {
for (int i = 0; i < buffers.length; i++) {
- IntBuffer buffer = buffers[i];
+ final IntBuffer buffer = buffers[i];
assertEquals(sizes[i], buffer.capacity());
assertEquals(0, buffer.position());
assertTrue(equals(buffer, values[i]));
}
}
-
+
@Test
public void staticTest() {
-
- CachedBufferFactory factory = CachedBufferFactory.create(10, true);
-
+
+ final CachedBufferFactory factory = CachedBufferFactory.create(10, true);
+
for (int i = 0; i < 5; i++) {
factory.newDirectByteBuffer(2);
}
-
+
try{
factory.newDirectByteBuffer(1);
fail();
- }catch (RuntimeException ex) {
+ }catch (final RuntimeException ex) {
// expected
}
-
+
}
-
- private void fill(IntBuffer buffer, int value) {
+
+ private void fill(final IntBuffer buffer, final int value) {
while(buffer.remaining() != 0)
buffer.put(value);
buffer.rewind();
}
-
- private boolean equals(IntBuffer buffer, int value) {
+
+ private boolean equals(final IntBuffer buffer, final int value) {
while(buffer.remaining() != 0) {
if(value != buffer.get())
return false;
@@ -181,37 +181,37 @@ public class CachedBufferFactoryTest extends JunitTracer {
buffer.rewind();
return true;
}
-
-
+
+
/* load testing */
-
+
private int size = 4;
- private int iterations = 10000;
-
+ private final int iterations = 10000;
+
// @Test
public Object loadTest() {
- CachedBufferFactory factory = CachedBufferFactory.create();
- ByteBuffer[] buffer = new ByteBuffer[iterations];
+ final CachedBufferFactory factory = CachedBufferFactory.create();
+ final ByteBuffer[] buffer = new ByteBuffer[iterations];
for (int i = 0; i < buffer.length; i++) {
buffer[i] = factory.newDirectByteBuffer(size);
}
return buffer;
}
-
+
// @Test
public Object referenceTest() {
- ByteBuffer[] buffer = new ByteBuffer[iterations];
+ final ByteBuffer[] buffer = new ByteBuffer[iterations];
for (int i = 0; i < buffer.length; i++) {
buffer[i] = Buffers.newDirectByteBuffer(size);
}
return buffer;
}
-
-
- public static void main(String[] args) {
-
+
+
+ public static void main(final String[] args) {
+
CachedBufferFactoryTest test = new CachedBufferFactoryTest();
-
+
out.print("warmup...");
Object obj = null;
for (int i = 0; i < 100; i++) {
@@ -220,33 +220,33 @@ public class CachedBufferFactoryTest extends JunitTracer {
gc();
}
out.println("done");
-
+
test = new CachedBufferFactoryTest();
gc();
-
+
for (int i = 0; i < 10; i++) {
-
+
out.println("allocation size: "+test.size);
-
+
long time = System.currentTimeMillis();
obj = test.referenceTest();
if(obj == null) return; // ref lock
-
+
out.println("reference: "+ (System.currentTimeMillis()-time));
gc();
-
+
time = currentTimeMillis();
obj = test.loadTest();
if(obj == null) return; // ref lock
-
+
out.println("factory: "+ (System.currentTimeMillis()-time));
-
+
gc();
-
+
test.size*=2;
}
-
+
}
-
+
}