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 java.util.concurrent.CountDownLatch;
15 import java.util.concurrent.Semaphore;
17 import org.simantics.db.testing.base.TestCommonPerf;
23 public class SynchronizationTest extends TestCommonPerf {
25 // public void test() throws Exception {
27 // class CounterThread extends Thread {
29 // final private Semaphore start;
30 // final private AtomicInteger counter;
31 // final private CountDownLatch latch;
33 // public CounterThread(Semaphore start, CountDownLatch latch, AtomicInteger counter) {
34 // this.start = start;
35 // this.counter = counter;
36 // this.latch = latch;
40 // public void run() {
44 // } catch (InterruptedException e) {
45 // e.printStackTrace();
49 // for(int i=0;i<10000000;i++) {
50 // counter.incrementAndGet();
51 // for(int j=0;j<100;j++) work++;
52 // counter.decrementAndGet();
57 // System.out.println("work=" + work);
63 // CountDownLatch latch = new CountDownLatch(8);
64 // AtomicInteger counter = new AtomicInteger(0);
65 // Semaphore starter = new Semaphore(0);
67 // for(int i=0;i<8;i++) new CounterThread(starter, latch, counter).start();
71 // starter.release(8);
72 // long start = System.nanoTime();
74 // long duration = System.nanoTime() - start;
76 // System.out.println("counter=" + counter.get());
78 // System.out.println("Finished in " + 1e-9*duration + "s.");
82 // public void test2() throws Exception {
84 // class CounterThread extends Thread {
86 // final private int index;
87 // final private Semaphore start;
88 // final private AtomicIntegerArray counter;
89 // final private CountDownLatch latch;
91 // public CounterThread(int index, Semaphore start, CountDownLatch latch, AtomicIntegerArray counter) {
92 // this.index = index;
93 // this.start = start;
94 // this.counter = counter;
95 // this.latch = latch;
99 // public void run() {
103 // } catch (InterruptedException e) {
104 // e.printStackTrace();
107 // for(int i=0;i<50000000;i++) {
108 // counter.incrementAndGet(index);
111 // latch.countDown();
117 // CountDownLatch latch = new CountDownLatch(4);
118 // AtomicIntegerArray counter = new AtomicIntegerArray(4);
119 // Semaphore starter = new Semaphore(0);
121 // for(int i=0;i<4;i++) new CounterThread(i, starter, latch, counter).start();
123 // Thread.sleep(500);
125 // starter.release(4);
126 // long start = System.nanoTime();
128 // long duration = System.nanoTime() - start;
130 // //System.out.println("counter=" + counter.get());
132 // System.out.println("[counter=" + counter + "]: Finished in " + 1e-9*duration + "s.");
136 // public void test3() throws Exception {
138 // class CounterThread extends Thread {
140 // final private int index;
141 // final private Semaphore start;
142 // final private AtomicInteger counter;
143 // final private CountDownLatch latch;
145 // public CounterThread(int index, Semaphore start, CountDownLatch latch, AtomicInteger counter) {
146 // this.index = index;
147 // this.start = start;
148 // this.counter = counter;
149 // this.latch = latch;
153 // public void run() {
157 // } catch (InterruptedException e) {
158 // e.printStackTrace();
161 // for(int i=0;i<50000000;i++) {
162 // counter.incrementAndGet();
165 // latch.countDown();
171 // CountDownLatch latch = new CountDownLatch(4);
172 // AtomicInteger counter = new AtomicInteger(0);
173 // Semaphore starter = new Semaphore(0);
175 // for(int i=0;i<4;i++) new CounterThread(i, starter, latch, counter).start();
177 // Thread.sleep(500);
179 // starter.release(4);
180 // long start = System.nanoTime();
182 // long duration = System.nanoTime() - start;
184 // //System.out.println("counter=" + counter.get());
186 // System.out.println("[counter=" + counter + "]: Finished in " + 1e-9*duration + "s.");
190 public void test4() throws Exception {
194 for(int i=0;i<2;i++) {
195 runTest3(1, size, 4);
196 runTest3(2, size, 4);
197 runTest3(4, size, 4);
198 runTest3(8, size, 4);
199 runTest3(16, size, 4);
200 runTest3(32, size, 4);
201 runTest3(64, size, 4);
202 runTest3(128, size, 4);
203 runTest3(256, size, 4);
204 runTest3(512, size, 4);
209 private void runTest3(final int STRIPE, final int size, final int threads) throws Exception {
211 final int[] work = new int[STRIPE*threads];
213 class CounterThread extends Thread {
215 final private int index;
216 final private Semaphore start;
217 final private CountDownLatch latch;
219 public CounterThread(int index, Semaphore start, CountDownLatch latch) {
230 } catch (InterruptedException e) {
234 int offset = STRIPE*index;
236 for(int i=0;i<size;i++) work[offset]++;
244 CountDownLatch latch = new CountDownLatch(threads);
245 Semaphore starter = new Semaphore(0);
247 for(int i=0;i<threads;i++) new CounterThread(i, starter, latch).start();
251 starter.release(threads);
252 long start = System.nanoTime();
254 long duration = System.nanoTime() - start;
257 for(int i=0;i<STRIPE*threads;i++) sum += work[i];
259 // System.out.println("work=" + sum);
262 //System.out.println("counter=" + counter.get());
264 System.out.println("[STRIPE=" + STRIPE + ", work=" + sum + "]: Finished in " + 1e-9*duration + "s.");