Загрузка данных


[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
$stats = @{}

# Список сайтов с прямой ссылкой на файл для замера скорости (HTTP/HTTPS)
$targets = @(
    @{ Name = 'yandex.ru';     Url = 'https://yandex.ru' },
    @{ Name = 'vk.com';        Url = 'https://vk.com' },
    @{ Name = 'mail.ru';      Url = 'https://mail.ru' },
    @{ Name = 'telegram.org'; Url = 'https://telegram.org' },
    @{ Name = 'rutube.ru';    Url = 'https://rutube.ru' },
    @{ Name = 'dzen.ru';      Url = 'https://dzen.ru' },
    @{ Name = 'ozon.ru';      Url = 'https://ozon.ru' },
    @{ Name = 'wildberries'; Url = 'https://www.wildberries.ru' },
    @{ Name = 'sberbank.ru';  Url = 'https://www.sberbank.ru' },
    @{ Name = 'tbank.ru';     Url = 'https://www.tbank.ru' },
    @{ Name = 'gosuslugi.ru'; Url = 'https://www.gosuslugi.ru' },
    @{ Name = 'avito.ru';     Url = 'https://www.avito.ru' },
    @{ Name = '2gis.ru';      Url = 'https://2gis.ru' },
    @{ Name = 'kinopoisk.ru'; Url = 'https://www.kinopoisk.ru' },
    @{ Name = 'habr.com';     Url = 'https://habr.com' },
    @{ Name = 'github.com';   Url = 'https://github.com' },
    @{ Name = 'selectel.ru';  Url = 'https://selectel.ru' },
    @{ Name = 'mts.ru';       Url = 'https://mts.ru' },
    @{ Name = 'megafon.ru';   Url = 'https://megafon.ru' },
    @{ Name = 'rostelecom';  Url = 'https://rostelecom.ru' }
)

foreach ($t in $targets) {
    $stats[$t.Name] = [PSCustomObject]@{ Sent = 0; Fail = 0; Times = [System.Collections.Generic.List[int]]::new(); Speeds = [System.Collections.Generic.List[double]]::new() }
}

while ($true) {
    $now = Get-Date -Format 'HH:mm:ss'
    Write-Host "`n=== ТЕКУЩИЙ ОПРОС HTTP/HTTPS [$now] ===" -ForegroundColor Cyan

    foreach ($target in $targets) {
        $item = $stats[$target.Name]
        $item.Sent++
        
        $sw = [System.Diagnostics.Stopwatch]::StartNew()
        try {
            $req = [System.Net.HttpWebRequest]::Create($target.Url)
            $req.Timeout = 4000
            $req.UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64)"
            $req.Method = "GET"
            
            $resp = $req.GetResponse()
            $sw.Stop()
            
            $lat = [int]$sw.ElapsedMilliseconds
            $item.Times.Add($lat)
            
            # Замер объема и вычисление скорости скачивания главной страницы
            $stream = $resp.GetResponseStream()
            $ms = [System.IO.MemoryStream]::new()
            $stream.CopyTo($ms)
            $bytes = $ms.Length
            
            $speedMbps = if ($lat -gt 0) { [math]::Round(($bytes * 8 / 1000000) / ($lat / 1000), 2) } else { 0 }
            if ($speedMbps -gt 0) { $item.Speeds.Add($speedMbps) }

            $resp.Close()
            Write-Host "[$now] $($target.Name,-15) : OK | Latency = ${lat}ms | Speed = ${speedMbps} Mbps" -ForegroundColor Green
        }
        catch {
            $sw.Stop()
            $item.Fail++
            Write-Host "[$now] $($target.Name,-15) : FAIL / BLOCKED ($($_.Exception.Message))" -ForegroundColor Red
        }
    }

    Write-Host "`n================ СВОДНАЯ СТАТИСТИКА ЗА ВСЕ ВРЕМЯ ================" -ForegroundColor Yellow
    foreach ($t in ($targets | Sort-Object { $_.Name })) {
        $name = $t.Name
        $item = $stats[$name]
        $total = $item.Sent
        $fail = $item.Fail
        $lossPerc = if ($total -gt 0) { [math]::Round(($fail / $total) * 100, 1) } else { 0 }
        
        $avgLat = if ($item.Times.Count -gt 0) { [math]::Round(($item.Times | Measure-Object -Average).Average) } else { 0 }
        $maxLat = if ($item.Times.Count -gt 0) { ($item.Times | Measure-Object -Maximum).Maximum } else { 0 }
        $avgSpd = if ($item.Speeds.Count -gt 0) { [math]::Round(($item.Speeds | Measure-Object -Average).Average, 2) } else { 0 }

        $color = if ($lossPerc -gt 5) { "Red" } elseif ($lossPerc -gt 0) { "Yellow" } else { "Green" }
        $line = "{0,-16} | Ошибки: {1,5}% ({2}/{3}) | Latency avg/max: {4}/{5} ms | Avg Speed: {6} Mbps" -f $name, $lossPerc, $fail, $total, $avgLat, $maxLat, $avgSpd
        Write-Host $line -ForegroundColor $color
    }

    Start-Sleep -Seconds 3
}