1 package com.protomatter.util; 2 3 52 53 import java.io.*; 54 import java.text.DecimalFormat ; 55 56 112 public class StackTraceUtil 113 { 114 private static StackTraceUtil instance = null; 115 116 static 117 { 118 try 122 { 123 instance = (StackTraceUtil)Class.forName("com.protomatter.util.JDK14StackTraceUtil").newInstance(); 124 instance.getInfo(0); 125 } 126 catch (Throwable t) 127 { 128 instance = new StackTraceUtil(); 129 } 130 } 131 132 135 protected StackTraceUtil() 136 { 137 } 138 139 private static String LINE_SEP = System.getProperty("line.separator"); 140 141 private static char SPACE = ' '; 142 private static char DOT = '.'; 143 private static char COLON = ':'; 144 private static char OPEN_P = '('; 145 private static char CLOSE_P = ')'; 146 147 150 public static StackTraceInfo whereAmI() 151 { 152 return instance.getInfo(1); 153 } 154 155 160 public static StackTraceInfo whereAmI(int stackOffset) 161 { 162 return instance.getInfo(++stackOffset); 163 } 164 165 protected StackTraceInfo getInfo(int stackOffset) 166 { 167 Throwable t = new Throwable (); 168 StringWriter sw = new StringWriter(256); 169 PrintWriter pw = new PrintWriter(sw); 170 t.printStackTrace(pw); 171 String stack = sw.toString(); 172 173 stackOffset++; 174 175 int start; 176 int end; 177 int dot; 178 179 String className = null; 180 String methodName = null; 181 int lineNumber = StackTraceInfo.LINE_NUMBER_UNKNOWN; 182 183 try 184 { 185 start = stack.indexOf(LINE_SEP) +1; 186 for (dot=0; dot<stackOffset; dot++) 187 { 188 start = stack.indexOf(LINE_SEP, ++start); 189 } 190 start = stack.indexOf(SPACE, start); 191 end = stack.indexOf(OPEN_P, start); 192 dot = stack.lastIndexOf(DOT, end); 193 194 className = stack.substring(++start, dot); 195 methodName = stack.substring(++dot, end); 196 197 dot = stack.indexOf(COLON, end); 199 if (dot > 0) 200 { 201 end = stack.indexOf(CLOSE_P, dot); 202 lineNumber = Integer.parseInt(stack.substring(++dot, end)); 203 } 204 } 205 catch (Exception x) 206 { 207 ; } 209 210 return new StackTraceInfo(className, methodName, lineNumber); 211 } 212 213 private static class TestThread 214 extends Thread  215 { 216 private int numRuns = 0; 217 private long time = 0; 218 private StackTraceInfo info = null; 219 220 public TestThread(int runs) 221 { 222 super(); 223 this.numRuns = runs; 224 } 225 226 public void run() 227 { 228 time = System.currentTimeMillis(); 229 for (int i=0; i<numRuns; i++) 230 { 231 info = StackTraceUtil.whereAmI(); 232 } 233 time = System.currentTimeMillis() - time; 234 } 235 236 public void info() 237 { 238 DecimalFormat format = new DecimalFormat ("####.######"); 239 DecimalFormat tf = new DecimalFormat ("###,###,##0"); 240 System.out.println(" " + tf.format(numRuns) + " runs in " + tf.format(time) + "ms"); 241 double average = ((double)time / (double)numRuns); 242 System.out.println(" Average is " + format.format(average) + "ms"); 243 System.out.println(" Stack trace info = " + info); 244 System.out.println(""); 245 } 246 } 247 248 253 public static void main(String args[]) 254 { 255 try 256 { 257 int numThreads = 5; 258 int numTries = 10000; 259 260 if (args.length == 2) 261 { 262 try 263 { 264 numThreads = Integer.parseInt(args[0]); 265 numTries = Integer.parseInt(args[1]); 266 } 267 catch (NumberFormatException x) 268 { 269 System.out.println("Usage: java com.protomatter.util.StackTraceUtil num-threads num-calls"); 270 System.exit(0); 271 } 272 } 273 else if ((args.length == 1) || (args.length > 2)) 274 { 275 System.out.println("Usage: java com.protomatter.util.StackTraceUtil num-threads num-calls"); 276 System.exit(0); 277 } 278 279 DecimalFormat tf = new DecimalFormat ("###,###,##0"); 280 System.out.println("StackTraceUtil.whereAmI() test:"); 281 System.out.println(""); 282 System.out.println("JVM Information:"); 283 System.out.println(" VM Name: " + System.getProperty("java.vm.name")); 284 System.out.println(" VM Version: " + System.getProperty("java.vm.version")); 285 System.out.println(" Runtime name: " + System.getProperty("java.runtime.name")); 286 System.out.println(" Runtime version: " + System.getProperty("java.runtime.version")); 287 System.out.println(""); 288 System.out.println("OS Information:"); 289 System.out.println(" " + System.getProperty("os.name") + " " + System.getProperty("os.version")); 290 System.out.println(""); 291 System.out.println("Creating " + numThreads + " test threads (" + tf.format(numTries) + " calls each)..."); 292 TestThread threads[] = new TestThread[numThreads]; 293 for (int i=0; i<numThreads; i++) 294 { 295 threads[i] = new TestThread(numTries); 296 } 297 298 System.out.println("Running test threads..."); 299 long time = System.currentTimeMillis(); 300 for (int i=0; i<numThreads; i++) 301 { 302 threads[i].start(); 303 } 304 for (int i=0; i<numThreads; i++) 305 { 306 threads[i].join(); 307 } 308 time = System.currentTimeMillis() - time; 309 310 System.out.println("Per-thread results:"); 311 for (int i=0; i<numThreads; i++) 312 { 313 threads[i].info(); 314 } 315 316 System.out.println(""); 317 System.out.println("Overall:"); 318 DecimalFormat format = new DecimalFormat ("####.######"); 319 System.out.println(" " + tf.format((numThreads * numTries)) + " runs in " + tf.format(time) + "ms"); 320 double average = ((double)time / (double)(numThreads * numTries)); 321 System.out.println(" Average is " + format.format(average) + "ms"); 322 System.out.println(""); 323 } 324 catch (Exception x) 325 { 326 x.printStackTrace(); 327 } 328 } 329 } 330
| Popular Tags
|