-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathreport.go
88 lines (78 loc) · 1.86 KB
/
report.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
82
83
84
85
86
87
88
package bench
import (
"encoding/json"
"io"
"log"
"os"
"path/filepath"
"strings"
"time"
"github.com/aristanetworks/goarista/monotime"
)
type Progress struct {
Processed uint64 `json:"processed"` // total bytes read or written so far
Delta uint64 `json:"delta"` // bytes written since last event
Duration time.Duration `json:"duration"` // time in ns since last event
}
// BPS returns the 'write/read speed' in bytes/s.
func (ev Progress) BPS() float64 {
return (float64(ev.Delta) / float64(ev.Duration)) * float64(time.Second)
}
func mononow() time.Duration {
return time.Duration(monotime.Now())
}
// ReadProgress reads JSON progress events in a file.
func ReadProgress(file string) ([]Progress, error) {
fd, err := os.Open(file)
if err != nil {
return nil, err
}
var pp []Progress
dec := json.NewDecoder(fd)
for {
var p Progress
if err := dec.Decode(&p); err == io.EOF {
break
} else if err != nil {
return pp, err
}
pp = append(pp, p)
}
return pp, nil
}
type Report struct {
Name string
Events []Progress
}
// MustReadReports reads all given progress event files.
func MustReadReports(files []string) []Report {
var reports []Report
for _, file := range files {
p, err := ReadProgress(file)
if err != nil {
log.Fatalf("%s: %v", file, err)
}
reports = append(reports, Report{
Events: p,
Name: strings.TrimSuffix(filepath.Base(file), filepath.Ext(file)),
})
}
return reports
}
// copyBytes returns an exact copy of the provided bytes.
func copyBytes(b []byte) (copiedBytes []byte) {
if b == nil {
return nil
}
copiedBytes = make([]byte, len(b))
copy(copiedBytes, b)
return
}
// copySlices returns an exact copy of the provided slices.
func copySlices(s [][]byte) (copiedSlice [][]byte) {
copiedSlice = make([][]byte, len(s))
for i := 0; i < len(s); i++ {
copy(copiedSlice[i], s[i])
}
return
}