Skip to content

Commit

Permalink
better format
Browse files Browse the repository at this point in the history
  • Loading branch information
or-shachar committed Nov 19, 2023
1 parent d21ccd3 commit e140a3c
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 30 deletions.
22 changes: 11 additions & 11 deletions cacheproc/cacheproc.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,17 +51,17 @@ type Process struct {
// shutting down.
Close func() error

Gets atomic.Int64
GetHits atomic.Int64
GetMisses atomic.Int64
GetErrors atomic.Int64
Puts atomic.Int64
PutErrors atomic.Int64
RemoteCacheEnabled bool
KBDownloaded func() int64
KBUploaded func() int64
AvgKBDownloadSpeed func() float64
AvgKBUploadSpeed func() float64
Gets atomic.Int64
GetHits atomic.Int64
GetMisses atomic.Int64
GetErrors atomic.Int64
Puts atomic.Int64
PutErrors atomic.Int64
RemoteCacheEnabled bool
BytesDownloaded func() int64
BytesUploaded func() int64
AvgBytesDownloadSpeed func() float64
AvgBytesUploadSpeed func() float64
}

func (p *Process) Run() error {
Expand Down
16 changes: 8 additions & 8 deletions cachers/s3.go
Original file line number Diff line number Diff line change
Expand Up @@ -244,20 +244,20 @@ func (c *S3Cache) uploadOutput(ctx context.Context, outputID string, client *s3.
}
}

func (c *S3Cache) KBDownloaded() int64 {
return c.bytesDownloaded.Load() / 1024
func (c *S3Cache) BytesDownloaded() int64 {
return c.bytesDownloaded.Load()
}

func (c *S3Cache) KBUploaded() int64 {
return c.bytesUploaded.Load() / 1024
func (c *S3Cache) BytesUploaded() int64 {
return c.bytesUploaded.Load()
}

func (c *S3Cache) AvgKBDownloadSpeed() float64 {
return c.avgBytesDownloadSpeed / 1024
func (c *S3Cache) AvgBytesDownloadSpeed() float64 {
return c.avgBytesDownloadSpeed
}

func (c *S3Cache) AvgKBUploadSpeed() float64 {
return c.avgBytesUploadSpeed / 1024
func (c *S3Cache) AvgBytesUploadSpeed() float64 {
return c.avgBytesUploadSpeed
}

func newAverage(oldAverage float64, count int64, newValue float64) float64 {
Expand Down
56 changes: 45 additions & 11 deletions cmd/go-cacher/cacher.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ package main
import (
"context"
"flag"
"fmt"
"log"
"os"
"path/filepath"
Expand Down Expand Up @@ -110,17 +111,22 @@ func main() {
log.Printf("cacher: closing; %d gets (%d hits, %d misses, %d errors); %d puts (%d errors)",
p.Gets.Load(), p.GetHits.Load(), p.GetMisses.Load(), p.GetErrors.Load(), p.Puts.Load(), p.PutErrors.Load())
if p.RemoteCacheEnabled {
log.Printf("%d KB downloaded (%.2f/s); %d KB uploaded (%.2f/s)", p.KBDownloaded(), p.AvgKBDownloadSpeed(), p.KBUploaded(), p.AvgKBUploadSpeed())
log.Printf("%s downloaded (%s/s); %s uploaded (%s/s)",
formatBytes(float64(p.BytesDownloaded())),
formatBytes(p.AvgBytesDownloadSpeed()),
formatBytes(float64(p.BytesUploaded())),
formatBytes(p.AvgBytesUploadSpeed()),
)
}
}
return nil
},
Get: dc.Get,
Put: dc.Put,
KBDownloaded: zeroIntFunc,
KBUploaded: zeroIntFunc,
AvgKBDownloadSpeed: zeroFloatFunc,
AvgKBUploadSpeed: zeroFloatFunc,
Get: dc.Get,
Put: dc.Put,
BytesDownloaded: zeroIntFunc,
BytesUploaded: zeroIntFunc,
AvgBytesDownloadSpeed: zeroFloatFunc,
AvgBytesUploadSpeed: zeroFloatFunc,
}

if *serverBase != "" {
Expand All @@ -140,14 +146,42 @@ func main() {
if s3Cache != nil {
p.Get = s3Cache.Get
p.Put = s3Cache.Put
p.KBDownloaded = s3Cache.KBDownloaded
p.KBUploaded = s3Cache.KBUploaded
p.AvgKBDownloadSpeed = s3Cache.AvgKBDownloadSpeed
p.AvgKBUploadSpeed = s3Cache.AvgKBUploadSpeed
p.BytesDownloaded = s3Cache.BytesDownloaded
p.BytesUploaded = s3Cache.BytesUploaded
p.AvgBytesDownloadSpeed = s3Cache.AvgBytesDownloadSpeed
p.AvgBytesUploadSpeed = s3Cache.AvgBytesUploadSpeed
p.RemoteCacheEnabled = true
}

if err := p.Run(); err != nil {
log.Fatal(err)
}
}

func formatBytes(size float64) string {
const (
kb = 1 << (10 * iota)
mb
gb
tb
pb
eb
)

switch {
case size < kb:
return fmt.Sprintf("%d B", size)
case size < mb:
return fmt.Sprintf("%.2f KB", size/float64(kb))
case size < gb:
return fmt.Sprintf("%.2f MB", size/float64(mb))
case size < tb:
return fmt.Sprintf("%.2f GB", size/float64(gb))
case size < pb:
return fmt.Sprintf("%.2f TB", size/float64(tb))
case size < eb:
return fmt.Sprintf("%.2f PB", size/float64(pb))
default:
return fmt.Sprintf("%.2f EB", size/float64(eb))
}
}

0 comments on commit e140a3c

Please sign in to comment.