KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > core > util > PDEPerfTesterUtil


1 /*******************************************************************************
2  * Copyright (c) 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11
12 package org.eclipse.pde.internal.core.util;
13
14 import java.io.IOException JavaDoc;
15 import java.io.Writer JavaDoc;
16
17 /**
18  * PDEPerformanceTester
19  *
20  */

21 public class PDEPerfTesterUtil {
22
23     private String JavaDoc fTag;
24     
25     private long fDuration;
26     
27     private long fStart;
28     
29     private long fEnd;
30     
31     private long fIteration;
32     
33     private long fTotalDuration;
34     
35     private long fAverageDuration;
36     
37     private final static long F_SECOND_IN_MS = 1000;
38
39     private final static long F_MINUTE_IN_MS = 60000;
40
41     private final static long F_HOUR_IN_MS = 3600000;
42     
43     /**
44      * @param tag
45      */

46     public PDEPerfTesterUtil(String JavaDoc tag) {
47         fTag = tag;
48         reset();
49     }
50     
51     /**
52      *
53      */

54     public void reset() {
55         fDuration = 0;
56         fStart = 0;
57         fEnd = 0;
58         fIteration = 0;
59         fTotalDuration = 0;
60         fAverageDuration = 0;
61     }
62     
63     /**
64      *
65      */

66     public void start() {
67         fIteration++;
68         fStart = System.currentTimeMillis();
69     }
70     
71     /**
72      *
73      */

74     public void stop() {
75         fEnd = System.currentTimeMillis();
76         calculateDuration();
77     }
78     
79     /**
80      *
81      */

82     private void calculateDuration() {
83         fDuration = (fEnd - fStart);
84         fTotalDuration = fTotalDuration + fDuration;
85         if (fIteration > 0) {
86             fAverageDuration = fTotalDuration / fIteration;
87         }
88     }
89     
90     /**
91      * @param duration
92      * @return
93      */

94     private String JavaDoc formatDuration(long duration) {
95         
96         String JavaDoc output = null;
97         int hours = 0;
98         int minutes = 0;
99         int seconds = 0;
100         long milliseconds = 0;
101         long timeDifference = duration;
102         
103         hours = (int)Math.rint(timeDifference / F_HOUR_IN_MS);
104         if (hours > 0) {
105             timeDifference = timeDifference - (hours * F_HOUR_IN_MS);
106         }
107         
108         minutes = (int)Math.rint(timeDifference / F_MINUTE_IN_MS);
109         if (minutes > 0) {
110             timeDifference = timeDifference - (minutes * F_MINUTE_IN_MS);
111         }
112
113         seconds = (int)Math.rint(timeDifference / F_SECOND_IN_MS);
114         if (seconds > 0) {
115             timeDifference = timeDifference - (seconds * F_SECOND_IN_MS);
116         }
117         
118         milliseconds = timeDifference;
119         
120         output = hours + " h " + //$NON-NLS-1$
121
minutes + " m " + //$NON-NLS-1$
122
seconds + " s " + //$NON-NLS-1$
123
milliseconds + " ms"; //$NON-NLS-1$
124

125         return output;
126     }
127     
128     /**
129      * @param writer
130      */

131     public void printDuration(Writer JavaDoc writer) {
132         String JavaDoc output = formatTag() +
133                         "(" + //$NON-NLS-1$
134
fIteration +
135                         "): " + //$NON-NLS-1$
136
formatDuration(fDuration) +
137                         "\n"; //$NON-NLS-1$
138
try {
139             writer.write(output);
140             writer.flush();
141         } catch (IOException JavaDoc e) {
142             // Ignore
143
}
144     }
145     
146     /**
147      * @param writer
148      */

149     public void printTotalDuration(Writer JavaDoc writer) {
150         String JavaDoc output = formatTag() +
151                         "(TOTAL " + //$NON-NLS-1$
152
fIteration +
153                         "): " + //$NON-NLS-1$
154
formatDuration(fTotalDuration) +
155                         "\n"; //$NON-NLS-1$
156
try {
157             writer.write(output);
158             writer.flush();
159         } catch (IOException JavaDoc e) {
160             // Ignore
161
}
162     }
163     
164     /**
165      * @param writer
166      */

167     public void printAverageDuration(Writer JavaDoc writer) {
168         String JavaDoc output = formatTag() +
169                         "(AVERAGE " + //$NON-NLS-1$
170
fIteration +
171                         "): " + //$NON-NLS-1$
172
formatDuration(fAverageDuration) +
173                         "\n"; //$NON-NLS-1$
174
try {
175             writer.write(output);
176             writer.flush();
177         } catch (IOException JavaDoc e) {
178             // Ignore
179
}
180     }
181     
182     /**
183      * @return
184      */

185     private String JavaDoc formatTag() {
186         return "[" + fTag + "]: "; //$NON-NLS-1$ //$NON-NLS-2$
187
}
188     
189 }
190
Popular Tags