KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jalisto > se > test > workbench > JalistoTimer


1 /*
2  * Jalisto - JAva LIght STOrage
3  * Copyright (C) 2000-2005 Xcalia http://www.xcalia.com
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
18  *
19  * Xcalia
20  * 71, rue Desnouettes
21  * 75014 Paris - France
22  * http://www.xcalia.com
23  */

24 package org.objectweb.jalisto.se.test.workbench;
25
26 import java.io.PrintStream JavaDoc;
27 import java.util.*;
28
29 public class JalistoTimer {
30     public JalistoTimer(String JavaDoc name) {
31         this.name = name;
32         this.sum = 0;
33         this.count = 0;
34         out = System.out;
35         mask = new BitSet(NEWLINE + 1);
36         mask.set(0, NEWLINE + 1, true);
37         verbose = true;
38     }
39
40     public void start() {
41         start = System.currentTimeMillis();
42     }
43
44     public void stop(String JavaDoc name, boolean show) {
45         long end = System.currentTimeMillis();
46         long result = (end - start);
47         if (show) {
48             out.println(name + " elapsed time : " + result + " ms");
49         }
50         sum += result;
51         count++;
52     }
53
54     public float getAverage() {
55         return sum / count;
56     }
57
58     private String JavaDoc getSummary() {
59         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
60         if (mask.get(NAME)) {
61             if (verbose) {
62                 sb.append("TIMER:");
63             }
64             sb.append(name).append(";");
65         }
66         if (mask.get(TIME)) {
67             if (verbose) {
68                 sb.append("TIME:");
69             }
70             sb.append(System.currentTimeMillis()).append(";");
71         }
72         if (mask.get(SUM)) {
73             if (verbose) {
74                 sb.append("SUM:");
75             }
76             sb.append(sum).append(";");
77         }
78         if (mask.get(COUNT)) {
79             if (verbose) {
80                 sb.append("COUNT:");
81             }
82             sb.append(count).append(";");
83         }
84         if (mask.get(AVG) && (count != 0)) {
85             if (verbose) {
86                 sb.append("AVG:");
87             }
88             sb.append(sum / count).append(";");
89         }
90         if (mask.get(NEWLINE)) {
91             sb.append("\n");
92         }
93         return sb.toString();
94     }
95
96     private void clean() {
97         this.sum = 0;
98         this.count = 0;
99     }
100
101
102     /**
103      * **************************** STATIC *****************************************
104      */

105
106     public static void createTimer(String JavaDoc name) {
107         JalistoTimer timer = (JalistoTimer) timers.get(name);
108         if (timer != null) {
109             throw new UnsupportedOperationException JavaDoc("Create over an existing timer");
110         }
111         timer = new JalistoTimer(name);
112         timers.put(name, timer);
113         timerNames.add(name);
114     }
115
116     public static void removeTimer(String JavaDoc name) {
117         timers.remove(name);
118         timerNames.remove(name);
119     }
120
121     public static void timerStart(String JavaDoc name) {
122         JalistoTimer timer = (JalistoTimer) timers.get(name);
123         if (timer == null) {
124             timer = new JalistoTimer(name);
125             timers.put(name, timer);
126             timerNames.add(name);
127         }
128         timer.start();
129     }
130
131     public static void timerStop(String JavaDoc name) {
132         timerStop(name, true);
133     }
134
135     public static void timerStop(String JavaDoc name, boolean show) {
136         JalistoTimer timer = (JalistoTimer) timers.get(name);
137         if (timer == null) {
138             throw new UnsupportedOperationException JavaDoc("Unable to stop an unexisting timer");
139         }
140         timer.stop(name, show);
141     }
142
143     public static void setOut(String JavaDoc name, PrintStream JavaDoc out) {
144         JalistoTimer timer = (JalistoTimer) timers.get(name);
145         if (timer == null) {
146             throw new UnsupportedOperationException JavaDoc("Unable to set Out of an unexisting timer");
147         }
148         timer.out = out;
149     }
150
151     public static void setFormat(String JavaDoc name, int format) {
152         JalistoTimer timer = (JalistoTimer) timers.get(name);
153         if (timer == null) {
154             throw new UnsupportedOperationException JavaDoc("Unable to set format of an unexisting timer");
155         }
156         if (timer.initialValues) {
157             timer.mask.clear();
158             timer.verbose = false;
159             timer.initialValues = false;
160         }
161         timer.mask.set(format);
162     }
163
164     public static void unsetFormat(String JavaDoc name, int format) {
165         JalistoTimer timer = (JalistoTimer) timers.get(name);
166         if (timer == null) {
167             throw new UnsupportedOperationException JavaDoc("Unable to unset format of an unexisting timer");
168         }
169         timer.mask.clear(format);
170     }
171
172     public static void setVerbose(String JavaDoc name, boolean verbose) {
173         JalistoTimer timer = (JalistoTimer) timers.get(name);
174         if (timer == null) {
175             throw new UnsupportedOperationException JavaDoc("Unable to set verbose value of an unexisting timer");
176         }
177         if (timer.initialValues) {
178             timer.mask.clear();
179             timer.initialValues = false;
180         }
181         timer.verbose = verbose;
182     }
183
184     public static void summary() {
185         for (int i = 0; i < timerNames.size(); i++) {
186             JalistoTimer timer = (JalistoTimer) timers.get(timerNames.get(i));
187             timer.out.print(timer.getSummary());
188         }
189     }
190
191     public static void summary(String JavaDoc name) {
192         JalistoTimer timer = (JalistoTimer) timers.get(name);
193         if (timer == null) {
194             throw new UnsupportedOperationException JavaDoc("Unable to print summary for an unexisting timer");
195         }
196         timer.out.print(timer.getSummary());
197     }
198
199     public static void clean(String JavaDoc name) {
200         JalistoTimer timer = (JalistoTimer) timers.get(name);
201         if (timer == null) {
202             throw new UnsupportedOperationException JavaDoc("Unable to cleanAll an unexisting timer");
203         }
204         timer.clean();
205     }
206
207     public static void deleteAllTimers() {
208         timers.clear();
209         timerNames.clear();
210     }
211
212     public static float getAverage(String JavaDoc name) {
213         JalistoTimer timer = (JalistoTimer) timers.get(name);
214         if (timer == null) {
215             throw new UnsupportedOperationException JavaDoc("Unable to get average on an unexisting timer");
216         }
217         return timer.getAverage();
218     }
219
220
221     private String JavaDoc name;
222     private long start;
223     private long sum;
224     private long count;
225     private PrintStream JavaDoc out;
226     private BitSet mask;
227     private boolean verbose;
228     private boolean initialValues = true;
229
230
231     private static Map timers = new HashMap();
232     private static ArrayList timerNames = new ArrayList();
233
234     public static final int TIME = 0;
235     public static final int NAME = 1;
236     public static final int SUM = 2;
237     public static final int COUNT = 3;
238     public static final int AVG = 4;
239     public static final int NEWLINE = 5;
240 }
241
Popular Tags