[15023] | 1 | /*
|
---|
| 2 | * The Apache Software License, Version 1.1
|
---|
| 3 | *
|
---|
| 4 | * Copyright (c) 2000-2002 The Apache Software Foundation. All rights
|
---|
| 5 | * reserved.
|
---|
| 6 | *
|
---|
| 7 | * Redistribution and use in source and binary forms, with or without
|
---|
| 8 | * modification, are permitted provided that the following conditions
|
---|
| 9 | * are met:
|
---|
| 10 | *
|
---|
| 11 | * 1. Redistributions of source code must retain the above copyright
|
---|
| 12 | * notice, this list of conditions and the following disclaimer.
|
---|
| 13 | *
|
---|
| 14 | * 2. Redistributions in binary form must reproduce the above copyright
|
---|
| 15 | * notice, this list of conditions and the following disclaimer in
|
---|
| 16 | * the documentation and/or other materials provided with the
|
---|
| 17 | * distribution.
|
---|
| 18 | *
|
---|
| 19 | * 3. The end-user documentation included with the redistribution, if
|
---|
| 20 | * any, must include the following acknowlegement:
|
---|
| 21 | * "This product includes software developed by the
|
---|
| 22 | * Apache Software Foundation (http://www.apache.org/)."
|
---|
| 23 | * Alternately, this acknowlegement may appear in the software itself,
|
---|
| 24 | * if and wherever such third-party acknowlegements normally appear.
|
---|
| 25 | *
|
---|
| 26 | * 4. The names "The Jakarta Project", "Ant", and "Apache Software
|
---|
| 27 | * Foundation" must not be used to endorse or promote products derived
|
---|
| 28 | * from this software without prior written permission. For written
|
---|
| 29 | * permission, please contact [email protected].
|
---|
| 30 | *
|
---|
| 31 | * 5. Products derived from this software may not be called "Apache"
|
---|
| 32 | * nor may "Apache" appear in their names without prior written
|
---|
| 33 | * permission of the Apache Group.
|
---|
| 34 | *
|
---|
| 35 | * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
|
---|
| 36 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
| 37 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
---|
| 38 | * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
|
---|
| 39 | * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
---|
| 40 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
---|
| 41 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
|
---|
| 42 | * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
---|
| 43 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
---|
| 44 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
|
---|
| 45 | * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
---|
| 46 | * SUCH DAMAGE.
|
---|
| 47 | * ====================================================================
|
---|
| 48 | *
|
---|
| 49 | * This software consists of voluntary contributions made by many
|
---|
| 50 | * individuals on behalf of the Apache Software Foundation. For more
|
---|
| 51 | * information on the Apache Software Foundation, please see
|
---|
| 52 | * <http://www.apache.org/>.
|
---|
| 53 | */
|
---|
| 54 | package ise.antelope.tasks;
|
---|
| 55 |
|
---|
| 56 | /**
|
---|
| 57 | * A stopwatch, useful for 'quick and dirty' performance testing. Typical usage:
|
---|
| 58 | * <pre>
|
---|
| 59 | * StopWatch sw = new StopWatch(); // automatically starts
|
---|
| 60 | * // do something here...
|
---|
| 61 | * sw.stop();
|
---|
| 62 | * System.out.println(sw.toString()); // print the total
|
---|
| 63 | * sw.start(); // restart the stopwatch
|
---|
| 64 | * // do some more things...
|
---|
| 65 | * sw.stop();
|
---|
| 66 | * System.out.println(sw.format(sw.elapsed()); // print the time since the last start
|
---|
| 67 | * System.out.println(sw.toString()); // print the cumulative total
|
---|
| 68 | *
|
---|
| 69 | * </pre>
|
---|
| 70 | * @author Dale Anson
|
---|
| 71 | * @version $Revision: 1.2 $
|
---|
| 72 | */
|
---|
| 73 | public class StopWatch {
|
---|
| 74 |
|
---|
| 75 | /** an identifying name for this stopwatch */
|
---|
| 76 | private String name = "";
|
---|
| 77 |
|
---|
| 78 | /** storage for start time */
|
---|
| 79 | private long startTime = 0;
|
---|
| 80 |
|
---|
| 81 | /** storage for stop time */
|
---|
| 82 | private long stopTime = 0;
|
---|
| 83 |
|
---|
| 84 | /** cumulative elapsed time */
|
---|
| 85 | private long totalTime = 0;
|
---|
| 86 |
|
---|
| 87 | /** is the stopwatch running? */
|
---|
| 88 | private boolean running = false;
|
---|
| 89 |
|
---|
| 90 | /**
|
---|
| 91 | * Starts the stopwatch.
|
---|
| 92 | */
|
---|
| 93 | public StopWatch() {
|
---|
| 94 | this( "" );
|
---|
| 95 | }
|
---|
| 96 |
|
---|
| 97 | /**
|
---|
| 98 | * Starts the stopwatch.
|
---|
| 99 | * @param name an identifying name for this StopWatch
|
---|
| 100 | */
|
---|
| 101 | public StopWatch( String name ) {
|
---|
| 102 | this.name = name;
|
---|
| 103 | start();
|
---|
| 104 | }
|
---|
| 105 |
|
---|
| 106 | /**
|
---|
| 107 | * Starts/restarts the stopwatch. <code>stop</code> must be called prior
|
---|
| 108 | * to restart.
|
---|
| 109 | *
|
---|
| 110 | * @return the start time, the long returned System.currentTimeMillis().
|
---|
| 111 | */
|
---|
| 112 | public long start() {
|
---|
| 113 | if ( !running )
|
---|
| 114 | startTime = System.currentTimeMillis();
|
---|
| 115 | running = true;
|
---|
| 116 | return startTime;
|
---|
| 117 | }
|
---|
| 118 |
|
---|
| 119 | /**
|
---|
| 120 | * Stops the stopwatch.
|
---|
| 121 | *
|
---|
| 122 | * @return the stop time, the long returned System.currentTimeMillis().
|
---|
| 123 | */
|
---|
| 124 | public long stop() {
|
---|
| 125 | stopTime = System.currentTimeMillis();
|
---|
| 126 | if ( running ) {
|
---|
| 127 | totalTime += stopTime - startTime;
|
---|
| 128 | }
|
---|
| 129 | startTime = stopTime;
|
---|
| 130 | running = false;
|
---|
| 131 | return stopTime;
|
---|
| 132 | }
|
---|
| 133 |
|
---|
| 134 | /**
|
---|
| 135 | * Total cumulative elapsed time, stops the stopwatch.
|
---|
| 136 | *
|
---|
| 137 | * @return the total time
|
---|
| 138 | */
|
---|
| 139 | public long total() {
|
---|
| 140 | stop();
|
---|
| 141 | long rtn = totalTime;
|
---|
| 142 | totalTime = 0;
|
---|
| 143 | return rtn;
|
---|
| 144 | }
|
---|
| 145 |
|
---|
| 146 | /**
|
---|
| 147 | * Elapsed time, difference between the last start time and now.
|
---|
| 148 | *
|
---|
| 149 | * @return the elapsed time
|
---|
| 150 | */
|
---|
| 151 | public long elapsed() {
|
---|
| 152 | return System.currentTimeMillis() - startTime;
|
---|
| 153 | }
|
---|
| 154 |
|
---|
| 155 | /**
|
---|
| 156 | * @return the name of this StopWatch
|
---|
| 157 | */
|
---|
| 158 | public String getName() {
|
---|
| 159 | return name;
|
---|
| 160 | }
|
---|
| 161 |
|
---|
| 162 | /**
|
---|
| 163 | * Formats the given time into decimal seconds.
|
---|
| 164 | * @return the time formatted as mm:ss.ddd
|
---|
| 165 | */
|
---|
| 166 | public String format( long ms ) {
|
---|
| 167 | String total = String.valueOf( ms );
|
---|
| 168 | String frontpad = "000";
|
---|
| 169 | int pad_length = 3 - total.length();
|
---|
| 170 | if ( pad_length >= 0 )
|
---|
| 171 | total = "0." + frontpad.substring( 0, pad_length ) + total;
|
---|
| 172 | else {
|
---|
| 173 | String dec = total.substring( total.length() - 3 );
|
---|
| 174 | total = "";
|
---|
| 175 | int min = 0, sec = 0;
|
---|
| 176 | min = ( int ) ( ms / 60000 );
|
---|
| 177 | sec = min > 0 ? ( int ) ( ( ms - ( min * 60000 ) ) / 1000 ) : ( int ) ( ms / 1000 );
|
---|
| 178 | if ( min > 0 ) {
|
---|
| 179 | total = String.valueOf( min ) + ":" + ( sec < 10 ? "0" : "" ) + String.valueOf( sec ) + "." + dec;
|
---|
| 180 | }
|
---|
| 181 | else {
|
---|
| 182 | total = String.valueOf( sec ) + "." + dec;
|
---|
| 183 | }
|
---|
| 184 | }
|
---|
| 185 | return total + " sec";
|
---|
| 186 | }
|
---|
| 187 |
|
---|
| 188 | /**
|
---|
| 189 | * Returns the total elapsed time of the stopwatch formatted in decimal seconds.
|
---|
| 190 | * @return [name: mm:ss.ddd]
|
---|
| 191 | */
|
---|
| 192 | public String toString() {
|
---|
| 193 | StringBuffer sb = new StringBuffer();
|
---|
| 194 | sb.append( "[" );
|
---|
| 195 | if ( name != null )
|
---|
| 196 | sb.append( name ).append( ": " );
|
---|
| 197 | sb.append( format( totalTime ) );
|
---|
| 198 | sb.append( "]" );
|
---|
| 199 | return sb.toString();
|
---|
| 200 | }
|
---|
| 201 |
|
---|
| 202 | public static void main ( String[] args ) {
|
---|
| 203 | StopWatch sw = new StopWatch( "test" );
|
---|
| 204 |
|
---|
| 205 | // test the formatter
|
---|
| 206 | System.out.println( sw.format( 1 ) );
|
---|
| 207 | System.out.println( sw.format( 10 ) );
|
---|
| 208 | System.out.println( sw.format( 100 ) );
|
---|
| 209 | System.out.println( sw.format( 1000 ) );
|
---|
| 210 | System.out.println( sw.format( 100000 ) );
|
---|
| 211 | System.out.println( sw.format( 1000000 ) );
|
---|
| 212 |
|
---|
| 213 | // test the stopwatch
|
---|
| 214 | try {
|
---|
| 215 | System.out.println( "StopWatch: " + sw.getName() );
|
---|
| 216 | Thread.currentThread().sleep( 2000 );
|
---|
| 217 | sw.stop();
|
---|
| 218 | System.out.println( sw.toString() );
|
---|
| 219 | sw.start();
|
---|
| 220 | Thread.currentThread().sleep( 2000 );
|
---|
| 221 | sw.stop();
|
---|
| 222 | System.out.println( "elapsed: " + sw.format( sw.elapsed() ) );
|
---|
| 223 | System.out.println( "total: " + sw.format( sw.total() ) );
|
---|
| 224 | }
|
---|
| 225 | catch ( Exception e ) {
|
---|
| 226 | e.printStackTrace();
|
---|
| 227 | }
|
---|
| 228 | }
|
---|
| 229 | }
|
---|
| 230 |
|
---|