KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > speedo > generation > start > Timer


1 /**
2  * Copyright (C) 2001-2004 France Telecom R&D
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  */

18
19 package org.objectweb.speedo.generation.start;
20
21 import java.util.HashMap JavaDoc;
22 import java.util.ArrayList JavaDoc;
23 import java.util.Arrays JavaDoc;
24 import java.util.Comparator JavaDoc;
25
26 import java.text.DecimalFormat JavaDoc;
27  
28 import java.io.PrintWriter JavaDoc;
29
30
31 /**
32  * Utility class for simple performance analysis.
33  */

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