1 package org.simantics.db.impl;
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;
11 public class PerformanceTests {
14 public static void collectionTest1() {
16 final BlockingQueue<Object> queue = new LinkedBlockingQueue<Object>();
17 final CountDownLatch latch1 = new CountDownLatch(9);
18 final CountDownLatch latch2 = new CountDownLatch(9);
20 class T extends Thread {
27 } catch (InterruptedException e) {
30 for(int i=0;i<500000;i++) queue.add(this);
34 } catch (InterruptedException e) {
42 for(int i=0;i<8;i++) new T().start();
47 long start = System.nanoTime();
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>();
55 done = System.nanoTime() - start;
56 System.out.println("took " + 1e-9*done);
58 } catch (InterruptedException e) {
64 public static void collectionTest2() {
66 final ConcurrentLinkedQueue<Object> queue = new ConcurrentLinkedQueue<Object>();
67 final CountDownLatch latch1 = new CountDownLatch(9);
68 final CountDownLatch latch2 = new CountDownLatch(9);
70 class T extends Thread {
77 } catch (InterruptedException e) {
80 for(int i=0;i<500000;i++) queue.add(this);
84 } catch (InterruptedException e) {
92 for(int i=0;i<8;i++) new T().start();
97 long start = System.nanoTime();
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);
108 } catch (InterruptedException e) {
114 public static void collectionTest3() {
116 final ArrayList<Object> queue = new ArrayList<Object>();
117 final CountDownLatch latch1 = new CountDownLatch(9);
118 final CountDownLatch latch2 = new CountDownLatch(9);
120 class T extends Thread {
127 } catch (InterruptedException e) {
130 for(int i=0;i<500000;i++) {
131 synchronized(queue) {
138 } catch (InterruptedException e) {
146 for(int i=0;i<8;i++) new T().start();
151 long start = System.nanoTime();
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>();
159 done = System.nanoTime() - start;
160 System.out.println("took " + 1e-9*done);
162 } catch (InterruptedException e) {
168 public static void collectionTest4() {
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();
176 class T extends Thread {
183 } catch (InterruptedException e) {
186 for(int i=0;i<500000;i++) {
195 } catch (InterruptedException e) {
203 for(int i=0;i<8;i++) new T().start();
208 long start = System.nanoTime();
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>();
216 done = System.nanoTime() - start;
217 System.out.println("took " + 1e-9*done);
219 } catch (InterruptedException e) {
225 public static void counterTest1() {
227 final CountDownLatch latch1 = new CountDownLatch(9);
228 final CountDownLatch latch2 = new CountDownLatch(9);
229 final AtomicInteger integer = new AtomicInteger();
231 class T extends Thread {
238 } catch (InterruptedException e) {
241 for(int i=0;i<500000;i++) {
242 integer.incrementAndGet();
247 } catch (InterruptedException e) {
255 for(int i=0;i<8;i++) new T().start();
260 long start = System.nanoTime();
263 long done = System.nanoTime() - start;
264 System.out.println("took " + 1e-9*done);
266 } catch (InterruptedException e) {
272 static int counter = 0;
274 public static void counterTest2() {
276 final CountDownLatch latch1 = new CountDownLatch(9);
277 final CountDownLatch latch2 = new CountDownLatch(9);
278 final ReentrantLock lock = new ReentrantLock();
280 class T extends Thread {
287 } catch (InterruptedException e) {
290 for(int i=0;i<500000;i++) {
298 } catch (InterruptedException e) {
306 for(int i=0;i<8;i++) new T().start();
311 long start = System.nanoTime();
314 long done = System.nanoTime() - start;
315 System.out.println("took " + 1e-9*done);
317 } catch (InterruptedException e) {
323 public static void counterTest3() {
325 final CountDownLatch latch1 = new CountDownLatch(9);
326 final CountDownLatch latch2 = new CountDownLatch(9);
327 //final ReentrantLock lock = new ReentrantLock();
329 class T extends Thread {
336 } catch (InterruptedException e) {
339 for(int i=0;i<500000;i++) {
347 } catch (InterruptedException e) {
355 for(int i=0;i<8;i++) new T().start();
360 long start = System.nanoTime();
363 long done = System.nanoTime() - start;
364 System.out.println("took " + 1e-9*done);
366 } catch (InterruptedException e) {
372 public static void main(String[] args) {
373 // collectionTest1();
374 // collectionTest2();
375 // collectionTest3();
376 // collectionTest4();