-
Notifications
You must be signed in to change notification settings - Fork 6
/
looper_test.go
320 lines (257 loc) · 9.13 KB
/
looper_test.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
package mybench
import (
"context"
"math"
"sync"
"testing"
"time"
"github.com/sirupsen/logrus"
"github.com/stretchr/testify/require"
)
// To test the looper, we need a way to reliably cause a certain amount of time
// to pass during a single Event() call. We do not want to use time.Sleep,
// because time.Sleep may put the goroutine to sleep and schedule another
// goroutine. Once the sleep time is done, it then tries to wake up the sleeping
// goroutine. This can introduce significant jitter, especially when the sleep
// duration is low. This jitter may result in unpredicable behavior in test (or
// significant variance in the results), which makes it hard to test without
// being flaky.
//
// Instead, we use a busy sleeper by computing a prime number inefficiently.
// The code starts by calibrating the prime number function and compute a linear
// function (via linear regression) that maps execution time and the number to
// be checked as prime. This is known as the BusySleeper. The BusySleeper.Sleep
// takes a time to sleep, calculates the required value and then call the prime
// number function.
//
// In my limited experimentation, this approach results in low jitter (+/- 30us)
// on a virtual machine.
type BusySleeper struct {
// v = beta * time + alpha
alpha float64
beta float64
r2 float64
maxSleepTimeUs float64
foundPrime int64 // Hopefully this means the computePrime function won't be optimized out
}
func NewBusySleeper(maxSleepTimeUs float64) *BusySleeper {
s := &BusySleeper{
maxSleepTimeUs: maxSleepTimeUs,
}
s.calibrate()
return s
}
func (s *BusySleeper) Sleep(timeUs float64) {
sleepTimeRemaining := timeUs
for sleepTimeRemaining > 0 {
timeToSleep := sleepTimeRemaining
if timeToSleep > s.maxSleepTimeUs {
timeToSleep = s.maxSleepTimeUs
}
v := math.Round(s.beta*timeToSleep + s.alpha)
s.computePrime(int64(v))
sleepTimeRemaining -= timeToSleep
}
}
// Very inefficient prime computation to burn time
func (s *BusySleeper) computePrime(v int64) {
var isPrime bool = true
for i := int64(1); i < v; i++ {
if v%i == 0 {
isPrime = false
}
}
if isPrime {
s.foundPrime += 1
}
}
func (s *BusySleeper) calibrate() {
var currentV int64 = 0
var currentTimingUs int64 = 0
diff := int64(200000)
timings := [][2]int64{}
for currentTimingUs < int64(s.maxSleepTimeUs) {
currentV += diff
start := time.Now()
s.computePrime(currentV)
currentTimingUs = time.Since(start).Microseconds()
row := [2]int64{currentTimingUs, currentV}
timings = append(timings, row)
// fmt.Printf("%d, %d\n", row[0], row[1])
}
s.alpha, s.beta, s.r2 = simpleLinearRegression(timings)
logrus.Debugf("v = %.3E * t + %.3f (r^2 = %.6f, n = %d)", s.beta, s.alpha, s.r2, len(timings))
logrus.Debugf("Ignore me: %d", s.foundPrime) // Hopefully means that computePrime won't be optimized out.
}
// y = alpha + beta * x
func simpleLinearRegression(data [][2]int64) (float64, float64, float64) {
xbar := 0.0
ybar := 0.0
xybar := 0.0
x2bar := 0.0
y2bar := 0.0
for _, row := range data {
xbar += float64(row[0])
ybar += float64(row[1])
xybar += float64(row[0] * row[1])
x2bar += float64(row[0] * row[0])
y2bar += float64(row[1] * row[1])
}
n := float64(len(data))
xbar /= n
ybar /= n
xybar /= n
x2bar /= n
y2bar /= n
top := 0.0
bottom := 0.0
for _, row := range data {
x := float64(row[0])
y := float64(row[1])
top += (x - xbar) * (y - ybar)
bottom += (x - xbar) * (x - xbar)
}
beta := top / bottom
alpha := ybar - (beta * xbar)
r := (xybar - xbar*ybar) / math.Sqrt((x2bar-xbar*xbar)*(y2bar-ybar*ybar))
return alpha, beta, r * r
}
var sleeper *BusySleeper = NewBusySleeper(25000)
func TestLooperNoBackPressure(t *testing.T) {
numEvents := int64(1000) // 5 seconds
looper := &DiscretizedLooper{
EventRate: 200,
OuterLoopRate: 50,
LooperType: LooperTypeUniform,
}
ctx, cancel := context.WithCancel(context.Background())
// Don't ant to allocate in the loop, so we allocate plenty of space
stats := make([]OuterLoopStat, 0, int(looper.OuterLoopRate*float64(numEvents)/looper.EventRate*10))
looper.Event = func(context.Context) error {
sleeper.Sleep(1000)
return nil
}
looper.TraceOuterLoop = func(stat OuterLoopStat) {
stats = append(stats, stat)
if stat.CumulativeNumberOfEvents >= numEvents {
cancel()
}
}
wg := &sync.WaitGroup{}
wg.Add(1)
start := time.Now()
go func() {
defer wg.Done()
looper.Run(ctx)
}()
wg.Wait()
delta := time.Since(start).Microseconds()
expectedDelta := int64(5000000) // expected 5 seconds
diff := float64(expectedDelta - delta)
percent := math.Abs(diff / float64(expectedDelta) * 100.0)
// The threshold is arbitrarily chosen. Works on my computer.
require.True(t, percent <= 15, "expected total looper duration is more than 15%% from the actual duration (expected = %d us, actual = %d us, diff = %.2f%%).", expectedDelta, delta, percent)
lastStat := stats[len(stats)-1]
require.Equal(t, int64(1000), lastStat.CumulativeNumberOfEvents)
for _, stat := range stats {
// Since the EventRate is a multiple of OuterLoopRate, the expected batch
// size should be constant as there should be no discretization problems.
require.Equal(t, int64(4), stat.EventBatchSize)
}
// Calculate the event rate per segment
numSegments := 5
width := len(stats) / numSegments
// Should have 50 events per segement as that's the outer loop rate
require.Equal(t, 50, width)
for i := 0; i < numSegments; i++ {
stat1 := stats[i*width]
stat2 := stats[(i+1)*width-1]
numEvents := stat2.CumulativeNumberOfEvents - stat1.CumulativeNumberOfEvents
deltaT := stat2.EventsEnd.Sub(stat1.ActualWakeupTime)
rate := float64(numEvents) / deltaT.Seconds()
diffPct := math.Abs((rate - looper.EventRate) / looper.EventRate * 100.0)
require.True(t, diffPct <= 15, "looper rate deviated more than 15%% from the expected (expected = %.1f Hz, actual = %.2f Hz, diff = %.2f%%)", looper.EventRate, rate, diffPct)
}
}
func TestLooperSignificantBackPressure(t *testing.T) {
// 5 seconds, 100 events, at a desired rate of 200 means it won't work.
numEvents := int64(100)
looper := &DiscretizedLooper{
EventRate: 200,
OuterLoopRate: 50,
LooperType: LooperTypeUniform,
}
ctx, cancel := context.WithCancel(context.Background())
// Don't ant to allocate in the loop, so we allocate plenty of space
stats := make([]OuterLoopStat, 0, int(looper.OuterLoopRate*float64(numEvents)/looper.EventRate*10))
looper.Event = func(context.Context) error {
// Want 5 seconds with 100 events means 20 events per seconds or 1/20
// seconds per event This means there's significant back pressure and the
// looper should switch to the busy loop style, which means the final loop
// rate should be close to 20Hz.
sleeper.Sleep(1.0 / 20.0 * 1000000)
return nil
}
looper.TraceOuterLoop = func(stat OuterLoopStat) {
stats = append(stats, stat)
if stat.CumulativeNumberOfEvents >= numEvents {
cancel()
}
}
wg := &sync.WaitGroup{}
wg.Add(1)
start := time.Now()
go func() {
defer wg.Done()
looper.Run(ctx)
}()
wg.Wait()
delta := time.Since(start).Microseconds()
expectedDelta := int64(5000000) // expected 5 seconds
diff := float64(expectedDelta - delta)
percent := math.Abs(diff / float64(expectedDelta) * 100.0)
// The threshold is arbitrarily chosen. Works on my computer.
require.True(t, percent <= 15, "expected total looper duration is more than 15%% from the actual duration (expected = %d us, actual = %d us, diff = %.2f%%).", expectedDelta, delta, percent)
lastStat := stats[len(stats)-1]
require.Equal(t, int64(100), lastStat.CumulativeNumberOfEvents)
for i, stat := range stats {
if i == 0 {
// Should always be executing only 1 batch, except the first one as the
// system kind of works out the timing.
require.Equal(t, int64(4), stat.EventBatchSize)
} else {
require.Equal(t, int64(1), stat.EventBatchSize)
// Check that sleep hasn't occured.
lastStat := stats[i-1]
delta := stat.ActualWakeupTime.Sub(lastStat.EventsEnd)
// Also just picked a random threshold value that worked for me
require.True(t, delta.Microseconds() < 500, "the loop shouldn't be sleeping at all, but seemed to have waited %d us", delta.Microseconds())
}
}
// Calculate the event rate per segment
numSegments := 5
width := len(stats) / numSegments
require.True(t, width >= 19)
require.True(t, width <= 21)
for i := 0; i < numSegments; i++ {
stat1 := stats[i*width]
stat2 := stats[(i+1)*width-1]
numEvents := stat2.CumulativeNumberOfEvents - stat1.CumulativeNumberOfEvents
deltaT := stat2.EventsEnd.Sub(stat1.ActualWakeupTime)
rate := float64(numEvents) / deltaT.Seconds()
expectedRate := 20.0
diffPct := math.Abs((rate - expectedRate) / expectedRate * 100.0)
if i == 0 {
continue // The first data point will be messed up due to how the looper assumes no back pressure to start.
}
require.True(t, diffPct <= 8, "looper rate deviated more than 8%% from the expected (expected = %.1f Hz, actual = %.2f Hz, diff = %.2f%%)", looper.EventRate, rate, diffPct)
}
}
func TestLooperVaryingEventDurationNoBackPressure(t *testing.T) {
// TODO
t.Skip()
}
func TestLooperVaryingEventDurationWithSometimesBackPressure(t *testing.T) {
// TODO
t.Skip()
}