1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License v1.0
6 * which accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.db.tests.performance.java;
14 import gnu.trove.map.hash.TIntIntHashMap;
16 import java.util.concurrent.CountDownLatch;
17 import java.util.concurrent.Semaphore;
18 import java.util.concurrent.locks.Lock;
19 import java.util.concurrent.locks.ReentrantLock;
21 import org.simantics.db.testing.base.TestCommonPerf;
24 public class SynchronizedAccessTest extends TestCommonPerf {
26 public void perform3(int threads) throws InterruptedException {
28 final Lock lock = new ReentrantLock(false);
29 final TIntIntHashMap map = new TIntIntHashMap();
31 class CounterThread extends Thread {
33 final private int index;
34 final private Semaphore start;
35 final private CountDownLatch latch;
37 public CounterThread(int index, Semaphore start, CountDownLatch latch) {
48 } catch (InterruptedException e) {
52 for(int i=0;i<1000000;i++) {
64 CountDownLatch latch = new CountDownLatch(threads);
65 Semaphore starter = new Semaphore(0);
67 for(int i=0;i<threads;i++) new CounterThread(i, starter, latch).start();
71 starter.release(threads);
72 long start = System.nanoTime();
74 long duration = System.nanoTime() - start;
75 System.err.println("took " + 1e-9*duration);
79 public void perform2(int threads) throws InterruptedException {
81 final TIntIntHashMap map = new TIntIntHashMap();
83 class CounterThread extends Thread {
85 final private int index;
86 final private Semaphore start;
87 final private CountDownLatch latch;
89 public CounterThread(int index, Semaphore start, CountDownLatch latch) {
100 } catch (InterruptedException e) {
104 for(int i=0;i<1000000;i++) {
116 CountDownLatch latch = new CountDownLatch(threads);
117 Semaphore starter = new Semaphore(0);
119 for(int i=0;i<threads;i++) new CounterThread(i, starter, latch).start();
123 starter.release(threads);
124 long start = System.nanoTime();
126 long duration = System.nanoTime() - start;
127 System.err.println("took " + 1e-9*duration);
131 public void perform(int threads) throws InterruptedException {
133 class CounterThread extends Thread {
135 final private int index;
136 final private Semaphore start;
137 final private CountDownLatch latch;
139 public CounterThread(int index, Semaphore start, CountDownLatch latch) {
150 } catch (InterruptedException e) {
154 final TIntIntHashMap map = new TIntIntHashMap();
156 for(int i=0;i<1000000;i++) {
168 CountDownLatch latch = new CountDownLatch(threads);
169 Semaphore starter = new Semaphore(0);
171 for(int i=0;i<threads;i++) new CounterThread(i, starter, latch).start();
175 starter.release(threads);
176 long start = System.nanoTime();
178 long duration = System.nanoTime() - start;
179 System.err.println("took " + 1e-9*duration);
183 public void test() throws Exception {
185 for(int i=0;i<5;i++) perform(4);
186 for(int i=0;i<5;i++) perform2(4);
187 for(int i=0;i<5;i++) perform3(4);