1 23 24 29 30 package com.sun.enterprise.tools.common.util.diagnostics; 31 32 import java.util.*; 33 import com.sun.enterprise.tools.common.util.diagnostics.Reporter; 34 import com.sun.enterprise.tools.common.util.StringUtils; 35 40 41 42 public class Profiler 43 { 44 public Profiler() 45 { 46 } 47 48 public void beginItem() 49 { 50 beginItem("No Description"); } 52 53 public void beginItem(String desc) 54 { 55 58 currItem = new Item(desc); 59 items.add(currItem); 60 } 61 62 public void endItem() 63 { 64 Item item = getLastNotEnded(); 65 66 if(item != null) 67 item.end(); 68 } 69 70 public void report() 71 { 72 73 } 74 75 public String toString() 76 { 77 StringBuffer sb = new StringBuffer (Item.getHeader()); 78 sb.append("\n\n"); 80 for(Iterator iter = items.iterator(); iter.hasNext(); ) 81 { 82 Item item = (Item)iter.next(); 83 sb.append(item.toString()); 84 sb.append("\n"); } 86 return sb.toString(); 87 } 88 89 private Item getLastNotEnded() 90 { 91 int index = items.size(); 92 93 while(--index >= 0) 94 { 95 Item item = (Item)items.get(index); 96 97 if(!item.hasEnded()) 98 return item; 99 } 100 return null; 101 } 102 103 private static class Item 104 { 105 Item(String desc) 106 { 107 title = desc; 108 startTime = System.currentTimeMillis(); 109 endTime = startTime; 110 111 if(title.length() > longestTitle) 112 longestTitle = title.length(); 113 } 114 115 boolean hasEnded() 116 { 117 return endTime > startTime; 118 } 119 120 void end() 121 { 122 endTime = System.currentTimeMillis(); 123 } 124 125 public String toString() 126 { 127 long finish = hasEnded() ? endTime : System.currentTimeMillis(); 128 129 String desc = StringUtils.padRight(title, longestTitle + 1); 130 String time = StringUtils.padLeft("" + (finish - startTime), 8); 132 if(!hasEnded()) 133 time += " ** STILL RUNNING **"; 135 return desc + time; 136 } 137 138 public static String getHeader() 139 { 140 return StringUtils.padRight("Description", longestTitle + 1) + StringUtils.padLeft("msec", 8); } 142 143 String title; 144 long startTime; 145 long endTime; 146 static int longestTitle = 12; 147 } 148 149 Item currItem = null;; 150 List items = new ArrayList(); 151 152 public static void main(String [] notUsed) 153 { 154 Profiler p = new Profiler(); 155 156 try 157 { 158 p.beginItem("first item"); Thread.sleep(3000); 160 p.beginItem("second item here dude whoa yowser yowser"); Thread.sleep(1500); 162 p.endItem(); 163 p.endItem(); 164 System.out.println("" + p); } 166 catch(Exception e) 167 { 168 } 169 } 170 171 172 } 173 | Popular Tags |