Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
<?xml version="1.0" encoding="utf-8"?>
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003" DefaultTargets="Test">
<Target Name="Test">
<PropertyGroup>
<StartTicks>$([System.DateTime]::UtcNow.Ticks)</StartTicks>
</PropertyGroup>
<Message Text="Starting ticks: $(StartTicks)"/>
<!-- Do some stuff. For this example, we'll launch notepad and wait for it to exit. -->
<Exec Command="notepad"/>
<PropertyGroup>
<FinishTicks>$([System.DateTime]::UtcNow.Ticks)</FinishTicks>
<ElapsedTicks>$([MSBuild]::Subtract($(FinishTicks), $(StartTicks)))</ElapsedTicks>
<Elapsed>$([System.TimeSpan]::FromTicks($(ElapsedTicks)))</Elapsed>
</PropertyGroup>
<Message Text="Elapsed time: $(Elapsed)"/>
</Target>
</Project>
<?xml version="1.0" encoding="utf-8"?>
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003" DefaultTargets="Test">
<Target Name="Test">
<GetTimeStamp>
<Output PropertyName="StartTimeStamp" TaskParameter="TimeStamp" />
</GetTimeStamp>
<Message Text="Starting timestamp: $(StartTimeStamp)"/>
<!-- Do some stuff. For this example, we'll launch notepad and wait for it to exit. -->
<Exec Command="notepad"/>
<GetElapsedTimeSpan StartTicks="$(StartTimeStamp)">
<Output PropertyName="EndTimeSpan" TaskParameter="ElapsedTimeSpan" />
</GetElapsedTimeSpan>
<Message Text="Elapsed time: $(EndTimeSpan)"/>
</Target>
<UsingTask TaskName="GetTimeStamp" TaskFactory="CodeTaskFactory" AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.v4.0.dll">
<ParameterGroup><TimeStamp ParameterType="System.Int64" Output="true" /></ParameterGroup>
<Task>
<Using Namespace="System.Diagnostics" />
<Code Type="Fragment" Language="cs">
<![CDATA[ TimeStamp = System.Diagnostics.Stopwatch.GetTimestamp(); ]]>
</Code>
</Task>
</UsingTask>
<UsingTask TaskName="GetElapsedTimeSpan" TaskFactory="CodeTaskFactory" AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.v4.0.dll">
<ParameterGroup>
<ElapsedTimeSpan ParameterType="System.String" Output="true" />
<StartTicks ParameterType="System.Int64" Required="true" />
</ParameterGroup>
<Task>
<Using Namespace="System.Diagnostics" />
<Code Type="Fragment" Language="cs">
<![CDATA[
// We can get the current timestamp and then calculate the elapsed ticks. To improve
// accuracy of the benchmark, you could expose another task parameter to allow passing in the
// current ticks, so that the overhead of calling this task isn't included in the elapsed
// calculation.
var currentTicks = Stopwatch.GetTimestamp();
var elapsedTicks = currentTicks - StartTicks;
// This code is taken from a combination of the static constructor and private GetElapsedDateTimeTicks method in Stopwatch.
if (Stopwatch.IsHighResolution)
{
var tickFrequency = 10000000.0d / (double) Stopwatch.Frequency;
elapsedTicks = (long) ((double) elapsedTicks * tickFrequency);
}
// Now we can get a more human-readable elapsed time. Unfortunately we can't return the TimeSpan
// as a parameter, as it doesn't implement IConvertible. So we'll just convert it to a string.
// If you really want to do more with the resulting TimeSpan, you could always refactor this to return the ticks,
// and create the TimeSpan using property functions as in the DateTime example (the Elapsed property).
ElapsedTimeSpan = TimeSpan.FromTicks(elapsedTicks).ToString();
]]>
</Code>
</Task>
</UsingTask>
</Project>
@dsmiley53

This comment has been minimized.

Copy link

commented Dec 19, 2016

Hi David,
These are great. Is there a license for this code? Is anyone free to use it or are there any restrictions I should know about?

@DavidMoore

This comment has been minimized.

Copy link
Owner Author

commented Jan 2, 2018

Hi David. Sorry for the over a year reply, but there are no restrictions on this code, if you happened to use it! :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.