Codebase list golang-github-vbauerster-mpb / 1c75337
Fix #20 Vladimir Bauer 8 years ago
14 changed file(s) with 30 addition(s) and 16 deletion(s). Raw diff Collapse all Expand all
5555 // simulating some work
5656 max := 100 * time.Millisecond
5757 for i := 0; i < total; i++ {
58 bar.StartBlock()
5859 time.Sleep(time.Duration(rand.Intn(10)+1) * max / 10)
5960 // increment by 1 (there is bar.IncrBy(int) method, if needed)
6061 bar.Increment()
8990 defer wg.Done()
9091 max := 100 * time.Millisecond
9192 for i := 0; i < total; i++ {
93 bar.StartBlock()
9294 time.Sleep(time.Duration(rand.Intn(10)+1) * max / 10)
9395 bar.Increment()
9496 }
5656 etaAlpha float64
5757 total int64
5858 current int64
59 lastIncrement int64
6059 totalAutoIncrTrigger int64
6160 totalAutoIncrBy int64
6261 trimLeftSpace bool
240239 }
241240 }
242241
242 // StartBlock updates start timestamp of the current increment block.
243 // It is optional to call, unless ETA decorator is used.
244 // If *bar.ProxyReader is used, it will be called implicitly.
245 func (b *Bar) StartBlock() {
246 now := time.Now()
247 select {
248 case b.operateState <- func(s *bState) { s.blockStartTime = now }:
249 case <-b.done:
250 }
251 }
252
243253 // IncrBy increments progress bar by amount of n
244254 func (b *Bar) IncrBy(n int) {
245 if n < 1 {
246 return
247 }
248255 now := time.Now()
249256 select {
250257 case b.operateState <- func(s *bState) {
251258 if s.current == 0 {
252259 s.startTime = now
253260 } else {
254 s.updateETA(now.Sub(s.blockStartTime))
255261 s.timeElapsed = now.Sub(s.startTime)
256262 }
257 s.blockStartTime = now
258 s.lastIncrement = int64(n)
259 s.current += s.lastIncrement
263 s.current += int64(n)
264 s.updateETA(n, now.Sub(s.blockStartTime))
260265 if s.dynamic {
261266 curp := decor.CalcPercentage(s.total, s.current, 100)
262267 if 100-curp <= s.totalAutoIncrTrigger {
292297 s.toComplete = true
293298 cancel = nil
294299 case <-b.shutdown:
295 s.updateETA(time.Since(s.blockStartTime))
296300 b.cacheState = s
297301 close(b.done)
298302 return
428432 }
429433 }
430434
431 func (s *bState) updateETA(lastBlockTime time.Duration) {
432 lastItemEstimate := float64(lastBlockTime) / float64(s.lastIncrement)
435 func (s *bState) updateETA(n int, lastBlockTime time.Duration) {
436 lastItemEstimate := float64(lastBlockTime) / float64(n)
433437 s.timePerItemEstimate = time.Duration((s.etaAlpha * lastItemEstimate) + (1-s.etaAlpha)*float64(s.timePerItemEstimate))
434438 s.timeRemaining = time.Duration(s.total-s.current) * s.timePerItemEstimate
435439 }
176176 // `width` width reservation to apply, ignored if `DwidthSync` bit is set
177177 //
178178 // `conf` bit set config, [DidentRight|DwidthSync|DextraSpace]
179 //
180 // To correctly estimate non io progress, *Bar.StartBlock should be called,
181 // before each increment block.
179182 func ETA(width, conf int) DecoratorFunc {
180183 format := "%%"
181184 if (conf & DidentRight) != 0 {
4545 defer wg.Done()
4646 max := 100 * time.Millisecond
4747 for !bar.Completed() {
48 bar.StartBlock()
4849 time.Sleep(time.Duration(rand.Intn(10)+1) * max / 10)
4950 bar.Increment()
5051 }
5253 }
5354
5455 p.Wait()
55 fmt.Println("done")
5656 }
6868 defer wg.Done()
6969 max := 100 * time.Millisecond
7070 for !b.Completed() {
71 b.StartBlock()
7172 time.Sleep(time.Duration(rand.Intn(10)+1) * max / 10)
7273 b.IncrBy(incrBy)
7374 }
4242 decor.CountersKibiByte("% 6.1f / % 6.1f", 18, 0),
4343 ),
4444 mpb.AppendDecorators(
45 decor.ETA(0, 0),
4546 decor.SpeedKibiByte("% 6.1f", 18, 0),
4647 ),
4748 )
3838 defer wg.Done()
3939 max := 100 * time.Millisecond
4040 for i := 0; i < total; i++ {
41 b.StartBlock()
4142 time.Sleep(time.Duration(rand.Intn(10)+1) * max / 10)
4243 b.Increment()
4344 }
4646 }
4747
4848 p.Wait()
49 fmt.Println("done")
5049 }
4343 defer wg.Done()
4444 max := 100 * time.Millisecond
4545 for i := 0; i < total; i++ {
46 b.StartBlock()
4647 if b.ID() == 2 && i == 42 {
4748 p.Abort(b)
4849 return
5455 }
5556
5657 p.Wait()
57 fmt.Println("done")
5858 }
3838 defer wg.Done()
3939 max := 100 * time.Millisecond
4040 for i := 0; i < total; i++ {
41 bar.StartBlock()
4142 time.Sleep(time.Duration(rand.Intn(10)+1) * max / 10)
4243 bar.Increment()
4344 }
3636 // simulating some work
3737 max := 100 * time.Millisecond
3838 for i := 0; i < total; i++ {
39 bar.StartBlock()
3940 time.Sleep(time.Duration(rand.Intn(10)+1) * max / 10)
4041 // Increment by 1 (there is bar.IncrBy(int) method, if needed)
4142 bar.Increment()
3838 defer wg.Done()
3939 max := 100 * time.Millisecond
4040 for i := 0; i < total; i++ {
41 b.StartBlock()
4142 time.Sleep(time.Duration(rand.Intn(10)+1) * max / 10)
4243 if i&1 == 1 {
4344 priority := total - int(b.Current())
4950 }
5051
5152 p.Wait()
52 fmt.Println("done")
5353 }
4040 defer wg.Done()
4141 max := 100 * time.Millisecond
4242 for i := 0; i < total; i++ {
43 bar.StartBlock()
4344 time.Sleep(time.Duration(rand.Intn(10)+1) * max / 10)
4445 bar.Increment()
4546 }
4748 }
4849
4950 p.Wait()
50 fmt.Println("done")
5151 }
88 }
99
1010 func (r *Reader) Read(p []byte) (int, error) {
11 r.bar.StartBlock()
1112 n, err := r.Reader.Read(p)
1213 r.bar.IncrBy(n)
1314 return n, err