-
Notifications
You must be signed in to change notification settings - Fork 91
Expand file tree
/
Copy pathsequencer_test.cc
More file actions
311 lines (268 loc) · 12.8 KB
/
sequencer_test.cc
File metadata and controls
311 lines (268 loc) · 12.8 KB
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
#include <chrono>
#include <memory>
#include "nighthawk/common/exception.h"
#include "nighthawk/common/platform_util.h"
#include "external/envoy/source/common/event/dispatcher_impl.h"
#include "external/envoy/source/common/stats/isolated_store_impl.h"
#include "external/envoy/test/mocks/event/mocks.h"
#include "external/envoy/test/test_common/simulated_time_system.h"
#include "source/common/rate_limiter_impl.h"
#include "source/common/sequencer_impl.h"
#include "source/common/statistic_impl.h"
#include "test/mocks/common/mock_platform_util.h"
#include "test/mocks/common/mock_rate_limiter.h"
#include "test/mocks/common/mock_termination_predicate.h"
#include "gtest/gtest.h"
using namespace std::chrono_literals;
using namespace nighthawk::client;
using namespace testing;
namespace Nighthawk {
class FakeSequencerTarget {
public:
virtual ~FakeSequencerTarget() = default;
// A fake method that matches the sequencer target signature.
virtual bool callback(OperationCallback) PURE;
};
class MockSequencerTarget : public FakeSequencerTarget {
public:
MOCK_METHOD(bool, callback, (OperationCallback), (override));
};
class SequencerTestBase : public testing::Test {
public:
SequencerTestBase()
: dispatcher_(std::make_unique<Envoy::Event::MockDispatcher>()), frequency_(10_Hz),
interval_(std::chrono::duration_cast<std::chrono::milliseconds>(frequency_.interval())),
sequencer_target_(
std::bind(&SequencerTestBase::callback_test, this, std::placeholders::_1)) {}
bool callback_test(const OperationCallback& f) {
callback_test_count_++;
f(true, true);
return true;
}
MockPlatformUtil platform_util_;
Envoy::Stats::IsolatedStoreImpl store_;
Envoy::Stats::Scope& scope_{*store_.rootScope()};
Envoy::Event::SimulatedTimeSystem time_system_;
std::unique_ptr<Envoy::Event::MockDispatcher> dispatcher_;
int callback_test_count_{0};
const Frequency frequency_;
const std::chrono::milliseconds interval_;
const uint64_t test_number_of_intervals_{5};
SequencerTarget sequencer_target_;
};
class SequencerTest : public SequencerTestBase {
public:
SequencerTest()
: rate_limiter_(std::make_unique<MockRateLimiter>()),
rate_limiter_unsafe_ref_(*rate_limiter_) {}
std::unique_ptr<MockRateLimiter> rate_limiter_;
// The sequencers that the tests construct will take ownership of rate_limiter_, we keep a
// reference, which will become invalid once the sequencer has been destructed.
MockRateLimiter& rate_limiter_unsafe_ref_;
};
class SequencerTestWithTimerEmulation : public SequencerTest {
public:
SequencerTestWithTimerEmulation() { setupDispatcherTimerEmulation(); }
// the Sequencer implementation is effectively driven by two timers. We set us up for emulating
// those timers firing and moving simulated time forward in simulateTimerloop() below.
void setupDispatcherTimerEmulation() {
timer1_ = new NiceMock<Envoy::Event::MockTimer>();
timer2_ = new NiceMock<Envoy::Event::MockTimer>();
EXPECT_CALL(*dispatcher_, createTimer_(_))
.WillOnce(Invoke([&](Envoy::Event::TimerCb cb) {
timer_cb_1_ = std::move(cb);
return timer1_;
}))
.WillOnce(Invoke([&](Envoy::Event::TimerCb cb) {
timer_cb_2_ = std::move(cb);
return timer2_;
}));
EXPECT_CALL(*timer1_, disableTimer()).WillOnce(Invoke([&]() { timer1_set_ = false; }));
EXPECT_CALL(*timer2_, disableTimer()).WillOnce(Invoke([&]() { timer2_set_ = false; }));
EXPECT_CALL(*timer1_, enableHRTimer(_, _))
.WillRepeatedly(Invoke([&](const std::chrono::microseconds,
const Envoy::ScopeTrackedObject*) { timer1_set_ = true; }));
EXPECT_CALL(*timer2_, enableHRTimer(_, _))
.WillRepeatedly(Invoke([&](const std::chrono::microseconds,
const Envoy::ScopeTrackedObject*) { timer2_set_ = true; }));
EXPECT_CALL(*dispatcher_, exit()).WillOnce(Invoke([&]() { stopped_ = true; }));
EXPECT_CALL(*dispatcher_, updateApproximateMonotonicTime()).Times(AtLeast(1));
simulation_start_ = time_system_.monotonicTime();
auto* unsafe_mock_termination_predicate = new MockTerminationPredicate();
termination_predicate_ =
std::unique_ptr<MockTerminationPredicate>(unsafe_mock_termination_predicate);
EXPECT_CALL(*unsafe_mock_termination_predicate, evaluateChain())
.WillRepeatedly(Invoke([this]() {
return (time_system_.monotonicTime() - simulation_start_) <=
(test_number_of_intervals_ * interval_)
? TerminationPredicate::Status::PROCEED
: TerminationPredicate::Status::TERMINATE;
}));
}
void expectDispatcherRun() {
EXPECT_CALL(*dispatcher_, run(_))
.WillOnce(Invoke([&](Envoy::Event::DispatcherImpl::RunType type) {
ASSERT_EQ(Envoy::Event::DispatcherImpl::RunType::RunUntilExit, type);
simulateTimerLoop();
}));
}
// Moves time forward 1ms, and runs the ballbacks of set timers.
void simulateTimerLoop() {
while (!stopped_) {
time_system_.setMonotonicTime(time_system_.monotonicTime() + NighthawkTimerResolution);
// TODO(oschaaf): This can be implemented more accurately, by keeping track of timer
// enablement preserving ordering of which timer should fire first. For now this seems to
// suffice for the tests that we have in here.
if (timer1_set_) {
timer1_set_ = false;
timer_cb_1_();
}
if (timer2_set_) {
timer2_set_ = false;
timer_cb_2_();
}
}
}
MockSequencerTarget* target() { return &target_; }
TerminationPredicatePtr termination_predicate_;
protected:
Envoy::MonotonicTime simulation_start_;
private:
NiceMock<Envoy::Event::MockTimer>* timer1_; // not owned
NiceMock<Envoy::Event::MockTimer>* timer2_; // not owned
Envoy::Event::TimerCb timer_cb_1_;
Envoy::Event::TimerCb timer_cb_2_;
MockSequencerTarget target_;
bool timer1_set_{};
bool timer2_set_{};
bool stopped_{};
};
// Basic rate limiter interaction test.
TEST_F(SequencerTestWithTimerEmulation, RateLimiterInteraction) {
SequencerTarget callback =
std::bind(&MockSequencerTarget::callback, target(), std::placeholders::_1);
SequencerImpl sequencer(platform_util_, *dispatcher_, time_system_, std::move(rate_limiter_),
callback, std::make_unique<StreamingStatistic>(),
std::make_unique<StreamingStatistic>(), SequencerIdleStrategy::SLEEP,
std::move(termination_predicate_), scope_);
// Have the mock rate limiter gate two calls, and block everything else.
EXPECT_CALL(rate_limiter_unsafe_ref_, tryAcquireOne())
.Times(AtLeast(3))
.WillOnce(Return(true))
.WillOnce(Return(true))
.WillRepeatedly(Return(false));
EXPECT_CALL(rate_limiter_unsafe_ref_, elapsed()).Times(2);
EXPECT_CALL(*target(), callback(_)).Times(2).WillOnce(Return(true)).WillOnce(Return(true));
expectDispatcherRun();
EXPECT_CALL(platform_util_, sleep(_)).Times(AtLeast(1));
sequencer.start();
sequencer.waitForCompletion();
}
// Saturated rate limiter interaction test.
TEST_F(SequencerTestWithTimerEmulation, RateLimiterSaturatedTargetInteraction) {
SequencerTarget callback =
std::bind(&MockSequencerTarget::callback, target(), std::placeholders::_1);
SequencerImpl sequencer(platform_util_, *dispatcher_, time_system_, std::move(rate_limiter_),
callback, std::make_unique<StreamingStatistic>(),
std::make_unique<StreamingStatistic>(), SequencerIdleStrategy::SLEEP,
std::move(termination_predicate_), scope_);
EXPECT_CALL(rate_limiter_unsafe_ref_, tryAcquireOne())
.Times(AtLeast(3))
.WillOnce(Return(true))
.WillOnce(Return(true))
.WillRepeatedly(Return(false));
EXPECT_CALL(rate_limiter_unsafe_ref_, elapsed()).Times(2);
EXPECT_CALL(*target(), callback(_)).Times(2).WillOnce(Return(true)).WillOnce(Return(false));
// The sequencer should call RateLimiter::releaseOne() when the target returns false.
EXPECT_CALL(rate_limiter_unsafe_ref_, releaseOne());
expectDispatcherRun();
EXPECT_CALL(platform_util_, sleep(_)).Times(AtLeast(1));
sequencer.start();
sequencer.waitForCompletion();
}
// The integration tests use a LinearRateLimiter.
class SequencerIntegrationTest : public SequencerTestWithTimerEmulation {
public:
SequencerIntegrationTest() {
Envoy::Event::SimulatedTimeSystem time_system;
rate_limiter_ = std::make_unique<LinearRateLimiter>(time_system_, frequency_);
expectDispatcherRun();
}
bool timeout_test(const std::function<void(bool, bool)>& /* f */) {
callback_test_count_++;
// We don't call f(); which will cause the sequencer to think there is in-flight work.
return true;
}
bool saturated_test(const std::function<void(bool, bool)>& /* f */) { return false; }
std::unique_ptr<LinearRateLimiter> rate_limiter_;
void testRegularFlow(SequencerIdleStrategy::SequencerIdleStrategyOptions idle_strategy) {
SequencerImpl sequencer(platform_util_, *dispatcher_, time_system_, std::move(rate_limiter_),
sequencer_target_, std::make_unique<StreamingStatistic>(),
std::make_unique<StreamingStatistic>(), idle_strategy,
std::move(termination_predicate_), scope_);
EXPECT_EQ(0, callback_test_count_);
EXPECT_EQ(0, sequencer.latencyStatistic().count());
sequencer.start();
sequencer.waitForCompletion();
EXPECT_EQ(test_number_of_intervals_, callback_test_count_);
EXPECT_EQ(test_number_of_intervals_, sequencer.latencyStatistic().count());
EXPECT_EQ(0, sequencer.blockedStatistic().count());
EXPECT_EQ(2, sequencer.statistics().size());
const auto execution_duration = time_system_.monotonicTime() - simulation_start_;
EXPECT_EQ(sequencer.executionDuration(), execution_duration);
}
};
TEST_F(SequencerIntegrationTest, IdleStrategySpin) {
EXPECT_CALL(platform_util_, yieldCurrentThread()).Times(AtLeast(1));
EXPECT_CALL(platform_util_, sleep(_)).Times(0);
testRegularFlow(SequencerIdleStrategy::SPIN);
}
TEST_F(SequencerIntegrationTest, IdleStrategyPoll) {
EXPECT_CALL(platform_util_, yieldCurrentThread()).Times(0);
EXPECT_CALL(platform_util_, sleep(_)).Times(0);
testRegularFlow(SequencerIdleStrategy::POLL);
}
TEST_F(SequencerIntegrationTest, IdleStrategySleep) {
EXPECT_CALL(platform_util_, yieldCurrentThread()).Times(0);
EXPECT_CALL(platform_util_, sleep(_)).Times(AtLeast(1));
testRegularFlow(SequencerIdleStrategy::SLEEP);
}
// Test an always saturated sequencer target. A concrete example would be a http benchmark client
// not being able to start any requests, for example due to misconfiguration or system conditions.
TEST_F(SequencerIntegrationTest, AlwaysSaturatedTargetTest) {
SequencerTarget callback =
std::bind(&SequencerIntegrationTest::saturated_test, this, std::placeholders::_1);
SequencerImpl sequencer(platform_util_, *dispatcher_, time_system_, std::move(rate_limiter_),
callback, std::make_unique<StreamingStatistic>(),
std::make_unique<StreamingStatistic>(), SequencerIdleStrategy::SLEEP,
std::move(termination_predicate_), scope_);
EXPECT_CALL(platform_util_, sleep(_)).Times(AtLeast(1));
sequencer.start();
sequencer.waitForCompletion();
EXPECT_EQ(0, sequencer.latencyStatistic().count());
EXPECT_EQ(1, sequencer.blockedStatistic().count());
}
// (SequencerIntegrationTest::timeout_test()) will never call back, effectively simulated a
// stalled benchmark client. Implicitly we test that we get past sequencer.waitForCompletion()
// timely, and don't hang.
TEST_F(SequencerIntegrationTest, CallbacksDoNotInfluenceTestDuration) {
SequencerTarget callback =
std::bind(&SequencerIntegrationTest::timeout_test, this, std::placeholders::_1);
SequencerImpl sequencer(platform_util_, *dispatcher_, time_system_, std::move(rate_limiter_),
callback, std::make_unique<StreamingStatistic>(),
std::make_unique<StreamingStatistic>(), SequencerIdleStrategy::SLEEP,
std::move(termination_predicate_), scope_);
EXPECT_CALL(platform_util_, sleep(_)).Times(AtLeast(1));
auto pre_timeout = time_system_.monotonicTime();
sequencer.start();
sequencer.waitForCompletion();
auto diff = time_system_.monotonicTime() - pre_timeout;
auto expected_duration = (test_number_of_intervals_ * interval_) + NighthawkTimerResolution;
EXPECT_EQ(expected_duration, diff);
// the test itself should have seen all callbacks...
EXPECT_EQ(5, callback_test_count_);
// ... but they ought to have not arrived at the Sequencer.
EXPECT_EQ(0, sequencer.latencyStatistic().count());
EXPECT_EQ(0, sequencer.blockedStatistic().count());
}
} // namespace Nighthawk