goose 0.19.1

A load testing framework inspired by Locust.
Documentation
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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
//! Gzip: a report or log named `.gz` is written gzipped, `--compress` gzips
//! what Goose writes into the run's folder, and Goose reads a `.json.gz`
//! report wherever it reads a JSON report. Each test writes into its own
//! temporary folder.

use httpmock::{Method::GET, Mock, MockServer};
use serial_test::serial;
use std::io::Read;
use std::path::{Path, PathBuf};

mod common;

use goose::config::GooseConfiguration;
use goose::prelude::*;

pub async fn get_index(user: &mut GooseUser) -> TransactionResult {
    let _goose = user.get("/").await?;
    Ok(())
}

pub async fn get_missing(user: &mut GooseUser) -> TransactionResult {
    let _goose = user.get("/missing").await?;
    Ok(())
}

pub async fn get_index_then_cancel(user: &mut GooseUser) -> TransactionResult {
    let _goose = user.get("/").await?;
    goose::trigger_killswitch("compression test");
    Ok(())
}

fn setup_mock_server_endpoints(server: &MockServer) -> (Mock<'_>, Mock<'_>) {
    (
        server.mock(|when, then| {
            when.method(GET).path("/");
            then.status(200).body("ok");
        }),
        server.mock(|when, then| {
            when.method(GET).path("/missing");
            then.status(404);
        }),
    )
}

/// A new empty folder for one test.
fn temp_dir(name: &str) -> PathBuf {
    let dir = std::env::temp_dir().join(format!(
        "goose-compression-{name}-{}-{}",
        std::process::id(),
        std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .unwrap()
            .as_nanos()
    ));
    std::fs::create_dir_all(&dir).unwrap();
    dir
}

fn names(dir: &Path) -> Vec<String> {
    let mut names: Vec<String> = std::fs::read_dir(dir)
        .unwrap()
        .filter_map(Result::ok)
        .map(|entry| entry.file_name().to_string_lossy().into_owned())
        .collect();
    names.sort();
    names
}

/// The bytes of a gzip file, every member, checked as `gzip -t` checks it:
/// an error for a stream that is not gzip, is cut short or fails its CRC.
fn gunzip(path: &Path) -> std::io::Result<Vec<u8>> {
    let mut bytes = Vec::new();
    flate2::read::MultiGzDecoder::new(std::fs::File::open(path)?).read_to_end(&mut bytes)?;
    Ok(bytes)
}

/// The gzip header's XFL byte: 2 at level 9, 4 at level 1, 0 between.
fn xfl(path: &Path) -> u8 {
    let bytes = std::fs::read(path).unwrap();
    assert_eq!(&bytes[..2], [0x1f, 0x8b], "{:?} is not gzip", path);
    bytes[8]
}

/// `bytes` gzipped at `level` as Goose gzips a report: the same encoder, so
/// the same bytes, which pins the level beyond what XFL tells.
fn gzip_at(bytes: &[u8], level: u32) -> Vec<u8> {
    use std::io::Write;
    let mut gzip = flate2::write::GzEncoder::new(Vec::new(), flate2::Compression::new(level));
    gzip.write_all(bytes).unwrap();
    gzip.finish().unwrap()
}

fn scenario(transaction: Transaction) -> Scenario {
    scenario!("LoadTest").register_transaction(transaction)
}

async fn run(server: &MockServer, args: Vec<&str>) -> GooseMetrics {
    run_with(server, args, scenario(transaction!(get_index))).await
}

async fn run_with(server: &MockServer, args: Vec<&str>, scenario: Scenario) -> GooseMetrics {
    let configuration = common::build_configuration(server, args);
    common::run_load_test(
        common::build_load_test(configuration, vec![scenario], None, None),
        None,
    )
    .await
}

fn requests(metrics: &GooseMetrics) -> usize {
    metrics
        .requests
        .values()
        .map(|r| r.success_count + r.fail_count)
        .sum()
}

/// Rule 14: a report named `.gz` is the same report, gzipped.
#[tokio::test]
#[serial]
async fn a_gz_report_is_the_same_report_gzipped() {
    let server = MockServer::start();
    let _mocks = setup_mock_server_endpoints(&server);
    let dir = temp_dir("reports");
    let path = |name: &str| dir.join(name).to_string_lossy().into_owned();
    let mut args = Vec::new();
    for name in ["r.json", "r.html", "r.md"] {
        args.push(path(name));
        args.push(path(&format!("{name}.gz")));
    }
    let mut flags = Vec::new();
    for arg in &args {
        flags.push("--report-file");
        flags.push(arg.as_str());
    }
    run(&server, flags).await;
    for name in ["r.json", "r.html", "r.md"] {
        let plain = std::fs::read(dir.join(name)).unwrap();
        let gzipped = dir.join(format!("{name}.gz"));
        assert_eq!(gunzip(&gzipped).unwrap(), plain, "{}", name);
        assert_eq!(xfl(&gzipped), 0, "level 6 by default");
        assert_eq!(
            std::fs::read(&gzipped).unwrap(),
            gzip_at(&plain, 6),
            "{} at level 6 by default",
            name
        );
    }
    std::fs::remove_dir_all(&dir).ok();
}

/// Rule 14: a log named `.gz` is written gzipped, one line for each request
/// in CSV and in JSON, and every other log opens as gzip.
#[tokio::test]
#[serial]
async fn gz_logs_are_whole_gzip_files() {
    let server = MockServer::start();
    let _mocks = setup_mock_server_endpoints(&server);
    let dir = temp_dir("logs");
    for (format, header) in [("csv", 1), ("json", 0)] {
        let log = dir.join(format!("requests.{format}.gz"));
        let log_str = log.to_string_lossy().into_owned();
        let metrics = run(
            &server,
            vec![
                "-R",
                &log_str,
                "--request-format",
                format,
                "--no-reset-metrics",
            ],
        )
        .await;
        let text = String::from_utf8(gunzip(&log).unwrap()).unwrap();
        assert_eq!(
            text.lines().count(),
            requests(&metrics) + header,
            "{}",
            format
        );
    }
    let path = |name: &str| dir.join(name).to_string_lossy().into_owned();
    let (transactions, scenarios, errors, debug) = (
        path("transactions.log.gz"),
        path("scenarios.json.gz"),
        path("errors.csv.gz"),
        path("debug.json.gz"),
    );
    run_with(
        &server,
        vec![
            "-T",
            &transactions,
            "--transaction-format",
            "raw",
            "-S",
            &scenarios,
            "-E",
            &errors,
            "--error-format",
            "csv",
            "-D",
            &debug,
        ],
        scenario(transaction!(get_missing)),
    )
    .await;
    for log in [&transactions, &scenarios, &errors] {
        let text = String::from_utf8(gunzip(Path::new(log)).unwrap()).unwrap();
        assert!(!text.is_empty(), "{}", log);
    }
    // Nothing called log_debug, so the debug log is an empty gzip file.
    assert!(gunzip(Path::new(&debug)).unwrap().is_empty());
    std::fs::remove_dir_all(&dir).ok();
}

/// Rule 15: `--compress` gzips every file in the run's folder and leaves a
/// path as it is named; `--compress LEVEL` sets the level, also for a
/// path named `.gz`.
#[tokio::test]
#[serial]
async fn compress_gzips_the_run_folder() {
    let server = MockServer::start();
    let _mocks = setup_mock_server_endpoints(&server);
    let dir = temp_dir("compress");
    let folder = dir.join("run").to_string_lossy().into_owned();
    let plain = dir.join("plain.html").to_string_lossy().into_owned();
    let named = dir.join("named.json.gz").to_string_lossy().into_owned();
    let named_log = dir.join("errors.csv").to_string_lossy().into_owned();
    let metrics = run(
        &server,
        vec![
            "--output-dir",
            &folder,
            "--report-file",
            "--report-file",
            &plain,
            "--report-file",
            &named,
            "-R",
            "--request-format",
            "csv",
            "-T",
            "-E",
            &named_log,
            "--error-format",
            "csv",
            "--compress",
            "9",
            "--no-reset-metrics",
        ],
    )
    .await;
    let folder = dir.join("run");
    assert_eq!(
        names(&folder),
        vec![
            "report.html.gz",
            "report.json.gz",
            "requests.csv.gz",
            "transactions.json.gz",
        ]
    );
    for name in names(&folder) {
        assert_eq!(xfl(&folder.join(&name)), 2, "{}", name);
        gunzip(&folder.join(&name)).unwrap();
    }
    assert_eq!(xfl(Path::new(&named)), 2);
    let html = std::fs::read_to_string(&plain).unwrap();
    assert!(html.starts_with("<!DOCTYPE html>"), "a path keeps its name");
    // A log path keeps its name too, and is written plain.
    let errors = std::fs::read(&named_log).unwrap();
    assert!(errors.starts_with(b"elapsed"), "plain CSV");
    assert!(!dir.join("errors.csv.gz").exists());
    let requests_csv = String::from_utf8(gunzip(&folder.join("requests.csv.gz")).unwrap()).unwrap();
    assert_eq!(requests_csv.lines().count(), requests(&metrics) + 1);

    // A bare --compress is level 6, and level 1 is the fastest.
    for (level, expected) in [(None, 0), (Some("1"), 4)] {
        let folder = dir.join(format!("level-{expected}"));
        let folder_str = folder.to_string_lossy().into_owned();
        let mut args = vec![
            "--output-dir",
            folder_str.as_str(),
            "--report-file",
            "--compress",
        ];
        args.extend(level);
        run(&server, args).await;
        let gz = folder.join("report.json.gz");
        assert_eq!(xfl(&gz), expected);
        assert_eq!(
            std::fs::read(&gz).unwrap(),
            gzip_at(
                &gunzip(&gz).unwrap(),
                level.map_or(6, |l| l.parse().unwrap())
            ),
            "{:?}",
            level
        );
    }

    std::fs::remove_dir_all(&dir).ok();
}

/// Levels outside 1 to 9 are refused, naming the flag, from the command line
/// and from code.
#[tokio::test]
#[serial]
async fn compress_levels_outside_one_to_nine_are_refused() {
    for level in ["0", "10"] {
        match GooseConfiguration::parse_args_default(&["--compress", level]) {
            Err(GooseError::InvalidOption { option, .. }) => {
                assert!(option.starts_with("--compress"), "{}", option)
            }
            other => panic!("{}: {:?}", level, other.map(|_| ())),
        }
    }
    let server = MockServer::start();
    let configuration = common::build_configuration(&server, vec![]);
    for level in [0, 10] {
        let result = common::build_load_test(
            configuration.clone(),
            vec![scenario(transaction!(get_index))],
            None,
            None,
        )
        .set_default(GooseDefault::Compress, level)
        .unwrap()
        .execute()
        .await;
        match result {
            Err(GooseError::InvalidOption { option, .. }) => {
                assert_eq!(option, "`configuration.compress`")
            }
            other => panic!("{}: {:?}", level, other.map(|_| ())),
        }
    }
}

/// Rule 14: a `.json.gz` report, and a folder that holds only
/// `report.json.gz`, load as a baseline and with `--render-report`; a
/// truncated or non gzip `.gz` is an error naming the flag, never a panic.
#[tokio::test]
#[serial]
async fn a_gz_report_is_read_wherever_a_json_report_is() {
    let server = MockServer::start();
    let _mocks = setup_mock_server_endpoints(&server);
    let dir = temp_dir("read");
    let baseline = dir.join("baseline.json.gz").to_string_lossy().into_owned();
    let folder = dir.join("small").to_string_lossy().into_owned();
    run(&server, vec!["--report-file", &baseline]).await;
    run(
        &server,
        vec!["--output-dir", &folder, "--report-file", "--compress"],
    )
    .await;
    assert_eq!(
        names(&dir.join("small")),
        vec!["report.html.gz", "report.json.gz"]
    );

    for (from, name) in [(&baseline, "file"), (&folder, "folder")] {
        let md = dir
            .join(format!("against-{name}.md"))
            .to_string_lossy()
            .into_owned();
        run(&server, vec!["--baseline-file", from, "--report-file", &md]).await;
        let md = std::fs::read_to_string(&md).unwrap();
        assert!(md.contains("(+") || md.contains("(-"), "deltas: {}", md);

        let rendered = dir
            .join(format!("rendered-{name}.md"))
            .to_string_lossy()
            .into_owned();
        let configuration = GooseConfiguration::parse_args_default(&[
            "--render-report",
            &if name == "folder" {
                format!("{folder}/report.json.gz")
            } else {
                baseline.clone()
            },
            "--report-file",
            &rendered,
        ])
        .unwrap();
        GooseAttack::initialize_with_config(configuration)
            .unwrap()
            .register_scenario(scenario(transaction!(get_index)))
            .execute()
            .await
            .unwrap();
        assert!(std::fs::read_to_string(&rendered).unwrap().contains("GET"));
    }

    // Cut short, and not gzip at all.
    let whole = std::fs::read(&baseline).unwrap();
    let truncated = dir.join("truncated.json.gz");
    std::fs::write(&truncated, &whole[..whole.len() / 2]).unwrap();
    let not_gzip = dir.join("not-gzip.json.gz");
    std::fs::write(&not_gzip, b"{\"not\": \"gzip\"}").unwrap();
    for bad in [&truncated, &not_gzip] {
        let bad = bad.to_string_lossy().into_owned();
        let configuration = common::build_configuration(&server, vec!["--baseline-file", &bad]);
        match common::build_load_test(
            configuration,
            vec![scenario(transaction!(get_index))],
            None,
            None,
        )
        .execute()
        .await
        {
            Err(GooseError::InvalidOption { option, .. }) => {
                assert_eq!(option, "--baseline-file")
            }
            other => panic!("{}: {:?}", bad, other.map(|_| ())),
        }
    }
    std::fs::remove_dir_all(&dir).ok();
}

/// A run that ends early leaves whole gzip logs, with their trailers.
#[tokio::test]
#[serial]
async fn a_run_stopped_early_leaves_whole_gz_logs() {
    let server = MockServer::start();
    let _mocks = setup_mock_server_endpoints(&server);
    let dir = temp_dir("early");
    let folder = dir.join("run").to_string_lossy().into_owned();
    goose::reset_killswitch();
    run_with(
        &server,
        vec![
            "--output-dir",
            &folder,
            "-R",
            "-T",
            "--compress",
            "--run-time",
            "30",
        ],
        scenario(transaction!(get_index_then_cancel)),
    )
    .await;
    goose::reset_killswitch();
    let folder = dir.join("run");
    assert_eq!(
        names(&folder),
        vec!["requests.json.gz", "transactions.json.gz"]
    );
    for name in names(&folder) {
        let text = String::from_utf8(gunzip(&folder.join(&name)).unwrap()).unwrap();
        assert!(text.lines().count() >= 1, "{}", name);
    }
    std::fs::remove_dir_all(&dir).ok();
}