With standard scp (version: 1:7.6p1-4ubuntu0.3) on Ubuntu 18.04, transferring a 110GB file to another host takes roughly 6-8 minutes.
When using Go's pkg/sftp, it takes double the amount of time:
Example:
package main
import (
"fmt"
"github.com/melbahja/goph"
"golang.org/x/crypto/ssh"
"io"
"log"
"os"
"time"
)
func main() {
auth, err := goph.Key("your/key", "")
if err != nil {
log.Fatalln(err)
}
client, err := goph.NewConn(&goph.Config{
User: "someUser",
Addr: "someHostname",
Port: 22,
Auth: auth,
//Timeout: time.Duration(timeout) * time.Second,
Callback: ssh.InsecureIgnoreHostKey(),
})
if err != nil {
log.Fatalln(err)
}
file := "/some/file"
start := time.Now()
local, err := os.Open(file)
if err != nil {
return
}
defer local.Close()
ftp, err := client.NewSftp()
// VARIATION 1 => ftp, err := client.NewSftp(sftp.MaxPacketUnchecked(1 << 16))
if err != nil {
return
}
defer ftp.Close()
remote, err := ftp.Create(file)
if err != nil {
return
}
defer remote.Close()
/*
VARIATION 1 => buffer := make([]byte, 1 << 16)
VARIATION 1 => _, err = io.CopyBuffer(remote, local, buffer)
*/
_, err = io.Copy(remote, local)
if err != nil {
log.Fatalln(err)
}
duration := time.Since(start)
fmt.Println(duration)
}
Note: I even attempted to increase the size of the read buffer (and tcp max packet size) with the commented out lines (see VARIATION 1), and it made no difference.
Any ideas as to why, and how to speed up the Go equivalent?