-
Notifications
You must be signed in to change notification settings - Fork 1
/
util.go
81 lines (67 loc) · 1.85 KB
/
util.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
// util/util.go
// Copyright(c) 2017 Matt Pharr
// BSD licensed; see LICENSE for details.
package util
import (
"fmt"
"io"
"log"
"time"
)
///////////////////////////////////////////////////////////////////////////
// ReportingReader
// Small wrapper around io.Reader that implements io.ReadCloser.
// Periodically logs how many bytes have been read and the rate of
// processing them in bytes / second.
type ReportingReader struct {
R io.Reader
Msg string
start time.Time
reportCounter, readBytes int64
}
const reportFrequency = 128 * 1024 * 1024
func (r *ReportingReader) Read(buf []byte) (int, error) {
if r.start.IsZero() {
r.start = time.Now()
r.reportCounter = reportFrequency
r.readBytes = 0
}
n, err := r.R.Read(buf)
r.readBytes += int64(n)
r.reportCounter -= int64(n)
if r.reportCounter < 0 {
r.report("")
r.reportCounter += reportFrequency
}
return n, err
}
func (r *ReportingReader) report(prefix string) {
delta := time.Now().Sub(r.start)
bytesPerSec := int64(float64(r.readBytes) / delta.Seconds())
log.Printf("%s%s %s [%s/s]", prefix, r.Msg, FmtBytes(r.readBytes),
FmtBytes(bytesPerSec))
}
func (r *ReportingReader) Close() error {
r.report("Finished. ")
if rc, ok := r.R.(io.ReadCloser); ok {
return rc.Close()
}
return nil
}
///////////////////////////////////////////////////////////////////////////
// Utility Functions
func FmtBytes(n int64) string {
if n >= 1024*1024*1024*1024 {
return fmt.Sprintf("%.2f TiB", float64(n)/(1024.*1024.*
1024.*1024.))
} else if n >= 1024*1024*1024 {
return fmt.Sprintf("%.2f GiB", float64(n)/(1024.*1024.*
1024.))
} else if n > 1024*1024 {
return fmt.Sprintf("%.2f MiB", float64(n)/(1024.*1024.))
} else if n > 1024 {
return fmt.Sprintf("%.2f kiB", float64(n)/1024.)
} else {
return fmt.Sprintf("%d B", n)
}
}