1 23 24 29 30 package com.sun.enterprise.util.diagnostics; 31 32 import java.util.*; 33 import com.sun.enterprise.util.diagnostics.Reporter; 34 import com.sun.enterprise.util.StringUtils; 35 36 41 42 43 public class ProfilerImpl 44 { 45 47 public ProfilerImpl() 48 { 49 } 50 51 53 public void reset() 54 { 55 currItem = null; 56 items.clear(); 57 numBegins = 0; 58 numEnds = 0; 59 numActualEnds = 0; 60 } 61 63 public void beginItem() 64 { 65 beginItem("No Description"); 66 } 67 68 71 public void beginItem(String desc) 72 { 73 76 currItem = new Item(desc); 77 items.add(currItem); 78 ++numBegins; 79 } 80 82 public void endItem() 83 { 84 ++numEnds; 85 Item item = getLastNotEnded(); 86 87 if(item != null) 88 item.end(); 89 ++numActualEnds; 90 } 91 93 public String toString() 94 { 95 StringBuffer sb = new StringBuffer (); 96 sb.append("\nBegins: " + numBegins + ", Ends: " + numEnds + 97 ", Actual Ends: " + numActualEnds + "\n"); 98 99 sb.append(Item.getHeader()); 100 sb.append("\n"); 101 102 103 for(Iterator iter = items.iterator(); iter.hasNext(); ) 104 { 105 Item item = (Item)iter.next(); 106 sb.append(item.toString()); 107 sb.append("\n"); 108 } 109 return sb.toString(); 110 } 111 112 114 private Item getLastNotEnded() 115 { 116 int index = items.size(); 117 118 while(--index >= 0) 119 { 120 Item item = (Item)items.get(index); 121 122 if(!item.hasEnded()) 123 return item; 124 } 125 return null; 126 } 127 128 130 private static class Item 131 { 132 Item(String desc) 133 { 134 title = desc; 135 startTime = System.currentTimeMillis(); 136 endTime = startTime; 137 138 if(title.length() > longestTitle) 139 longestTitle = title.length(); 140 } 141 142 boolean hasEnded() 143 { 144 return ended; 145 } 147 148 void end() 149 { 150 endTime = System.currentTimeMillis(); 151 ended = true; 152 } 153 154 public String toString() 155 { 156 long finish = hasEnded() ? endTime : System.currentTimeMillis(); 157 158 String totalTime = "" + (finish - startTime); 159 160 if(totalTime.equals("0")) 161 totalTime = "< 1"; 162 163 String desc = StringUtils.padRight(title, longestTitle + 1); 164 String time = StringUtils.padLeft(totalTime, 8); 165 166 if(!hasEnded()) 167 time += " ** STILL RUNNING **"; 168 169 return desc + time; 170 } 171 172 public static String getHeader() 173 { 174 return "\n" + StringUtils.padRight("Description", longestTitle + 1) + StringUtils.padLeft("msec", 8); 175 } 176 177 String title; 178 long startTime; 179 long endTime; 180 static int longestTitle = 12; 181 boolean ended = false; 182 } 183 184 186 Item currItem = null; 187 List items = new ArrayList(); 188 int numBegins = 0; 189 int numEnds = 0; 190 int numActualEnds = 0; 191 192 195 public static void main(String [] notUsed) 196 { 197 ProfilerImpl p = new ProfilerImpl(); 198 199 try 200 { 201 p.beginItem("first item"); 202 Thread.sleep(3000); 203 p.beginItem("second item here dude whoa yowser yowser"); 204 Thread.sleep(1500); 205 p.endItem(); 206 p.endItem(); 207 System.out.println("" + p); 208 } 209 catch(Exception e) 210 { 211 } 212 } 213 } 214 | Popular Tags |