Skip to content

Instantly share code, notes, and snippets.

@DavidMoore
Last active January 24, 2022 07:18
Show Gist options
  • Select an option

  • Save DavidMoore/3bcc180796c116f55a2b to your computer and use it in GitHub Desktop.

Select an option

Save DavidMoore/3bcc180796c116f55a2b to your computer and use it in GitHub Desktop.
<?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>
@DeirhX
Copy link
Copy Markdown

DeirhX commented Aug 2, 2019

Thanks, saved me some work!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment