X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=bundles%2Forg.simantics.db.impl%2Fsrc%2Forg%2Fsimantics%2Fdb%2Fimpl%2FPerformanceTests.java;h=1db903547eb32c9c969bf2a8d111f965597bad55;hb=159d04234f7fbf7554910a154b29a5dd7bbc6068;hp=657e8a2e780112855b3df3458547814f4e985440;hpb=969bd23cab98a79ca9101af33334000879fb60c5;p=simantics%2Fplatform.git diff --git a/bundles/org.simantics.db.impl/src/org/simantics/db/impl/PerformanceTests.java b/bundles/org.simantics.db.impl/src/org/simantics/db/impl/PerformanceTests.java index 657e8a2e7..1db903547 100644 --- a/bundles/org.simantics.db.impl/src/org/simantics/db/impl/PerformanceTests.java +++ b/bundles/org.simantics.db.impl/src/org/simantics/db/impl/PerformanceTests.java @@ -1,380 +1,380 @@ -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 queue = new LinkedBlockingQueue(); - 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 sink = new ArrayList(); - 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 queue = new ConcurrentLinkedQueue(); - 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 sink = new ArrayList(); - 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 queue = new ArrayList(); - 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 sink = new ArrayList(); - 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 queue = new ArrayList(); - 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 sink = new ArrayList(); - 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(); - } - -} +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 queue = new LinkedBlockingQueue(); + 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 sink = new ArrayList(); + 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 queue = new ConcurrentLinkedQueue(); + 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 sink = new ArrayList(); + 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 queue = new ArrayList(); + 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 sink = new ArrayList(); + 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 queue = new ArrayList(); + 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 sink = new ArrayList(); + 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(); + } + +}