-
Notifications
You must be signed in to change notification settings - Fork 3
/
time_tracker.go
149 lines (125 loc) · 3.81 KB
/
time_tracker.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
// Copyright (c) 2021 rookie-ninja
//
// Use of this source code is governed by an Apache-style
// license that can be found in the LICENSE file.
package rkquery
import (
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"strconv"
"time"
)
type timeTracker struct {
name string
indexCurr int64
lastTimestampMs int64
countTotal int64
elapsedTotalMs int64
isFinished bool
}
// Create a new timeTracker with name.
// Name should be unique.
func newTimeTracker(name string) *timeTracker {
if len(name) == 0 {
return nil
}
return &timeTracker{
name: name,
indexCurr: 0,
lastTimestampMs: 0,
countTotal: 0,
elapsedTotalMs: 0,
isFinished: false,
}
}
// GetName returns name of current timeTracker.
func (tracker *timeTracker) GetName() string {
return tracker.name
}
// GetCount returns count of how many times Start() has been called.
func (tracker *timeTracker) GetCount() int64 {
return tracker.countTotal
}
// GetElapsedMs returns elapsed time in milli seconds.
func (tracker *timeTracker) GetElapsedMs() int64 {
return tracker.elapsedTotalMs
}
// Start current timer with provided timestamp.
func (tracker *timeTracker) Start(nowMs int64) {
if nowMs < 0 {
return
}
// This is a little bit hard to understand bellow logic
// For example, for every duplicated event, we track every calls.
//
// | 22 333
// |111111111111
// +-------------
//
// In case above, the x-axis represents the time, y-axis represents concurrent event.
// The total time elapsed would be 18 with this formula: 17 = 12 + 3 + 2
if tracker.indexCurr > 0 {
tracker.elapsedTotalMs += tracker.indexCurr * (nowMs - tracker.lastTimestampMs)
}
tracker.lastTimestampMs = nowMs
tracker.countTotal++
tracker.indexCurr++
}
// End current timer with provided timestamp.
func (tracker *timeTracker) End(nowMs int64) {
if tracker.indexCurr < 1 || nowMs < 0 {
return
}
tracker.elapsedTotalMs += tracker.indexCurr * (nowMs - tracker.lastTimestampMs)
tracker.lastTimestampMs = nowMs
tracker.indexCurr--
}
// Elapse will force to elapse timer.
func (tracker *timeTracker) Elapse(elapseTimeMs int64) {
if elapseTimeMs < 0 {
return
}
tracker.ElapseWithSample(elapseTimeMs, 1)
}
// ElapseWithSample elapse timer with number of sample.
func (tracker *timeTracker) ElapseWithSample(elapseTimeMs int64, numSample int64) {
if elapseTimeMs < 0 || numSample < 0 {
return
}
tracker.countTotal += numSample
tracker.elapsedTotalMs += elapseTimeMs
}
// Finish stops current timer.
func (tracker *timeTracker) Finish() {
tracker.isFinished = true
if tracker.indexCurr == 0 {
return
}
nowMs := toMillisecond(time.Now())
tracker.elapsedTotalMs += tracker.indexCurr * (nowMs - tracker.lastTimestampMs)
tracker.lastTimestampMs = nowMs
tracker.indexCurr = 0
}
// ToZapFields convert to zap fields.
func (tracker *timeTracker) ToZapFields(enc *zapcore.MapObjectEncoder) []zap.Field {
if tracker.indexCurr == 0 {
if enc != nil {
enc.AddInt64(tracker.name+".elapsedMs", tracker.elapsedTotalMs)
enc.AddInt64(tracker.name+".count", tracker.countTotal)
}
return []zap.Field{
zap.Int64(tracker.name+".elapsedMs", tracker.elapsedTotalMs),
zap.Int64(tracker.name+".count", tracker.countTotal),
}
}
nowMs := toMillisecond(time.Now())
elapsedMs := tracker.elapsedTotalMs + tracker.indexCurr*(nowMs-tracker.lastTimestampMs)
if enc != nil {
enc.AddInt64(tracker.name+openMarker+strconv.FormatInt(tracker.indexCurr, 10)+".elapsedMs", tracker.elapsedTotalMs)
enc.AddInt64(tracker.name+openMarker+strconv.FormatInt(tracker.indexCurr, 10)+".count", tracker.countTotal)
}
return []zap.Field{
zap.Int64(tracker.name+openMarker+strconv.FormatInt(tracker.indexCurr, 10)+".elapsedMs", elapsedMs),
zap.Int64(tracker.name+openMarker+strconv.FormatInt(tracker.indexCurr, 10)+".count", tracker.countTotal),
}
}