-
Notifications
You must be signed in to change notification settings - Fork 34
Expand file tree
/
Copy pathcmd_profiler.go
More file actions
78 lines (61 loc) · 1.87 KB
/
cmd_profiler.go
File metadata and controls
78 lines (61 loc) · 1.87 KB
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
package utils
import (
"sync"
"time"
"github.com/ProtonMail/gluon/profiling"
)
// DurationCmdProfiler records the duration of the duration between invocations of IMAP Commands.
type DurationCmdProfiler struct {
durations [profiling.CmdTypeTotal][]time.Duration
start [profiling.CmdTypeTotal]time.Time
}
func (c *DurationCmdProfiler) Start(cmdType int) {
// We can use time since Go 1.9 they have switch to monotonic clocks.
c.start[cmdType] = time.Now()
}
func (c *DurationCmdProfiler) Stop(cmdType int) {
elapsed := time.Since(c.start[cmdType])
c.durations[cmdType] = append(c.durations[cmdType], elapsed)
}
func NewDurationCmdProfiler() *DurationCmdProfiler {
profiler := &DurationCmdProfiler{}
for i := range len(profiler.durations) {
profiler.durations[i] = make([]time.Duration, 0, 128)
}
return profiler
}
type DurationCmdProfilerBuilder struct {
mutex sync.Mutex
profilers []*DurationCmdProfiler
}
func (c *DurationCmdProfilerBuilder) New() profiling.CmdProfiler {
return NewDurationCmdProfiler()
}
func (c *DurationCmdProfilerBuilder) Collect(profiler profiling.CmdProfiler) {
switch v := profiler.(type) {
case *DurationCmdProfiler:
c.mutex.Lock()
defer c.mutex.Unlock()
c.profilers = append(c.profilers, v)
}
}
// Merge merges all collected command profilers into a single timing Calculator for each IMAP command.
func (c *DurationCmdProfilerBuilder) Merge() [profiling.CmdTypeTotal][]time.Duration {
c.mutex.Lock()
defer c.mutex.Unlock()
var result [profiling.CmdTypeTotal][]time.Duration
for _, v := range c.profilers {
for i := range len(result) {
result[i] = append(result[i], v.durations[i]...)
}
}
return result
}
func (c *DurationCmdProfilerBuilder) Clear() {
c.mutex.Lock()
defer c.mutex.Unlock()
c.profilers = nil
}
func NewDurationCmdProfilerBuilder() *DurationCmdProfilerBuilder {
return &DurationCmdProfilerBuilder{}
}