Created
November 14, 2019 19:10
-
-
Save vbatts/c4bf24d58c0582469cf8061178146791 to your computer and use it in GitHub Desktop.
quickest rsync server response
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"bufio" | |
"fmt" | |
"io" | |
"log" | |
"os" | |
"os/exec" | |
"sort" | |
"strings" | |
"sync" | |
"time" | |
) | |
func main() { | |
log.SetOutput(os.Stderr) | |
fh, err := os.Open(os.Args[1]) | |
if err != nil { | |
log.Fatal(err) | |
} | |
defer fh.Close() | |
results := []Result{} | |
buf := bufio.NewReader(fh) | |
var wg sync.WaitGroup | |
for { | |
line, err := buf.ReadString('\n') | |
if err == io.EOF { | |
break | |
} | |
if err != nil { | |
log.Fatal(err) | |
} | |
if strings.HasPrefix(line, "#") { | |
// ignore comments | |
continue | |
} | |
line = strings.TrimSpace(line) | |
if line == "" { | |
continue | |
} | |
if strings.HasSuffix(line, "/") { | |
// don't get a directory listing of the remote, just a path validation | |
line = line[0 : len(line)-1] | |
} | |
wg.Add(1) | |
go func() { | |
defer wg.Done() | |
cmd := exec.Command("rsync", "--no-motd", line) | |
start := time.Now() | |
out, err := cmd.CombinedOutput() | |
if err != nil { | |
log.Printf("%s failed with: %s", line, out) | |
return | |
} | |
elapsed := time.Since(start) | |
results = append(results, Result{T: elapsed, URL: line}) | |
}() | |
} | |
wg.Wait() | |
for _, r := range results { | |
log.Printf("time %s (%s)", r.T, r.URL) | |
} | |
sort.Sort(ByTime(results)) | |
fmt.Println(results[0].URL) | |
} | |
type Result struct { | |
URL string | |
T time.Duration | |
} | |
type ByTime []Result | |
func (r ByTime) Len() int { return len(r) } | |
func (r ByTime) Swap(i, j int) { r[i], r[j] = r[j], r[i] } | |
func (r ByTime) Less(i, j int) bool { return r[i].T < r[j].T } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# list of mirrors to checkfor fastest | |
# find more "rsync://" at https://mirrors.slackware.com/mirrorlist/ | |
rsync://mirrors.kernel.org/slackware/slackware64-current | |
rsync://slackware.osuosl.org/slackware/slackware64-current | |
rsync://mirror.cs.princeton.edu/slackware/slackware64-current | |
rsync://mirror.cs.princeton.edu/slackware/slackware64-current/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
vbatts@poly:~$ /usr/bin/go version | |
go version go1.12.2 gccgo (GCC) 9.2.0 linux/amd64 | |
vbatts@poly:~$ /usr/bin/go build slackware/quickest.go | |
vbatts@poly:~$ ls -lsh quickest | |
76K -rwxr-xr-x 1 vbatts users 76K Nov 14 14:10 quickest* | |
vbatts@poly:~$ ldd quickest | |
linux-vdso.so.1 (0x00007ffc64cc6000) | |
libgo.so.14 => /usr/lib64/libgo.so.14 (0x00007f7c02860000) | |
libm.so.6 => /lib64/libm.so.6 (0x00007f7c02713000) | |
libgcc_s.so.1 => /usr/lib64/libgcc_s.so.1 (0x00007f7c026f9000) | |
libc.so.6 => /lib64/libc.so.6 (0x00007f7c02514000) | |
/lib64/ld-linux-x86-64.so.2 (0x00007f7c03fe0000) | |
libpthread.so.0 => /lib64/libpthread.so.0 (0x00007f7c024f2000) | |
vbatts@poly:~$ ./quickest slackware/rsync-mirrors.list | |
2019/11/14 14:10:25 time 432.591008ms (rsync://mirror.cs.princeton.edu/slackware/slackware64-current) | |
2019/11/14 14:10:25 time 751.698997ms (rsync://slackware.osuosl.org/slackware/slackware64-current) | |
2019/11/14 14:10:25 time 885.012469ms (rsync://mirrors.kernel.org/slackware/slackware64-current) | |
2019/11/14 14:10:25 time 1.382961223s (rsync://mirror.cs.princeton.edu/slackware/slackware64-current) | |
rsync://mirror.cs.princeton.edu/slackware/slackware64-current | |
vbatts@poly:~$ ./quickest slackware/rsync-mirrors.list 2>/dev/null | |
rsync://mirror.cs.princeton.edu/slackware/slackware64-current |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment