KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > columba > core > util > StackProfiler


1 package org.columba.core.util;
2
3 import java.util.Hashtable JavaDoc;
4 import java.util.Iterator JavaDoc;
5 import java.util.Vector JavaDoc;
6
7 import org.columba.core.logging.Logging;
8
9 public class StackProfiler {
10
11     private static final java.util.logging.Logger JavaDoc LOG =
12         java.util.logging.Logger.getLogger("org.columba.core.util"); //$NON-NLS-1$
13

14     /**
15      * keeps a list of profile point ids
16      */

17     private Vector JavaDoc vector = new Vector JavaDoc();
18
19     /**
20      * key is the id, value is the profile data
21      */

22     private Hashtable JavaDoc hashtable = new Hashtable JavaDoc();
23
24     /**
25      * current path in simple tree-like hierarchy
26      */

27     private String JavaDoc currentPath = "";
28
29     /**
30      * Set new profiling starting point. This is where the time measurment
31      * actually starts.
32      *
33      * @param id
34      * unique id of profiling point
35      */

36     public void push(String JavaDoc id) {
37         // abort if not in debugging mode
38
if (Logging.DEBUG == false)
39             return;
40
41         // current time
42
long currentTime = System.currentTimeMillis();
43
44         // store profiling point data
45
hashtable.put(id, new ProfileData(id, currentPath,
46                 new Long JavaDoc(currentTime).longValue()));
47
48         // this profiling point is parent of the next one
49
currentPath = currentPath + "/" + id;
50
51         // store id
52
vector.add(id);
53     }
54
55     /**
56      * Close profiling point. This is where the time measurement actually ends.
57      *
58      * @param id
59      * unique id of profiling point
60      */

61     public void pop(String JavaDoc id) {
62         System.out.println("id="+id);
63         // abort if not in debugging mode
64
if (Logging.DEBUG == false)
65             return;
66
67         if (id == null)
68             throw new IllegalArgumentException JavaDoc("id == null");
69
70         if (hashtable.containsKey(id) == false)
71             throw new IllegalArgumentException JavaDoc("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 JavaDoc firstItem = (String JavaDoc) vector.get(0);
79         if (firstItem.equals(id)) {
80             // print all collected profile data
81

82             LOG.info("profiler info:"); //$NON-NLS-1$
83
Iterator JavaDoc it = vector.listIterator();
84
85             while (it.hasNext()) {
86                 String JavaDoc nextId = (String JavaDoc) it.next();
87                 printDuration(nextId);
88             }
89         } else {
90             // current path is the parent of the current element
91
int index = currentPath.lastIndexOf("/");
92             currentPath = currentPath.substring(0, index);
93         }
94     }
95
96     /**
97      * Print debug data.
98      *
99      * @param id
100      * unique profiling point id
101      */

102     private void printDuration(String JavaDoc id) {
103         if (id == null)
104             throw new IllegalArgumentException JavaDoc("id == null");
105         if (hashtable.containsKey(id) == false)
106             throw new IllegalArgumentException JavaDoc("id not found");
107
108         ProfileData bean = (ProfileData) hashtable.get(id);
109         String JavaDoc 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); //$NON-NLS-2$
116
}
117
118     /**
119      * Structure containing profiling data.
120      */

121     private class ProfileData {
122         protected String JavaDoc id;
123
124         protected String JavaDoc path;
125
126         protected long startTime;
127
128         protected long endTime;
129
130         protected ProfileData(String JavaDoc id, String JavaDoc path, long startTime) {
131             this.id = id;
132             this.path = path;
133             this.startTime = startTime;
134         }
135
136     }
137 }
138
Popular Tags