Codebase list golang-github-vbauerster-mpb / cd74f59
speed decor with producer func Vladimir Bauer 6 years ago
1 changed file(s) with 57 addition(s) and 57 deletion(s). Raw diff Collapse all Expand all
2121 // Note that it's necessary to supply bar.Incr* methods with incremental
2222 // work duration as second argument, in order for this decorator to
2323 // work correctly. This decorator is a wrapper of MovingAverageSpeed.
24 func EwmaSpeed(unit int, fmt string, age float64, wcc ...WC) Decorator {
25 return MovingAverageSpeed(unit, fmt, ewma.NewMovingAverage(age), wcc...)
24 func EwmaSpeed(unit int, format string, age float64, wcc ...WC) Decorator {
25 return MovingAverageSpeed(unit, format, ewma.NewMovingAverage(age), wcc...)
2626 }
2727
2828 // MovingAverageSpeed decorator relies on MovingAverage implementation
3030 //
3131 // `unit` one of [0|UnitKiB|UnitKB] zero for no unit
3232 //
33 // `fmt` printf compatible verb for value, like "%f" or "%d"
33 // `format` printf compatible verb for value, like "%f" or "%d"
3434 //
3535 // `average` MovingAverage implementation
3636 //
3737 // `wcc` optional WC config
3838 //
39 // fmt examples:
39 // format examples:
4040 //
41 // unit=UnitKiB, fmt="%.1f" output: "1.0MiB/s"
42 // unit=UnitKiB, fmt="% .1f" output: "1.0 MiB/s"
43 // unit=UnitKB, fmt="%.1f" output: "1.0MB/s"
44 // unit=UnitKB, fmt="% .1f" output: "1.0 MB/s"
41 // unit=UnitKiB, format="%.1f" output: "1.0MiB/s"
42 // unit=UnitKiB, format="% .1f" output: "1.0 MiB/s"
43 // unit=UnitKB, format="%.1f" output: "1.0MB/s"
44 // unit=UnitKB, format="% .1f" output: "1.0 MB/s"
4545 //
46 func MovingAverageSpeed(unit int, fmt string, average MovingAverage, wcc ...WC) Decorator {
46 func MovingAverageSpeed(unit int, format string, average MovingAverage, wcc ...WC) Decorator {
4747 var wc WC
4848 for _, widthConf := range wcc {
4949 wc = widthConf
5050 }
51 if format == "" {
52 format = "%.0f"
53 }
5154 wc.Init()
52 if fmt == "" {
53 fmt = "%.0f"
54 }
5555 d := &movingAverageSpeed{
5656 WC: wc,
57 unit: unit,
58 fmt: fmt,
5957 average: average,
58 }
59 switch unit {
60 case UnitKiB:
61 d.producer = func(speed float64) string {
62 return fmt.Sprintf(format, &speedType{SizeB1024(math.Round(speed))})
63 }
64 case UnitKB:
65 d.producer = func(speed float64) string {
66 return fmt.Sprintf(format, &speedType{SizeB1000(math.Round(speed))})
67 }
68 default:
69 d.producer = func(speed float64) string {
70 return fmt.Sprintf(format, speed)
71 }
6072 }
6173 return d
6274 }
6375
6476 type movingAverageSpeed struct {
6577 WC
66 unit int
67 fmt string
78 producer func(float64) string
6879 average ewma.MovingAverage
6980 msg string
7081 completeMsg *string
7889 return d.FormatMsg(d.msg)
7990 }
8091
81 speed := d.average.Value()
82
83 var val interface{}
84 switch d.unit {
85 case UnitKiB:
86 val = &speedType{SizeB1024(math.Round(speed))}
87 case UnitKB:
88 val = &speedType{SizeB1000(math.Round(speed))}
89 default:
90 val = speed
91 }
92 d.msg = fmt.Sprintf(d.fmt, val)
92 d.msg = d.producer(d.average.Value())
9393
9494 return d.FormatMsg(d.msg)
9595 }
112112
113113 // AverageSpeed decorator with dynamic unit measure adjustment. It's
114114 // a wrapper of NewAverageSpeed.
115 func AverageSpeed(unit int, fmt string, wcc ...WC) Decorator {
116 return NewAverageSpeed(unit, fmt, time.Now(), wcc...)
115 func AverageSpeed(unit int, format string, wcc ...WC) Decorator {
116 return NewAverageSpeed(unit, format, time.Now(), wcc...)
117117 }
118118
119119 // NewAverageSpeed decorator with dynamic unit measure adjustment and
121121 //
122122 // `unit` one of [0|UnitKiB|UnitKB] zero for no unit
123123 //
124 // `fmt` printf compatible verb for value, like "%f" or "%d"
124 // `format` printf compatible verb for value, like "%f" or "%d"
125125 //
126126 // `startTime` start time
127127 //
128128 // `wcc` optional WC config
129129 //
130 // fmt examples:
130 // format examples:
131131 //
132 // unit=UnitKiB, fmt="%.1f" output: "1.0MiB/s"
133 // unit=UnitKiB, fmt="% .1f" output: "1.0 MiB/s"
134 // unit=UnitKB, fmt="%.1f" output: "1.0MB/s"
135 // unit=UnitKB, fmt="% .1f" output: "1.0 MB/s"
132 // unit=UnitKiB, format="%.1f" output: "1.0MiB/s"
133 // unit=UnitKiB, format="% .1f" output: "1.0 MiB/s"
134 // unit=UnitKB, format="%.1f" output: "1.0MB/s"
135 // unit=UnitKB, format="% .1f" output: "1.0 MB/s"
136136 //
137 func NewAverageSpeed(unit int, fmt string, startTime time.Time, wcc ...WC) Decorator {
137 func NewAverageSpeed(unit int, format string, startTime time.Time, wcc ...WC) Decorator {
138138 var wc WC
139139 for _, widthConf := range wcc {
140140 wc = widthConf
141141 }
142 if format == "" {
143 format = "%.0f"
144 }
142145 wc.Init()
143 if fmt == "" {
144 fmt = "%.0f"
145 }
146146 d := &averageSpeed{
147147 WC: wc,
148 unit: unit,
149148 startTime: startTime,
150 fmt: fmt,
149 }
150 switch unit {
151 case UnitKiB:
152 d.producer = func(speed float64) string {
153 return fmt.Sprintf(format, &speedType{SizeB1024(math.Round(speed))})
154 }
155 case UnitKB:
156 d.producer = func(speed float64) string {
157 return fmt.Sprintf(format, &speedType{SizeB1000(math.Round(speed))})
158 }
159 default:
160 d.producer = func(speed float64) string {
161 return fmt.Sprintf(format, speed)
162 }
151163 }
152164 return d
153165 }
154166
155167 type averageSpeed struct {
156168 WC
157 unit int
158169 startTime time.Time
159 fmt string
170 producer func(float64) string
160171 msg string
161172 completeMsg *string
162173 }
169180 return d.FormatMsg(d.msg)
170181 }
171182
172 timeElapsed := time.Since(d.startTime)
173 speed := float64(st.Current) / timeElapsed.Seconds()
174
175 var val interface{}
176 switch d.unit {
177 case UnitKiB:
178 val = &speedType{SizeB1024(math.Round(speed))}
179 case UnitKB:
180 val = &speedType{SizeB1000(math.Round(speed))}
181 default:
182 val = speed
183 }
184 d.msg = fmt.Sprintf(d.fmt, val)
183 speed := float64(st.Current) / time.Since(d.startTime).Seconds()
184 d.msg = d.producer(speed)
185185
186186 return d.FormatMsg(d.msg)
187187 }