Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

display throughputs #4

Merged
merged 2 commits into from
Jul 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
module github.com/replicate/pget

go 1.19

require github.com/dustin/go-humanize v1.0.1 // indirect
6 changes: 6 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
github.com/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkpeCY=
github.com/dustin/go-humanize v1.0.1/go.mod h1:Mu1zIs6XwVuF/gI1OepvI0qD18qycQx+mFykh5fBlto=
github.com/tmthrgd/go-shm v0.0.0-20230106080200-1ec4c2ba35cf h1:zllFA+KeQ5EGsnMv8YtqBZQTEERX9UZkY+h2nKYY2JY=
github.com/tmthrgd/go-shm v0.0.0-20230106080200-1ec4c2ba35cf/go.mod h1:sreX9Ec5xasLXkHjdpdrCXuYWgnB8a2T2ElLH+weMWY=
golang.org/x/sys v0.4.0 h1:Zr2JFtRQNX3BCZ8YtxRE9hNJYC8J6I1MVbMg6owUp18=
golang.org/x/sys v0.4.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
14 changes: 14 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,12 @@ import (
"runtime"
"sync"
"time"

"github.com/dustin/go-humanize"
)

var _fileSize int64

func getRemoteFileSize(url string) (int64, error) {
resp, err := http.DefaultClient.Head(url)
if err != nil {
Expand All @@ -26,6 +30,7 @@ func getRemoteFileSize(url string) (int64, error) {
if fileSize <= 0 {
return int64(-1), fmt.Errorf("unable to determine file size")
}
_fileSize = fileSize
return fileSize, nil
}

Expand All @@ -41,6 +46,7 @@ func downloadFileToBuffer(url string, concurrency int) (*bytes.Buffer, error) {

data := make([]byte, fileSize)
errc := make(chan error, concurrency)
startTime := time.Now()

for i := 0; i < concurrency; i++ {
start := int64(i) * chunkSize
Expand Down Expand Up @@ -109,12 +115,16 @@ func downloadFileToBuffer(url string, concurrency int) (*bytes.Buffer, error) {
return nil, err // return the first error we encounter
}
}
elapsed := time.Since(startTime).Seconds()
througput := humanize.Bytes(uint64(float64(fileSize) / elapsed))
fmt.Printf("Downloaded %s bytes in %.3fs (%s/s)\n", humanize.Bytes(uint64(fileSize)), elapsed, througput)

buffer := bytes.NewBuffer(data)
return buffer, nil
}

func extractTarFile(buffer *bytes.Buffer, destDir string) error {
startTime := time.Now()
tarReader := tar.NewReader(buffer)

for {
Expand Down Expand Up @@ -155,6 +165,10 @@ func extractTarFile(buffer *bytes.Buffer, destDir string) error {
return fmt.Errorf("unsupported file type for %s, typeflag %s", header.Name, string(header.Typeflag))
}
}
elapsed := time.Since(startTime).Seconds()
size := humanize.Bytes(uint64(_fileSize))
throughput := humanize.Bytes(uint64(float64(_fileSize) / elapsed))
fmt.Printf("Extracted %s in %.3fs (%s/s)\n", size, elapsed, throughput)

return nil
}
Expand Down