Looking to find out how long it tasks to execute a MSBuild task? Below is a quick method that I recently used to accomplish this. (Based on this blog)
- Before the task, you will need to set a property that is equal to the current UTC ticks.
- After the task, you will again need to set a property that is equal to the current UTC ticks.
- Now you will need to subtract #1 from #2, which will give you the number of ticks that task executed for.
- Convert the number of ticks to a friendly time value
Note – There are ten million ticks in a second
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003" DefaultTargets="Init">
<Target Name="Init">
<!-- Set start property -->
<PropertyGroup>
<StartUTCTicks>$([System.DateTime]::UtcNow.Ticks)</StartUTCTicks>
</PropertyGroup>
<!-- Command to simulate sleeping for 4 seconds -->
<Exec Command="ping 127.0.0.1 -n 5 -w 1000 >NUL"/>
<!-- Set the finish time, calculate overall time, and then output in freiendly format -->
<PropertyGroup>
<FinishUTCTicks>$([System.DateTime]::UtcNow.Ticks)</FinishUTCTicks>
<ExecutionTicks>$([MSBuild]::Subtract($(FinishUTCTicks), $(StartUTCTicks)))</ExecutionTicks>
<FriendlyTime>$([System.TimeSpan]::FromTicks($(ExecutionTicks)))</FriendlyTime>
</PropertyGroup>
<Message Text="Init Task Total Time: $(FriendlyTime)"/>
</Target>
</Project>
Below a the results with the execution time displayed in the standard format.
![]()