]> gerrit.simantics Code Review - simantics/platform.git/blob - 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
1 package org.simantics.db.tests.performance.java;
2
3 import gnu.trove.list.array.TByteArrayList;
4
5 import java.io.ByteArrayOutputStream;
6 import java.io.DataOutputStream;
7 import java.nio.ByteBuffer;
8 import java.nio.ByteOrder;
9 import java.util.Arrays;
10
11 import org.simantics.db.testing.base.TestCommonPerf;
12
13 public class BufferTest extends TestCommonPerf {
14
15     public static void test() throws Exception {
16         final boolean DEBUG = false;
17         final int BUFFER_SIZE = 100000000;
18         final int LOOP_SIZE = BUFFER_SIZE / 10;
19         {
20             ByteBuffer b = ByteBuffer.wrap(new byte[BUFFER_SIZE]);
21             long start = System.nanoTime();
22             b.order(ByteOrder.LITTLE_ENDIAN);
23             for(int i=0;i<LOOP_SIZE;i++) {
24                 b.asShortBuffer().put((short)i);
25                 b.asIntBuffer().put(i);
26             }
27             long duration = System.nanoTime() - start;
28             if (DEBUG)
29                 System.err.println("took " + 1e-9*duration);
30         }
31         {
32             DataOutputStream ds = new DataOutputStream(new ByteArrayOutputStream(BUFFER_SIZE));
33             long start2 = System.nanoTime();
34             for(int i=0;i<LOOP_SIZE;i++) {
35                 ds.writeShort(i);
36                 ds.writeInt(i);
37             }
38             long duration2 = System.nanoTime() - start2;
39             if (DEBUG)
40                 System.err.println("took " + 1e-9*duration2);
41         }
42         byte[] arr = new byte[BUFFER_SIZE];
43         {
44             long start3 = System.nanoTime();
45             int index = 0;
46             for(int i=0;i<LOOP_SIZE;i++) {
47                 short s = (short)i;
48                 arr[index++] = (byte)(s & 0xFF);
49                 arr[index++] = (byte)(s >>> 8);
50                 arr[index++] = (byte)(i & 0xFF);
51                 arr[index++] = (byte)((i >>> 8) & 0xFF);
52                 arr[index++] = (byte)((i >>> 16) & 0xFF);
53                 arr[index++] = (byte)((i >>> 24) & 0xFF);
54             }
55             long duration3 = System.nanoTime() - start3;
56             if (DEBUG) {
57                 System.err.println("took " + 1e-9*duration3);
58                 System.err.println(Arrays.hashCode(arr));
59             }
60         }
61         {
62             TByteArrayList list = new TByteArrayList(BUFFER_SIZE);
63             long start4 = System.nanoTime();
64             for(int i=0;i<LOOP_SIZE;i++) {
65                 short s = (short)i;
66                 list.add((byte)(s & 0xFF));
67                 list.add((byte)(s >>> 8));
68                 list.add((byte)(i & 0xFF));
69                 list.add((byte)((i >>> 8) & 0xFF));
70                 list.add((byte)((i >>> 16) & 0xFF));
71                 list.add((byte)((i >>> 24) & 0xFF));
72             }
73             long duration4 = System.nanoTime() - start4;
74             if (DEBUG) {
75                 System.err.println("took " + 1e-9*duration4);
76                 System.err.println(list.hashCode());
77             }
78         }
79         {
80             byte[] arr2 = new byte[BUFFER_SIZE];
81             long start5 = System.nanoTime();
82             for(int i=0;i<LOOP_SIZE;i++) {
83                 short s = (short)i;
84                 writeShort(arr2, s);
85                 writeInt(arr2, i);
86             }
87             long duration5 = System.nanoTime() - start5;
88             if (DEBUG) {
89                 System.err.println("took " + 1e-9*duration5);
90                 System.err.println(Arrays.hashCode(arr2));
91             }
92         }
93         {
94             byte[] arr3 = new byte[BUFFER_SIZE];
95             long start6 = System.nanoTime();
96             System.arraycopy(arr, 0, arr3, 0, BUFFER_SIZE);
97             long duration6 = System.nanoTime() - start6;
98             if (DEBUG)
99                 System.err.println("took " + 1e-9*duration6);
100         }
101     }
102
103     static private int byteIndex = 0;
104     
105     static private void writeShort(byte[] bytes, short s) {
106         bytes[byteIndex++] = (byte)(s & 0xFF);
107         bytes[byteIndex++] = (byte)(s >>> 8);
108     }
109
110     static private void writeInt(byte[] bytes, int i) {
111         bytes[byteIndex++] = (byte)(i & 0xFF);
112         bytes[byteIndex++] = (byte)((i >>> 8) & 0xFF);
113         bytes[byteIndex++] = (byte)((i >>> 16) & 0xFF);
114         bytes[byteIndex++] = (byte)((i >>> 24) & 0xFF);
115     }
116
117     public static void main(String[] args) {
118         try {
119             test();
120         } catch (Exception e) {
121             e.printStackTrace();
122         }
123     }
124
125 }