Using MSBuild property functions and inline tasks: Example doing performance calculations
The Problem
User RandDavis on Reddit asked a question about capturing elapsed time of tasks in MSBuild:
I'm using MSBuild 4.0 (I also have MSBuild.Community.Tasks available). Note that I'm new to the syntax involved. All I'm trying to do is this: store the current time to a property, run a process, and determine the time that has elapsed. I've managed to write System.DateTime.Now to a property, but I don't know how to do a simple datediff or construct a TimeSpan, so that I can get at what I'm looking for. I'd be utterly shamed if I had to resort to string comparisons or writing a custom task.
Options
The good thing is that he's using MSBuild 4.0, which means he can use any combination of property functions and inline tasks to achieve all he wants from within the MSBuild code, without having to compile and version custom MSBuild task assemblies, which can become a pain in the ass to move forwards with.
When benchmarking from .NET, the best practice is to use the Stopwatch class in System.Diagnostics. Using DateTime functions is the natural but more inaccurate and naive way to benchmark. Eric Lippert of C# compiler fame did a good series on benchmarking mistakes, with Stopwatch mentioned in part 2.
In MSBuild, you’re limited to what classes you can use for property functions, so this means we have to resort to using inline tasks if we want to use Stopwatch. And because MSBuild is an imperative, XML-based language, we can’t really use Stopwatch in the normal way (get an instance, start and stop it, etc).
Because Rand (I’m guessing that’s his name) doesn’t seem to require much precision, using DateTime might be more than enough, and it can also make for some simpler code.
Solution 1: Using property functions and DateTime ticks
So here’s the first example, using property functions and DateTime:
{{<gist “3bcc180796c116f55a2b” “ElapsedTime.DateTime.proj”>}}
Interestingly, the Stopwatch uses DateTime Ticks if it can’t use high precision time. So this is likely as good as we’re going to get using DateTime. If we run the project using MSBuild, Notepad should pop up. We can leave it open for a bit, then close it down, and see what measurement we got:
Microsoft.Playwright.PlaywrightException: Error: The language "cmd" has no grammar.
at Object.highlight (http://127.0.0.1:41899/main.js:3978:11)
at eval (eval at evaluate (:226:30), :2:18)
at UtilityScript.evaluate (:233:19)
at UtilityScript. (:1:44)
at Microsoft.Playwright.Transport.Connection.InnerSendMessageToServerAsync[T](ChannelOwnerBase object, String method, Dictionary`2 dictionary, Boolean keepNulls) in /_/src/Playwright/Transport/Connection.cs:line 209
at Microsoft.Playwright.Transport.Connection.WrapApiCallAsync[T](Func`1 action, Boolean isInternal) in /_/src/Playwright/Transport/Connection.cs:line 535
at Microsoft.Playwright.Core.Frame.EvaluateAsync[T](String script, Object arg) in /_/src/Playwright/Core/Frame.cs:line 548
at SiteGen.Extensions.Markdown.Prism.PrismHost.Highlight(String source, String language)
at SiteGen.Extensions.Markdown.Prism.PrismCodeBlockRenderer.Write(HtmlRenderer renderer, PrismCodeBlock obj)
OK, seems fair enough! The code is quite simple too, and could be fine if we’re not needing much precision.
Solution 2: Using inline tasks and Stopwatch
OK, so how can we use inline tasks to leverage the power of the .NET framework?
{{<gist “3bcc180796c116f55a2b” “ElapsedTime.Stopwatch.proj”>}}
Phew! Ok this code would be smaller and simpler if you removed some of my comments, and Microsoft had made some of the key methods and properties in the Stopwatch class available.
So here's how that looks:
Microsoft.Playwright.PlaywrightException: Error: The language "cmd" has no grammar.
at Object.highlight (http://127.0.0.1:41899/main.js:3978:11)
at eval (eval at evaluate (:226:30), :2:18)
at UtilityScript.evaluate (:233:19)
at UtilityScript. (:1:44)
at Microsoft.Playwright.Transport.Connection.InnerSendMessageToServerAsync[T](ChannelOwnerBase object, String method, Dictionary`2 dictionary, Boolean keepNulls) in /_/src/Playwright/Transport/Connection.cs:line 209
at Microsoft.Playwright.Transport.Connection.WrapApiCallAsync[T](Func`1 action, Boolean isInternal) in /_/src/Playwright/Transport/Connection.cs:line 535
at Microsoft.Playwright.Core.Frame.EvaluateAsync[T](String script, Object arg) in /_/src/Playwright/Core/Frame.cs:line 548
at SiteGen.Extensions.Markdown.Prism.PrismHost.Highlight(String source, String language)
at SiteGen.Extensions.Markdown.Prism.PrismCodeBlockRenderer.Write(HtmlRenderer renderer, PrismCodeBlock obj)
Would it be possible some way to return a Stopwatch object that could be started and stopped, as you would do in C# code? Possibly! But I think that wouldn’t fit well with the way MSBuild works.