KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > tools > common > util > diagnostics > Profiler


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24 /*
25  * Profiler.java
26  *
27  * Created on September 17, 2001, 12:42 PM
28  */

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 /**
36  *
37  * @author bnevins
38  * @version
39  */

40
41
42 public class Profiler
43 {
44     public Profiler()
45     {
46     }
47
48     public void beginItem()
49     {
50         beginItem("No Description"); // NOI18N
51
}
52     
53     public void beginItem(String JavaDoc desc)
54     {
55         //if(currItem != null)
56
//Reporter.assertIt(currItem.hasEnded());
57

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 JavaDoc toString()
76     {
77         StringBuffer JavaDoc sb = new StringBuffer JavaDoc(Item.getHeader());
78         sb.append("\n\n"); // NOI18N
79

80         for(Iterator iter = items.iterator(); iter.hasNext(); )
81         {
82             Item item = (Item)iter.next();
83             sb.append(item.toString());
84             sb.append("\n"); // NOI18N
85
}
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 JavaDoc 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 JavaDoc toString()
126         {
127             long finish = hasEnded() ? endTime : System.currentTimeMillis();
128
129             String JavaDoc desc = StringUtils.padRight(title, longestTitle + 1);
130             String JavaDoc time = StringUtils.padLeft("" + (finish - startTime), 8); // NOI18N
131

132             if(!hasEnded())
133                 time += " ** STILL RUNNING **"; // NOI18N
134

135             return desc + time;
136         }
137         
138         public static String JavaDoc getHeader()
139         {
140             return StringUtils.padRight("Description", longestTitle + 1) + StringUtils.padLeft("msec", 8); // NOI18N
141
}
142         
143         String JavaDoc 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 JavaDoc[] notUsed)
153     {
154         Profiler p = new Profiler();
155         
156         try
157         {
158             p.beginItem("first item"); // NOI18N
159
Thread.sleep(3000);
160             p.beginItem("second item here dude whoa yowser yowser"); // NOI18N
161
Thread.sleep(1500);
162             p.endItem();
163             p.endItem();
164             System.out.println("" + p); // NOI18N
165
}
166         catch(Exception JavaDoc e)
167         {
168         }
169     }
170
171
172 }
173
Popular Tags