| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
package net.sf.antcontrib.perf; |
| 17 |
|
| 18 |
import java.util.Hashtable; |
| 19 |
|
| 20 |
import org.apache.tools.ant.BuildException; |
| 21 |
import org.apache.tools.ant.Task; |
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
public class StopWatchTask extends Task { |
| 30 |
|
| 31 |
|
| 32 |
private String name = null; |
| 33 |
|
| 34 |
|
| 35 |
private String action = null; |
| 36 |
|
| 37 |
|
| 38 |
private static Hashtable watches = null; |
| 39 |
|
| 40 |
|
| 41 |
private static final String STOP = "stop"; |
| 42 |
private static final String START = "start"; |
| 43 |
private static final String ELAPSED = "elapsed"; |
| 44 |
private static final String TOTAL = "total"; |
| 45 |
|
| 46 |
|
| 47 |
public void setName( String name ) { |
| 48 |
this.name = name; |
| 49 |
} |
| 50 |
|
| 51 |
public void setAction( String action ) { |
| 52 |
action = action.toLowerCase(); |
| 53 |
if ( action.equals( STOP ) || |
| 54 |
action.equals( START ) || |
| 55 |
action.equals( ELAPSED ) || |
| 56 |
action.equals( TOTAL ) ) { |
| 57 |
this.action = action; |
| 58 |
} |
| 59 |
else { |
| 60 |
throw new BuildException( "invalid action: " + action ); |
| 61 |
} |
| 62 |
} |
| 63 |
|
| 64 |
public void execute() { |
| 65 |
if ( name == null ) |
| 66 |
throw new BuildException( "name is null" ); |
| 67 |
if ( action == null ) |
| 68 |
action = START; |
| 69 |
if ( watches == null ) |
| 70 |
watches = new Hashtable(); |
| 71 |
StopWatch sw = ( StopWatch ) watches.get( name ); |
| 72 |
if ( sw == null && action.equals( START ) ) { |
| 73 |
sw = new StopWatch( name ); |
| 74 |
watches.put( name, sw ); |
| 75 |
return ; |
| 76 |
} |
| 77 |
if ( sw == null ) |
| 78 |
return ; |
| 79 |
if ( action.equals( START) ) { |
| 80 |
sw.start(); |
| 81 |
return; |
| 82 |
} |
| 83 |
if ( action.equals( STOP ) ) { |
| 84 |
sw.stop(); |
| 85 |
return ; |
| 86 |
} |
| 87 |
if ( action.equals( TOTAL ) ) { |
| 88 |
String time = sw.format( sw.total() ); |
| 89 |
log( "[" + name + ": " + time + "]" ); |
| 90 |
getProject().setProperty(name, time); |
| 91 |
return ; |
| 92 |
} |
| 93 |
if ( action.equals( ELAPSED ) ) { |
| 94 |
String time = sw.format( sw.elapsed() ); |
| 95 |
log( "[" + name + ": " + time + "]" ); |
| 96 |
getProject().setProperty(name, time); |
| 97 |
return ; |
| 98 |
} |
| 99 |
} |
| 100 |
} |