]> gerrit.simantics Code Review - simantics/platform.git/blob - tests/org.simantics.db.tests/src/org/simantics/db/tests/performance/java/SynchronizedAccessTest.java
Added missing org.simantics.db.{tests,testing} plug-ins.
[simantics/platform.git] / tests / org.simantics.db.tests / src / org / simantics / db / tests / performance / java / SynchronizedAccessTest.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management
3  * in Industry THTH ry.
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
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.db.tests.performance.java;
13
14 import gnu.trove.map.hash.TIntIntHashMap;
15
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;
20
21 import org.simantics.db.testing.base.TestCommonPerf;
22
23
24 public class SynchronizedAccessTest extends TestCommonPerf {
25
26         public void perform3(int threads) throws InterruptedException {
27
28                 final Lock lock = new ReentrantLock(false);
29                 final TIntIntHashMap map = new TIntIntHashMap();
30                 
31                 class CounterThread extends Thread {
32
33                         final private int index;
34                         final private Semaphore start;
35                         final private CountDownLatch latch;
36                         
37                         public CounterThread(int index, Semaphore start, CountDownLatch latch) {
38                                 this.index = index;
39                                 this.start = start;
40                                 this.latch = latch;
41                         }
42                         
43                         @Override
44                         public void run() {
45                                 
46                                 try {
47                                         start.acquire();
48                                 } catch (InterruptedException e) {
49                                         e.printStackTrace();
50                                 }
51                                 
52                                 for(int i=0;i<1000000;i++) {
53                                         lock.lock();
54                                         map.put(i, i);
55                                         lock.unlock();
56                                 }
57                                 
58                                 latch.countDown();
59                                 
60                         }
61                         
62                 }
63                 
64                 CountDownLatch latch = new CountDownLatch(threads);
65                 Semaphore starter = new Semaphore(0);
66                 
67                 for(int i=0;i<threads;i++) new CounterThread(i, starter, latch).start();
68                 
69                 Thread.sleep(500);
70                 
71                 starter.release(threads);
72                 long start = System.nanoTime();
73                 latch.await();
74                 long duration = System.nanoTime() - start;
75                 System.err.println("took " + 1e-9*duration);
76                 
77         }
78
79         public void perform2(int threads) throws InterruptedException {
80
81                 final TIntIntHashMap map = new TIntIntHashMap();
82                 
83                 class CounterThread extends Thread {
84
85                         final private int index;
86                         final private Semaphore start;
87                         final private CountDownLatch latch;
88                         
89                         public CounterThread(int index, Semaphore start, CountDownLatch latch) {
90                                 this.index = index;
91                                 this.start = start;
92                                 this.latch = latch;
93                         }
94                         
95                         @Override
96                         public void run() {
97                                 
98                                 try {
99                                         start.acquire();
100                                 } catch (InterruptedException e) {
101                                         e.printStackTrace();
102                                 }
103                                 
104                                 for(int i=0;i<1000000;i++) {
105                                         synchronized(map) {
106                                                 map.put(i, i);
107                                         }
108                                 }
109                                 
110                                 latch.countDown();
111                                 
112                         }
113                         
114                 }
115                 
116                 CountDownLatch latch = new CountDownLatch(threads);
117                 Semaphore starter = new Semaphore(0);
118                 
119                 for(int i=0;i<threads;i++) new CounterThread(i, starter, latch).start();
120                 
121                 Thread.sleep(500);
122                 
123                 starter.release(threads);
124                 long start = System.nanoTime();
125                 latch.await();
126                 long duration = System.nanoTime() - start;
127                 System.err.println("took " + 1e-9*duration);
128                 
129         }
130
131         public void perform(int threads) throws InterruptedException {
132
133                 class CounterThread extends Thread {
134
135                         final private int index;
136                         final private Semaphore start;
137                         final private CountDownLatch latch;
138                         
139                         public CounterThread(int index, Semaphore start, CountDownLatch latch) {
140                                 this.index = index;
141                                 this.start = start;
142                                 this.latch = latch;
143                         }
144                         
145                         @Override
146                         public void run() {
147                                 
148                                 try {
149                                         start.acquire();
150                                 } catch (InterruptedException e) {
151                                         e.printStackTrace();
152                                 }
153
154                                 final TIntIntHashMap map = new TIntIntHashMap();
155                                 
156                                 for(int i=0;i<1000000;i++) {
157                                         synchronized(map) {
158                                                 map.put(i, i);
159                                         }
160                                 }
161                                 
162                                 latch.countDown();
163                                 
164                         }
165                         
166                 }
167                 
168                 CountDownLatch latch = new CountDownLatch(threads);
169                 Semaphore starter = new Semaphore(0);
170                 
171                 for(int i=0;i<threads;i++) new CounterThread(i, starter, latch).start();
172                 
173                 Thread.sleep(500);
174                 
175                 starter.release(threads);
176                 long start = System.nanoTime();
177                 latch.await();
178                 long duration = System.nanoTime() - start;
179                 System.err.println("took " + 1e-9*duration);
180                 
181         }
182         
183         public void test() throws Exception {
184
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);
188                 
189         }
190
191 }