| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
package net.sf.antcontrib.perf; |
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
public class StopWatch { |
| 37 |
|
| 38 |
|
| 39 |
private String name = ""; |
| 40 |
|
| 41 |
|
| 42 |
private long startTime = 0; |
| 43 |
|
| 44 |
|
| 45 |
private long stopTime = 0; |
| 46 |
|
| 47 |
|
| 48 |
private long totalTime = 0; |
| 49 |
|
| 50 |
|
| 51 |
private boolean running = false; |
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
public StopWatch() { |
| 57 |
this( "" ); |
| 58 |
} |
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
public StopWatch( String name ) { |
| 65 |
this.name = name; |
| 66 |
start(); |
| 67 |
} |
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
public long start() { |
| 76 |
if ( !running ) |
| 77 |
startTime = System.currentTimeMillis(); |
| 78 |
running = true; |
| 79 |
return startTime; |
| 80 |
} |
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 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 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
public long total() { |
| 103 |
stop(); |
| 104 |
long rtn = totalTime; |
| 105 |
totalTime = 0; |
| 106 |
return rtn; |
| 107 |
} |
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
public long elapsed() { |
| 115 |
return System.currentTimeMillis() - startTime; |
| 116 |
} |
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
public String getName() { |
| 122 |
return name; |
| 123 |
} |
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 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 |
|
| 153 |
|
| 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 |
|
| 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 |
|
| 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 |
|