4

I am having build script in msbuild which will be invoked by powershell.

Build log file would look like this.

Build started 12/19/2011 2:01:54 PM.

Build succeeded. 0 Warning(s) 0 Error(s)

Time Elapsed 00:00:00.28

At starting point it will specify the build execution initiated time and the end it will say Time elapsed.

I would like to find the execution time using powershell or msbuild. How can i add all the time elapsed value or find the difference between last build started occurrence with first build occurrence?

As it is in log file , i don't know how to retrieve this and measure.

Samselvaprabu
  • 16,830
  • 32
  • 144
  • 230
  • Perhaps you can use the Measure CmdLet in powershell: http://technet.microsoft.com/en-us/library/ee176899.aspx – AVee Dec 19 '11 at 09:00

2 Answers2

6

Notice that if you use Measure-Command { }, it will consume the console output of your build. So any output would be gone.

A less intrusive way of doing this is by simply doing a subtraction of Get-Date values. For example, here would be your build.ps1 script:

$before = Get-Date
<your build command line>
$after = Get-Date

$time = $after - $before
$buildTime = "`nBuild finished in ";
if ($time.Minutes -gt 0)
{
    $buildTime += "{0} minute(s) " -f $time.Minutes;
}

$buildTime += "{0} second(s)" -f $time.Seconds;
Emperor XLII
  • 13,014
  • 11
  • 65
  • 75
supermem613
  • 606
  • 4
  • 7
  • This is what I would do, though I would proabably just format the datetime string like "{0} minutes {1} seconds" -f, $time.minutes, $time.seconds to avoid any complicated if statements. – HungryHippos Apr 08 '12 at 22:49
5

Use Measure-Commmand to see how long is your build process. Here's an example:

Measure-Command { ./build.ps1 }

The output on my machine was:

PS D:\> Measure-Command { ./build.ps1 }


Days              : 0
Hours             : 0
Minutes           : 0
Seconds           : 6
Milliseconds      : 443
Ticks             : 64439301
TotalDays         : 7.45825243055556E-05
TotalHours        : 0.00178998058333333
TotalMinutes      : 0.107398835
TotalSeconds      : 6.4439301
TotalMilliseconds : 6443.9301
Abbas
  • 6,720
  • 4
  • 35
  • 49
  • Msbuild itself will provide measurement as time elapsed. My script has many msbuild commands and i want to know collective measure. Your answer meant for one msbuild command – Samselvaprabu Dec 19 '11 at 11:24
  • The execution time of the script itself can be measured. Show me how you invoke your script. – Abbas Dec 19 '11 at 11:44
  • You can run PowerShell from within PowerShell, just wrap the command within the braces. I am updating my answer to show you what I ran and it's output. – Abbas Dec 19 '11 at 14:37
  • What was I thinking...there's no need to run PowerShell from within PowerShell. You can just run your build script within the Measure-Command: Measure-Command { ./build.ps1 }. I have updated my answer as well. – Abbas Dec 20 '11 at 17:20