-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRunspaceTest.ps1
85 lines (68 loc) · 2.88 KB
/
RunspaceTest.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
$scriptPath = $PSScriptRoot
$folderLocation = Join-Path -Path $scriptPath -ChildPath "MTSequential"
if (Test-Path $folderLocation) {
if (Test-Path "$folderLocation\*") {
Get-ChildItem -Path $folderLocation | Remove-Item -Force
}
} else {
New-Item -Path $folderLocation -ItemType Directory -Force | Out-Null
}
$ScriptBlock = {
Param (
[string]$fileName,
[string]$url
)
$contents = Invoke-WebRequest $url -UseBasicParsing
Set-Content $fileName $myString # use a common variable
Add-Content $fileName $contents # add the text download from the www
}
Write-Warning "Run the processes sequentially"
Write-Output "First lets create the 500 text files by running the process sequentially"
$startTime = Get-Date
$myString = "This is not a session state"
1..500 | ForEach-Object {
$fileName = "test$_.txt"
$filePath = Join-Path -Path $folderLocation -ChildPath $fileName
Invoke-Command -ScriptBlock $ScriptBlock -ArgumentList $filePath, "http://www.textfiles.com/100/adventur.txt"
}
$endTime = Get-Date
$totalSeconds = "{0:N4}" -f ($endTime - $startTime).TotalSeconds
Write-Output "All files created in $totalSeconds seconds"
Write-Warning "Run the processes in parallel"
$folderLocation = Join-Path -Path $scriptPath -ChildPath "MTParallel"
Write-Output "Now lets try creating 500 files by running up $numThreads background threads"
if (Test-Path $folderLocation) {
if (Test-Path "$folderLocation\*") {
Get-ChildItem -Path $folderLocation | Remove-Item -Force
}
} else {
New-Item -Path $folderLocation -ItemType Directory -Force | Out-Null
}
# Create session state
$myString = "This is a session state!"
$sessionState = [System.Management.Automation.Runspaces.InitialSessionState]::CreateDefault()
$sessionstate.Variables.Add((New-Object -TypeName System.Management.Automation.Runspaces.SessionStateVariableEntry -ArgumentList "myString", $myString, "example string"))
# Create runspace pool consisting of $numThreads runspaces
$numThreads = 5000
$RunspacePool = [RunspaceFactory]::CreateRunspacePool(1, $numThreads, $sessionState, $Host)
$RunspacePool.Open()
$startTime = Get-Date
$Jobs = @()
1..500 | ForEach-Object {
$fileName = "test$_.txt"
$fileName = Join-Path -Path $folderLocation -ChildPath $fileName
$Job = [powershell]::Create().AddScript($ScriptBlock).AddParameter("fileName", $fileName).AddParameter("url", "http://www.textfiles.com/100/adventur.txt")
$Job.RunspacePool = $RunspacePool
$Jobs += New-Object PSObject -Property @{
RunNum = $_
Job = $Job
Result = $Job.BeginInvoke()}
}
Write-Warning "Waiting.."
Do {
Write-Warning "."
Start-Sleep -Seconds 10
} While ( $Jobs.Result.IsCompleted -contains $false) #Jobs.Result is a collection
$endTime = Get-Date
$totalSeconds = "{0:N4}" -f ($endTime - $startTime).TotalSeconds
Write-Output "All files created in $totalSeconds seconds"