Ответ на пост «Тест моего старого сервачка на прочность. Пикабу, управляй моим PowerShell (я серьезно!)»1
$duration = 120
$cores = [Environment]::ProcessorCount
$end = (Get-Date).AddSeconds($duration)
$testDir = Join-Path $env:TEMP "GrandpaStressTest"
$jobs = @()
New-Item -ItemType Directory -Force -Path $testDir | Out-Null
Write-Host "CPU THREADS : $cores"
Write-Host "DURATION : $duration seconds"
Write-Host "CPU : FULL LOAD"
Write-Host "RAM : UP TO 4 GB"
Write-Host "DISK : READ / WRITE"
Write-Host "========================================" -ForegroundColor Red
try {
for ($c = 0; $c -lt $cores; $c++) {
$jobs += Start-Job -ArgumentList $end -ScriptBlock {
param($end)
while ((Get-Date) -lt $end) {
$x = 0.0
for ($i = 1; $i -le 100000; $i++) {
$x += [Math]::Sqrt($i)
}
}
}
}
$availableMB = (Get-CimInstance Win32_OperatingSystem).FreePhysicalMemory / 1024
$targetMB = [int][Math]::Min(4096, $availableMB * 0.5)
$jobs += Start-Job -ArgumentList $end,$targetMB -ScriptBlock {
param($end,$targetMB)
$blocks = New-Object 'System.Collections.Generic.List[byte[]]'
try {
for ($n = 0; $n -lt $targetMB; $n += 16) {
if ((Get-Date) -ge $end) { break }
$block = New-Object byte[] (16MB)
for ($i = 0; $i -lt $block.Length; $i += 4096) {
$block[$i] = 255
}
$blocks.Add($block)
}
while ((Get-Date) -lt $end) {
foreach ($block in $blocks) {
$block[0] = [byte](($block[0] + 1) % 256)
}
Start-Sleep -Milliseconds 100
}
}
finally {
$blocks.Clear()
}
}
$jobs += Start-Job -ArgumentList $end,$testDir -ScriptBlock {
param($end,$dir)
$file = Join-Path $dir "stress.bin"
$buffer = New-Object byte[] (1MB)
$rng = New-Object Random
$rng.NextBytes($buffer)
try {
while ((Get-Date) -lt $end) {
$fs = [IO.File]::Open(
$file,
[IO.FileMode]::Create,
[IO.FileAccess]::ReadWrite,
[IO.FileShare]::None
)
try {
for ($i = 0; $i -lt 256; $i++) {
if ((Get-Date) -ge $end) { break }
$fs.Write($buffer, 0, $buffer.Length)
}
$fs.Flush($true)
$fs.Position = 0
while ($fs.Read($buffer, 0, $buffer.Length) -gt 0) {
if ((Get-Date) -ge $end) { break }
}
}
finally {
$fs.Dispose()
}
}
}
finally {
Remove-Item $file -Force -ErrorAction SilentlyContinue
}
}
Write-Host "STRESS TEST RUNNING..." -ForegroundColor Yellow
$jobs | Wait-Job | Out-Null
$failures = @($jobs | Where-Object { $_.State -ne 'Completed' })
if ($failures.Count -eq 0) {
Write-Host " Выжил!" -ForegroundColor Green
}
else {
Write-Host "Сдох...." -ForegroundColor Red
$jobs | Receive-Job
}
}
finally {
$jobs | Stop-Job -ErrorAction SilentlyContinue
$jobs | Remove-Job -Force -ErrorAction SilentlyContinue
Remove-Item $testDir -Recurse -Force -ErrorAction SilentlyContinue
}
Write-Host "TEST FINISHED." -ForegroundColor Cyan