28package com.jogamp.common.nio;
30import java.nio.ByteBuffer;
31import java.nio.ByteOrder;
32import java.nio.IntBuffer;
33import java.util.ArrayList;
35import java.util.Random;
36import java.util.concurrent.Callable;
37import java.util.concurrent.CountDownLatch;
38import java.util.concurrent.ExecutionException;
39import java.util.concurrent.ExecutorService;
40import java.util.concurrent.Executors;
41import org.junit.After;
42import org.junit.Before;
45import com.jogamp.junit.util.SingletonJunitCase;
47import static java.lang.System.*;
48import static org.junit.Assert.*;
54import org.junit.FixMethodOrder;
55import org.junit.runners.MethodSorters;
57@FixMethodOrder(MethodSorters.NAME_ASCENDING)
60 private final int BUFFERCOUNT = 120;
62 private static int[] sizes;
63 private static int[] values;
64 private static IntBuffer[] buffers;
69 sizes =
new int[BUFFERCOUNT];
70 values =
new int[sizes.length];
71 buffers =
new IntBuffer[sizes.length];
73 final Random rnd =
new Random(7);
76 for (
int i = 0; i < sizes.length; i++) {
77 sizes[i] = rnd.nextInt(80)+1;
78 values[i] = rnd.nextInt();
96 for (
int i = 0; i < sizes.length; i++) {
98 assertEquals(ByteOrder.nativeOrder(), buffers[i].order());
99 fill(buffers[i], values[i]);
103 checkBuffers(buffers, sizes, values);
112 final List<Callable<Object>> callables =
new ArrayList<Callable<Object>>();
114 final CountDownLatch latch =
new CountDownLatch(10);
117 for (
int i = 0; i < sizes.length; i++) {
119 final Callable<Object> c =
new Callable<Object>() {
120 public Object call()
throws Exception {
124 fill(buffers[n], values[n]);
131 final ExecutorService dathVader = Executors.newFixedThreadPool(10);
132 dathVader.invokeAll(callables);
134 dathVader.shutdown();
137 checkBuffers(buffers, sizes, values);
141 private void checkBuffers(
final IntBuffer[] buffers,
final int[] sizes,
final int[] values) {
142 for (
int i = 0; i < buffers.length; i++) {
143 final IntBuffer buffer = buffers[i];
144 assertEquals(sizes[i], buffer.capacity());
145 assertEquals(0, buffer.position());
146 assertTrue(equals(buffer, values[i]));
155 for (
int i = 0; i < 5; i++) {
162 }
catch (
final RuntimeException ex) {
168 private void fill(
final IntBuffer buffer,
final int value) {
169 while(buffer.remaining() != 0)
175 private boolean equals(
final IntBuffer buffer,
final int value) {
176 while(buffer.remaining() != 0) {
177 if(value != buffer.get())
188 private int size = 4;
189 private final int iterations = 10000;
194 final ByteBuffer[] buffer =
new ByteBuffer[iterations];
195 for (
int i = 0; i < buffer.length; i++) {
203 final ByteBuffer[] buffer =
new ByteBuffer[iterations];
204 for (
int i = 0; i < buffer.length; i++) {
211 public static void main(
final String[] args) {
215 out.print(
"warmup...");
217 for (
int i = 0; i < 100; i++) {
227 for (
int i = 0; i < 10; i++) {
229 out.println(
"allocation size: "+test.size);
231 long time = System.currentTimeMillis();
233 if(obj ==
null)
return;
235 out.println(
"reference: "+ (System.currentTimeMillis()-time));
239 time = currentTimeMillis();
241 if(obj ==
null)
return;
243 out.println(
"factory: "+ (System.currentTimeMillis()-time));
Utility methods allowing easy java.nio.Buffer manipulations.
static ByteBuffer newDirectByteBuffer(final int numElements)
Allocates a new direct ByteBuffer with the specified number of elements.
Buffer factory attempting to reduce buffer creation overhead.
IntBuffer newDirectIntBuffer(final int numElements)
static CachedBufferFactory create()
Creates a factory with initial size and allocation size set to DEFAULT_ALLOCATION_SIZE.
static CachedBufferFactory createSynchronized()
Synchronized version of create().
ByteBuffer newDirectByteBuffer(final int size)
static void main(final String[] args)
void dynamicConcurrentTest()