-
Notifications
You must be signed in to change notification settings - Fork 0
/
stopwatch.go
108 lines (85 loc) · 2.23 KB
/
stopwatch.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package stopwatch
import (
"bytes"
"container/list"
"errors"
"fmt"
"time"
)
type taskInfo struct {
taskName string
taskElapsed int64
}
type StopWatch struct {
id string
latestTaskName string
taskList *list.List
latestStartTime time.Time
taskCnt int
totalElapsed int64
}
func New(id string) *StopWatch {
return &StopWatch{id: id, taskList: list.New()}
}
func (w *StopWatch) Start(taskName string) error {
if taskName == "" {
return errors.New("task name must not be empty")
}
if w.latestTaskName != "" {
return fmt.Errorf("can not start new stopwatch, current task: %s is running", w.latestTaskName)
}
w.latestTaskName = taskName
w.latestStartTime = time.Now()
return nil
}
func (w *StopWatch) MustStart(taskName string) {
err := w.Start(taskName)
if err != nil {
panic(err)
}
}
func (w *StopWatch) Stop() error {
if w.latestTaskName == "" {
return errors.New("can not stop StopWatch: it's not running")
}
elapsed := time.Since(w.latestStartTime).Nanoseconds()
w.totalElapsed += elapsed
lastTask := taskInfo{taskName: w.latestTaskName, taskElapsed: elapsed}
w.taskList.PushBack(lastTask)
w.taskCnt++
w.latestTaskName = ""
return nil
}
func (w *StopWatch) MustStop() {
if err := w.Stop(); err != nil {
panic(err)
}
}
func (w *StopWatch) ShortSummary() string {
return fmt.Sprintf("StopWatch '"+w.id+"': running time (ms) = %d\n", w.totalElapsed/1000000)
}
func (w *StopWatch) PrettyPrint() string {
var buf bytes.Buffer
buf.WriteString(w.ShortSummary())
buf.WriteString("-----------------------------------------\n")
buf.WriteString("ms % Task name\n")
buf.WriteString("-----------------------------------------\n")
for e := w.taskList.Front(); e != nil; e = e.Next() {
taskInfo := e.Value.(taskInfo)
var elapsed int64
if w.totalElapsed != 0 {
elapsed = taskInfo.taskElapsed*100/w.totalElapsed
}
buf.WriteString(fmt.Sprintf("%-10d", taskInfo.taskElapsed/1000000))
buf.WriteString(fmt.Sprintf("%-10s", fmt.Sprintf("%d%%", elapsed)))
buf.WriteString(fmt.Sprintf("%s\n", taskInfo.taskName))
}
return buf.String()
}
func (w *StopWatch) Clear() {
w.taskList = list.New()
w.totalElapsed = 0
w.latestTaskName = ""
w.taskCnt = 0
w.id = ""
}