]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.impl/src/org/simantics/db/impl/PerformanceTests.java
Disabled BOOKKEEPING flag for normal use
[simantics/platform.git] / bundles / org.simantics.db.impl / src / org / simantics / db / impl / PerformanceTests.java
1 package org.simantics.db.impl;
2
3 import java.util.ArrayList;
4 import java.util.concurrent.BlockingQueue;
5 import java.util.concurrent.ConcurrentLinkedQueue;
6 import java.util.concurrent.CountDownLatch;
7 import java.util.concurrent.LinkedBlockingQueue;
8 import java.util.concurrent.atomic.AtomicInteger;
9 import java.util.concurrent.locks.ReentrantLock;
10
11 public class PerformanceTests {
12
13         
14         public static void collectionTest1() {
15
16                 final BlockingQueue<Object> queue = new LinkedBlockingQueue<Object>();
17                 final CountDownLatch latch1 = new CountDownLatch(9);
18                 final CountDownLatch latch2 = new CountDownLatch(9);
19                 
20                 class T extends Thread {
21
22                         @Override
23                         public void run() {
24                                 try {
25                                         latch1.countDown();
26                                         latch1.await();
27                                 } catch (InterruptedException e) {
28                                         e.printStackTrace();
29                                 }
30                                 for(int i=0;i<500000;i++) queue.add(this);
31                                 try {
32                                         latch2.countDown();
33                                         latch2.await();
34                                 } catch (InterruptedException e) {
35                                         e.printStackTrace();
36                                 }
37                         }
38                         
39                 }
40                 
41
42                 for(int i=0;i<8;i++) new T().start();
43                 
44                 try {
45                         latch1.countDown();
46                         latch1.await();
47                         long start = System.nanoTime();
48                         latch2.countDown();
49                         latch2.await();
50                         long done = System.nanoTime() - start;
51                         System.out.println("took " + 1e-9*done);
52                         start = System.nanoTime();
53                         ArrayList<Object> sink = new ArrayList<Object>();
54                         queue.drainTo(sink);
55                         done = System.nanoTime() - start;
56                         System.out.println("took " + 1e-9*done);
57                         
58                 } catch (InterruptedException e) {
59                         e.printStackTrace();
60                 }
61                 
62         }
63         
64         public static void collectionTest2() {
65
66                 final ConcurrentLinkedQueue<Object> queue = new ConcurrentLinkedQueue<Object>();
67                 final CountDownLatch latch1 = new CountDownLatch(9);
68                 final CountDownLatch latch2 = new CountDownLatch(9);
69                 
70                 class T extends Thread {
71
72                         @Override
73                         public void run() {
74                                 try {
75                                         latch1.countDown();
76                                         latch1.await();
77                                 } catch (InterruptedException e) {
78                                         e.printStackTrace();
79                                 }
80                                 for(int i=0;i<500000;i++) queue.add(this);
81                                 try {
82                                         latch2.countDown();
83                                         latch2.await();
84                                 } catch (InterruptedException e) {
85                                         e.printStackTrace();
86                                 }
87                         }
88                         
89                 }
90                 
91
92                 for(int i=0;i<8;i++) new T().start();
93                 
94                 try {
95                         latch1.countDown();
96                         latch1.await();
97                         long start = System.nanoTime();
98                         latch2.countDown();
99                         latch2.await();
100                         long done = System.nanoTime() - start;
101                         System.out.println("took " + 1e-9*done);
102                         start = System.nanoTime();
103                         ArrayList<Object> sink = new ArrayList<Object>();
104                         for(Object o : queue) sink.add(o);
105                         done = System.nanoTime() - start;
106                         System.out.println("took " + 1e-9*done);
107                         
108                 } catch (InterruptedException e) {
109                         e.printStackTrace();
110                 }
111                 
112         }
113
114         public static void collectionTest3() {
115
116                 final ArrayList<Object> queue = new ArrayList<Object>();
117                 final CountDownLatch latch1 = new CountDownLatch(9);
118                 final CountDownLatch latch2 = new CountDownLatch(9);
119                 
120                 class T extends Thread {
121
122                         @Override
123                         public void run() {
124                                 try {
125                                         latch1.countDown();
126                                         latch1.await();
127                                 } catch (InterruptedException e) {
128                                         e.printStackTrace();
129                                 }
130                                 for(int i=0;i<500000;i++) {
131                                         synchronized(queue) {
132                                                 queue.add(this);
133                                         }
134                                 }
135                                 try {
136                                         latch2.countDown();
137                                         latch2.await();
138                                 } catch (InterruptedException e) {
139                                         e.printStackTrace();
140                                 }
141                         }
142                         
143                 }
144                 
145
146                 for(int i=0;i<8;i++) new T().start();
147                 
148                 try {
149                         latch1.countDown();
150                         latch1.await();
151                         long start = System.nanoTime();
152                         latch2.countDown();
153                         latch2.await();
154                         long done = System.nanoTime() - start;
155                         System.out.println("took " + 1e-9*done);
156                         start = System.nanoTime();
157                         ArrayList<Object> sink = new ArrayList<Object>();
158                         sink.addAll(queue);
159                         done = System.nanoTime() - start;
160                         System.out.println("took " + 1e-9*done);
161                         
162                 } catch (InterruptedException e) {
163                         e.printStackTrace();
164                 }
165                 
166         }
167
168         public static void collectionTest4() {
169
170                 final ArrayList<Object> queue = new ArrayList<Object>();
171                 final CountDownLatch latch1 = new CountDownLatch(9);
172                 final CountDownLatch latch2 = new CountDownLatch(9);
173                 final ReentrantLock lock = new ReentrantLock();
174                 final ReentrantLock lock2 = new ReentrantLock();
175                 
176                 class T extends Thread {
177
178                         @Override
179                         public void run() {
180                                 try {
181                                         latch1.countDown();
182                                         latch1.await();
183                                 } catch (InterruptedException e) {
184                                         e.printStackTrace();
185                                 }
186                                 for(int i=0;i<500000;i++) {
187                                         lock.lock();
188                                         queue.add(this);
189                                         lock.unlock();
190                                         lock2.tryLock();
191                                 }
192                                 try {
193                                         latch2.countDown();
194                                         latch2.await();
195                                 } catch (InterruptedException e) {
196                                         e.printStackTrace();
197                                 }
198                         }
199                         
200                 }
201                 
202
203                 for(int i=0;i<8;i++) new T().start();
204                 
205                 try {
206                         latch1.countDown();
207                         latch1.await();
208                         long start = System.nanoTime();
209                         latch2.countDown();
210                         latch2.await();
211                         long done = System.nanoTime() - start;
212                         System.out.println("took " + 1e-9*done);
213                         start = System.nanoTime();
214                         ArrayList<Object> sink = new ArrayList<Object>();
215                         sink.addAll(queue);
216                         done = System.nanoTime() - start;
217                         System.out.println("took " + 1e-9*done);
218                         
219                 } catch (InterruptedException e) {
220                         e.printStackTrace();
221                 }
222                 
223         }
224
225         public static void counterTest1() {
226
227                 final CountDownLatch latch1 = new CountDownLatch(9);
228                 final CountDownLatch latch2 = new CountDownLatch(9);
229                 final AtomicInteger integer = new AtomicInteger();
230                 
231                 class T extends Thread {
232
233                         @Override
234                         public void run() {
235                                 try {
236                                         latch1.countDown();
237                                         latch1.await();
238                                 } catch (InterruptedException e) {
239                                         e.printStackTrace();
240                                 }
241                                 for(int i=0;i<500000;i++) {
242                                         integer.incrementAndGet();
243                                 }
244                                 try {
245                                         latch2.countDown();
246                                         latch2.await();
247                                 } catch (InterruptedException e) {
248                                         e.printStackTrace();
249                                 }
250                         }
251                         
252                 }
253                 
254
255                 for(int i=0;i<8;i++) new T().start();
256                 
257                 try {
258                         latch1.countDown();
259                         latch1.await();
260                         long start = System.nanoTime();
261                         latch2.countDown();
262                         latch2.await();
263                         long done = System.nanoTime() - start;
264                         System.out.println("took " + 1e-9*done);
265                         
266                 } catch (InterruptedException e) {
267                         e.printStackTrace();
268                 }
269                 
270         }
271
272         static int counter = 0;
273         
274         public static void counterTest2() {
275
276                 final CountDownLatch latch1 = new CountDownLatch(9);
277                 final CountDownLatch latch2 = new CountDownLatch(9);
278                 final ReentrantLock lock = new ReentrantLock();
279                 
280                 class T extends Thread {
281
282                         @Override
283                         public void run() {
284                                 try {
285                                         latch1.countDown();
286                                         latch1.await();
287                                 } catch (InterruptedException e) {
288                                         e.printStackTrace();
289                                 }
290                                 for(int i=0;i<500000;i++) {
291                                         lock.lock();
292                                         counter++;
293                                         lock.unlock();
294                                 }
295                                 try {
296                                         latch2.countDown();
297                                         latch2.await();
298                                 } catch (InterruptedException e) {
299                                         e.printStackTrace();
300                                 }
301                         }
302                         
303                 }
304                 
305
306                 for(int i=0;i<8;i++) new T().start();
307                 
308                 try {
309                         latch1.countDown();
310                         latch1.await();
311                         long start = System.nanoTime();
312                         latch2.countDown();
313                         latch2.await();
314                         long done = System.nanoTime() - start;
315                         System.out.println("took " + 1e-9*done);
316                         
317                 } catch (InterruptedException e) {
318                         e.printStackTrace();
319                 }
320                 
321         }
322
323         public static void counterTest3() {
324
325                 final CountDownLatch latch1 = new CountDownLatch(9);
326                 final CountDownLatch latch2 = new CountDownLatch(9);
327                 //final ReentrantLock lock = new ReentrantLock();
328                 
329                 class T extends Thread {
330
331                         @Override
332                         public void run() {
333                                 try {
334                                         latch1.countDown();
335                                         latch1.await();
336                                 } catch (InterruptedException e) {
337                                         e.printStackTrace();
338                                 }
339                                 for(int i=0;i<500000;i++) {
340                                         //lock.lock();
341                                         counter++;
342                                         //lock.unlock();
343                                 }
344                                 try {
345                                         latch2.countDown();
346                                         latch2.await();
347                                 } catch (InterruptedException e) {
348                                         e.printStackTrace();
349                                 }
350                         }
351                         
352                 }
353                 
354
355                 for(int i=0;i<8;i++) new T().start();
356                 
357                 try {
358                         latch1.countDown();
359                         latch1.await();
360                         long start = System.nanoTime();
361                         latch2.countDown();
362                         latch2.await();
363                         long done = System.nanoTime() - start;
364                         System.out.println("took " + 1e-9*done);
365                         
366                 } catch (InterruptedException e) {
367                         e.printStackTrace();
368                 }
369                 
370         }
371         
372         public static void main(String[] args) {
373 //              collectionTest1();
374 //              collectionTest2();
375 //              collectionTest3();
376 //              collectionTest4();
377                 counterTest3();
378         }
379         
380 }