forked from Seagate/s3bench
-
Notifications
You must be signed in to change notification settings - Fork 0
/
progress_notification.go
68 lines (60 loc) · 1.42 KB
/
progress_notification.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
package main
import (
"fmt"
"time"
)
const (
lineLen = 80
)
// ProgressNotifier is used to store current progress
type ProgressNotifier struct {
name string
tbd uint
done uint
errors uint
started time.Time
fillLen uint
}
func (prg *ProgressNotifier) showProgress() {
donePrc := float64(prg.done) / float64(prg.tbd)
curDur := time.Since(prg.started).Seconds()
var eta float64
if prg.done > 0 {
eta = float64(curDur) / donePrc
}
var statusStr string
if prg.done < prg.tbd {
statusStr = fmt.Sprintf("%s | %d/%d (%.2f%%) | time %v eta %v | errors %d",
prg.name,
prg.done, prg.tbd, donePrc * 100,
time.Duration(curDur) * time.Second, time.Duration(eta - curDur) * time.Second,
prg.errors)
} else {
statusStr = fmt.Sprintf("%s done in %v with %d errors",
prg.name, time.Duration(curDur) * time.Second, prg.errors)
}
fmt.Printf("\r%-*s", prg.fillLen, statusStr)
if prg.done >= prg.tbd {
fmt.Println()
}
}
func startTestWithFill(testName string, total uint, flen uint) *ProgressNotifier {
ret := &ProgressNotifier{
name: testName,
tbd: total,
done: 0,
errors: 0,
started: time.Now(),
fillLen: flen,
}
ret.showProgress()
return ret
}
func startTest(testName string, total uint) *ProgressNotifier {
return startTestWithFill(testName, total, lineLen)
}
func (prg *ProgressNotifier) updateProgress(prog uint, errs uint) {
prg.done += prog
prg.errors += errs
prg.showProgress()
}