Location: JWare Software » AntXtras » Documents » How Tos » Time Tasks
Time Tasks
Q: How do I use variables to time a task or set of tasks?
This is a common request so AntXtras has a straightforward way to record and capture the time it takes a group of tasks to execute. First you need to record your start time using a variable (which can be set and reset for any number of iterations) with the special “now” operation. Then after the tasks-to-be-timed have completed, you can update that same variable with the duration using the special “-now” operation. The following snippet demonstrates how to do this:
<target name="timedtarget"> <assign var="time" op="now"/> [...tasks to be timed...] <assign var="time" op="-now"/> </target>
One problem with the snippet above, is it captures the duration as milliseconds (a long number). If you want to capture the duration as a human readable string like say “30sec.213ms” for 30 seconds 213 milliseconds, you could use the $duration: function shortcut with the captured duration like so:
<target name="timedtarget"> <assign var="time" op="now"/> [...tasks to be timed...] <assign var="time" op="-now"/> <assign var="time" value="${$var:time|$duration:}"/> </target> [alternatively with help of $now: shortcut from 2.0.0] <target name="timedtarget"> <assign var="time" op="now"/> [...tasks to be timed...] <assign var="time" value="${$now:--${$var:time}|$duration:}"/> </target>
[2.0+] An interesting presetdef wrapper task1) for targets or tasksets you’d liked timed might look like the snippet below which extends the examples above with the $fixture: shortcut to automatically stamp the time it takes to run an entire target.
<presetdef name="timed"> <protect> <assign var="_#_${$fixture:target}" op="now"/> <always> <show level="info" message="[T] ${$now:--${$var:_#_${$fixture:target}}|$duration:}"/> </always> </protect> </presetdef> [to be used like...] <target name="timedtarget"> <timed> [work work work...] </timed> </target>