Codebase list golang-github-vbauerster-mpb / aa8d7eb
TotalAverageSpeed and TotalAverageETA Vladimir Bauer 8 years ago
3 changed file(s) with 123 addition(s) and 91 deletion(s). Raw diff Collapse all Expand all
135135 io.WriteString(st, res)
136136 }
137137
138 // CountersNoUnit returns raw counters decorator
139 //
140 // `pairFormat` printf compatible verbs for current and total, like "%f" or "%d"
141 //
142 // `wcc` optional WC config
138 // CountersNoUnit is a wrapper around Counters with no unit param.
143139 func CountersNoUnit(pairFormat string, wcc ...WC) Decorator {
144 return counters(0, pairFormat, wcc...)
140 return Counters(0, pairFormat, wcc...)
145141 }
146142
147 // CountersKibiByte returns human friendly byte counters decorator, where counters unit is multiple by 1024.
148 //
149 // `pairFormat` printf compatible verbs for current and total, like "%f" or "%d"
150 //
151 // `wcc` optional WC config
152 //
153 // pairFormat example:
154 //
155 // "%.1f / %.1f" = "1.0MiB / 12.0MiB" or "% .1f / % .1f" = "1.0 MiB / 12.0 MiB"
143 // CountersKibiByte is a wrapper around Counters with predefined unit UnitKiB (bytes/1024).
156144 func CountersKibiByte(pairFormat string, wcc ...WC) Decorator {
157 return counters(UnitKiB, pairFormat, wcc...)
145 return Counters(UnitKiB, pairFormat, wcc...)
158146 }
159147
160 // CountersKiloByte returns human friendly byte counters decorator, where counters unit is multiple by 1000.
148 // CountersKiloByte is a wrapper around Counters with predefined unit UnitKB (bytes/1000).
149 func CountersKiloByte(pairFormat string, wcc ...WC) Decorator {
150 return Counters(UnitKB, pairFormat, wcc...)
151 }
152
153 // Counters decorator with dynamic unit measure adjustement
154 //
155 // `unit` one of [0|UnitKiB|UnitKB] zero for no unit
161156 //
162157 // `pairFormat` printf compatible verbs for current and total, like "%f" or "%d"
163158 //
166161 // pairFormat example:
167162 //
168163 // "%.1f / %.1f" = "1.0MB / 12.0MB" or "% .1f / % .1f" = "1.0 MB / 12.0 MB"
169 func CountersKiloByte(pairFormat string, wcc ...WC) Decorator {
170 return counters(UnitKB, pairFormat, wcc...)
171 }
172
173 func counters(unit int, pairFormat string, wcc ...WC) Decorator {
164 func Counters(unit int, pairFormat string, wcc ...WC) Decorator {
174165 var wc WC
175166 for _, widthConf := range wcc {
176167 wc = widthConf
88 "github.com/vbauerster/mpb/internal"
99 )
1010
11 // ETA returns exponential-weighted-moving-average ETA decorator.
11 // EwmaETA exponential-weighted-moving-average based ETA decorator.
1212 //
1313 // `style` one of [ET_STYLE_GO|ET_STYLE_HHMMSS|ET_STYLE_HHMM|ET_STYLE_MMSS]
1414 //
1515 // `age` is the previous N samples to average over.
16 // If zero value provided, it defaults to 30.
1716 //
18 // `sb` is a start block receive channel. User suppose to send time.Now()
19 // to this channel on each iteration of a start block, right before actual job.
20 // The channel will be auto closed on bar shutdown event, so there is no need
21 // to close from user side.
17 // `sb` is a start block receive channel. It's required by MovingAverage algorithm,
18 // therefore result of time.Now() must be sent to this channel on each iteration
19 // of a start block, right before the actual job. There is no need to close the channel,
20 // as it will be closed automatically on bar completion event.
2221 //
2322 // `wcc` optional WC config
24 func ETA(style int, age float64, sb chan time.Time, wcc ...WC) Decorator {
23 func EwmaETA(style int, age float64, sb chan time.Time, wcc ...WC) Decorator {
2524 return MovingAverageETA(style, ewma.NewMovingAverage(age), sb, wcc...)
2625 }
2726
28 // MovingAverageETA returns ETA decorator, which relies on MovingAverage implementation to calculate average.
29 // Default ETA decorator relies on ewma implementation. However you're free to provide your own implementation
30 // or use alternative one, which is provided by decor package:
27 // MovingAverageETA decorator relies on MovingAverage implementation to calculate its average.
3128 //
32 // decor.MovingAverageETA(decor.ET_STYLE_GO, decor.NewMedian(), sb)
29 // `style` one of [ET_STYLE_GO|ET_STYLE_HHMMSS|ET_STYLE_HHMM|ET_STYLE_MMSS]
30 //
31 // `average` MovingAverage implementation
32 //
33 // `sb` is a start block receive channel. It's required by MovingAverage algorithm,
34 // therefore result of time.Now() must be sent to this channel on each iteration
35 // of a start block, right before the actual job. There is no need to close the channel,
36 // as it will be closed automatically on bar completion event.
37 //
38 // `wcc` optional WC config
3339 func MovingAverageETA(style int, average MovingAverage, sb chan time.Time, wcc ...WC) Decorator {
3440 if sb == nil {
3541 panic("start block channel must not be nil")
119125 s.sbStreamer <- now
120126 }
121127 }
128
129 // TotalAverageETA decorator.
130 //
131 // `style` one of [ET_STYLE_GO|ET_STYLE_HHMMSS|ET_STYLE_HHMM|ET_STYLE_MMSS]
132 //
133 // `wcc` optional WC config
134 func TotalAverageETA(style int, wcc ...WC) Decorator {
135 var wc WC
136 for _, widthConf := range wcc {
137 wc = widthConf
138 }
139 wc.BuildFormat()
140 startTime := time.Now()
141 return DecoratorFunc(func(st *Statistics, widthAccumulator chan<- int, widthDistributor <-chan int) string {
142 var str string
143 timeElapsed := time.Since(startTime)
144
145 v := internal.Round(float64(timeElapsed) / float64(st.Current))
146 if math.IsInf(v, 0) || math.IsNaN(v) {
147 v = .0
148 }
149 remaining := time.Duration((st.Total - st.Current) * int64(v))
150 hours := int64((remaining / time.Hour) % 60)
151 minutes := int64((remaining / time.Minute) % 60)
152 seconds := int64((remaining / time.Second) % 60)
153
154 switch style {
155 case ET_STYLE_GO:
156 str = fmt.Sprint(time.Duration(remaining.Seconds()) * time.Second)
157 case ET_STYLE_HHMMSS:
158 str = fmt.Sprintf("%02d:%02d:%02d", hours, minutes, seconds)
159 case ET_STYLE_HHMM:
160 str = fmt.Sprintf("%02d:%02d", hours, minutes)
161 case ET_STYLE_MMSS:
162 str = fmt.Sprintf("%02d:%02d", minutes, seconds)
163 }
164 return wc.FormatMsg(str, widthAccumulator, widthDistributor)
165 })
166 }
117117 io.WriteString(st, res)
118118 }
119119
120 // SpeedNoUnit returns raw I/O operation speed decorator.
120 // EwmaSpeed exponential-weighted-moving-average based speed decorator,
121 // with dynamic unit measure adjustement.
122 //
123 // `unit` one of [0|UnitKiB|UnitKB] zero for no unit
121124 //
122125 // `unitFormat` printf compatible verb for value, like "%f" or "%d"
123126 //
124 // `age` is the previous N samples to average over.
125 // If zero value provided, it defaults to 30.
126 //
127 // `sb` is a start block receive channel. User suppose to send time.Now()
128 // to this channel on each iteration of a start block, right before actual job.
129 // The channel will be auto closed on bar shutdown event, so there is no need
130 // to close from user side.
127 // `average` MovingAverage implementation
128 //
129 // `sb` is a start block receive channel. It's required by MovingAverage algorithm,
130 // therefore result of time.Now() must be sent to this channel on each iteration
131 // of a start block, right before the actual job. There is no need to close the channel,
132 // as it will be closed automatically on bar completion event.
131133 //
132134 // `wcc` optional WC config
133 //
134 // unitFormat example:
135 //
136 // "%.1f" = "1.0" or "% .1f" = "1.0"
137 func SpeedNoUnit(unitFormat string, age float64, sb chan time.Time, wcc ...WC) Decorator {
138 return MovingAverageSpeed(0, unitFormat, ewma.NewMovingAverage(age), sb, wcc...)
139 }
140
141 // SpeedKibiByte returns human friendly I/O operation speed decorator,
135 func EwmaSpeed(unit int, unitFormat string, age float64, sb chan time.Time, wcc ...WC) Decorator {
136 return MovingAverageSpeed(unit, unitFormat, ewma.NewMovingAverage(age), sb, wcc...)
137 }
138
139 // MovingAverageSpeed decorator relies on MovingAverage implementation to calculate its average.
140 //
141 // `unit` one of [0|UnitKiB|UnitKB] zero for no unit
142142 //
143143 // `unitFormat` printf compatible verb for value, like "%f" or "%d"
144144 //
145 // `age` is the previous N samples to average over.
146 // If zero value provided, it defaults to 30.
147 //
148 // `sb` is a start block receive channel. User suppose to send time.Now()
149 // to this channel on each iteration of a start block, right before actual job.
150 // The channel will be auto closed on bar shutdown event, so there is no need
151 // to close from user side.
145 // `average` MovingAverage implementation
146 //
147 // `sb` is a start block receive channel. It's required by MovingAverage algorithm,
148 // therefore result of time.Now() must be sent to this channel on each iteration
149 // of a start block, right before the actual job. There is no need to close the channel,
150 // as it will be closed automatically on bar completion event.
152151 //
153152 // `wcc` optional WC config
154 //
155 // unitFormat example:
156 //
157 // "%.1f" = "1.0MiB/s" or "% .1f" = "1.0 MiB/s"
158 func SpeedKibiByte(unitFormat string, age float64, sb chan time.Time, wcc ...WC) Decorator {
159 return MovingAverageSpeed(UnitKiB, unitFormat, ewma.NewMovingAverage(age), sb, wcc...)
160 }
161
162 // SpeedKiloByte returns human friendly I/O operation speed decorator,
163 //
164 // `unitFormat` printf compatible verb for value, like "%f" or "%d"
165 //
166 // `age` is the previous N samples to average over.
167 // If zero value provided, it defaults to 30.
168 //
169 // `sb` is a start block receive channel. User suppose to send time.Now()
170 // to this channel on each iteration of a start block, right before actual job.
171 // The channel will be auto closed on bar shutdown event, so there is no need
172 // to close from user side.
173 //
174 // `wcc` optional WC config
175 //
176 // unitFormat example:
177 //
178 // "%.1f" = "1.0MB/s" or "% .1f" = "1.0 MB/s"
179 func SpeedKiloByte(unitFormat string, age float64, sb chan time.Time, wcc ...WC) Decorator {
180 return MovingAverageSpeed(UnitKB, unitFormat, ewma.NewMovingAverage(age), sb, wcc...)
181 }
182
183 // MovingAverageSpeed returns Speed decorator, which relies on MovingAverage implementation to calculate average.
184 // Default Speed decorator relies on ewma implementation. However you're free to provide your own implementation
185 // or use alternative one, which is provided by decor package:
186 //
187 // decor.MovingAverageSpeed(decor.UnitKiB, "% .2f", decor.NewMedian(), sb)
188153 func MovingAverageSpeed(unit int, unitFormat string, average MovingAverage, sb chan time.Time, wcc ...WC) Decorator {
189154 if sb == nil {
190155 panic("start block channel must not be nil")
263228 s.sbStreamer <- now
264229 }
265230 }
231
232 // TotalAverageSpeed decorator with dynamic unit measure adjustement.
233 //
234 // `unit` one of [0|UnitKiB|UnitKB] zero for no unit
235 //
236 // `unitFormat` printf compatible verb for value, like "%f" or "%d"
237 //
238 // `wcc` optional WC config
239 func TotalAverageSpeed(unit int, unitFormat string, wcc ...WC) Decorator {
240 var wc WC
241 for _, widthConf := range wcc {
242 wc = widthConf
243 }
244 wc.BuildFormat()
245 startTime := time.Now()
246 return DecoratorFunc(func(st *Statistics, widthAccumulator chan<- int, widthDistributor <-chan int) string {
247 var str string
248 timeElapsed := time.Since(startTime)
249 speed := float64(st.Current) / timeElapsed.Seconds()
250
251 switch unit {
252 case UnitKiB:
253 str = fmt.Sprintf(unitFormat, SpeedKiB(speed))
254 case UnitKB:
255 str = fmt.Sprintf(unitFormat, SpeedKB(speed))
256 default:
257 str = fmt.Sprintf(unitFormat, speed)
258 }
259 return wc.FormatMsg(str, widthAccumulator, widthDistributor)
260 })
261 }