]> gerrit.simantics Code Review - simantics/platform.git/blob - tests/org.simantics.db.tests/src/org/simantics/db/tests/performance/java/SynchronizationTest.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 / SynchronizationTest.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 java.util.concurrent.CountDownLatch;
15 import java.util.concurrent.Semaphore;
16
17 import org.simantics.db.testing.base.TestCommonPerf;
18
19
20 /**
21  *
22  */
23 public class SynchronizationTest extends TestCommonPerf {
24
25 //      public void test() throws Exception {
26 //
27 //              class CounterThread extends Thread {
28 //
29 //                      final private Semaphore start;
30 //                      final private AtomicInteger counter;
31 //                      final private CountDownLatch latch;
32 //                      
33 //                      public CounterThread(Semaphore start, CountDownLatch latch, AtomicInteger counter) {
34 //                              this.start = start;
35 //                              this.counter = counter;
36 //                              this.latch = latch;
37 //                      }
38 //                      
39 //                      @Override
40 //                      public void run() {
41 //                              
42 //                              try {
43 //                                      start.acquire();
44 //                              } catch (InterruptedException e) {
45 //                                      e.printStackTrace();
46 //                              }
47 //
48 //                              int work = 0;
49 //                              for(int i=0;i<10000000;i++) {
50 //                                      counter.incrementAndGet();
51 //                                      for(int j=0;j<100;j++) work++;
52 //                                      counter.decrementAndGet();
53 //                              }
54 //                              
55 //                              latch.countDown();
56 //                              
57 //                              System.out.println("work=" + work);
58 //                              
59 //                      }
60 //                      
61 //              }
62 //              
63 //              CountDownLatch latch = new CountDownLatch(8);
64 //              AtomicInteger counter = new AtomicInteger(0);
65 //              Semaphore starter = new Semaphore(0);
66 //              
67 //              for(int i=0;i<8;i++) new CounterThread(starter, latch, counter).start();
68 //              
69 //              Thread.sleep(500);
70 //
71 //              starter.release(8);
72 //              long start = System.nanoTime();
73 //              latch.await();
74 //              long duration = System.nanoTime() - start;
75 //
76 //              System.out.println("counter=" + counter.get());
77 //              
78 //              System.out.println("Finished in " + 1e-9*duration + "s.");
79 //              
80 //      }
81 //
82 //      public void test2() throws Exception {
83 //
84 //              class CounterThread extends Thread {
85 //
86 //                      final private int index;
87 //                      final private Semaphore start;
88 //                      final private AtomicIntegerArray counter;
89 //                      final private CountDownLatch latch;
90 //                      
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;
96 //                      }
97 //                      
98 //                      @Override
99 //                      public void run() {
100 //                              
101 //                              try {
102 //                                      start.acquire();
103 //                              } catch (InterruptedException e) {
104 //                                      e.printStackTrace();
105 //                              }
106 //
107 //                              for(int i=0;i<50000000;i++) {
108 //                                      counter.incrementAndGet(index);
109 //                              }
110 //                              
111 //                              latch.countDown();
112 //                              
113 //                      }
114 //                      
115 //              }
116 //              
117 //              CountDownLatch latch = new CountDownLatch(4);
118 //              AtomicIntegerArray counter = new AtomicIntegerArray(4);
119 //              Semaphore starter = new Semaphore(0);
120 //              
121 //              for(int i=0;i<4;i++) new CounterThread(i, starter, latch, counter).start();
122 //              
123 //              Thread.sleep(500);
124 //
125 //              starter.release(4);
126 //              long start = System.nanoTime();
127 //              latch.await();
128 //              long duration = System.nanoTime() - start;
129 //
130 //              //System.out.println("counter=" + counter.get());
131 //              
132 //              System.out.println("[counter=" + counter + "]: Finished in " + 1e-9*duration + "s.");
133 //              
134 //      }
135 //      
136 //      public void test3() throws Exception {
137 //
138 //              class CounterThread extends Thread {
139 //
140 //                      final private int index;
141 //                      final private Semaphore start;
142 //                      final private AtomicInteger counter;
143 //                      final private CountDownLatch latch;
144 //                      
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;
150 //                      }
151 //                      
152 //                      @Override
153 //                      public void run() {
154 //                              
155 //                              try {
156 //                                      start.acquire();
157 //                              } catch (InterruptedException e) {
158 //                                      e.printStackTrace();
159 //                              }
160 //
161 //                              for(int i=0;i<50000000;i++) {
162 //                                      counter.incrementAndGet();
163 //                              }
164 //                              
165 //                              latch.countDown();
166 //                              
167 //                      }
168 //                      
169 //              }
170 //              
171 //              CountDownLatch latch = new CountDownLatch(4);
172 //              AtomicInteger counter = new AtomicInteger(0);
173 //              Semaphore starter = new Semaphore(0);
174 //              
175 //              for(int i=0;i<4;i++) new CounterThread(i, starter, latch, counter).start();
176 //              
177 //              Thread.sleep(500);
178 //
179 //              starter.release(4);
180 //              long start = System.nanoTime();
181 //              latch.await();
182 //              long duration = System.nanoTime() - start;
183 //
184 //              //System.out.println("counter=" + counter.get());
185 //              
186 //              System.out.println("[counter=" + counter + "]: Finished in " + 1e-9*duration + "s.");
187 //              
188 //      }
189
190         public void test4() throws Exception {
191
192                 int size = 50000000;
193
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);
205                 }
206                 
207         }
208         
209         private void runTest3(final int STRIPE, final int size, final int threads) throws Exception {
210                 
211                 final int[] work = new int[STRIPE*threads];
212                 
213                 class CounterThread extends Thread {
214
215                         final private int index;
216                         final private Semaphore start;
217                         final private CountDownLatch latch;
218                         
219                         public CounterThread(int index, Semaphore start, CountDownLatch latch) {
220                                 this.index = index;
221                                 this.start = start;
222                                 this.latch = latch;
223                         }
224                         
225                         @Override
226                         public void run() {
227                                 
228                                 try {
229                                         start.acquire();
230                                 } catch (InterruptedException e) {
231                                         e.printStackTrace();
232                                 }
233                                 
234                                 int offset = STRIPE*index;
235
236                                 for(int i=0;i<size;i++) work[offset]++; 
237                                 
238                                 latch.countDown();
239                                 
240                         }
241                         
242                 }
243                 
244                 CountDownLatch latch = new CountDownLatch(threads);
245                 Semaphore starter = new Semaphore(0);
246                 
247                 for(int i=0;i<threads;i++) new CounterThread(i, starter, latch).start();
248                 
249                 Thread.sleep(500);
250                 
251                 starter.release(threads);
252                 long start = System.nanoTime();
253                 latch.await();
254                 long duration = System.nanoTime() - start;
255
256                 int sum = 0;
257                 for(int i=0;i<STRIPE*threads;i++) sum += work[i];
258                 
259 //              System.out.println("work=" + sum);
260
261                 
262                 //System.out.println("counter=" + counter.get());
263                 
264                 System.out.println("[STRIPE=" + STRIPE + ", work=" + sum + "]: Finished in " + 1e-9*duration + "s.");
265                 
266         }
267         
268 }