GlueGen: src/junit/com/jogamp/common/nio/TestCachedBufferFactory.java Source File

GlueGen v2.6.0-rc-20250706 GlueGen, Native Binding Generator for Java™ (public API).

TestCachedBufferFactory.java
Go to the documentation of this file.
1/*
2 * Copyright 2011 JogAmp Community. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without modification, are
5 * permitted provided that the following conditions are met:
6 *
7 * 1. Redistributions of source code must retain the above copyright notice, this list of
8 * conditions and the following disclaimer.
9 *
10 * 2. Redistributions in binary form must reproduce the above copyright notice, this list
11 * of conditions and the following disclaimer in the documentation and/or other materials
12 * provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY JogAmp Community ``AS IS'' AND ANY EXPRESS OR IMPLIED
15 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
16 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JogAmp Community OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
19 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
20 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
21 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
22 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23 *
24 * The views and conclusions contained in the software and documentation are those of the
25 * authors and should not be interpreted as representing official policies, either expressed
26 * or implied, of JogAmp Community.
27 */
28package com.jogamp.common.nio;
29
30import java.nio.ByteBuffer;
31import java.nio.ByteOrder;
32import java.nio.IntBuffer;
33import java.util.ArrayList;
34import java.util.List;
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;
43import org.junit.Test;
44
45import com.jogamp.junit.util.SingletonJunitCase;
46
47import static java.lang.System.*;
48import static org.junit.Assert.*;
49
50/**
51 *
52 * @author Michael Bien
53 */
54import org.junit.FixMethodOrder;
55import org.junit.runners.MethodSorters;
56
57@FixMethodOrder(MethodSorters.NAME_ASCENDING)
58public class TestCachedBufferFactory extends SingletonJunitCase {
59
60 private final int BUFFERCOUNT = 120;
61
62 private static int[] sizes;
63 private static int[] values;
64 private static IntBuffer[] buffers;
65
66 @Before
67 public void setup() {
68
69 sizes = new int[BUFFERCOUNT];
70 values = new int[sizes.length];
71 buffers = new IntBuffer[sizes.length];
72
73 final Random rnd = new Random(7);
74
75 // setup
76 for (int i = 0; i < sizes.length; i++) {
77 sizes[i] = rnd.nextInt(80)+1;
78 values[i] = rnd.nextInt();
79 }
80
81 }
82
83 @After
84 public void teardown() {
85 sizes = null;
86 values = null;
87 buffers = null;
88 }
89
90 @Test
91 public void dynamicTest() {
92
93 final CachedBufferFactory factory = CachedBufferFactory.create(64);
94
95 // create
96 for (int i = 0; i < sizes.length; i++) {
97 buffers[i] = factory.newDirectIntBuffer(sizes[i]);
98 assertEquals(ByteOrder.nativeOrder(), buffers[i].order());
99 fill(buffers[i], values[i]);
100 }
101
102 // check
103 checkBuffers(buffers, sizes, values);
104
105 }
106
107 @Test
108 public void dynamicConcurrentTest() throws InterruptedException, ExecutionException {
109
110 final CachedBufferFactory factory = CachedBufferFactory.createSynchronized(24);
111
112 final List<Callable<Object>> callables = new ArrayList<Callable<Object>>();
113
114 final CountDownLatch latch = new CountDownLatch(10);
115
116 // create
117 for (int i = 0; i < sizes.length; i++) {
118 final int n = i;
119 final Callable<Object> c = new Callable<Object>() {
120 public Object call() throws Exception {
121 latch.countDown();
122 latch.await();
123 buffers[n] = factory.newDirectIntBuffer(sizes[n]);
124 fill(buffers[n], values[n]);
125 return null;
126 }
127 };
128 callables.add(c);
129 }
130
131 final ExecutorService dathVader = Executors.newFixedThreadPool(10);
132 dathVader.invokeAll(callables);
133
134 dathVader.shutdown();
135
136 // check
137 checkBuffers(buffers, sizes, values);
138
139 }
140
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]));
147 }
148 }
149
150 @Test
151 public void staticTest() {
152
153 final CachedBufferFactory factory = CachedBufferFactory.create(10, true);
154
155 for (int i = 0; i < 5; i++) {
156 factory.newDirectByteBuffer(2);
157 }
158
159 try{
160 factory.newDirectByteBuffer(1);
161 fail();
162 }catch (final RuntimeException ex) {
163 // expected
164 }
165
166 }
167
168 private void fill(final IntBuffer buffer, final int value) {
169 while(buffer.remaining() != 0)
170 buffer.put(value);
171
172 buffer.rewind();
173 }
174
175 private boolean equals(final IntBuffer buffer, final int value) {
176 while(buffer.remaining() != 0) {
177 if(value != buffer.get())
178 return false;
179 }
180
181 buffer.rewind();
182 return true;
183 }
184
185
186 /* load testing */
187
188 private int size = 4;
189 private final int iterations = 10000;
190
191// @Test
192 public Object loadTest() {
193 final CachedBufferFactory factory = CachedBufferFactory.create();
194 final ByteBuffer[] buffer = new ByteBuffer[iterations];
195 for (int i = 0; i < buffer.length; i++) {
196 buffer[i] = factory.newDirectByteBuffer(size);
197 }
198 return buffer;
199 }
200
201// @Test
202 public Object referenceTest() {
203 final ByteBuffer[] buffer = new ByteBuffer[iterations];
204 for (int i = 0; i < buffer.length; i++) {
205 buffer[i] = Buffers.newDirectByteBuffer(size);
206 }
207 return buffer;
208 }
209
210
211 public static void main(final String[] args) {
212
213 TestCachedBufferFactory test = new TestCachedBufferFactory();
214
215 out.print("warmup...");
216 Object obj = null;
217 for (int i = 0; i < 100; i++) {
218 obj = test.referenceTest();
219 obj = test.loadTest();
220 gc();
221 }
222 out.println("done");
223
224 test = new TestCachedBufferFactory();
225 gc();
226
227 for (int i = 0; i < 10; i++) {
228
229 out.println("allocation size: "+test.size);
230
231 long time = System.currentTimeMillis();
232 obj = test.referenceTest();
233 if(obj == null) return; // ref lock
234
235 out.println("reference: "+ (System.currentTimeMillis()-time));
236
237 gc();
238
239 time = currentTimeMillis();
240 obj = test.loadTest();
241 if(obj == null) return; // ref lock
242
243 out.println("factory: "+ (System.currentTimeMillis()-time));
244
245 gc();
246
247 test.size*=2;
248 }
249
250 }
251
252}
com.jogamp.common.nio.Buffers
Utility methods allowing easy java.nio.Buffer manipulations.
Definition: Buffers.java:70
com.jogamp.common.nio.Buffers.newDirectByteBuffer
static ByteBuffer newDirectByteBuffer(final int numElements)
Allocates a new direct ByteBuffer with the specified number of elements.
Definition: Buffers.java:92
com.jogamp.common.nio.CachedBufferFactory
Buffer factory attempting to reduce buffer creation overhead.
Definition: CachedBufferFactory.java:64
com.jogamp.common.nio.CachedBufferFactory.newDirectIntBuffer
IntBuffer newDirectIntBuffer(final int numElements)
Definition: CachedBufferFactory.java:243
com.jogamp.common.nio.CachedBufferFactory.create
static CachedBufferFactory create()
Creates a factory with initial size and allocation size set to DEFAULT_ALLOCATION_SIZE.
Definition: CachedBufferFactory.java:88
com.jogamp.common.nio.CachedBufferFactory.createSynchronized
static CachedBufferFactory createSynchronized()
Synchronized version of create().
Definition: CachedBufferFactory.java:121
com.jogamp.common.nio.CachedBufferFactory.newDirectByteBuffer
ByteBuffer newDirectByteBuffer(final int size)
Definition: CachedBufferFactory.java:177
com.jogamp.common.nio.TestCachedBufferFactory
Definition: TestCachedBufferFactory.java:58
com.jogamp.common.nio.TestCachedBufferFactory.staticTest
void staticTest()
Definition: TestCachedBufferFactory.java:151
com.jogamp.common.nio.TestCachedBufferFactory.teardown
void teardown()
Definition: TestCachedBufferFactory.java:84
com.jogamp.common.nio.TestCachedBufferFactory.dynamicTest
void dynamicTest()
Definition: TestCachedBufferFactory.java:91
com.jogamp.common.nio.TestCachedBufferFactory.setup
void setup()
Definition: TestCachedBufferFactory.java:67
com.jogamp.common.nio.TestCachedBufferFactory.referenceTest
Object referenceTest()
Definition: TestCachedBufferFactory.java:202
com.jogamp.common.nio.TestCachedBufferFactory.main
static void main(final String[] args)
Definition: TestCachedBufferFactory.java:211
com.jogamp.common.nio.TestCachedBufferFactory.loadTest
Object loadTest()
Definition: TestCachedBufferFactory.java:192
com.jogamp.common.nio.TestCachedBufferFactory.dynamicConcurrentTest
void dynamicConcurrentTest()
Definition: TestCachedBufferFactory.java:108
com.jogamp.junit.util.SingletonJunitCase
Definition: SingletonJunitCase.java:39