nohwnd / Profiler

Script, ScriptBlock and module performance profiler for PowerShell 5, and PowerShell 7.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Will this work with runspaces?

mrboring opened this issue · comments

Hi

I've got some modules that use Foreach-Object -Parallel and Start-ThreadJob. I did a quick test and it does not look like Profiler analyses code in runspaces. Is this correct?

Thanks.

Did not try that. Would help me if you posted a simple repro. Are you seeing a ton of debug output when running your scripts even after running warmup? If so this would be a symptom of failing to hook the profiler in the runspace. It replaces TraceLine method during runtime and it does not fully work on first run in PS7.

I'll try to put something together to reproduce the issue.

The built in profiler that will ship in PowerShell 7 somewhere in the future will work. I am planning to upload a build from the respective powershell repo branch, so people can just download it when they want to play around with it, it tracks runspaces.

The hack I am currently using plugs into TraceLine which does not run in the other runspaces.

# mine
 1..10 | foreach -parallel { "a" }; "b"
DEBUG:    1+  >>>> 1..10 | foreach -parallel { "a" }; "b"
a
a
a
a
a
a
a
a
a
a
DEBUG:    1+ 1..10 | foreach -parallel { "a" };  >>>> "b"
b

C:\p\profiler [duration-and-own-duration +0 ~1 -0 !]> $trace.events | ft

Path                                            Line Column Text                                  Timestamp StartTime           Duration         CallDuration     Source Extent
----                                            ---- ------ ----                                  --------- ---------           --------         ------------     ------ ------
C:\p\profiler\Profiler\Trace-ScriptInternal.ps1  141     17 & $ScriptBlock                    8671888093886 10.00:53:08.8093886 00:00:00.0019418 00:00:00                Profiler.ScriptExtentEventData
                                                   1     23 {                                 8671888113304 10.00:53:08.8113304 00:00:00.0000036 00:00:00.0000036        Profiler.ScriptExtentEventData
                                                   1     25 1..10 | foreach -parallel { "a" } 8671888113340 10.00:53:08.8113340 00:00:00.0366068 00:00:00.0366068        Profiler.ScriptExtentEventData
                                                   1     58 }                                 8671888479408 10.00:53:08.8479408 00:00:00.0000141 00:00:00                Profiler.ScriptExtentEventData
C:\p\profiler\Profiler\Trace-ScriptInternal.ps1  144     17 Set-PSDebug -Trace 0              8671888479549 10.00:53:08.8479549 00:00:00.0000647 00:00:00                Profiler.ScriptExtentEventData


# built in
Measure-Script {  1..10 | foreach -parallel { "a" }; "b" }  | ft

StartTime        Duration         Source                            Extent                                             RunspaceId                           ParentScriptBlockId                  ScriptBlockId
---------        --------         ------                            ------                                             ----------                           -------------------                  -------------
10:39:00.4773540 00:00:00.0000032 {                                 System.Management.Automation.ScriptExtentEventData 3aa6522a-3882-4c2a-ae20-719579e5238d aed4f983-d91e-4786-8dee-98c0803b29ce 2ea1159c-a718-…
10:39:00.4864822 00:00:00.0076024 "a"                               System.Management.Automation.ScriptExtentEventData 37ed435b-0fea-4c8a-8740-4ea06324727e 00000000-0000-0000-0000-000000000000 b1245f4a-8d02-…
10:39:00.4907435 00:00:00.0068297 "a"                               System.Management.Automation.ScriptExtentEventData 80b24bb7-23a3-4ced-bdd3-fb5226f4bd01 00000000-0000-0000-0000-000000000000 6877b975-a821-…
10:39:00.4940846 00:00:00.0069582 "a"                               System.Management.Automation.ScriptExtentEventData 37ed435b-0fea-4c8a-8740-4ea06324727e 00000000-0000-0000-0000-000000000000 14e4d5a0-f8e1-…
10:39:00.4975732 00:00:00.0066036 "a"                               System.Management.Automation.ScriptExtentEventData 80b24bb7-23a3-4ced-bdd3-fb5226f4bd01 00000000-0000-0000-0000-000000000000 190ff5d0-c3cd-…
10:39:00.5010428 00:00:00.0066382 "a"                               System.Management.Automation.ScriptExtentEventData 37ed435b-0fea-4c8a-8740-4ea06324727e 00000000-0000-0000-0000-000000000000 77845a3c-865d-…
10:39:00.5041768 00:00:00.0068938 "a"                               System.Management.Automation.ScriptExtentEventData 80b24bb7-23a3-4ced-bdd3-fb5226f4bd01 00000000-0000-0000-0000-000000000000 e257a0c8-9e8c-…
10:39:00.5076810 00:00:00.0067441 "a"                               System.Management.Automation.ScriptExtentEventData 37ed435b-0fea-4c8a-8740-4ea06324727e 00000000-0000-0000-0000-000000000000 1016def1-cfe1-…
10:39:00.5110706 00:00:00.0072583 "a"                               System.Management.Automation.ScriptExtentEventData 80b24bb7-23a3-4ced-bdd3-fb5226f4bd01 00000000-0000-0000-0000-000000000000 0254ea5a-bf4c-…
10:39:00.4773572 00:00:00.0410599 1..10 | foreach -parallel { "a" } System.Management.Automation.ScriptExtentEventData 3aa6522a-3882-4c2a-ae20-719579e5238d aed4f983-d91e-4786-8dee-98c0803b29ce 2ea1159c-a718-…
10:39:00.5184171 00:00:00.0000023 "b"                               System.Management.Automation.ScriptExtentEventData 3aa6522a-3882-4c2a-ae20-719579e5238d aed4f983-d91e-4786-8dee-98c0803b29ce 2ea1159c-a718-…
10:39:00.5184194 00:00:00         }                                 System.Management.Automation.ScriptExtentEventData 3aa6522a-3882-4c2a-ae20-719579e5238d aed4f983-d91e-4786-8dee-98c0803b29ce 2ea1159c-a718-…
10:39:00.5144251 00:00:00         "a"                               System.Management.Automation.ScriptExtentEventData 37ed435b-0fea-4c8a-8740-4ea06324727e 00000000-0000-0000-0000-000000000000 2c0a382e-0ba7-…
10:39:00.5183289 00:00:00         "a"                               System.Management.Automation.ScriptExtentEventData 80b24bb7-23a3-4ced-bdd3-fb5226f4bd01 00000000-0000-0000-0000-000000000000 2073e70c-78cc-…

Thanks for the update.