bin/TestCode/Test-MultiThreading.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50

#$ModulePath = (Get-ChildItem -Path $PSScriptRoot\..\..\dist -Include *.psm1 -Recurse).FullName
#Remove-Module PsRunspace -Force -ErrorAction SilentlyContinue *> $null
#Import-Module $ModulePath #*> $null
$SrcPath = (Get-ChildItem -Path $PSScriptRoot\..\..\src -Include *.ps1 -Recurse).FullName
Remove-Module PsRunspace -Force -ErrorAction SilentlyContinue *> $null
$SrcPath |
ForEach-Object {
    . $_
}


#[string]$Folder = 'C:\Test'
#$Samples = Get-ChildItem -Path $Folder -Recurse | Select -ExpandProperty FullName
$Samples = 1..10
$Count = ($Samples | Measure-Object).Count

#$Command = "Write-Output"
#$Command = [scriptblock]::create("Write-Output `$args[0]")
$Command = "$PSScriptRoot\Test-Command.ps1"

Remove-Module TestModule -ErrorAction SilentlyContinue *> $null
Import-Module $PSScriptRoot\TestModule\TestModule.psm1 *> $null
#$Command = "Test-Function"

#$Start = Get-Date
#$Samples | ForEach-Object {
# & $Command $_
#}
#$End = Get-Date
#$Elapsed3 = New-TimeSpan -Start $Start -End $End

$Start = Get-Date
$Samples | Split-Thread -Command $Command
$End = Get-Date
$Elapsed = New-TimeSpan -Start $Start -End $End

#$Start = Get-Date
#$Samples | Split-Thread -Command $Command -OutputStream All
#$End = Get-Date
#$Elapsed2 = New-TimeSpan -Start $Start -End $End


" "
"Multithreading benefit are even more significant with long-running operations"
"These $Count operations each generated 6 output streams then slept for 1 second to simulate a long-running operation"
#"$($Elapsed3.TotalSeconds) seconds for ForEach-Object for $Count input objects"
"$($Elapsed.TotalSeconds) seconds for Split-Thread for $Count input objects"
#"$($Elapsed2.TotalSeconds) seconds for Split-Thread -OutputStream All for $Count input objects"