-
Notifications
You must be signed in to change notification settings - Fork 1
/
measure.go
133 lines (115 loc) · 3.43 KB
/
measure.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
package raft
import (
"encoding/csv"
"fmt"
"os"
"time"
)
// Latency is a slice of CSV records.
type Latency [][]string
// NewLatency returns a Latency struct initialized with a header record.
func NewLatency() *Latency {
lat := new(Latency)
*lat = append(*lat, []string{"start", "end"})
return lat
}
// Record records a new CSV record with start as the start time and time.Now()
// as the end time.
func (l *Latency) Record(start time.Time) {
now := time.Now()
*l = append(*l, []string{
fmt.Sprintf("%d", start.UnixNano()),
fmt.Sprintf("%d", now.UnixNano()),
})
}
// Write writes all records to a file.
func (l *Latency) Write(path string) {
f, err := os.Create(path)
if err != nil {
panic("error creating file: " + err.Error())
}
w := csv.NewWriter(f)
w.WriteAll(*l) // Checking error below.
if err := w.Error(); err != nil {
panic("error writing csv: " + err.Error())
}
}
// EventType is the types of event that Event can record.
type EventType int
const (
// EventCatchup a follower invoked a catchup.
EventCatchup EventType = 0
// EventFailure a server suspected another server of failing.
EventFailure EventType = 1
// EventElection an election was initiated.
EventElection EventType = 2
// EventPreElection a pre-election was initiated.
EventPreElection EventType = 3
// EventBecomeLeader a candidate won an election.
EventBecomeLeader EventType = 4
// EventProposeAddServer a leader received a add server request.
EventProposeAddServer EventType = 5
// EventProposeRemoveServer a leader received a remove server request.
EventProposeRemoveServer EventType = 6
// EventCaughtUp indicates that the a server has caught up to the point
// where it has applied the configuration change which added it to the
// cluster.
EventCaughtUp EventType = 7
// EventRemoved the remove server request was committed.
EventRemoved EventType = 8
// EventAdded the add server request was committed.
EventAdded EventType = 9
// EventApplyConfiguration a new configuration is now being used.
EventApplyConfiguration EventType = 10
// EventTerminated a server received a termination signal.
EventTerminated EventType = 11
// EventStartReplicate the leader started replicating entries to a
// server.
EventStartReplicate = 12
// EventInjectEntries the leader responds to a catchup request by
// injecting the missing entries in the next request.
EventInjectEntries = 13
)
var eventName = map[EventType]string{
0: "catchup",
1: "failure",
2: "election",
3: "preelection",
4: "becomeleader",
5: "proposeaddserver",
6: "proposeremoveserver",
7: "caughtup",
8: "removed",
9: "added",
10: "applyconfiguration",
11: "terminated",
12: "startreplicate",
13: "injectentries",
}
// Event is a slice of CSV records.
type Event [][]string
// NewEvent returns a Event struct initialized with a header record.
func NewEvent() *Event {
e := new(Event)
*e = append(*e, []string{"event", "time"})
return e
}
// Record records a new CSV record with time set to time.Now().
func (e *Event) Record(event EventType) {
*e = append(*e, []string{
fmt.Sprintf("%s", eventName[event]),
fmt.Sprintf("%d", time.Now().UnixNano()),
})
}
// Write writes all records to a file.
func (e *Event) Write(path string) {
f, err := os.Create(path)
if err != nil {
panic("error creating file: " + err.Error())
}
w := csv.NewWriter(f)
w.WriteAll(*e) // Checking error below.
if err := w.Error(); err != nil {
panic("error writing csv: " + err.Error())
}
}