eZeeNow.com

it's your's

How to speed up Background JOBS or Services

Background jobs can be a great thing to speed up scripts because they can do things in parallel. However, background jobs only work well if the code run does not produce large amounts of data - because transporting back the data via XML serialization often takes more time than you can save by executing things in parallel.

So while doing the Background JOBS we need to take only the small amount of data.

Example from powershell script.

$start = Get-Date

$code1 = { Get-Hotfix }
$code2 = { Get-ChildItem $env:windir\system32\*.dll }
$code3 = { Get-Content -Path C:\Windows\WindowsUpdate.log }

$job1 = Start-Job -ScriptBlock $code1
$job2 = Start-Job -ScriptBlock $code2
$result3 = & $code3

$alljobs = Wait-Job $job1, $job2

Remove-Job -Job $alljobs
$result1, $result2 = Receive-Job $alljobs

$end = Get-Date
$timespan = $end - $start
$seconds = $timespan.TotalSeconds
Write-Host "This took me $seconds seconds."

This takes about half a minute. When you run all three jobs sequentially and do not use background jobs, they just take 5 seconds.

$start = Get-Date

$result1 = Get-Hotfix
$result2 = Get-ChildItem $env:windir\system32\*.dll
$result3 = Get-Content -Path C:\Windows\WindowsUpdate.log

$end = Get-Date
$timespan = $end - $start
$seconds = $timespan.TotalSeconds
Write-Host "This took me $seconds seconds."




Loading