]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - tests/org.simantics.db.tests/src/org/simantics/db/tests/performance/java/BufferTest.java
Added missing org.simantics.db.{tests,testing} plug-ins.
[simantics/platform.git] / tests / org.simantics.db.tests / src / org / simantics / db / tests / performance / java / BufferTest.java
diff --git a/tests/org.simantics.db.tests/src/org/simantics/db/tests/performance/java/BufferTest.java b/tests/org.simantics.db.tests/src/org/simantics/db/tests/performance/java/BufferTest.java
new file mode 100644 (file)
index 0000000..f9d2cbb
--- /dev/null
@@ -0,0 +1,125 @@
+package org.simantics.db.tests.performance.java;
+
+import gnu.trove.list.array.TByteArrayList;
+
+import java.io.ByteArrayOutputStream;
+import java.io.DataOutputStream;
+import java.nio.ByteBuffer;
+import java.nio.ByteOrder;
+import java.util.Arrays;
+
+import org.simantics.db.testing.base.TestCommonPerf;
+
+public class BufferTest extends TestCommonPerf {
+
+    public static void test() throws Exception {
+        final boolean DEBUG = false;
+        final int BUFFER_SIZE = 100000000;
+        final int LOOP_SIZE = BUFFER_SIZE / 10;
+        {
+            ByteBuffer b = ByteBuffer.wrap(new byte[BUFFER_SIZE]);
+            long start = System.nanoTime();
+            b.order(ByteOrder.LITTLE_ENDIAN);
+            for(int i=0;i<LOOP_SIZE;i++) {
+                b.asShortBuffer().put((short)i);
+                b.asIntBuffer().put(i);
+            }
+            long duration = System.nanoTime() - start;
+            if (DEBUG)
+                System.err.println("took " + 1e-9*duration);
+        }
+        {
+            DataOutputStream ds = new DataOutputStream(new ByteArrayOutputStream(BUFFER_SIZE));
+            long start2 = System.nanoTime();
+            for(int i=0;i<LOOP_SIZE;i++) {
+                ds.writeShort(i);
+                ds.writeInt(i);
+            }
+            long duration2 = System.nanoTime() - start2;
+            if (DEBUG)
+                System.err.println("took " + 1e-9*duration2);
+        }
+        byte[] arr = new byte[BUFFER_SIZE];
+        {
+            long start3 = System.nanoTime();
+            int index = 0;
+            for(int i=0;i<LOOP_SIZE;i++) {
+                short s = (short)i;
+                arr[index++] = (byte)(s & 0xFF);
+                arr[index++] = (byte)(s >>> 8);
+                arr[index++] = (byte)(i & 0xFF);
+                arr[index++] = (byte)((i >>> 8) & 0xFF);
+                arr[index++] = (byte)((i >>> 16) & 0xFF);
+                arr[index++] = (byte)((i >>> 24) & 0xFF);
+            }
+            long duration3 = System.nanoTime() - start3;
+            if (DEBUG) {
+                System.err.println("took " + 1e-9*duration3);
+                System.err.println(Arrays.hashCode(arr));
+            }
+        }
+        {
+            TByteArrayList list = new TByteArrayList(BUFFER_SIZE);
+            long start4 = System.nanoTime();
+            for(int i=0;i<LOOP_SIZE;i++) {
+                short s = (short)i;
+                list.add((byte)(s & 0xFF));
+                list.add((byte)(s >>> 8));
+                list.add((byte)(i & 0xFF));
+                list.add((byte)((i >>> 8) & 0xFF));
+                list.add((byte)((i >>> 16) & 0xFF));
+                list.add((byte)((i >>> 24) & 0xFF));
+            }
+            long duration4 = System.nanoTime() - start4;
+            if (DEBUG) {
+                System.err.println("took " + 1e-9*duration4);
+                System.err.println(list.hashCode());
+            }
+        }
+        {
+            byte[] arr2 = new byte[BUFFER_SIZE];
+            long start5 = System.nanoTime();
+            for(int i=0;i<LOOP_SIZE;i++) {
+                short s = (short)i;
+                writeShort(arr2, s);
+                writeInt(arr2, i);
+            }
+            long duration5 = System.nanoTime() - start5;
+            if (DEBUG) {
+                System.err.println("took " + 1e-9*duration5);
+                System.err.println(Arrays.hashCode(arr2));
+            }
+        }
+        {
+            byte[] arr3 = new byte[BUFFER_SIZE];
+            long start6 = System.nanoTime();
+            System.arraycopy(arr, 0, arr3, 0, BUFFER_SIZE);
+            long duration6 = System.nanoTime() - start6;
+            if (DEBUG)
+                System.err.println("took " + 1e-9*duration6);
+        }
+    }
+
+    static private int byteIndex = 0;
+    
+    static private void writeShort(byte[] bytes, short s) {
+        bytes[byteIndex++] = (byte)(s & 0xFF);
+        bytes[byteIndex++] = (byte)(s >>> 8);
+    }
+
+    static private void writeInt(byte[] bytes, int i) {
+        bytes[byteIndex++] = (byte)(i & 0xFF);
+        bytes[byteIndex++] = (byte)((i >>> 8) & 0xFF);
+        bytes[byteIndex++] = (byte)((i >>> 16) & 0xFF);
+        bytes[byteIndex++] = (byte)((i >>> 24) & 0xFF);
+    }
+
+    public static void main(String[] args) {
+        try {
+            test();
+        } catch (Exception e) {
+            e.printStackTrace();
+        }
+    }
+
+}