Skip to content

Instantly share code, notes, and snippets.

@yorgabr
Last active December 12, 2017 17:48
Show Gist options
  • Save yorgabr/dc1292d76d2fb765bba1972bf696139c to your computer and use it in GitHub Desktop.
Save yorgabr/dc1292d76d2fb765bba1972bf696139c to your computer and use it in GitHub Desktop.
Use this small script to come across the fastest Cygwin Setup mirror for your location.
function purge-any-pending-job {Get-Job | Remove-Job}
$measure_mirror_latency = {
Param([string] $argument)
$mirror_url = ($argument -split ";")[0]
$sum_url = "$($mirror_url)x86_64/sha512.sum"
$req = [System.Net.WebRequest]::Create($sum_url)
$req.Timeout = 2000
try {
$dur = (Measure-Command {
$res = $req.getResponse()
}).TotalSeconds
}
catch [Exception] {
$dur = "unreachable"
}
Write-Output "$dur`t$mirror_url"
}
# Become proxy aware.
(New-Object System.Net.WebClient).Proxy.Credentials = [System.Net.CredentialCache]::DefaultNetworkCredentials
[Environment]::SetEnvironmentVariable("NO_PROXY", "127.0.0.1", "User")
# Parallel measure each mirror latency.
purge-any-pending-job
$max_threads = (Get-WmiObject -Class win32_processor).NumberOfCores
$mirrors = ((wget https://cygwin.com/mirrors.lst).Content -split "`n")
$total = $mirrors.Count
$jobs_count = 0
foreach($mirror in $mirrors){
if($mirror.Length -eq 0){continue}
Start-Job -Scriptblock $measure_mirror_latency -ArgumentList $mirror | Out-Null
$jobs_count++
Write-Progress -Activity "Verifying all mirrors..." -PercentComplete (100 * $jobs_count / $total)
While ($(Get-Job -state running).count -ge $max_threads){
Start-Sleep -Milliseconds 30
}
}
# Wait for all measuring jobs to finish.
While ($(Get-Job -State Running).count -gt 0){
Start-Sleep -Milliseconds 100
}
# Get each measure results.
$results = @()
$jobs = Get-Job
foreach($job in $jobs){
$result = Receive-Job -Id ($job.Id)
$results += $result
}
purge-any-pending-job
# Return the fastest mirror.
(($results | Sort-Object)[0] -split "`t")[1]
@yorgabr
Copy link
Author

yorgabr commented Nov 21, 2017

Remember to use one PowerShell terminal to run it.

If you want to run it in a regular command prompt, wrap its code inside a .bat file this way:

@ECHO OFF
powershell -Command ^
"^
Put here all the fastest_cygwin_mirror.ps1 code lines, each one terminated with ;^
"

@opm22
Copy link

opm22 commented Nov 23, 2017

já tentou usar o mobaxterm?

@yorgabr
Copy link
Author

yorgabr commented Dec 12, 2017

Hum... Dá para usar em ambientes "hostis" de rede? ;)

@yorgabr
Copy link
Author

yorgabr commented Dec 12, 2017

Acabei de olhar o MobaXterm... UAU! Muito obrigado pela dica!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment