Debian Codegraph @

src/main/java/net/sf/antcontrib/perf/StopWatch.java

raw symbols SCIP
1 /*
2 * Copyright (c) 2001-2004 Ant-Contrib project. All rights reserved.
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 net.sf.antcontrib.perf;
17
18 /**
19 * A stopwatch, useful for 'quick and dirty' performance testing. Typical usage:
20 * <pre>
21 * StopWatch sw = new StopWatch(); // automatically starts
22 * // do something here...
23 * sw.stop();
24 * System.out.println(sw.toString()); // print the total
25 * sw.start(); // restart the stopwatch
26 * // do some more things...
27 * sw.stop();
28 * System.out.println(sw.format(sw.elapsed()); // print the time since the last start
29 * System.out.println(sw.toString()); // print the cumulative total
30 * </pre>
31 * <p>Developed for use with Antelope, migrated to ant-contrib Oct 2003.
32 *
33 * @author Dale Anson
34 * @version $Revision: 1.4 $
35 */
36 public class StopWatch {
37
38 /** an identifying name for this stopwatch */
39 private String name = "";
40
41 /** storage for start time */
42 private long startTime = 0;
43
44 /** storage for stop time */
45 private long stopTime = 0;
46
47 /** cumulative elapsed time */
48 private long totalTime = 0;
49
50 /** is the stopwatch running? */
51 private boolean running = false;
52
53 /**
54 * Starts the stopwatch.
55 */
56 public StopWatch() {
57 this( "" );
58 }
59
60 /**
61 * Starts the stopwatch.
62 * @param name an identifying name for this StopWatch
63 */
64 public StopWatch( String name ) {
65 this.name = name;
66 start();
67 }
68
69 /**
70 * Starts/restarts the stopwatch. <code>stop</code> must be called prior
71 * to restart.
72 *
73 * @return the start time, the long returned System.currentTimeMillis().
74 */
75 public long start() {
76 if ( !running )
77 startTime = System.currentTimeMillis();
78 running = true;
79 return startTime;
80 }
81
82 /**
83 * Stops the stopwatch.
84 *
85 * @return the stop time, the long returned System.currentTimeMillis().
86 */
87 public long stop() {
88 stopTime = System.currentTimeMillis();
89 if ( running ) {
90 totalTime += stopTime - startTime;
91 }
92 startTime = stopTime;
93 running = false;
94 return stopTime;
95 }
96
97 /**
98 * Total cumulative elapsed time.
99 *
100 * @return the total time
101 */
102 public long total() {
103 stop();
104 long rtn = totalTime;
105 totalTime = 0;
106 return rtn;
107 }
108
109 /**
110 * Elapsed time, difference between the last start time and now.
111 *
112 * @return the elapsed time
113 */
114 public long elapsed() {
115 return System.currentTimeMillis() - startTime;
116 }
117
118 /**
119 * @return the name of this StopWatch
120 */
121 public String getName() {
122 return name;
123 }
124
125 /**
126 * Formats the given time into decimal seconds.
127 * @return the time formatted as mm:ss.ddd
128 */
129 public String format( long ms ) {
130 String total = String.valueOf( ms );
131 String frontpad = "000";
132 int pad_length = 3 - total.length();
133 if ( pad_length >= 0 )
134 total = "0." + frontpad.substring( 0, pad_length ) + total;
135 else {
136 String dec = total.substring( total.length() - 3 );
137 total = "";
138 int min = 0, sec = 0;
139 min = ( int ) ( ms / 60000 );
140 sec = min > 0 ? ( int ) ( ( ms - ( min * 60000 ) ) / 1000 ) : ( int ) ( ms / 1000 );
141 if ( min > 0 ) {
142 total = String.valueOf( min ) + ":" + ( sec < 10 ? "0" : "" ) + String.valueOf( sec ) + "." + dec;
143 }
144 else {
145 total = String.valueOf( sec ) + "." + dec;
146 }
147 }
148 return total + " sec";
149 }
150
151 /**
152 * Returns the total elapsed time of the stopwatch formatted in decimal seconds.
153 * @return [name: mm:ss.ddd]
154 */
155 public String toString() {
156 StringBuffer sb = new StringBuffer();
157 sb.append( "[" );
158 if ( name != null )
159 sb.append( name ).append( ": " );
160 sb.append( format( totalTime ) );
161 sb.append( "]" );
162 return sb.toString();
163 }
164
165 public static void main ( String[] args ) {
166 StopWatch sw = new StopWatch( "test" );
167
168 // test the formatter
169 System.out.println( sw.format( 1 ) );
170 System.out.println( sw.format( 10 ) );
171 System.out.println( sw.format( 100 ) );
172 System.out.println( sw.format( 1000 ) );
173 System.out.println( sw.format( 100000 ) );
174 System.out.println( sw.format( 1000000 ) );
175
176 // test the stopwatch
177 try {
178 System.out.println( "StopWatch: " + sw.getName() );
179 Thread.currentThread().sleep( 2000 );
180 sw.stop();
181 System.out.println( sw.toString() );
182 sw.start();
183 Thread.currentThread().sleep( 2000 );
184 sw.stop();
185 System.out.println( "elapsed: " + sw.format( sw.elapsed() ) );
186 System.out.println( "total: " + sw.format( sw.total() ) );
187 }
188 catch ( Exception e ) {
189 e.printStackTrace();
190 }
191 }
192 }
193