KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > ibatis > common > util > Stopwatch


1 /*
2  * Copyright 2004 Clinton Begin
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package com.ibatis.common.util;
17
18 import com.ibatis.common.logging.Log;
19 import com.ibatis.common.logging.LogFactory;
20
21 import java.util.HashMap JavaDoc;
22 import java.util.Iterator JavaDoc;
23 import java.util.Map JavaDoc;
24
25 /**
26  * This class is used for testing...how else could it be used? You tell us. ;-)
27  */

28 public class Stopwatch {
29
30   private static final Log log = LogFactory.getLog(Stopwatch.class);
31
32   private Map JavaDoc taskMap = new HashMap JavaDoc();
33
34   private String JavaDoc currentTaskName = null;
35   private long currentTaskTime = 0;
36
37   /**
38    * Get an iterator of the tasks
39    * @return - the Iterator
40    */

41   public Iterator JavaDoc getTaskNames() {
42     return taskMap.keySet().iterator();
43   }
44
45   /**
46    * Get the number of times assigned to a task
47    * @param taskName - the name of the task
48    * @return - the number of times
49    */

50   public long getTaskCount(String JavaDoc taskName) {
51     return ((TaskStat) taskMap.get(taskName)).getCount();
52   }
53
54   /**
55    * Get the total time added to a task
56    * @param taskName - the name of the task
57    * @return - the total time added to the task
58    */

59   public long getTotalTaskTime(String JavaDoc taskName) {
60     return ((TaskStat) taskMap.get(taskName)).getTotal();
61   }
62
63   /**
64    * Get the maximum time added to a task
65    * @param taskName - the name of the task
66    * @return - the maximum time added to a task
67    */

68   public long getMaxTaskTime(String JavaDoc taskName) {
69     return ((TaskStat) taskMap.get(taskName)).getMax();
70   }
71
72   /**
73    * Get the minimum time added to a task
74    * @param taskName - the name of the task
75    * @return - the minimum time added to a task
76    */

77   public long getMinTaskTime(String JavaDoc taskName) {
78     return ((TaskStat) taskMap.get(taskName)).getMin();
79   }
80
81   /**
82    * Get the average time added to a task
83    * @param taskName - the name of the task
84    * @return - the average time added to a task
85    */

86   public long getAvgTaskTime(String JavaDoc taskName) {
87     return ((TaskStat) taskMap.get(taskName)).getAverage();
88   }
89
90   /**
91    * Start (create) a task
92    * @param taskName - the name of the task
93    */

94   public void start(String JavaDoc taskName) {
95     if (log.isDebugEnabled()) {
96       log.debug("Starting: " + taskName);
97     }
98     this.currentTaskName = taskName;
99     currentTaskTime = System.currentTimeMillis();
100   }
101
102   /**
103    * Stop the timer on a task
104    */

105   public void stop() {
106     if (log.isDebugEnabled()) {
107       log.debug("Stopping: " + currentTaskName);
108     }
109     currentTaskTime = System.currentTimeMillis() - currentTaskTime;
110     appendTaskTime(currentTaskName, currentTaskTime);
111   }
112
113   private synchronized void appendTaskTime(String JavaDoc taskName, long taskTime) {
114     TaskStat stat = (TaskStat) taskMap.get(taskName);
115     if (stat == null) {
116       stat = new TaskStat();
117       taskMap.put(taskName, stat);
118     }
119     stat.appendTaskTime(taskTime);
120   }
121
122   /**
123    * Merge another StopWatch into this one
124    * @param watch - the StopWatch to merge into this one
125    */

126   public void mergeStopwatch(Stopwatch watch) {
127     Iterator JavaDoc names = watch.getTaskNames();
128     while (names.hasNext()) {
129       String JavaDoc name = (String JavaDoc) names.next();
130       long taskTime = watch.getTotalTaskTime(name);
131       appendTaskTime(name, taskTime);
132     }
133   }
134
135   /**
136    * Reset all of the timers in this StopWatch
137    */

138   public synchronized void reset() {
139     taskMap.clear();
140   }
141
142   public String JavaDoc toString() {
143     StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
144     buffer.append("Task,Count,Total,Max,Min,Avg\n");
145     Iterator JavaDoc names = getTaskNames();
146     while (names.hasNext()) {
147       String JavaDoc name = (String JavaDoc) names.next();
148       long taskCount = getTaskCount(name);
149       long taskTime = getTotalTaskTime(name);
150       long taskMin = getMinTaskTime(name);
151       long taskMax = getMaxTaskTime(name);
152       long taskAvg = getAvgTaskTime(name);
153       buffer.append(name + "," + taskCount + "," + taskTime + "," + taskMax + "," + taskMin + "," + taskAvg + "\n");
154     }
155     return buffer.toString();
156   }
157
158   private class TaskStat {
159     private static final long UNSET = -999999;
160
161     private long count = 0;
162     private long total = 0;
163     private long min = UNSET;
164     private long max = UNSET;
165
166     /**
167      * Add some time to a task
168      * @param taskTime - the time to add
169      */

170     public void appendTaskTime(long taskTime) {
171       count++;
172       total += taskTime;
173       if (max == UNSET || taskTime > max) {
174         max = taskTime;
175       }
176       if (min == UNSET || taskTime < min) {
177         min = taskTime;
178       }
179     }
180
181     /**
182      * Get the total time for the task
183      * @return - the total time
184      */

185     public long getTotal() {
186       return total;
187     }
188
189     /**
190      * Get the maximum of the times added to the task
191      * @return - the max value
192      */

193     public long getMax() {
194       return max;
195     }
196
197     /**
198      * Get the minimum of the times added to the task
199      * @return - the minimum value
200      */

201     public long getMin() {
202       return min;
203     }
204
205     /**
206      * Get the number of times added to the task
207      * @return - the number of times
208      */

209     public long getCount() {
210       return count;
211     }
212
213     /**
214      * Get the average of the times added to the task
215      * @return - the average
216      */

217     public long getAverage() {
218       if (count > 0) {
219         return Math.round((double) total / (double) count);
220       } else {
221         return 0;
222       }
223     }
224
225   }
226
227 }
228
229
230
231
Popular Tags