KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tctest > longrunning > LongrunningGCTestApp


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.longrunning;
5
6 import com.tc.logging.LogLevel;
7 import com.tc.logging.TCLogger;
8 import com.tc.object.config.ConfigVisitor;
9 import com.tc.object.config.DSOApplicationConfig;
10 import com.tc.object.config.DSOClientConfigHelper;
11 import com.tc.object.config.TransparencyClassSpec;
12 import com.tc.simulator.app.Application;
13 import com.tc.simulator.app.ApplicationConfig;
14 import com.tc.simulator.listener.ListenerProvider;
15 import com.tc.simulator.listener.StatsListener;
16
17 import java.io.CharArrayWriter JavaDoc;
18 import java.io.PrintStream JavaDoc;
19 import java.io.PrintWriter JavaDoc;
20 import java.text.DateFormat JavaDoc;
21 import java.text.NumberFormat JavaDoc;
22 import java.util.ArrayList JavaDoc;
23 import java.util.Date JavaDoc;
24 import java.util.List JavaDoc;
25 import java.util.Properties JavaDoc;
26 import java.util.Random JavaDoc;
27
28 public class LongrunningGCTestApp implements Application {
29
30   private TCLogger logger = new TCLogger() {
31
32                                                  PrintStream JavaDoc out = System.out;
33
34                                                  DateFormat JavaDoc df = DateFormat.getDateTimeInstance();
35
36                                                  private void println(String JavaDoc header, Object JavaDoc message) {
37                                                    out.println(format(header + message));
38                                                  }
39
40                                                  private void println(String JavaDoc header, Object JavaDoc message, Throwable JavaDoc t) {
41                                                    out.println(format(header + message, t));
42                                                  }
43
44                                                  private String JavaDoc format(Object JavaDoc message) {
45                                                    return df.format(new java.util.Date JavaDoc()) + message;
46                                                  }
47
48                                                  private String JavaDoc format(Object JavaDoc message, Throwable JavaDoc t) {
49                                                    CharArrayWriter JavaDoc chaw = new CharArrayWriter JavaDoc();
50                                                    PrintWriter JavaDoc ps = new PrintWriter JavaDoc(chaw);
51                                                    t.printStackTrace(ps);
52                                                    ps.flush();
53
54                                                    return format(message + chaw.toString());
55                                                  }
56
57                                                  public void debug(Object JavaDoc message) {
58                                                    println(" DEBUG ", message);
59                                                  }
60
61                                                  public void debug(Object JavaDoc message, Throwable JavaDoc t) {
62                                                    println(" DEBUG ", message, t);
63                                                  }
64
65                                                  public void error(Object JavaDoc message) {
66                                                    println(" ERROR ", message);
67                                                  }
68
69                                                  public void error(Object JavaDoc message, Throwable JavaDoc t) {
70                                                    println(" ERROR ", message, t);
71                                                  }
72
73                                                  public void fatal(Object JavaDoc message) {
74                                                    println(" FATAL ", message);
75                                                  }
76
77                                                  public void fatal(Object JavaDoc message, Throwable JavaDoc t) {
78                                                    println(" FATAL ", message, t);
79                                                  }
80
81                                                  public void info(Object JavaDoc message) {
82                                                    println(" INFO ", message);
83                                                  }
84
85                                                  public void info(Object JavaDoc message, Throwable JavaDoc t) {
86                                                    println(" INFO ", message, t);
87                                                  }
88
89                                                  public void warn(Object JavaDoc message) {
90                                                    println(" WARN ", message);
91                                                  }
92
93                                                  public void warn(Object JavaDoc message, Throwable JavaDoc t) {
94                                                    println(" WARN ", message, t);
95                                                  }
96
97                                                  public void log(LogLevel level, Object JavaDoc message) {
98                                                    println(level.toString(), message);
99                                                  }
100
101                                                  public void log(LogLevel level, Object JavaDoc message, Throwable JavaDoc t) {
102                                                    println(level.toString(), message, t);
103                                                  }
104
105                                                  public boolean isDebugEnabled() {
106                                                    return true;
107                                                  }
108
109                                                  public boolean isInfoEnabled() {
110                                                    return true;
111                                                  }
112
113                                                  public void setLevel(LogLevel level) {
114                                                    return;
115                                                  }
116
117                                                  public LogLevel getLevel() {
118                                                    return null;
119                                                  }
120
121                                                  public String JavaDoc getName() {
122                                                    return getClass().getName();
123                                                  }
124
125
126                                                };
127
128   private static final long TEST_DURATION = 1000 * 60 * 60 * 24 * 7;
129   private final Date JavaDoc endDate;
130
131   private String JavaDoc appId;
132   private final StatsListener statsListener;
133   private List JavaDoc list = new ArrayList JavaDoc();
134   private transient List JavaDoc unmanaged = new ArrayList JavaDoc();
135   private transient List JavaDoc agedUnmanaged = new ArrayList JavaDoc();
136   private long loopSleepTime = 0;
137
138   private boolean useUnmanaged = true;
139   private boolean useAgedUnmanaged = true;
140   private boolean beRandom = true;
141   private boolean doComplex = true;
142
143   public LongrunningGCTestApp(String JavaDoc appId, ApplicationConfig cfg, ListenerProvider listeners) {
144     this.appId = appId;
145     Properties JavaDoc properties = new Properties JavaDoc();
146     properties.setProperty("sample_name", "100 iterations");
147     this.statsListener = listeners.newStatsListener(properties);
148     if (cfg instanceof LongrunningGCTestAppConfig) {
149       loopSleepTime = ((LongrunningGCTestAppConfig) cfg).getLoopSleepTime();
150     }
151     endDate = new Date JavaDoc(System.currentTimeMillis() + TEST_DURATION);
152   }
153
154   public String JavaDoc getApplicationId() {
155     return appId;
156   }
157
158   public boolean interpretResult(Object JavaDoc result) {
159     return true;
160   }
161
162   public void run() {
163     println("starting " + getClass());
164     println("Will run until " + endDate);
165     if (doComplex) doComplex();
166     else doSimple();
167     println("All done.");
168   }
169
170   private boolean shouldContinue() {
171     return System.currentTimeMillis() < endDate.getTime();
172   }
173
174   private void doSimple() {
175     println("doSimple...");
176     int count = 0;
177     NumberFormat JavaDoc nf = NumberFormat.getInstance();
178     long t0 = System.currentTimeMillis();
179     while (shouldContinue()) {
180
181       if (count % 200 == 0) {
182         synchronized (list) {
183           println("clearing list (" + list.size() + ")");
184           list.clear();
185         }
186       }
187
188       Tree tree = new Tree();
189       tree.makeTree(2, 2);
190       synchronized (list) {
191         if (count % 50 == 0) {
192           println("adding to list (" + list.size() + ")");
193         }
194         list.add(tree);
195       }
196
197       if (count % 100 == 0) {
198         long delta = System.currentTimeMillis() - t0;
199         println("count=" + nf.format(count) + ", time=" + nf.format(delta) + " ms.");
200         t0 = System.currentTimeMillis();
201       }
202
203       count++;
204     }
205   }
206
207   private void doComplex() {
208     println("doComplex...");
209     println("useUnmanaged : " + useUnmanaged);
210     println("useAgedUnmanaged: " + useAgedUnmanaged);
211     println("beRandom : " + beRandom);
212     println("loopSleepTime : " + loopSleepTime);
213     NumberFormat JavaDoc nf = NumberFormat.getInstance();
214     boolean migratedAgedUnmanaged = false;
215     boolean clearedUnmanagedAndList = false;
216     boolean addedUnmanagedToList = false;
217     Random JavaDoc random = new Random JavaDoc(System.currentTimeMillis() + System.identityHashCode(new Object JavaDoc()));
218     int count = 0;
219     long t0 = System.currentTimeMillis();
220     while (shouldContinue()) {
221       if (count % 100 == 0) {
222         long delta = System.currentTimeMillis() - t0;
223         println("count=" + nf.format(count) + ", time=" + nf.format(delta) + " ms.");
224         this.statsListener.sample(delta, "");
225         println("migratedAgedUnmanaged : " + migratedAgedUnmanaged);
226         println("clearedUnmanagedAndList: " + clearedUnmanagedAndList);
227         println("addedUnmanagedToList : " + addedUnmanagedToList);
228
229         t0 = System.currentTimeMillis();
230         migratedAgedUnmanaged = false;
231         clearedUnmanagedAndList = false;
232         addedUnmanagedToList = false;
233       }
234
235       if (useAgedUnmanaged && count % random(random, 1001) == 0) {
236         // keep a stash of objects around for a little longer to be added back to
237
// the managed list later.
238
synchronized (agedUnmanaged) {
239           synchronized (list) {
240             println("adding agedUnmanaged (" + agedUnmanaged.size() + ") to list (" + list.size() + ")...");
241             list.addAll(agedUnmanaged);
242           }
243           println("Clearing agedUnmanaged: " + agedUnmanaged.size());
244           agedUnmanaged.clear();
245           synchronized (list) {
246             if (list.size() > 0) {
247               println("adding list element 0 to agedUnmanaged...");
248               agedUnmanaged.add(list.get(0));
249             }
250           }
251         }
252         migratedAgedUnmanaged = true;
253       }
254
255       if (count % random(random, 500) == 0) {
256         synchronized (list) {
257           println("Clearing unmanaged: " + unmanaged.size());
258           unmanaged.clear();
259           println("Clearing managed: " + list.size());
260           list.clear();
261         }
262         clearedUnmanagedAndList = true;
263       }
264       // move the items from the unmanaged set to the managed list to make sure that
265
// managed objects referenced from an unmanaged graph don't get garbage collected.
266
if (useUnmanaged && count % random(random, 250) == 0) {
267         synchronized (list) {
268           println("Adding elements from unmanaged (" + unmanaged.size() + ") to managed list (" + list.size() + ")...");
269           list.addAll(unmanaged);
270         }
271         addedUnmanagedToList = true;
272       }
273
274       Tree tree = new Tree();
275       tree.makeTree(2, 2);
276
277       synchronized (list) {
278         list.add(tree);
279         if (useUnmanaged) {
280           unmanaged.add(tree);
281         }
282       }
283       count++;
284
285       sleep(loopSleepTime);
286     }
287   }
288
289   public int random(Random JavaDoc random, int max) {
290     if (!beRandom) return max / 2;
291     int rv;
292     while ((rv = random.nextInt(max)) == 0) {
293       //
294
}
295     return rv;
296   }
297
298   private void println(Object JavaDoc o) {
299     logger.info("appId[" + appId + "]: " + o);
300   }
301
302   private void sleep(long ms) {
303     try {
304       Thread.sleep(ms);
305     } catch (InterruptedException JavaDoc e) {
306       throw new AssertionError JavaDoc(e);
307     }
308   }
309
310   public static void visitL1DSOConfig(ConfigVisitor visitor, DSOClientConfigHelper config) {
311     String JavaDoc testClassName = LongrunningGCTestApp.class.getName();
312     TransparencyClassSpec spec = config.getOrCreateSpec(testClassName);
313     spec.addRoot("list", testClassName + ".list");
314     String JavaDoc methodExpression = "* " + testClassName + ".*(..)";
315     System.err.println("Adding autolock for: " + methodExpression);
316     config.addWriteAutolock(methodExpression);
317
318     spec.addTransient("unmanaged");
319     spec.addTransient("agedUnmanaged");
320
321     config.addIncludePattern(Tree.class.getName());
322     config.addExcludePattern(LongrunningGCTestAppConfig.class.getName());
323     config.addExcludePattern(LongrunningGCTestAppConfigObject.class.getName());
324   }
325
326   public static void visitDSOApplicationConfig(ConfigVisitor visitor, DSOApplicationConfig config) {
327     String JavaDoc classname = LongrunningGCTestApp.class.getName();
328     config.addRoot("LongrunningGCTestAppList", classname + ".list");
329     config.addWriteAutolock("* " + classname + ".*(..)");
330
331     config.addIncludePattern(LongrunningGCTestApp.class.getName(), true);
332     config.addIncludePattern(Tree.class.getName());
333   }
334
335 }
336
Popular Tags