KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tctest > ConcurrentHashMapMultipleNodesTestApp


1 /*
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
3  */

4 package com.tctest;
5
6 import com.tc.object.config.ConfigVisitor;
7 import com.tc.object.config.DSOClientConfigHelper;
8 import com.tc.object.config.TransparencyClassSpec;
9 import com.tc.simulator.app.ApplicationConfig;
10 import com.tc.simulator.listener.ListenerProvider;
11 import com.tc.util.Assert;
12 import com.tctest.runner.AbstractTransparentApp;
13
14 import java.util.Random JavaDoc;
15 import java.util.UUID JavaDoc;
16 import java.util.concurrent.ConcurrentHashMap JavaDoc;
17 import java.util.concurrent.CyclicBarrier JavaDoc;
18
19 public class ConcurrentHashMapMultipleNodesTestApp extends AbstractTransparentApp {
20   private static final int CACHE_CONCURRENCY_LEVEL = 12;
21   private static final int NUM_OF_OBJECTS = 800;
22   private static final int MAX_OBJECTS = 400;
23   private static final boolean OP_SUCCEEDED = true;
24   private static final boolean OP_FAILED = false;
25   private static final int DURATION = 300000;
26
27   private final CyclicBarrier JavaDoc barrier;
28   private final SessionCache cache = new SessionCache();
29
30   public ConcurrentHashMapMultipleNodesTestApp(String JavaDoc appId, ApplicationConfig cfg, ListenerProvider listenerProvider) {
31     super(appId, cfg, listenerProvider);
32     barrier = new CyclicBarrier JavaDoc(getParticipantCount());
33   }
34
35   public void run() {
36     try {
37       int index = barrier.await();
38
39       if (index == 0) {
40         loadData();
41       }
42
43       barrier.await();
44
45       if (index != 0) {
46         runTest();
47       }
48
49       barrier.await();
50
51     } catch (Throwable JavaDoc t) {
52       notifyError(t);
53     }
54   }
55   
56   private void runTest() throws Throwable JavaDoc {
57     System.err.println("Start Running Test");
58     long currentTime = System.currentTimeMillis();
59     long spentTime = 0;
60     int count = 0;
61     while (spentTime < DURATION) {
62       System.err.println("Running " + (++count));
63       runReadTest();
64       runInsertTest();
65       long endTime = System.currentTimeMillis();
66       spentTime = spentTime + (endTime - currentTime);
67     }
68     System.err.println("Test FINISHED");
69   }
70
71   private void runReadTest() throws Throwable JavaDoc {
72     UUID JavaDoc uuid = UUID.randomUUID();
73     Random JavaDoc random = new Random JavaDoc(uuid.getLeastSignificantBits());
74     int id = (int)(random.nextGaussian() * MAX_OBJECTS);
75     
76     if (id < 0 || id >= MAX_OBJECTS) {
77       System.err.println("Skipping non existent user id" + id);
78       return;
79     }
80     
81     UserIdCacheKey key = new UserIdCacheKey(id);
82     System.err.println("Getting session for user id" + key.getUserId());
83     TerracottaSession session = cache.getSession(key);
84     if (session == null) {
85       System.err.println("Skipping non existent user id" + id);
86       return;
87     }
88     System.err.println("Got session user id" + key.getUserId());
89     Assert.assertEquals(key.getUserId(), session.getI());
90     System.err.flush();
91   }
92   
93   private void runInsertTest() throws Throwable JavaDoc {
94     UUID JavaDoc uuid = UUID.randomUUID();
95     Random JavaDoc random = new Random JavaDoc(uuid.getLeastSignificantBits());
96     int id = (int)(random.nextGaussian() * MAX_OBJECTS);
97     
98     if (id < 0 || id >= MAX_OBJECTS) {
99       return;
100     }
101     
102     UserIdCacheKey key = new UserIdCacheKey(id);
103     TerracottaSession session = cache.getSession(key);
104     if (session == null) {
105       session = new TerracottaSession(key.getUserId());
106       cache.insertSession(key, session);
107     }
108   }
109
110   private void loadData() {
111     for (int i = 0; i < NUM_OF_OBJECTS; i++) {
112       System.err.println("Loading object " + i);
113       UserIdCacheKey key = new UserIdCacheKey(i);
114       TerracottaSession session = new TerracottaSession(key.getUserId());
115       
116       cache.insertSession(key, session);
117     }
118   }
119
120   public static void visitL1DSOConfig(ConfigVisitor visitor, DSOClientConfigHelper config) {
121     String JavaDoc testClass = ConcurrentHashMapMultipleNodesTestApp.class.getName();
122     TransparencyClassSpec spec = config.getOrCreateSpec(testClass);
123
124     config.addIncludePattern(testClass + "$*", false, false, true);
125
126     String JavaDoc methodExpression = "* " + testClass + "*.*(..)";
127     config.addWriteAutolock(methodExpression);
128
129     spec.addRoot("barrier", "barrier");
130     spec.addRoot("cache", "cache");
131   }
132
133   private static class SessionCache {
134     private final ConcurrentHashMap JavaDoc cache = new ConcurrentHashMap JavaDoc(NUM_OF_OBJECTS, 0.75f, CACHE_CONCURRENCY_LEVEL);
135
136     public TerracottaSession getSession(UserIdCacheKey key) {
137       TerracottaSession session = null;
138       session = (TerracottaSession) cache.get(key);
139       return session;
140     }
141
142     public boolean insertSession(UserIdCacheKey key, TerracottaSession value) {
143       TerracottaSession session = null;
144       session = (TerracottaSession) cache.put(key, value);
145       if (session != null) {
146         System.err.println("Found TerracottaSession with user id: " + key.getUserId() + " already. Duplicate insert");
147         return OP_FAILED;
148       }
149       return OP_SUCCEEDED;
150     }
151   }
152
153   private static class TerracottaSession {
154     private final int i;
155
156     public TerracottaSession(int i) {
157       this.i = i;
158     }
159
160     public int getI() {
161       return i;
162     }
163   }
164
165   private static class UserIdCacheKey {
166     private int userId;
167
168     public UserIdCacheKey(int u) {
169       userId = u;
170     }
171
172     public boolean equals(Object JavaDoc other) {
173       if (null == other) return false;
174       if (!(other instanceof UserIdCacheKey)) return false;
175
176       return userId == ((UserIdCacheKey) other).userId;
177     }
178
179     public int hashCode() {
180       return userId;
181     }
182
183     public String JavaDoc toString() {
184       return Integer.toString(userId);
185     }
186
187     public int getUserId() {
188       return userId;
189     }
190   }
191 }
192
Popular Tags