-
Notifications
You must be signed in to change notification settings - Fork 71
/
Copy pathscenarios.rs
312 lines (272 loc) · 10.5 KB
/
scenarios.rs
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
/// Validate that Goose only runs the selected Scenario filtered by --scenarios.
use httpmock::{Method::GET, Mock, MockServer};
use serial_test::serial;
mod common;
use goose::config::GooseConfiguration;
use goose::goose::GooseMethod;
use goose::prelude::*;
// Paths used in load tests performed during these tests.
const SCENARIOA1: &str = "/path/a/1";
const SCENARIOA2: &str = "/path/a/2";
const SCENARIOB1: &str = "/path/b/1";
const SCENARIOB2: &str = "/path/b/2";
// Indexes to the above paths.
const SCENARIOA1_KEY: usize = 0;
const SCENARIOA2_KEY: usize = 1;
const SCENARIOB1_KEY: usize = 2;
const SCENARIOB2_KEY: usize = 3;
// Load test configuration.
const EXPECT_WORKERS: usize = 2;
// There are multiple test variations in this file.
enum TestType {
// Limit scenarios with --scenarios option.
ScenariosOption,
// Limit scenarios with GooseDefault::Scenarios.
ScenariosDefault,
}
// Test transaction.
pub async fn get_scenarioa1(user: &mut GooseUser) -> TransactionResult {
let _goose = user.get(SCENARIOA1).await?;
Ok(())
}
// Test transaction.
pub async fn get_scenarioa2(user: &mut GooseUser) -> TransactionResult {
let _goose = user.get(SCENARIOA2).await?;
Ok(())
}
// Test transaction.
pub async fn get_scenariob1(user: &mut GooseUser) -> TransactionResult {
let _goose = user.get(SCENARIOB1).await?;
Ok(())
}
// Test transaction.
pub async fn get_scenariob2(user: &mut GooseUser) -> TransactionResult {
let _goose = user.get(SCENARIOB2).await?;
Ok(())
}
// All tests in this file run against a common endpoint.
fn setup_mock_server_endpoints(server: &MockServer) -> Vec<Mock> {
vec![
// SCENARIOA1 is stored in vector at SCENARIOA1_KEY.
server.mock(|when, then| {
when.method(GET).path(SCENARIOA1);
then.status(200);
}),
// SCENARIOA2 is stored in vector at SCENARIOA2_KEY.
server.mock(|when, then| {
when.method(GET).path(SCENARIOA2);
then.status(200);
}),
// SCENARIOB1 is stored in vector at SCENARIOB1_KEY.
server.mock(|when, then| {
when.method(GET).path(SCENARIOB1);
then.status(200);
}),
// SCENARIOB2 is stored in vector at SCENARIOB2_KEY.
server.mock(|when, then| {
when.method(GET).path(SCENARIOB2);
then.status(200);
}),
]
}
// Build appropriate configuration for these tests.
fn common_build_configuration(server: &MockServer, test_type: &TestType) -> GooseConfiguration {
// In all cases throttle requests to allow asserting metrics precisely.
let configuration = match test_type {
TestType::ScenariosOption => {
// Start 10 users in 1 second, then run for 1 more second.
vec![
"--users",
"10",
"--hatch-rate",
"10",
"--run-time",
"1",
"--no-reset-metrics",
// Only run Scenario A1 and Scenario A2
"--scenarios",
"scenarioa",
]
}
TestType::ScenariosDefault => {
// Start 10 users in 1 second, then run for 1 more second.
vec![
"--users",
"10",
"--hatch-rate",
"10",
"--run-time",
"1",
"--no-reset-metrics",
]
}
};
// Build the resulting configuration.
common::build_configuration(server, configuration)
}
// Helper to confirm all variations generate appropriate results.
fn validate_loadtest(
goose_metrics: &GooseMetrics,
mock_endpoints: &[Mock],
configuration: &GooseConfiguration,
test_type: TestType,
) {
assert!(goose_metrics.total_users == configuration.users.unwrap());
assert!(goose_metrics.maximum_users == 10);
assert!(goose_metrics.total_users == 10);
match test_type {
TestType::ScenariosOption => {
// Get scenarioa1 metrics.
let scenarioa1_metrics = goose_metrics
.requests
.get(&format!("GET {}", SCENARIOA1))
.unwrap();
// Confirm that the path and method are correct in the statistics.
assert!(scenarioa1_metrics.path == SCENARIOA1);
assert!(scenarioa1_metrics.method == GooseMethod::Get);
// There should not have been any failures during this test.
assert!(scenarioa1_metrics.fail_count == 0);
// Confirm Goose and the mock endpoint agree on the number of requests made.
assert!(mock_endpoints[SCENARIOA1_KEY].hits() <= scenarioa1_metrics.success_count);
// Get scenarioa2 metrics.
let scenarioa2_metrics = goose_metrics
.requests
.get(&format!("GET {}", SCENARIOA2))
.unwrap();
// Confirm that the path and method are correct in the statistics.
assert!(scenarioa2_metrics.path == SCENARIOA2);
assert!(scenarioa2_metrics.method == GooseMethod::Get);
// There should not have been any failures during this test.
assert!(scenarioa2_metrics.fail_count == 0);
// Confirm Goose and the mock endpoint agree on the number of requests made.
assert!(mock_endpoints[SCENARIOA2_KEY].hits() <= scenarioa2_metrics.success_count);
// scenariob1 and scenariob2 should not have been loaded due to `--scenarios scenarioa`.
assert!(mock_endpoints[SCENARIOB1_KEY].hits() == 0);
assert!(mock_endpoints[SCENARIOB2_KEY].hits() == 0);
}
TestType::ScenariosDefault => {
// scenarioa1 and scenarioa2 should not have been loaded due to `GooseDefault::Scenarios`.
assert!(mock_endpoints[SCENARIOA1_KEY].hits() == 0);
assert!(mock_endpoints[SCENARIOA2_KEY].hits() == 0);
assert!(mock_endpoints[SCENARIOB1_KEY].hits() > 0);
assert!(mock_endpoints[SCENARIOB2_KEY].hits() > 0);
}
}
}
// Returns the appropriate scenarios needed to build these tests.
fn get_scenarios() -> Vec<Scenario> {
vec![
scenario!("Scenario A1").register_transaction(transaction!(get_scenarioa1)),
scenario!("Scenario A2").register_transaction(transaction!(get_scenarioa2)),
scenario!("Scenario B1").register_transaction(transaction!(get_scenariob1)),
scenario!("Scenario B2").register_transaction(transaction!(get_scenariob2)),
]
}
// Helper to run all standalone tests.
async fn run_standalone_test(test_type: TestType) {
// Start the mock server.
let server = MockServer::start();
// Setup the endpoints needed for this test on the mock server.
let mock_endpoints = setup_mock_server_endpoints(&server);
// Build common configuration elements.
let configuration = common_build_configuration(&server, &test_type);
let mut goose = common::build_load_test(configuration.clone(), get_scenarios(), None, None);
// By default, only run scenarios starting with `scenariob`.
goose = *goose
.set_default(GooseDefault::Scenarios, "scenariob")
.unwrap();
// Run the Goose Attack.
let goose_metrics = common::run_load_test(goose, None).await;
// Confirm that the load test ran correctly.
validate_loadtest(&goose_metrics, &mock_endpoints, &configuration, test_type);
}
// Helper to run all standalone tests.
async fn run_gaggle_test(test_type: TestType) {
// Start the mock server.
let server = MockServer::start();
// Setup the endpoints needed for this test on the mock server.
let mock_endpoints = setup_mock_server_endpoints(&server);
// Each worker has the same identical configuration.
let worker_configuration = common::build_configuration(&server, vec!["--worker"]);
// Workers launched in own threads, store thread handles.
let worker_handles = common::launch_gaggle_workers(EXPECT_WORKERS, || {
common::build_load_test(worker_configuration.clone(), get_scenarios(), None, None)
});
// Build common configuration elements, adding Manager Gaggle flags.
let manager_configuration = match test_type {
TestType::ScenariosOption => common::build_configuration(
&server,
vec![
"--manager",
"--expect-workers",
&EXPECT_WORKERS.to_string(),
"--no-reset-metrics",
"--users",
"10",
"--hatch-rate",
"10",
"--run-time",
"1",
"--scenarios",
"scenarioa",
],
),
TestType::ScenariosDefault => common::build_configuration(
&server,
vec![
"--manager",
"--expect-workers",
&EXPECT_WORKERS.to_string(),
"--no-reset-metrics",
"--users",
"10",
"--hatch-rate",
"10",
"--run-time",
"1",
],
),
};
// Build the load test for the Manager.
let mut manager_goose_attack =
common::build_load_test(manager_configuration.clone(), get_scenarios(), None, None);
// By default, only run scenarios starting with `scenariob`.
manager_goose_attack = *manager_goose_attack
.set_default(GooseDefault::Scenarios, "scenariob")
.unwrap();
// Run the Goose Attack.
let goose_metrics = common::run_load_test(manager_goose_attack, Some(worker_handles)).await;
// Confirm that the load test ran correctly.
validate_loadtest(
&goose_metrics,
&mock_endpoints,
&manager_configuration,
test_type,
);
}
/* With `--scenarios` */
#[tokio::test]
// Run only half the configured scenarios.
async fn test_scenarios_option() {
run_standalone_test(TestType::ScenariosOption).await;
}
#[ignore]
#[tokio::test(flavor = "multi_thread", worker_threads = 8)]
#[serial]
// Run only half the configured scenarios, in Gaggle mode.
async fn test_scenarios_option_gaggle() {
run_gaggle_test(TestType::ScenariosOption).await;
}
/* With `GooseDefault::Scenarios` */
#[tokio::test]
// Run only half the configured scenarios.
async fn test_scenarios_default() {
run_standalone_test(TestType::ScenariosDefault).await;
}
#[ignore]
#[tokio::test(flavor = "multi_thread", worker_threads = 8)]
#[serial]
// Run only half the configured scenarios, in Gaggle mode.
async fn test_scenarios_default_gaggle() {
run_gaggle_test(TestType::ScenariosDefault).await;
}