]> gerrit.simantics Code Review - simantics/platform.git/blob - tests/org.simantics.db.tests/src/org/simantics/db/tests/performance/java/ThreadLocalTest.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 / ThreadLocalTest.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 import java.util.concurrent.atomic.AtomicInteger;
17
18 import org.simantics.db.testing.base.TestCommonPerf;
19
20
21 /**
22  *
23  */
24 public class ThreadLocalTest extends TestCommonPerf {
25
26         public void test() throws Exception {
27
28                 final ThreadLocal<AtomicInteger> local = new ThreadLocal<AtomicInteger>();
29                 
30                 class CounterThread extends Thread {
31
32                         final private Semaphore start;
33                         final private AtomicInteger counter;
34                         final private CountDownLatch latch;
35                         
36                         public CounterThread(Semaphore start, CountDownLatch latch, AtomicInteger counter) {
37                                 this.start = start;
38                                 this.counter = counter;
39                                 this.latch = latch;
40                         }
41                         
42                         @Override
43                         public void run() {
44                                 
45                                 local.set(new AtomicInteger());
46                                 
47                                 try {
48                                         start.acquire();
49                                 } catch (InterruptedException e) {
50                                         e.printStackTrace();
51                                 }
52
53                                 int work = 0;
54                                 for(int i=0;i<1000000;i++) {
55                                         for(int j=0;j<100;j++) local.get().incrementAndGet();
56                                 }
57                                 
58                                 latch.countDown();
59                                 
60                                 System.out.println("work=" + work);
61                                 
62                         }
63                         
64                 }
65                 
66                 CountDownLatch latch = new CountDownLatch(8);
67                 AtomicInteger counter = new AtomicInteger(0);
68                 Semaphore starter = new Semaphore(0);
69                 
70                 for(int i=0;i<8;i++) new CounterThread(starter, latch, counter).start();
71                 
72                 Thread.sleep(500);
73
74                 starter.release(8);
75                 long start = System.nanoTime();
76                 latch.await();
77                 long duration = System.nanoTime() - start;
78
79                 System.out.println("counter=" + counter.get());
80                 
81                 System.out.println("Finished in " + 1e-9*duration + "s.");
82                 
83         }
84         
85         public void test2() throws Exception {
86
87                 class CounterThread extends Thread {
88
89                         final private Semaphore start;
90                         final private AtomicInteger counter;
91                         final private CountDownLatch latch;
92                         
93                         public CounterThread(Semaphore start, CountDownLatch latch, AtomicInteger counter) {
94                                 this.start = start;
95                                 this.counter = counter;
96                                 this.latch = latch;
97                         }
98                         
99                         @Override
100                         public void run() {
101                                 
102                                 AtomicInteger ai = new AtomicInteger();
103                                 
104                                 try {
105                                         start.acquire();
106                                 } catch (InterruptedException e) {
107                                         e.printStackTrace();
108                                 }
109
110                                 int work = 0;
111                                 for(int i=0;i<1000000;i++) {
112                                         for(int j=0;j<100;j++) ai.incrementAndGet();
113                                 }
114                                 
115                                 latch.countDown();
116                                 
117                                 System.out.println("work=" + work);
118                                 
119                         }
120                         
121                 }
122                 
123                 CountDownLatch latch = new CountDownLatch(8);
124                 AtomicInteger counter = new AtomicInteger(0);
125                 Semaphore starter = new Semaphore(0);
126                 
127                 for(int i=0;i<8;i++) new CounterThread(starter, latch, counter).start();
128                 
129                 Thread.sleep(500);
130
131                 starter.release(8);
132                 long start = System.nanoTime();
133                 latch.await();
134                 long duration = System.nanoTime() - start;
135
136                 System.out.println("counter=" + counter.get());
137                 
138                 System.out.println("Finished in " + 1e-9*duration + "s.");
139                 
140         }
141
142 }