-package org.simantics.db.impl;\r
-\r
-import java.util.ArrayList;\r
-import java.util.concurrent.BlockingQueue;\r
-import java.util.concurrent.ConcurrentLinkedQueue;\r
-import java.util.concurrent.CountDownLatch;\r
-import java.util.concurrent.LinkedBlockingQueue;\r
-import java.util.concurrent.atomic.AtomicInteger;\r
-import java.util.concurrent.locks.ReentrantLock;\r
-\r
-public class PerformanceTests {\r
-\r
- \r
- public static void collectionTest1() {\r
-\r
- final BlockingQueue<Object> queue = new LinkedBlockingQueue<Object>();\r
- final CountDownLatch latch1 = new CountDownLatch(9);\r
- final CountDownLatch latch2 = new CountDownLatch(9);\r
- \r
- class T extends Thread {\r
-\r
- @Override\r
- public void run() {\r
- try {\r
- latch1.countDown();\r
- latch1.await();\r
- } catch (InterruptedException e) {\r
- e.printStackTrace();\r
- }\r
- for(int i=0;i<500000;i++) queue.add(this);\r
- try {\r
- latch2.countDown();\r
- latch2.await();\r
- } catch (InterruptedException e) {\r
- e.printStackTrace();\r
- }\r
- }\r
- \r
- }\r
- \r
-\r
- for(int i=0;i<8;i++) new T().start();\r
- \r
- try {\r
- latch1.countDown();\r
- latch1.await();\r
- long start = System.nanoTime();\r
- latch2.countDown();\r
- latch2.await();\r
- long done = System.nanoTime() - start;\r
- System.out.println("took " + 1e-9*done);\r
- start = System.nanoTime();\r
- ArrayList<Object> sink = new ArrayList<Object>();\r
- queue.drainTo(sink);\r
- done = System.nanoTime() - start;\r
- System.out.println("took " + 1e-9*done);\r
- \r
- } catch (InterruptedException e) {\r
- e.printStackTrace();\r
- }\r
- \r
- }\r
- \r
- public static void collectionTest2() {\r
-\r
- final ConcurrentLinkedQueue<Object> queue = new ConcurrentLinkedQueue<Object>();\r
- final CountDownLatch latch1 = new CountDownLatch(9);\r
- final CountDownLatch latch2 = new CountDownLatch(9);\r
- \r
- class T extends Thread {\r
-\r
- @Override\r
- public void run() {\r
- try {\r
- latch1.countDown();\r
- latch1.await();\r
- } catch (InterruptedException e) {\r
- e.printStackTrace();\r
- }\r
- for(int i=0;i<500000;i++) queue.add(this);\r
- try {\r
- latch2.countDown();\r
- latch2.await();\r
- } catch (InterruptedException e) {\r
- e.printStackTrace();\r
- }\r
- }\r
- \r
- }\r
- \r
-\r
- for(int i=0;i<8;i++) new T().start();\r
- \r
- try {\r
- latch1.countDown();\r
- latch1.await();\r
- long start = System.nanoTime();\r
- latch2.countDown();\r
- latch2.await();\r
- long done = System.nanoTime() - start;\r
- System.out.println("took " + 1e-9*done);\r
- start = System.nanoTime();\r
- ArrayList<Object> sink = new ArrayList<Object>();\r
- for(Object o : queue) sink.add(o);\r
- done = System.nanoTime() - start;\r
- System.out.println("took " + 1e-9*done);\r
- \r
- } catch (InterruptedException e) {\r
- e.printStackTrace();\r
- }\r
- \r
- }\r
-\r
- public static void collectionTest3() {\r
-\r
- final ArrayList<Object> queue = new ArrayList<Object>();\r
- final CountDownLatch latch1 = new CountDownLatch(9);\r
- final CountDownLatch latch2 = new CountDownLatch(9);\r
- \r
- class T extends Thread {\r
-\r
- @Override\r
- public void run() {\r
- try {\r
- latch1.countDown();\r
- latch1.await();\r
- } catch (InterruptedException e) {\r
- e.printStackTrace();\r
- }\r
- for(int i=0;i<500000;i++) {\r
- synchronized(queue) {\r
- queue.add(this);\r
- }\r
- }\r
- try {\r
- latch2.countDown();\r
- latch2.await();\r
- } catch (InterruptedException e) {\r
- e.printStackTrace();\r
- }\r
- }\r
- \r
- }\r
- \r
-\r
- for(int i=0;i<8;i++) new T().start();\r
- \r
- try {\r
- latch1.countDown();\r
- latch1.await();\r
- long start = System.nanoTime();\r
- latch2.countDown();\r
- latch2.await();\r
- long done = System.nanoTime() - start;\r
- System.out.println("took " + 1e-9*done);\r
- start = System.nanoTime();\r
- ArrayList<Object> sink = new ArrayList<Object>();\r
- sink.addAll(queue);\r
- done = System.nanoTime() - start;\r
- System.out.println("took " + 1e-9*done);\r
- \r
- } catch (InterruptedException e) {\r
- e.printStackTrace();\r
- }\r
- \r
- }\r
-\r
- public static void collectionTest4() {\r
-\r
- final ArrayList<Object> queue = new ArrayList<Object>();\r
- final CountDownLatch latch1 = new CountDownLatch(9);\r
- final CountDownLatch latch2 = new CountDownLatch(9);\r
- final ReentrantLock lock = new ReentrantLock();\r
- final ReentrantLock lock2 = new ReentrantLock();\r
- \r
- class T extends Thread {\r
-\r
- @Override\r
- public void run() {\r
- try {\r
- latch1.countDown();\r
- latch1.await();\r
- } catch (InterruptedException e) {\r
- e.printStackTrace();\r
- }\r
- for(int i=0;i<500000;i++) {\r
- lock.lock();\r
- queue.add(this);\r
- lock.unlock();\r
- lock2.tryLock();\r
- }\r
- try {\r
- latch2.countDown();\r
- latch2.await();\r
- } catch (InterruptedException e) {\r
- e.printStackTrace();\r
- }\r
- }\r
- \r
- }\r
- \r
-\r
- for(int i=0;i<8;i++) new T().start();\r
- \r
- try {\r
- latch1.countDown();\r
- latch1.await();\r
- long start = System.nanoTime();\r
- latch2.countDown();\r
- latch2.await();\r
- long done = System.nanoTime() - start;\r
- System.out.println("took " + 1e-9*done);\r
- start = System.nanoTime();\r
- ArrayList<Object> sink = new ArrayList<Object>();\r
- sink.addAll(queue);\r
- done = System.nanoTime() - start;\r
- System.out.println("took " + 1e-9*done);\r
- \r
- } catch (InterruptedException e) {\r
- e.printStackTrace();\r
- }\r
- \r
- }\r
-\r
- public static void counterTest1() {\r
-\r
- final CountDownLatch latch1 = new CountDownLatch(9);\r
- final CountDownLatch latch2 = new CountDownLatch(9);\r
- final AtomicInteger integer = new AtomicInteger();\r
- \r
- class T extends Thread {\r
-\r
- @Override\r
- public void run() {\r
- try {\r
- latch1.countDown();\r
- latch1.await();\r
- } catch (InterruptedException e) {\r
- e.printStackTrace();\r
- }\r
- for(int i=0;i<500000;i++) {\r
- integer.incrementAndGet();\r
- }\r
- try {\r
- latch2.countDown();\r
- latch2.await();\r
- } catch (InterruptedException e) {\r
- e.printStackTrace();\r
- }\r
- }\r
- \r
- }\r
- \r
-\r
- for(int i=0;i<8;i++) new T().start();\r
- \r
- try {\r
- latch1.countDown();\r
- latch1.await();\r
- long start = System.nanoTime();\r
- latch2.countDown();\r
- latch2.await();\r
- long done = System.nanoTime() - start;\r
- System.out.println("took " + 1e-9*done);\r
- \r
- } catch (InterruptedException e) {\r
- e.printStackTrace();\r
- }\r
- \r
- }\r
-\r
- static int counter = 0;\r
- \r
- public static void counterTest2() {\r
-\r
- final CountDownLatch latch1 = new CountDownLatch(9);\r
- final CountDownLatch latch2 = new CountDownLatch(9);\r
- final ReentrantLock lock = new ReentrantLock();\r
- \r
- class T extends Thread {\r
-\r
- @Override\r
- public void run() {\r
- try {\r
- latch1.countDown();\r
- latch1.await();\r
- } catch (InterruptedException e) {\r
- e.printStackTrace();\r
- }\r
- for(int i=0;i<500000;i++) {\r
- lock.lock();\r
- counter++;\r
- lock.unlock();\r
- }\r
- try {\r
- latch2.countDown();\r
- latch2.await();\r
- } catch (InterruptedException e) {\r
- e.printStackTrace();\r
- }\r
- }\r
- \r
- }\r
- \r
-\r
- for(int i=0;i<8;i++) new T().start();\r
- \r
- try {\r
- latch1.countDown();\r
- latch1.await();\r
- long start = System.nanoTime();\r
- latch2.countDown();\r
- latch2.await();\r
- long done = System.nanoTime() - start;\r
- System.out.println("took " + 1e-9*done);\r
- \r
- } catch (InterruptedException e) {\r
- e.printStackTrace();\r
- }\r
- \r
- }\r
-\r
- public static void counterTest3() {\r
-\r
- final CountDownLatch latch1 = new CountDownLatch(9);\r
- final CountDownLatch latch2 = new CountDownLatch(9);\r
- //final ReentrantLock lock = new ReentrantLock();\r
- \r
- class T extends Thread {\r
-\r
- @Override\r
- public void run() {\r
- try {\r
- latch1.countDown();\r
- latch1.await();\r
- } catch (InterruptedException e) {\r
- e.printStackTrace();\r
- }\r
- for(int i=0;i<500000;i++) {\r
- //lock.lock();\r
- counter++;\r
- //lock.unlock();\r
- }\r
- try {\r
- latch2.countDown();\r
- latch2.await();\r
- } catch (InterruptedException e) {\r
- e.printStackTrace();\r
- }\r
- }\r
- \r
- }\r
- \r
-\r
- for(int i=0;i<8;i++) new T().start();\r
- \r
- try {\r
- latch1.countDown();\r
- latch1.await();\r
- long start = System.nanoTime();\r
- latch2.countDown();\r
- latch2.await();\r
- long done = System.nanoTime() - start;\r
- System.out.println("took " + 1e-9*done);\r
- \r
- } catch (InterruptedException e) {\r
- e.printStackTrace();\r
- }\r
- \r
- }\r
- \r
- public static void main(String[] args) {\r
-// collectionTest1();\r
-// collectionTest2();\r
-// collectionTest3();\r
-// collectionTest4();\r
- counterTest3();\r
- }\r
- \r
-}\r
+package org.simantics.db.impl;
+
+import java.util.ArrayList;
+import java.util.concurrent.BlockingQueue;
+import java.util.concurrent.ConcurrentLinkedQueue;
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.LinkedBlockingQueue;
+import java.util.concurrent.atomic.AtomicInteger;
+import java.util.concurrent.locks.ReentrantLock;
+
+public class PerformanceTests {
+
+
+ public static void collectionTest1() {
+
+ final BlockingQueue<Object> queue = new LinkedBlockingQueue<Object>();
+ final CountDownLatch latch1 = new CountDownLatch(9);
+ final CountDownLatch latch2 = new CountDownLatch(9);
+
+ class T extends Thread {
+
+ @Override
+ public void run() {
+ try {
+ latch1.countDown();
+ latch1.await();
+ } catch (InterruptedException e) {
+ e.printStackTrace();
+ }
+ for(int i=0;i<500000;i++) queue.add(this);
+ try {
+ latch2.countDown();
+ latch2.await();
+ } catch (InterruptedException e) {
+ e.printStackTrace();
+ }
+ }
+
+ }
+
+
+ for(int i=0;i<8;i++) new T().start();
+
+ try {
+ latch1.countDown();
+ latch1.await();
+ long start = System.nanoTime();
+ latch2.countDown();
+ latch2.await();
+ long done = System.nanoTime() - start;
+ System.out.println("took " + 1e-9*done);
+ start = System.nanoTime();
+ ArrayList<Object> sink = new ArrayList<Object>();
+ queue.drainTo(sink);
+ done = System.nanoTime() - start;
+ System.out.println("took " + 1e-9*done);
+
+ } catch (InterruptedException e) {
+ e.printStackTrace();
+ }
+
+ }
+
+ public static void collectionTest2() {
+
+ final ConcurrentLinkedQueue<Object> queue = new ConcurrentLinkedQueue<Object>();
+ final CountDownLatch latch1 = new CountDownLatch(9);
+ final CountDownLatch latch2 = new CountDownLatch(9);
+
+ class T extends Thread {
+
+ @Override
+ public void run() {
+ try {
+ latch1.countDown();
+ latch1.await();
+ } catch (InterruptedException e) {
+ e.printStackTrace();
+ }
+ for(int i=0;i<500000;i++) queue.add(this);
+ try {
+ latch2.countDown();
+ latch2.await();
+ } catch (InterruptedException e) {
+ e.printStackTrace();
+ }
+ }
+
+ }
+
+
+ for(int i=0;i<8;i++) new T().start();
+
+ try {
+ latch1.countDown();
+ latch1.await();
+ long start = System.nanoTime();
+ latch2.countDown();
+ latch2.await();
+ long done = System.nanoTime() - start;
+ System.out.println("took " + 1e-9*done);
+ start = System.nanoTime();
+ ArrayList<Object> sink = new ArrayList<Object>();
+ for(Object o : queue) sink.add(o);
+ done = System.nanoTime() - start;
+ System.out.println("took " + 1e-9*done);
+
+ } catch (InterruptedException e) {
+ e.printStackTrace();
+ }
+
+ }
+
+ public static void collectionTest3() {
+
+ final ArrayList<Object> queue = new ArrayList<Object>();
+ final CountDownLatch latch1 = new CountDownLatch(9);
+ final CountDownLatch latch2 = new CountDownLatch(9);
+
+ class T extends Thread {
+
+ @Override
+ public void run() {
+ try {
+ latch1.countDown();
+ latch1.await();
+ } catch (InterruptedException e) {
+ e.printStackTrace();
+ }
+ for(int i=0;i<500000;i++) {
+ synchronized(queue) {
+ queue.add(this);
+ }
+ }
+ try {
+ latch2.countDown();
+ latch2.await();
+ } catch (InterruptedException e) {
+ e.printStackTrace();
+ }
+ }
+
+ }
+
+
+ for(int i=0;i<8;i++) new T().start();
+
+ try {
+ latch1.countDown();
+ latch1.await();
+ long start = System.nanoTime();
+ latch2.countDown();
+ latch2.await();
+ long done = System.nanoTime() - start;
+ System.out.println("took " + 1e-9*done);
+ start = System.nanoTime();
+ ArrayList<Object> sink = new ArrayList<Object>();
+ sink.addAll(queue);
+ done = System.nanoTime() - start;
+ System.out.println("took " + 1e-9*done);
+
+ } catch (InterruptedException e) {
+ e.printStackTrace();
+ }
+
+ }
+
+ public static void collectionTest4() {
+
+ final ArrayList<Object> queue = new ArrayList<Object>();
+ final CountDownLatch latch1 = new CountDownLatch(9);
+ final CountDownLatch latch2 = new CountDownLatch(9);
+ final ReentrantLock lock = new ReentrantLock();
+ final ReentrantLock lock2 = new ReentrantLock();
+
+ class T extends Thread {
+
+ @Override
+ public void run() {
+ try {
+ latch1.countDown();
+ latch1.await();
+ } catch (InterruptedException e) {
+ e.printStackTrace();
+ }
+ for(int i=0;i<500000;i++) {
+ lock.lock();
+ queue.add(this);
+ lock.unlock();
+ lock2.tryLock();
+ }
+ try {
+ latch2.countDown();
+ latch2.await();
+ } catch (InterruptedException e) {
+ e.printStackTrace();
+ }
+ }
+
+ }
+
+
+ for(int i=0;i<8;i++) new T().start();
+
+ try {
+ latch1.countDown();
+ latch1.await();
+ long start = System.nanoTime();
+ latch2.countDown();
+ latch2.await();
+ long done = System.nanoTime() - start;
+ System.out.println("took " + 1e-9*done);
+ start = System.nanoTime();
+ ArrayList<Object> sink = new ArrayList<Object>();
+ sink.addAll(queue);
+ done = System.nanoTime() - start;
+ System.out.println("took " + 1e-9*done);
+
+ } catch (InterruptedException e) {
+ e.printStackTrace();
+ }
+
+ }
+
+ public static void counterTest1() {
+
+ final CountDownLatch latch1 = new CountDownLatch(9);
+ final CountDownLatch latch2 = new CountDownLatch(9);
+ final AtomicInteger integer = new AtomicInteger();
+
+ class T extends Thread {
+
+ @Override
+ public void run() {
+ try {
+ latch1.countDown();
+ latch1.await();
+ } catch (InterruptedException e) {
+ e.printStackTrace();
+ }
+ for(int i=0;i<500000;i++) {
+ integer.incrementAndGet();
+ }
+ try {
+ latch2.countDown();
+ latch2.await();
+ } catch (InterruptedException e) {
+ e.printStackTrace();
+ }
+ }
+
+ }
+
+
+ for(int i=0;i<8;i++) new T().start();
+
+ try {
+ latch1.countDown();
+ latch1.await();
+ long start = System.nanoTime();
+ latch2.countDown();
+ latch2.await();
+ long done = System.nanoTime() - start;
+ System.out.println("took " + 1e-9*done);
+
+ } catch (InterruptedException e) {
+ e.printStackTrace();
+ }
+
+ }
+
+ static int counter = 0;
+
+ public static void counterTest2() {
+
+ final CountDownLatch latch1 = new CountDownLatch(9);
+ final CountDownLatch latch2 = new CountDownLatch(9);
+ final ReentrantLock lock = new ReentrantLock();
+
+ class T extends Thread {
+
+ @Override
+ public void run() {
+ try {
+ latch1.countDown();
+ latch1.await();
+ } catch (InterruptedException e) {
+ e.printStackTrace();
+ }
+ for(int i=0;i<500000;i++) {
+ lock.lock();
+ counter++;
+ lock.unlock();
+ }
+ try {
+ latch2.countDown();
+ latch2.await();
+ } catch (InterruptedException e) {
+ e.printStackTrace();
+ }
+ }
+
+ }
+
+
+ for(int i=0;i<8;i++) new T().start();
+
+ try {
+ latch1.countDown();
+ latch1.await();
+ long start = System.nanoTime();
+ latch2.countDown();
+ latch2.await();
+ long done = System.nanoTime() - start;
+ System.out.println("took " + 1e-9*done);
+
+ } catch (InterruptedException e) {
+ e.printStackTrace();
+ }
+
+ }
+
+ public static void counterTest3() {
+
+ final CountDownLatch latch1 = new CountDownLatch(9);
+ final CountDownLatch latch2 = new CountDownLatch(9);
+ //final ReentrantLock lock = new ReentrantLock();
+
+ class T extends Thread {
+
+ @Override
+ public void run() {
+ try {
+ latch1.countDown();
+ latch1.await();
+ } catch (InterruptedException e) {
+ e.printStackTrace();
+ }
+ for(int i=0;i<500000;i++) {
+ //lock.lock();
+ counter++;
+ //lock.unlock();
+ }
+ try {
+ latch2.countDown();
+ latch2.await();
+ } catch (InterruptedException e) {
+ e.printStackTrace();
+ }
+ }
+
+ }
+
+
+ for(int i=0;i<8;i++) new T().start();
+
+ try {
+ latch1.countDown();
+ latch1.await();
+ long start = System.nanoTime();
+ latch2.countDown();
+ latch2.await();
+ long done = System.nanoTime() - start;
+ System.out.println("took " + 1e-9*done);
+
+ } catch (InterruptedException e) {
+ e.printStackTrace();
+ }
+
+ }
+
+ public static void main(String[] args) {
+// collectionTest1();
+// collectionTest2();
+// collectionTest3();
+// collectionTest4();
+ counterTest3();
+ }
+
+}