question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

`Measure-Command` reports incorrect results

See original GitHub issue

Prerequisites

Steps to reproduce

I have a little .NET app (targeting .NET 6/7), and I want to measure how long it takes to launch it. The app automatically closes itself upon launch and reports the elapsed time. The time reported by Measure-Command cmdlet and inconsistent with the time reported by the app.

When I ran the cmdlet I get these results from the cmdlet:

PS throwaway\winforms-nativeaot> Measure-Command { .\bin\Release\net6.0-windows\winforms-nativeaot.exe }

Days              : 0
Hours             : 0
Minutes           : 0
Seconds           : 0
Milliseconds      : 88
Ticks             : 881686
TotalDays         : 1.02046990740741E-06
TotalHours        : 2.44912777777778E-05
TotalMinutes      : 0.00146947666666667
TotalSeconds      : 0.0881686
TotalMilliseconds : 88.1686

DebugViewer report the data from the app:

[28008] winforms_nativeaot: time taken: 146.5512 ms || 1465512 ticks // 146 ms 

When I ran a comparable command from bash, I get these results:

$ time ./bin/Release/net6.0-windows/winforms-nativeaot.exe

real    0m0.266s
user    0m0.000s
sys     0m0.000s

And I also tried the good ol’ cmd:

D:\Development\throwaway\winforms-nativeaot>cmd /v:on /c "echo !TIME! & .\bin\Release\net6.0-windows\winforms-nativeaot.exe & echo !TIME!"         
14:33:16.35 
14:33:16.59
App PowerShell 5.1 PowerShell 7.2 Bash Cmd
146 ms 34.337 88.1686 266 240

It’s clear that PowerShell reports grossly incorrect results as it can’t take less time than the actual app. Bash reports the correct time as there’s some overhead to launch the app before it gets to the Main entrypoint.

To verify, I have also run this, and results are surprising to say the least:

 Measure-Command { timeout 3 }

Days              : 0
Hours             : 0
Minutes           : 0
Seconds           : 2
Milliseconds      : 322
Ticks             : 23221427
TotalDays         : 2.68766516203704E-05
TotalHours        : 0.000645039638888889
TotalMinutes      : 0.0387023783333333
TotalSeconds      : 2.3221427
TotalMilliseconds : 2322.1427
.NET app code
using System.Diagnostics;

namespace winforms_nativeaot;

static class Program
{
    [STAThread]
    static void Main()
    {
        Stopwatch stopwatch = Stopwatch.StartNew();

        // To customize application configuration such as set high DPI settings or default font,
        // see https://aka.ms/applicationconfiguration.
        ApplicationConfiguration.Initialize();

        Application.ApplicationExit += (s, e) =>
        {
            stopwatch.Stop();
            Trace.WriteLine($"winforms_nativeaot: time taken: {stopwatch.Elapsed.TotalMilliseconds} ms || {stopwatch.ElapsedTicks} ticks // {stopwatch.ElapsedMilliseconds} ms");
        };

        Application.Run(new Form1());
    }    
}

public partial class Form1 : Form
{
    public Form1()
    {
        this.Activated += (_, __) => Application.Exit();
    }
}

Expected behavior

The reported time is correct, at least within the same order of magnitude.

Actual behavior

The reported time is grossly inaccurate.

Error details

No response

Environment data

Name                           Value
----                           -----
PSVersion                      5.1.22621.608
PSEdition                      Desktop
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
BuildVersion                   10.0.22621.608
CLRVersion                     4.0.30319.42000
WSManStackVersion              3.0
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1

----

Name                           Value
----                           -----
PSVersion                      7.2.7
PSEdition                      Core
GitCommitId                    7.2.7
OS                             Microsoft Windows 10.0.22621
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0

Visuals

No response

Issue Analytics

  • State:open
  • Created a year ago
  • Comments:5

github_iconTop GitHub Comments

4reactions
jhoneillcommented, Oct 25, 2022

Measure-Command {notepad.exe} Will start notepad, and return to the PowerShell prompt. The time reported is the time to return from launching notepad, - the command doesn’t wait for notepad to terminate
to do that you need to use Measure-Command {start -wait notepad.exe}

0reactions
jhoneillcommented, Oct 26, 2022

(h)[-1].Duration

Works alone (h is an alias for get history)

The time in cmd will be the time to return to the prompt - again try it with notepad - you’ll get a time before the app has closed. It’s likely there is some overhead starting the process and I suspect start process polls to see if the process ID is still running so it won’t give super accurate times.

$p = new-object System.Diagnostics.Process
# Start info has other properties which to get process exactly as desired.  
$p.StartInfo.FileName = "C:\Windows\System32\notepad.exe" 
$p.start()
$p.WaitForExit()
$p.ExitTime - $p.StartTime

Should be more accurate.

Read more comments on GitHub >

github_iconTop Results From Across the Web

powershell - Measure-Command reports inaccurate time?
I have a script that I was to measure the execution time for, however when I use Measure-Command I receive what appears to...
Read more >
Measure-Command (Microsoft.PowerShell.Utility)
The Measure-Command cmdlet runs a script block or cmdlet internally, times the execution of the operation, and returns the execution time.
Read more >
Solved: Measure Total Incorrect - Microsoft Power BI Community
Solved: Hi Guys, I have created a measure to calculate the value of items based on quantity * cost Cover Value =
Read more >
Why does top report the incorrect CPU usage?
Surely the reported usage should be ~6.25% (1 of 16 cores). Is it incorrect because it's a virtual machine? How could I calculate...
Read more >
linux - How to measure average execution time of a script?
No, your idea of averaging is correct. ... To focus on total time, you execute the script one hundred times and average the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found