1 26 package org.objectweb.speedo.j2eedo.common; 27 28 import java.util.Vector ; 29 30 public class TraceTime { 31 public Vector trace; 32 private boolean running; 33 34 37 public TraceTime() { 38 this.trace = new Vector (); 39 this.running = false; 40 } 41 42 public void startTask(String task) { 43 if (this.running) { 44 this.endTask(); 45 } 46 TraceTimeElement tt = new TraceTimeElement(task); 47 this.trace.add(tt); 48 running = true; 49 } 50 51 public void endTask() { 52 try { 53 TraceTimeElement tt = (TraceTimeElement) this.trace.lastElement(); 54 tt.endTask(); 55 } catch (Exception e) { 56 57 } 58 this.running = false; 59 } 60 61 public void reset() { 62 if (this.running) { 63 this.endTask(); 64 } 65 this.trace = null; 66 this.trace = new Vector (); 67 this.running = false; 68 } 69 70 public String reportXMLComment() { 71 return "<!-- TraceTime Report:\n\t" + report() + "\n-->"; 72 73 } 74 public String report() { 75 StringBuffer sb= new StringBuffer (); 76 try { 77 if (this.trace.size() > 0) { 78 if (this.running) { 79 this.endTask(); 80 } 81 82 TraceTimeElement tt = null; 83 for (int i = 0; i < this.trace.size(); i++) { 84 tt = (TraceTimeElement) this.trace.get(i); 85 sb.append(tt.task); 86 sb.append(":"); 87 sb.append(tt.duration()); 88 sb.append(" ms\t"); 89 tt = null; 90 } 91 long duration = 92 ((TraceTimeElement) this.trace.lastElement()) 93 .endTime 94 .getTime() 95 - ((TraceTimeElement) this.trace.firstElement()) 96 .beginTime 97 .getTime(); 98 sb.append("Total:"); 99 sb.append(duration); 100 sb.append(" ms\t"); 101 } else { 102 sb.append("Nothing\t"); 103 } 104 } catch (Exception e) { 105 } 106 107 return sb.toString(); 108 } 109 110 public static void main(String [] args) { 111 TraceTime tt = new TraceTime(); 112 tt.startTask("sample"); 113 try { 114 Thread.sleep(1000); 115 116 } catch (Exception e) { 117 System.err.println("Exception:" + e); 118 } 119 tt.endTask(); 120 tt.startTask("other sample"); 121 try { 122 Thread.sleep(300); 123 124 } catch (Exception e) { 125 System.err.println("Exception:" + e); 126 } 127 System.out.println(tt.report()); 128 } 129 } 130 | Popular Tags |