1 package org.columba.core.util; 2 3 import java.util.Hashtable ; 4 import java.util.Iterator ; 5 import java.util.Vector ; 6 7 import org.columba.core.logging.Logging; 8 9 public class StackProfiler { 10 11 private static final java.util.logging.Logger LOG = 12 java.util.logging.Logger.getLogger("org.columba.core.util"); 14 17 private Vector vector = new Vector (); 18 19 22 private Hashtable hashtable = new Hashtable (); 23 24 27 private String currentPath = ""; 28 29 36 public void push(String id) { 37 if (Logging.DEBUG == false) 39 return; 40 41 long currentTime = System.currentTimeMillis(); 43 44 hashtable.put(id, new ProfileData(id, currentPath, 46 new Long (currentTime).longValue())); 47 48 currentPath = currentPath + "/" + id; 50 51 vector.add(id); 53 } 54 55 61 public void pop(String id) { 62 System.out.println("id="+id); 63 if (Logging.DEBUG == false) 65 return; 66 67 if (id == null) 68 throw new IllegalArgumentException ("id == null"); 69 70 if (hashtable.containsKey(id) == false) 71 throw new IllegalArgumentException ("id=" + id + " not found"); 72 73 long currentTime = System.currentTimeMillis(); 74 75 ProfileData bean = (ProfileData) hashtable.get(id); 76 bean.endTime = currentTime; 77 78 String firstItem = (String ) vector.get(0); 79 if (firstItem.equals(id)) { 80 82 LOG.info("profiler info:"); Iterator it = vector.listIterator(); 84 85 while (it.hasNext()) { 86 String nextId = (String ) it.next(); 87 printDuration(nextId); 88 } 89 } else { 90 int index = currentPath.lastIndexOf("/"); 92 currentPath = currentPath.substring(0, index); 93 } 94 } 95 96 102 private void printDuration(String id) { 103 if (id == null) 104 throw new IllegalArgumentException ("id == null"); 105 if (hashtable.containsKey(id) == false) 106 throw new IllegalArgumentException ("id not found"); 107 108 ProfileData bean = (ProfileData) hashtable.get(id); 109 String splits[] = bean.path.split("/"); 110 111 for (int i = 0; i < splits.length; i++) 112 System.out.print(" "); 113 114 long duration = bean.endTime - bean.startTime; 115 LOG.info("[" + duration + "ms] - " + id); } 117 118 121 private class ProfileData { 122 protected String id; 123 124 protected String path; 125 126 protected long startTime; 127 128 protected long endTime; 129 130 protected ProfileData(String id, String path, long startTime) { 131 this.id = id; 132 this.path = path; 133 this.startTime = startTime; 134 } 135 136 } 137 } 138 | Popular Tags |