-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathexecutionplan.go
163 lines (136 loc) · 4.47 KB
/
executionplan.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
package main
import (
"bufio"
"fmt"
"os"
"time"
)
type request struct {
when time.Duration // How many microseconds since the beginning of the benchmark until this request shall be sent
responseTime time.Duration // How long time elapsed since "when" until the response was received
writtenBytes int
writtenDone bool
completed bool
error bool
httpCode int
responseReader ResponseReader
workerID int
socketfd int
}
type executionPlan struct {
reqs []request
workerPos []int // Element i in this slice represents the position in reqs of the next request the i'th worker must send.
workerPosTimeout []int // Element i in this slice represents the position in reqs of the next request the i'th worker should time out if it was not yet finished.
latestTimeoutReq []*request // Element i in this slice is the current req that the i'th worker should time out if it was not yet finished.
}
func newExecutionPlan(rps int, seconds int, workerCount int) (e *executionPlan) {
e = &executionPlan{}
secondsPerRequest := time.Duration(float64(time.Second) / float64(rps))
e.reqs = make([]request, rps*seconds)
for i := 1; i < len(e.reqs); i++ {
e.reqs[i].when = e.reqs[i-1].when + secondsPerRequest
e.reqs[i].workerID = i % workerCount
}
// Initialize worker positions to point to the first request each worker should send.
e.workerPos = make([]int, workerCount, workerCount)
e.workerPosTimeout = make([]int, workerCount, workerCount)
e.latestTimeoutReq = make([]*request, workerCount, workerCount)
for workerID := 0; workerID < workerCount; workerID++ {
nextPos := 0
for nextPos < len(e.reqs) && e.reqs[nextPos].workerID != workerID {
nextPos++
}
e.workerPos[workerID] = nextPos
e.workerPosTimeout[workerID] = nextPos
}
return
}
func (e *executionPlan) getNext(workerID int) (r *request) {
if e.done(workerID) {
return
}
r = &e.reqs[e.workerPos[workerID]]
// Set worker position to point to the next request this worker should send.
nextPos := e.workerPos[workerID]
nextPos++
for nextPos < len(e.reqs) && e.reqs[nextPos].workerID != workerID {
nextPos++
}
e.workerPos[workerID] = nextPos
return
}
// Gets the next request that could potentially time out.
func (e *executionPlan) getNextPotentialTimeoutReq(workerID int) (r *request) {
e.latestTimeoutReq[workerID] = nil
if e.workerPosTimeout[workerID] == len(e.reqs) {
return
}
if e.workerPosTimeout[workerID] == e.workerPos[workerID] {
// There are no requests currently in flight that we haven't already monitored for time out.
return
}
r = &e.reqs[e.workerPosTimeout[workerID]]
e.latestTimeoutReq[workerID] = r
// Set worker timeout position to point to the next request this worker should time out if it was not yet finished.
nextPos := e.workerPosTimeout[workerID]
nextPos++
for nextPos < len(e.reqs) && e.reqs[nextPos].workerID != workerID && e.workerPosTimeout[workerID] < e.workerPos[workerID] && !e.reqs[nextPos].completed && !e.reqs[nextPos].error {
nextPos++
}
e.workerPosTimeout[workerID] = nextPos
return
}
func (e *executionPlan) peekNext(workerID int) (r *request) {
if e.done(workerID) {
return
}
r = &e.reqs[e.workerPos[workerID]]
return
}
func (e *executionPlan) done(workerID int) bool {
return e.workerPos[workerID] == len(e.reqs)
}
func (e *executionPlan) writeResultsFile(filename string) {
resultsFile, err := os.Create(filename)
defer resultsFile.Close()
if err != nil {
fmt.Printf("%v\n", err)
return
}
resultsFileWriter := bufio.NewWriter(resultsFile)
fmt.Fprintf(resultsFileWriter, "whenNs")
fmt.Fprintf(resultsFileWriter, ",written")
fmt.Fprintf(resultsFileWriter, ",completed")
fmt.Fprintf(resultsFileWriter, ",error")
fmt.Fprintf(resultsFileWriter, ",httpCode")
fmt.Fprintf(resultsFileWriter, ",latencyMs")
fmt.Fprintf(resultsFileWriter, "\n")
for _, r := range e.reqs {
fmt.Fprintf(resultsFileWriter, "%d", r.when)
w := 0
if r.writtenDone {
w = 1
}
fmt.Fprintf(resultsFileWriter, ",%d", w)
c := 0
if r.completed {
c = 1
}
fmt.Fprintf(resultsFileWriter, ",%d", c)
e := 0
if r.error {
e = 1
}
fmt.Fprintf(resultsFileWriter, ",%d", e)
fmt.Fprintf(resultsFileWriter, ",%d", r.httpCode)
if r.responseTime != 0 {
v := float64(r.responseTime) / float64(time.Millisecond)
fmt.Fprintf(resultsFileWriter, ",%7f", v)
} else {
fmt.Fprintf(resultsFileWriter, ",")
}
fmt.Fprintf(resultsFileWriter, "\n")
}
resultsFileWriter.Flush()
resultsFile.Close()
}