KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > jdo > api > persistence > enhancer > util > Timer


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 package com.sun.jdo.api.persistence.enhancer.util;
26
27 import java.util.HashMap JavaDoc;
28 import java.util.ArrayList JavaDoc;
29 import java.util.Arrays JavaDoc;
30 import java.util.Comparator JavaDoc;
31
32 import java.text.DecimalFormat JavaDoc;
33
34 import java.io.PrintWriter JavaDoc;
35
36
37 /**
38  * Utility class for simple performance analysis.
39  */

40 //@olsen: added class
41
public final class Timer {
42     // a method's timing descriptor
43
static private class MethodDescriptor {
44         final String JavaDoc name;
45         int instantiations;
46         int calls;
47         long self;
48         long total;
49
50         MethodDescriptor(String JavaDoc name) {
51             this.name = name;
52         }
53     }
54
55     // a method call's timing descriptor
56
static private class MethodCall {
57         final MethodDescriptor method;
58         final String JavaDoc message;
59         long self;
60         long total;
61
62         MethodCall(MethodDescriptor method,
63                    String JavaDoc message,
64                    long self,
65                    long total) {
66             this.method = method;
67             this.message = message;
68             this.self = self;
69             this.total = total;
70         }
71     }
72
73     // output device
74
PrintWriter JavaDoc out = new PrintWriter JavaDoc(System.out, true);
75
76     // methods
77
HashMap JavaDoc methods = new HashMap JavaDoc();
78
79     // method call stack
80
private final ArrayList JavaDoc calls = new ArrayList JavaDoc(16);
81     
82     public Timer()
83     {
84         this.out = out;
85     }
86
87     public Timer(PrintWriter JavaDoc out)
88     {
89         this.out = out;
90     }
91
92     public final synchronized void push(String JavaDoc name) {
93         push(name, name);
94     }
95     
96     public final synchronized void push(String JavaDoc name, String JavaDoc message) {
97         // get time
98
final long now = System.currentTimeMillis();
99
100         // get a method descriptor
101
MethodDescriptor current = (MethodDescriptor)methods.get(name);
102         if (current == null) {
103             current = new MethodDescriptor(name);
104             methods.put(name, current);
105         }
106
107         // update method descriptor
108
current.calls++;
109         current.instantiations++;
110
111         // update method call stack
112
calls.add(new MethodCall(current, message, now, now));
113     }
114
115     public final synchronized void pop()
116     {
117         // get time
118
final long now = System.currentTimeMillis();
119
120         // update method call stack
121
final MethodCall call = (MethodCall)calls.remove(calls.size()-1);
122
123         // get current call's time
124
final long currentSelf = now - call.self;
125         final long currentTotal = now - call.total;
126
127         // update previous call's self time
128
if (calls.size() > 0) {
129             final MethodCall previous = (MethodCall)calls.get(calls.size()-1);
130             previous.self += currentTotal;
131         }
132
133         // update method descriptor
134
final MethodDescriptor current = call.method;
135         current.self += currentSelf;
136         if (--current.instantiations == 0) {
137             current.total += currentTotal;
138         }
139
140         if (false) {
141             out.println("Timer (n,g): " + call.message + " : ("
142                         + currentSelf + ", " + currentTotal + ")");
143         }
144     }
145
146     static private final String JavaDoc pad(String JavaDoc s, int i)
147     {
148         StringBuffer JavaDoc b = new StringBuffer JavaDoc();
149         for (i -= s.length(); i > 0; i--)
150             b.append((char)' ');
151         b.append(s);
152         return b.toString();
153     }
154     
155     public final synchronized void print()
156     {
157         out.println("Timer : printing accumulated times ...");
158         final Object JavaDoc[] calls = methods.values().toArray();
159
160         Arrays.sort(calls,
161                     new Comparator JavaDoc() {
162                             public int compare(Object JavaDoc o1,
163                                                Object JavaDoc o2) {
164                                 return (int)(((MethodDescriptor)o2).total
165                                              - ((MethodDescriptor)o1).total);
166                             }
167                             public boolean equals(Object JavaDoc obj) {
168                                 return (compare(this, obj) == 0);
169                             }
170                         });
171         
172         out.println("Timer : total s self s #calls name");
173         DecimalFormat JavaDoc nf = new DecimalFormat JavaDoc();
174         nf.setMaximumFractionDigits(2);
175         nf.setMinimumFractionDigits(2);
176         //nf.applyPattern("#,##0.00");
177
//out.println("Timer : pattern = " + nf.toPattern());
178
for (int i = 0; i < calls.length; i++) {
179             final MethodDescriptor current = (MethodDescriptor)calls[i];
180
181             out.println("Timer : "
182                         + pad(nf.format(current.total / 1000.0), 8) + " "
183                         + pad(nf.format(current.self / 1000.0), 8) + " "
184                         + pad(String.valueOf(current.calls), 6) + " "
185                         + current.name);
186         }
187     }
188 }
189
Popular Tags