-
Notifications
You must be signed in to change notification settings - Fork 0
/
latency.go
48 lines (42 loc) · 1.02 KB
/
latency.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
package gomark
import (
"math"
"time"
"github.com/forrestjgq/gomark/gmi"
"github.com/forrestjgq/glog"
)
// Latency provide a convenient way to record a single latency.
// It is bound with a latency recorder which is provided at creation
type Latency struct {
marker gmi.Marker
start time.Time
latency int32
}
// Mark calculate how many ms has passed since creation of this Latency
// and mark it.
func (l *Latency) Mark() {
du := time.Since(l.start).Milliseconds()
if du > math.MaxInt32 {
glog.Warningf("latency overflow: %d ms", du)
du = math.MaxInt32
}
l.latency = int32(du)
if l.marker != nil {
l.marker.Mark(l.latency)
}
}
// Cancel will clear this latency and it will not be able to use any more.
func (l *Latency) Cancel() {
l.start = time.Time{}
l.marker = nil
}
func (l *Latency) Latency() int32 {
return l.latency
}
// NewLatency create a latency for a Latency Recorder created by NewLatencyRecorder.
func NewLatency(marker gmi.Marker) *Latency {
return &Latency{
marker: marker,
start: time.Now(),
}
}