CPUStressTest.psm1

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
function Start-CPUStressTest {
    [CmdletBinding()]
    param (
        [ValidateRange(0, 100)]
        [Int]
        $loadPct,
        $CPUs,
        [ValidateSet("Lowest", "BelowNormal", "Normal", "AboveNormal", "Highest")]
        $Priority = "Normal")
    begin {
        $loadPm = $loadPct * 10
        if ($CPUs -gt $env:NUMBER_OF_PROCESSORS) { $CPUs = $env:NUMBER_OF_PROCESSORS }
    }
    process {
        $ScriptBlock = {
            $loadPm = $args[0]
            [System.Threading.Thread]::CurrentThread.Priority = $args[1]
            $StopWatch = [System.Diagnostics.Stopwatch]::New()
            while ($true) {
                $StopWatch.Start()
                while ($StopWatch.ElapsedMilliseconds -lt $loadPm) {
                    $i++
                }
                Start-Sleep -Milliseconds $(1000 - $loadPm)
                $StopWatch.Reset()
            }
        }

        $Script:StressJob = 1..$CPUs | foreach {Start-Job -ScriptBlock $ScriptBlock -ArgumentList $loadPm, $Priority}
    }
    end { }
}
function Stop-CPUStressTest {
    [CmdletBinding()]
    param (
        
    )
    
    begin {
    }
    
    process {
        $Script:StressJob.StopJob()
        $Script:StressJob | Remove-Job
    }
    
    end {
    }
}