KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > dom4j > samples > performance > Timer


1 /*
2  * Copyright 2001-2004 (C) MetaStuff, Ltd. All Rights Reserved.
3  *
4  * This software is open source.
5  * See the bottom of this file for the licence.
6  *
7  * $Id: Timer.java,v 1.4 2005/01/29 14:53:13 maartenc Exp $
8  */

9
10 package org.dom4j.samples.performance;
11
12 /**
13  * A timer for use in performance monitoring
14  *
15  * @author <a HREF="mailto:james.strachan@metastuff.com">James Strachan </a>
16  * @version $Revision: 1.4 $
17  */

18 public class Timer {
19
20     /** Whether the performance of each run is printed */
21     protected static boolean VERBOSE = false;
22
23     /** Default number of loops */
24     protected static final int DEFAULT_LOOP_COUNT = 40;
25
26     /** The number of the first loops to display */
27     private int displayCount = 4;
28
29     /** Number of loops to perform */
30     private int loopCount = DEFAULT_LOOP_COUNT;
31
32     private Task task;
33
34     public Timer() {
35     }
36
37     public Timer(Task task) {
38         this.task = task;
39     }
40
41     /**
42      * Performs a piece of code a number of times in a loop
43      *
44      * @param loopCount
45      * is the number of loops to perform
46      */

47     public void run() throws Exception JavaDoc {
48         Task task = getTask();
49         int size = getLoopCount();
50         if (size <= 0 || task == null) {
51             return;
52         }
53
54         long[] times = new long[size];
55         for (int i = 0; i < size; i++) {
56             long start = System.currentTimeMillis();
57
58             task.run();
59
60             long end = System.currentTimeMillis();
61             times[i] = end - start;
62         }
63
64         printSummary(times);
65     }
66
67     // Properties
68
// -------------------------------------------------------------------------
69
public Task getTask() {
70         return task;
71     }
72
73     public void setTask(Task task) {
74         this.task = task;
75     }
76
77     public int getLoopCount() {
78         return loopCount;
79     }
80
81     public void setLoopCount(int loopCount) {
82         this.loopCount = loopCount;
83     }
84
85     // Implementation methods
86
// -------------------------------------------------------------------------
87
protected void printSummary(long[] times) {
88         println("Performance summary");
89
90         println("Number of runs: " + loopCount);
91
92         if (VERBOSE || loopCount < displayCount) {
93             displayCount = loopCount;
94         }
95         for (int i = 0; i < displayCount; i++) {
96             println("run: " + i + " took: " + times[i] + " (ms)");
97         }
98
99         long minimum = times[0];
100         for (int i = 1; i < loopCount; i++) {
101             long time = times[i];
102             if (time < minimum) {
103                 minimum = time;
104             }
105         }
106
107         println("Minimum time of run : " + minimum + " (ms)");
108
109         // average ignoring first loop
110
long total = 0;
111         for (int i = 0; i < loopCount; i++) {
112             total += times[i];
113         }
114
115         double average = total / loopCount;
116
117         println("Average time of run : " + average + " (ms)");
118
119         if (loopCount == 1) {
120             return;
121         }
122         long total_1 = total - times[0];
123         average = total_1 / (loopCount - 1);
124
125         println("Average (excluding first run) : " + average + " (ms)");
126
127         if (loopCount == 2) {
128             return;
129         }
130         long total_2 = total_1 - times[1];
131         average = total_2 / (loopCount - 2);
132
133         println("Average (excluding first & second run): " + average + " (ms)");
134
135         println("Total time of run : " + total + " (ms)");
136         println("Total (excluding first run) : " + total_1 + " (ms)");
137         println("Total (excluding first & second run) : " + total_2 + " (ms)");
138
139         return;
140     }
141
142     protected void println(String JavaDoc text) {
143         System.out.println(text);
144     }
145 }
146
147 /*
148  * Redistribution and use of this software and associated documentation
149  * ("Software"), with or without modification, are permitted provided that the
150  * following conditions are met:
151  *
152  * 1. Redistributions of source code must retain copyright statements and
153  * notices. Redistributions must also contain a copy of this document.
154  *
155  * 2. Redistributions in binary form must reproduce the above copyright notice,
156  * this list of conditions and the following disclaimer in the documentation
157  * and/or other materials provided with the distribution.
158  *
159  * 3. The name "DOM4J" must not be used to endorse or promote products derived
160  * from this Software without prior written permission of MetaStuff, Ltd. For
161  * written permission, please contact dom4j-info@metastuff.com.
162  *
163  * 4. Products derived from this Software may not be called "DOM4J" nor may
164  * "DOM4J" appear in their names without prior written permission of MetaStuff,
165  * Ltd. DOM4J is a registered trademark of MetaStuff, Ltd.
166  *
167  * 5. Due credit should be given to the DOM4J Project - http://www.dom4j.org
168  *
169  * THIS SOFTWARE IS PROVIDED BY METASTUFF, LTD. AND CONTRIBUTORS ``AS IS'' AND
170  * ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
171  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
172  * ARE DISCLAIMED. IN NO EVENT SHALL METASTUFF, LTD. OR ITS CONTRIBUTORS BE
173  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
174  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
175  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
176  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
177  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
178  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
179  * POSSIBILITY OF SUCH DAMAGE.
180  *
181  * Copyright 2001-2004 (C) MetaStuff, Ltd. All Rights Reserved.
182  *
183  * $Id: Timer.java,v 1.4 2005/01/29 14:53:13 maartenc Exp $
184  */

185
Popular Tags