-
Notifications
You must be signed in to change notification settings - Fork 0
/
timeRecorder.go
137 lines (118 loc) · 2.71 KB
/
timeRecorder.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package kbutils
import (
"bufio"
"fmt"
"io"
"os"
"sort"
"sync"
"sync/atomic"
"time"
)
type TimeRecorder struct {
mutex sync.RWMutex
records map[string]*timeRecord
}
type timeRecord struct {
Times int64
TotalUsedTime int64
MaxUsedTime int64
MinUsedTime int64
}
type avgTimeRecord struct {
Name string
Times int64
AvgUsedTime int64
MinUsedTime int64
MaxUsedTime int64
TotalUsedTime int64
}
type avgTimeRecords []*avgTimeRecord
func (this avgTimeRecords) Len() int {
return len(this)
}
func (this avgTimeRecords) Swap(i, j int) {
this[i], this[j] = this[j], this[i]
}
func (this avgTimeRecords) Less(i, j int) bool {
return this[i].AvgUsedTime > this[j].AvgUsedTime || (this[i].AvgUsedTime == this[j].AvgUsedTime && this[i].Times < this[j].Times)
}
func NewTimeRecorder() *TimeRecorder {
return &TimeRecorder{
records: make(map[string]*timeRecord),
}
}
func (tr *TimeRecorder) getRecord(name string) *timeRecord {
tr.mutex.Lock()
defer tr.mutex.Unlock()
r, exists := tr.records[name]
if !exists {
r = new(timeRecord)
tr.records[name] = r
}
return r
}
func (tr *TimeRecorder) getRecords() avgTimeRecords {
tr.mutex.RLock()
defer tr.mutex.RUnlock()
results := make(avgTimeRecords, 0, len(tr.records))
for name, d := range tr.records {
results = append(results, &avgTimeRecord{
Name: name,
Times: d.Times,
AvgUsedTime: d.TotalUsedTime / d.Times,
MaxUsedTime: d.MaxUsedTime,
MinUsedTime: d.MinUsedTime,
TotalUsedTime: d.TotalUsedTime,
})
}
return results
}
func (tr *TimeRecorder) Record(name string, elapsed time.Duration) {
r := tr.getRecord(name)
usedNano := elapsed.Nanoseconds()
atomic.AddInt64(&r.Times, 1)
atomic.AddInt64(&r.TotalUsedTime, usedNano)
for {
old := atomic.LoadInt64(&r.MaxUsedTime)
if old >= usedNano || atomic.CompareAndSwapInt64(&r.MaxUsedTime, old, usedNano) {
break
}
}
for {
old := atomic.LoadInt64(&r.MinUsedTime)
if old <= usedNano || atomic.CompareAndSwapInt64(&r.MinUsedTime, old, usedNano) {
break
}
}
}
func (tr *TimeRecorder) WriteCSV(writer io.Writer) error {
results := tr.getRecords()
sort.Sort(results)
buf := bufio.NewWriter(writer)
if _, err := fmt.Fprintln(writer, "name,times,avg,min,max,total"); err != nil {
return err
}
for _, r := range results {
if _, err := fmt.Fprintf(writer,
"%s,%d,%dns,%dns,%dns,%d nanosecond\n",
r.Name,
r.Times,
r.AvgUsedTime,
r.MinUsedTime,
r.MaxUsedTime,
r.TotalUsedTime,
); err != nil {
return err
}
}
return buf.Flush()
}
func (tr *TimeRecorder) SaveCSV(filename string) error {
file, err := os.Create(filename)
if err != nil {
return err
}
defer file.Close()
return tr.WriteCSV(file)
}