| 118 | 118 |
io.WriteString(st, res)
|
| 119 | 119 |
}
|
| 120 | 120 |
|
| 121 | |
// EwmaSpeed exponential-weighted-moving-average based speed decorator,
|
| 122 | |
// with dynamic unit measure adjustment.
|
| 123 | |
//
|
| 124 | |
// `unit` one of [0|UnitKiB|UnitKB] zero for no unit
|
| 125 | |
//
|
| 126 | |
// `unitFormat` printf compatible verb for value, like "%f" or "%d"
|
| 127 | |
//
|
| 128 | |
// `age` ewma age
|
| 129 | |
//
|
| 130 | |
// `wcc` optional WC config
|
| 131 | |
//
|
| 132 | |
// unitFormat example if UnitKiB is chosen:
|
| 133 | |
//
|
| 134 | |
// "%.1f" = "1.0MiB/s" or "% .1f" = "1.0 MiB/s"
|
| 135 | |
func EwmaSpeed(unit int, unitFormat string, age float64, wcc ...WC) Decorator {
|
| 136 | |
return MovingAverageSpeed(unit, unitFormat, ewma.NewMovingAverage(age), wcc...)
|
|
121 |
// EwmaSpeed exponential-weighted-moving-average based speed decorator.
|
|
122 |
// Note that it's necessary to supply bar.Incr* methods with incremental
|
|
123 |
// work duration as second argument, in order for this decorator to
|
|
124 |
// work correctly. This decorator is a wrapper of MovingAverageSpeed.
|
|
125 |
func EwmaSpeed(unit int, fmt string, age float64, wcc ...WC) Decorator {
|
|
126 |
return MovingAverageSpeed(unit, fmt, ewma.NewMovingAverage(age), wcc...)
|
| 137 | 127 |
}
|
| 138 | 128 |
|
| 139 | 129 |
// MovingAverageSpeed decorator relies on MovingAverage implementation
|
|
| 141 | 131 |
//
|
| 142 | 132 |
// `unit` one of [0|UnitKiB|UnitKB] zero for no unit
|
| 143 | 133 |
//
|
| 144 | |
// `unitFormat` printf compatible verb for value, like "%f" or "%d"
|
|
134 |
// `fmt` printf compatible verb for value, like "%f" or "%d"
|
| 145 | 135 |
//
|
| 146 | 136 |
// `average` MovingAverage implementation
|
| 147 | 137 |
//
|
| 148 | 138 |
// `wcc` optional WC config
|
| 149 | |
func MovingAverageSpeed(unit int, unitFormat string, average MovingAverage, wcc ...WC) Decorator {
|
|
139 |
//
|
|
140 |
// fmt examples:
|
|
141 |
//
|
|
142 |
// unit=UnitKiB, fmt="%.1f" output: "1.0MiB/s"
|
|
143 |
// unit=UnitKiB, fmt="% .1f" output: "1.0 MiB/s"
|
|
144 |
// unit=UnitKB, fmt="%.1f" output: "1.0MB/s"
|
|
145 |
// unit=UnitKB, fmt="% .1f" output: "1.0 MB/s"
|
|
146 |
//
|
|
147 |
func MovingAverageSpeed(unit int, fmt string, average MovingAverage, wcc ...WC) Decorator {
|
| 150 | 148 |
var wc WC
|
| 151 | 149 |
for _, widthConf := range wcc {
|
| 152 | 150 |
wc = widthConf
|
| 153 | 151 |
}
|
| 154 | 152 |
wc.Init()
|
| 155 | 153 |
d := &movingAverageSpeed{
|
| 156 | |
WC: wc,
|
| 157 | |
unit: unit,
|
| 158 | |
unitFormat: unitFormat,
|
| 159 | |
average: average,
|
|
154 |
WC: wc,
|
|
155 |
unit: unit,
|
|
156 |
fmt: fmt,
|
|
157 |
average: average,
|
| 160 | 158 |
}
|
| 161 | 159 |
return d
|
| 162 | 160 |
}
|
|
| 164 | 162 |
type movingAverageSpeed struct {
|
| 165 | 163 |
WC
|
| 166 | 164 |
unit int
|
| 167 | |
unitFormat string
|
|
165 |
fmt string
|
| 168 | 166 |
average ewma.MovingAverage
|
| 169 | 167 |
msg string
|
| 170 | 168 |
completeMsg *string
|
|
| 181 | 179 |
speed := d.average.Value()
|
| 182 | 180 |
switch d.unit {
|
| 183 | 181 |
case UnitKiB:
|
| 184 | |
d.msg = fmt.Sprintf(d.unitFormat, SpeedKiB(speed))
|
|
182 |
d.msg = fmt.Sprintf(d.fmt, SpeedKiB(speed))
|
| 185 | 183 |
case UnitKB:
|
| 186 | |
d.msg = fmt.Sprintf(d.unitFormat, SpeedKB(speed))
|
| 187 | |
default:
|
| 188 | |
d.msg = fmt.Sprintf(d.unitFormat, speed)
|
|
184 |
d.msg = fmt.Sprintf(d.fmt, SpeedKB(speed))
|
|
185 |
default:
|
|
186 |
d.msg = fmt.Sprintf(d.fmt, speed)
|
| 189 | 187 |
}
|
| 190 | 188 |
|
| 191 | 189 |
return d.FormatMsg(d.msg)
|
|
| 207 | 205 |
d.completeMsg = &msg
|
| 208 | 206 |
}
|
| 209 | 207 |
|
| 210 | |
// AverageSpeed decorator with dynamic unit measure adjustment.
|
| 211 | |
//
|
| 212 | |
// `unit` one of [0|UnitKiB|UnitKB] zero for no unit
|
| 213 | |
//
|
| 214 | |
// `unitFormat` printf compatible verb for value, like "%f" or "%d"
|
| 215 | |
//
|
| 216 | |
// `wcc` optional WC config
|
| 217 | |
//
|
| 218 | |
// unitFormat example if UnitKiB is chosen:
|
| 219 | |
//
|
| 220 | |
// "%.1f" = "1.0MiB/s" or "% .1f" = "1.0 MiB/s"
|
| 221 | |
func AverageSpeed(unit int, unitFormat string, wcc ...WC) Decorator {
|
| 222 | |
return NewAverageSpeed(unit, unitFormat, time.Now(), wcc...)
|
|
208 |
// AverageSpeed decorator with dynamic unit measure adjustment. It's
|
|
209 |
// a wrapper of NewAverageSpeed.
|
|
210 |
func AverageSpeed(unit int, fmt string, wcc ...WC) Decorator {
|
|
211 |
return NewAverageSpeed(unit, fmt, time.Now(), wcc...)
|
| 223 | 212 |
}
|
| 224 | 213 |
|
| 225 | 214 |
// NewAverageSpeed decorator with dynamic unit measure adjustment and
|
|
| 227 | 216 |
//
|
| 228 | 217 |
// `unit` one of [0|UnitKiB|UnitKB] zero for no unit
|
| 229 | 218 |
//
|
| 230 | |
// `unitFormat` printf compatible verb for value, like "%f" or "%d"
|
|
219 |
// `fmt` printf compatible verb for value, like "%f" or "%d"
|
| 231 | 220 |
//
|
| 232 | 221 |
// `startTime` start time
|
| 233 | 222 |
//
|
| 234 | 223 |
// `wcc` optional WC config
|
| 235 | 224 |
//
|
| 236 | |
// unitFormat example if UnitKiB is chosen:
|
| 237 | |
//
|
| 238 | |
// "%.1f" = "1.0MiB/s" or "% .1f" = "1.0 MiB/s"
|
| 239 | |
func NewAverageSpeed(unit int, unitFormat string, startTime time.Time, wcc ...WC) Decorator {
|
|
225 |
// fmt examples:
|
|
226 |
//
|
|
227 |
// unit=UnitKiB, fmt="%.1f" output: "1.0MiB/s"
|
|
228 |
// unit=UnitKiB, fmt="% .1f" output: "1.0 MiB/s"
|
|
229 |
// unit=UnitKB, fmt="%.1f" output: "1.0MB/s"
|
|
230 |
// unit=UnitKB, fmt="% .1f" output: "1.0 MB/s"
|
|
231 |
//
|
|
232 |
func NewAverageSpeed(unit int, fmt string, startTime time.Time, wcc ...WC) Decorator {
|
| 240 | 233 |
var wc WC
|
| 241 | 234 |
for _, widthConf := range wcc {
|
| 242 | 235 |
wc = widthConf
|
| 243 | 236 |
}
|
| 244 | 237 |
wc.Init()
|
| 245 | 238 |
d := &averageSpeed{
|
| 246 | |
WC: wc,
|
| 247 | |
unit: unit,
|
| 248 | |
unitFormat: unitFormat,
|
| 249 | |
startTime: startTime,
|
|
239 |
WC: wc,
|
|
240 |
unit: unit,
|
|
241 |
fmt: fmt,
|
|
242 |
startTime: startTime,
|
| 250 | 243 |
}
|
| 251 | 244 |
return d
|
| 252 | 245 |
}
|
|
| 254 | 247 |
type averageSpeed struct {
|
| 255 | 248 |
WC
|
| 256 | 249 |
unit int
|
| 257 | |
unitFormat string
|
|
250 |
fmt string
|
| 258 | 251 |
startTime time.Time
|
| 259 | 252 |
msg string
|
| 260 | 253 |
completeMsg *string
|
|
| 273 | 266 |
|
| 274 | 267 |
switch d.unit {
|
| 275 | 268 |
case UnitKiB:
|
| 276 | |
d.msg = fmt.Sprintf(d.unitFormat, SpeedKiB(speed))
|
|
269 |
d.msg = fmt.Sprintf(d.fmt, SpeedKiB(speed))
|
| 277 | 270 |
case UnitKB:
|
| 278 | |
d.msg = fmt.Sprintf(d.unitFormat, SpeedKB(speed))
|
| 279 | |
default:
|
| 280 | |
d.msg = fmt.Sprintf(d.unitFormat, speed)
|
|
271 |
d.msg = fmt.Sprintf(d.fmt, SpeedKB(speed))
|
|
272 |
default:
|
|
273 |
d.msg = fmt.Sprintf(d.fmt, speed)
|
| 281 | 274 |
}
|
| 282 | 275 |
|
| 283 | 276 |
return d.FormatMsg(d.msg)
|