KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > util > diagnostics > ProfilerImpl


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  * ProfilerImpl.java
26  *
27  * Created on September 17, 2001, 12:42 PM
28  */

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 /** Simple class for profiling code. beginItem/endItem pairs start and stop the timing for an item.
37  *
38  * @author bnevins
39  * @version
40  */

41
42
43 public class ProfilerImpl
44 {
45     /** Create an empty object
46      */

47     public ProfilerImpl()
48     {
49     }
50
51     /**Reset all the timing information
52      */

53     public void reset()
54     {
55         currItem = null;
56         items.clear();
57         numBegins = 0;
58         numEnds = 0;
59         numActualEnds = 0;
60     }
61     /** Start timing an item.
62      **/

63     public void beginItem()
64     {
65         beginItem("No Description");
66     }
67
68     /** Start timing an item.
69      * @param desc - Descriptive text for the item
70      **/

71     public void beginItem(String JavaDoc desc)
72     {
73         //if(currItem != null)
74
//Reporter.assert(currItem.hasEnded());
75

76         currItem = new Item(desc);
77         items.add(currItem);
78         ++numBegins;
79     }
80     /** Stop timing an item and store the information.
81      **/

82     public void endItem()
83     {
84         ++numEnds;
85         Item item = getLastNotEnded();
86         
87         if(item != null)
88             item.end();
89         ++numActualEnds;
90     }
91     /** Return a formatted String with the timing information
92      **/

93     public String JavaDoc toString()
94     {
95         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
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     ////////////////////////////////////////////////////////////////////////////
113

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     ////////////////////////////////////////////////////////////////////////////
129

130     private static class Item
131     {
132         Item(String JavaDoc 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             //return endTime > startTime;
146
}
147         
148         void end()
149         {
150             endTime = System.currentTimeMillis();
151             ended = true;
152         }
153         
154         public String JavaDoc toString()
155         {
156             long finish = hasEnded() ? endTime : System.currentTimeMillis();
157             
158             String JavaDoc totalTime = "" + (finish - startTime);
159             
160             if(totalTime.equals("0"))
161                 totalTime = "< 1";
162
163             String JavaDoc desc = StringUtils.padRight(title, longestTitle + 1);
164             String JavaDoc time = StringUtils.padLeft(totalTime, 8);
165
166             if(!hasEnded())
167                 time += " ** STILL RUNNING **";
168             
169             return desc + time;
170         }
171         
172         public static String JavaDoc getHeader()
173         {
174             return "\n" + StringUtils.padRight("Description", longestTitle + 1) + StringUtils.padLeft("msec", 8);
175         }
176         
177         String JavaDoc title;
178         long startTime;
179         long endTime;
180         static int longestTitle = 12;
181         boolean ended = false;
182     }
183     
184     ////////////////////////////////////////////////////////////////////////////
185

186     Item currItem = null;
187     List items = new ArrayList();
188     int numBegins = 0;
189     int numEnds = 0;
190     int numActualEnds = 0;
191     
192     ////////////////////////////////////////////////////////////////////////////
193
/** Simple unit test
194      **/

195     public static void main(String JavaDoc[] 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 JavaDoc e)
210         {
211         }
212     }
213 }
214
Popular Tags