1 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 ; 18 import java.io.PrintStream ; 19 import java.io.PrintWriter ; 20 import java.text.DateFormat ; 21 import java.text.NumberFormat ; 22 import java.util.ArrayList ; 23 import java.util.Date ; 24 import java.util.List ; 25 import java.util.Properties ; 26 import java.util.Random ; 27 28 public class LongrunningGCTestApp implements Application { 29 30 private TCLogger logger = new TCLogger() { 31 32 PrintStream out = System.out; 33 34 DateFormat df = DateFormat.getDateTimeInstance(); 35 36 private void println(String header, Object message) { 37 out.println(format(header + message)); 38 } 39 40 private void println(String header, Object message, Throwable t) { 41 out.println(format(header + message, t)); 42 } 43 44 private String format(Object message) { 45 return df.format(new java.util.Date ()) + message; 46 } 47 48 private String format(Object message, Throwable t) { 49 CharArrayWriter chaw = new CharArrayWriter (); 50 PrintWriter ps = new PrintWriter (chaw); 51 t.printStackTrace(ps); 52 ps.flush(); 53 54 return format(message + chaw.toString()); 55 } 56 57 public void debug(Object message) { 58 println(" DEBUG ", message); 59 } 60 61 public void debug(Object message, Throwable t) { 62 println(" DEBUG ", message, t); 63 } 64 65 public void error(Object message) { 66 println(" ERROR ", message); 67 } 68 69 public void error(Object message, Throwable t) { 70 println(" ERROR ", message, t); 71 } 72 73 public void fatal(Object message) { 74 println(" FATAL ", message); 75 } 76 77 public void fatal(Object message, Throwable t) { 78 println(" FATAL ", message, t); 79 } 80 81 public void info(Object message) { 82 println(" INFO ", message); 83 } 84 85 public void info(Object message, Throwable t) { 86 println(" INFO ", message, t); 87 } 88 89 public void warn(Object message) { 90 println(" WARN ", message); 91 } 92 93 public void warn(Object message, Throwable t) { 94 println(" WARN ", message, t); 95 } 96 97 public void log(LogLevel level, Object message) { 98 println(level.toString(), message); 99 } 100 101 public void log(LogLevel level, Object message, Throwable 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 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 endDate; 130 131 private String appId; 132 private final StatsListener statsListener; 133 private List list = new ArrayList (); 134 private transient List unmanaged = new ArrayList (); 135 private transient List agedUnmanaged = new ArrayList (); 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 appId, ApplicationConfig cfg, ListenerProvider listeners) { 144 this.appId = appId; 145 Properties properties = new Properties (); 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 (System.currentTimeMillis() + TEST_DURATION); 152 } 153 154 public String getApplicationId() { 155 return appId; 156 } 157 158 public boolean interpretResult(Object 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 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 nf = NumberFormat.getInstance(); 214 boolean migratedAgedUnmanaged = false; 215 boolean clearedUnmanagedAndList = false; 216 boolean addedUnmanagedToList = false; 217 Random random = new Random (System.currentTimeMillis() + System.identityHashCode(new Object ())); 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 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 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 random, int max) { 290 if (!beRandom) return max / 2; 291 int rv; 292 while ((rv = random.nextInt(max)) == 0) { 293 } 295 return rv; 296 } 297 298 private void println(Object o) { 299 logger.info("appId[" + appId + "]: " + o); 300 } 301 302 private void sleep(long ms) { 303 try { 304 Thread.sleep(ms); 305 } catch (InterruptedException e) { 306 throw new AssertionError (e); 307 } 308 } 309 310 public static void visitL1DSOConfig(ConfigVisitor visitor, DSOClientConfigHelper config) { 311 String testClassName = LongrunningGCTestApp.class.getName(); 312 TransparencyClassSpec spec = config.getOrCreateSpec(testClassName); 313 spec.addRoot("list", testClassName + ".list"); 314 String 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 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 |