-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathtime_recorder.go
149 lines (121 loc) · 3.25 KB
/
time_recorder.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
138
139
140
141
142
143
144
145
146
147
148
149
package pprof
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
}
func NewTimeRecorder() *TimeRecorder {
return &TimeRecorder{
records: make(map[string]*timeRecord),
}
}
func (tr *TimeRecorder) Record(name string, usedTime time.Duration) {
r := tr.getRecord(name, usedTime)
usedNano := usedTime.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) getRecord(name string, usedTime time.Duration) *timeRecord {
tr.mutex.Lock()
defer tr.mutex.Unlock()
r, exists := tr.records[name]
if !exists {
r = new(timeRecord)
usedNano := usedTime.Nanoseconds()
r.MinUsedTime = usedNano
r.MaxUsedTime = usedNano
tr.records[name] = r
}
return r
}
func (tr *TimeRecorder) SaveCSV(filename string) error {
file, err := os.Create(filename)
if err != nil {
return err
}
defer file.Close()
return tr.WriteCSV(file)
}
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,%d,%d,%d,%d\n",
r.Name,
r.Times,
r.AvgUsedTime,
r.MinUsedTime,
r.MaxUsedTime,
r.TotalUsedTime,
); err != nil {
return err
}
}
return buf.Flush()
}
func (tr *TimeRecorder) getRecords() sortTimeRecords {
tr.mutex.RLock()
defer tr.mutex.RUnlock()
results := make(sortTimeRecords, 0, len(tr.records))
for name, d := range tr.records {
results = append(results, &sortTimeRecord{
Name: name,
Times: d.Times,
AvgUsedTime: d.TotalUsedTime / d.Times,
MaxUsedTime: d.MaxUsedTime,
MinUsedTime: d.MinUsedTime,
TotalUsedTime: d.TotalUsedTime,
})
}
return results
}
type sortTimeRecord struct {
Name string
Times int64
AvgUsedTime int64
MinUsedTime int64
MaxUsedTime int64
TotalUsedTime int64
}
type sortTimeRecords []*sortTimeRecord
func (this sortTimeRecords) Len() int {
return len(this)
}
func (this sortTimeRecords) Swap(i, j int) {
this[i], this[j] = this[j], this[i]
}
func (this sortTimeRecords) Less(i, j int) bool {
return this[i].AvgUsedTime > this[j].AvgUsedTime || (this[i].AvgUsedTime == this[j].AvgUsedTime && this[i].Times < this[j].Times)
}