-
Notifications
You must be signed in to change notification settings - Fork 29
/
progstate.go
159 lines (140 loc) · 4.48 KB
/
progstate.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package main
import (
"fmt"
"math"
"net/url"
"os"
"time"
"github.com/Azure/blobporter/internal"
"github.com/Azure/blobporter/pipeline"
"github.com/Azure/blobporter/transfer"
"github.com/Azure/blobporter/util"
)
type progressState struct {
quietMode bool
numOfReaders int
numOfWorkers int
bufferLevel int
committedCount int
processedData float64
global globalState
}
type globalState struct {
startTime time.Time
cumWriteDuration time.Duration
cumDataSize float64
cumCommittedCount int
cumNumOfBlocks int64
}
var progress = &progressState{}
func newProgressState(quietMode bool, numOfReaders int, numOfWorkers int) *progressState {
return &progressState{quietMode: quietMode,
numOfReaders: numOfReaders,
numOfWorkers: numOfWorkers,
global: globalState{
startTime: time.Now(),
},
}
}
func (p *progressState) reset() {
p.bufferLevel = 0
p.committedCount = 0
p.processedData = 0
}
func (p *progressState) resetTransferState() {
p.bufferLevel = 0
p.committedCount = 0
p.processedData = 0
}
func (p *progressState) newTransfer(totalSize float64, sourcesInfo []pipeline.SourceInfo, transferType transfer.Definition) {
//init event sink
//only reset the transfer counters and keep the globalones
p.resetTransferState()
p.addEndOfTransferDelegate()
p.displayFilesToTransfer(sourcesInfo, transferType)
if p.quietMode {
return
}
p.addProgressBarDelegate(totalSize)
}
func (p *progressState) addEndOfTransferDelegate() {
endTransDelegate := func(e internal.EventItem, a internal.EventItemAggregate) {
switch e.Name {
case transfer.DataWrittenEvent:
p.global.cumDataSize += a.Value
case transfer.WrittenPartEvent:
p.global.cumNumOfBlocks += int64(a.Value)
case transfer.WriteDurationEvent:
p.global.cumWriteDuration += time.Duration(int64(a.Value))
case transfer.CommitEvent:
p.global.cumCommittedCount += int(a.Value)
default:
return
}
}
internal.EventSink.AddSubscription(internal.CommitListHandler, internal.OnDone, endTransDelegate)
internal.EventSink.AddSubscription(internal.Worker, internal.OnDone, endTransDelegate)
}
func (p *progressState) displayFilesToTransfer(sourcesInfo []pipeline.SourceInfo, transferType transfer.Definition) {
fmt.Printf("\nFiles to Transfer (%v) :\n", transferType)
var totalSize uint64
summary := ""
for _, source := range sourcesInfo {
//if the source is URL, remove the QS
display := source.SourceName
if u, err := url.Parse(source.SourceName); err == nil {
display = fmt.Sprintf("%v%v", u.Hostname(), u.Path)
}
summary = summary + fmt.Sprintf("Source: %v Size:%v \n", display, source.Size)
totalSize = totalSize + source.Size
}
if len(sourcesInfo) < 10 {
fmt.Printf(summary)
return
}
fmt.Printf("%v files. Total size:%v\n", len(sourcesInfo), totalSize)
return
}
func (p *progressState) displayGlobalSummary() {
var netMB float64 = 1000000
duration := time.Now().Sub(p.global.startTime)
fmt.Printf("\nThe data transfer took %v to run.\n", duration)
MBs := float64(p.global.cumDataSize) / netMB / duration.Seconds()
fmt.Printf("Throughput: %1.2f MB/s (%1.2f Mb/s) \n", MBs, MBs*8)
fmt.Printf("Configuration: R=%d, W=%d, DataSize=%s, Blocks=%d\n",
p.numOfReaders, p.numOfWorkers, util.PrintSize(uint64(p.global.cumDataSize)), p.global.cumNumOfBlocks)
fmt.Printf("Cumulative Writes Duration: Total=%v, Avg Per Worker=%v\n",
p.global.cumWriteDuration, time.Duration(p.global.cumWriteDuration.Nanoseconds()/int64(p.numOfWorkers)))
}
func (p *progressState) addProgressBarDelegate(totalSize float64) {
progressDelegate := func(e internal.EventItem, a internal.EventItemAggregate) {
var pp int
switch e.Name {
case transfer.BufferEvent:
p.bufferLevel = e.Data[0].Value.(int)
case transfer.DataWrittenEvent:
p.processedData = a.Value
case transfer.CommitEvent:
p.committedCount = int(a.Value)
default:
return
}
pp = int(math.Ceil((p.processedData / totalSize) * 100))
var ind string
var pchar string
for i := 0; i < 25; i++ {
if i+1 > pp/4 {
pchar = "."
} else {
pchar = "|"
}
ind = ind + pchar
}
if !util.Verbose {
fmt.Fprintf(os.Stdout, "\r --> %3d %% [%v] Committed Count: %v Buffer Level: %03d%%", pp, ind, p.committedCount, p.bufferLevel)
}
}
//Buffer and WrittenPart event are signaled by the worker. Commit event by the comitter
internal.EventSink.AddSubscription(internal.CommitListHandler, internal.RealTime, progressDelegate)
internal.EventSink.AddSubscription(internal.Worker, internal.RealTime, progressDelegate)
}