goose 0.7.4

A load testing tool 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
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
use num_format::{Locale, ToFormattedString};
use std::collections::{BTreeMap, HashMap};
use std::f32;

use crate::goose::GooseRequest;
use crate::{util, GooseAttack};

/// A helper function that merges together response times.
///
/// Used in `lib.rs` to merge together per-thread response times, and in `stats.rs`
/// to aggregate all response times.
pub fn merge_response_times(
    mut global_response_times: BTreeMap<usize, usize>,
    local_response_times: BTreeMap<usize, usize>,
) -> BTreeMap<usize, usize> {
    // Iterate over client response times, and merge into global response times.
    for (response_time, count) in &local_response_times {
        let counter = match global_response_times.get(&response_time) {
            // We've seen this response_time before, increment counter.
            Some(c) => *c + count,
            // First time we've seen this response time, initialize counter.
            None => *count,
        };
        global_response_times.insert(*response_time, counter);
    }
    global_response_times
}

// Update global minimum response time based on local resposne time.
pub fn update_min_response_time(mut global_min: usize, min: usize) -> usize {
    if global_min == 0 || (min > 0 && min < global_min) {
        global_min = min;
    }
    global_min
}

// Update global maximum response time based on local resposne time.
pub fn update_max_response_time(mut global_max: usize, max: usize) -> usize {
    if global_max < max {
        global_max = max;
    }
    global_max
}

/// Get the response time that a certain number of percent of the requests finished within.
fn calculate_response_time_percentile(
    response_times: &BTreeMap<usize, usize>,
    total_requests: usize,
    min: usize,
    max: usize,
    percent: f32,
) -> usize {
    let percentile_request = (total_requests as f32 * percent).round() as usize;
    debug!(
        "percentile: {}, request {} of total {}",
        percent, percentile_request, total_requests
    );

    let mut total_count: usize = 0;

    for (value, counter) in response_times {
        total_count += counter;
        if total_count >= percentile_request {
            if *value < min {
                return min;
            } else if *value > max {
                return max;
            } else {
                return *value;
            }
        }
    }
    0
}

/// Display a table of requests and fails.
pub fn print_requests_and_fails(requests: &HashMap<String, GooseRequest>, elapsed: usize) {
    debug!("entering print_requests_and_fails");
    // Display stats from merged HashMap
    println!("------------------------------------------------------------------------------ ");
    println!(
        " {:<23} | {:<14} | {:<14} | {:<6} | {:<5}",
        "Name", "# reqs", "# fails", "req/s", "fail/s"
    );
    println!(" ----------------------------------------------------------------------------- ");
    let mut aggregate_fail_count = 0;
    let mut aggregate_total_count = 0;
    for (request_key, request) in requests {
        let total_count = request.success_count + request.fail_count;
        let fail_percent = if request.fail_count > 0 {
            request.fail_count as f32 / total_count as f32 * 100.0
        } else {
            0.0
        };
        // Compress 100.0 and 0.0 to 100 and 0 respectively to save width.
        if fail_percent as usize == 100 || fail_percent as usize == 0 {
            println!(
                " {:<23} | {:<14} | {:<14} | {:<6} | {:<5}",
                util::truncate_string(&request_key, 23),
                total_count.to_formatted_string(&Locale::en),
                format!(
                    "{} ({}%)",
                    request.fail_count.to_formatted_string(&Locale::en),
                    fail_percent as usize
                ),
                (total_count / elapsed).to_formatted_string(&Locale::en),
                (request.fail_count / elapsed).to_formatted_string(&Locale::en),
            );
        } else {
            println!(
                " {:<23} | {:<14} | {:<14} | {:<6} | {:<5}",
                util::truncate_string(&request_key, 23),
                total_count.to_formatted_string(&Locale::en),
                format!(
                    "{} ({:.1}%)",
                    request.fail_count.to_formatted_string(&Locale::en),
                    fail_percent
                ),
                (total_count / elapsed).to_formatted_string(&Locale::en),
                (request.fail_count / elapsed).to_formatted_string(&Locale::en),
            );
        }
        aggregate_total_count += total_count;
        aggregate_fail_count += request.fail_count;
    }
    if requests.len() > 1 {
        let aggregate_fail_percent = if aggregate_fail_count > 0 {
            aggregate_fail_count as f32 / aggregate_total_count as f32 * 100.0
        } else {
            0.0
        };
        println!(" ------------------------+----------------+----------------+--------+--------- ");
        // Compress 100.0 and 0.0 to 100 and 0 respectively to save width.
        if aggregate_fail_percent as usize == 100 || aggregate_fail_percent as usize == 0 {
            println!(
                " {:<23} | {:<14} | {:<14} | {:<6} | {:<5}",
                "Aggregated",
                aggregate_total_count.to_formatted_string(&Locale::en),
                format!(
                    "{} ({}%)",
                    aggregate_fail_count.to_formatted_string(&Locale::en),
                    aggregate_fail_percent as usize
                ),
                (aggregate_total_count / elapsed).to_formatted_string(&Locale::en),
                (aggregate_fail_count / elapsed).to_formatted_string(&Locale::en),
            );
        } else {
            println!(
                " {:<23} | {:<14} | {:<14} | {:<6} | {:<5}",
                "Aggregated",
                aggregate_total_count.to_formatted_string(&Locale::en),
                format!(
                    "{} ({:.1}%)",
                    aggregate_fail_count.to_formatted_string(&Locale::en),
                    aggregate_fail_percent
                ),
                (aggregate_total_count / elapsed).to_formatted_string(&Locale::en),
                (aggregate_fail_count / elapsed).to_formatted_string(&Locale::en),
            );
        }
    }
}

fn print_response_times(requests: &HashMap<String, GooseRequest>, display_percentiles: bool) {
    debug!("entering print_response_times");
    let mut aggregate_response_times: BTreeMap<usize, usize> = BTreeMap::new();
    let mut aggregate_total_response_time: usize = 0;
    let mut aggregate_response_time_counter: usize = 0;
    let mut aggregate_min_response_time: usize = 0;
    let mut aggregate_max_response_time: usize = 0;
    println!("-------------------------------------------------------------------------------");
    println!(
        " {:<23} | {:<10} | {:<10} | {:<10} | {:<10}",
        "Name", "Avg (ms)", "Min", "Max", "Median"
    );
    println!(" ----------------------------------------------------------------------------- ");
    for (request_key, request) in requests.clone() {
        // Iterate over client response times, and merge into global response times.
        aggregate_response_times =
            merge_response_times(aggregate_response_times, request.response_times.clone());

        // Increment total response time counter.
        aggregate_total_response_time += &request.total_response_time;

        // Increment counter tracking individual response times seen.
        aggregate_response_time_counter += &request.response_time_counter;

        // If client had new fastest response time, update global fastest response time.
        aggregate_min_response_time =
            update_min_response_time(aggregate_min_response_time, request.min_response_time);

        // If client had new slowest response time, update global slowest resposne time.
        aggregate_max_response_time =
            update_max_response_time(aggregate_max_response_time, request.max_response_time);

        println!(
            " {:<23} | {:<10.2} | {:<10.2} | {:<10.2} | {:<10.2}",
            util::truncate_string(&request_key, 23),
            request.total_response_time / request.response_time_counter,
            request.min_response_time,
            request.max_response_time,
            util::median(
                &request.response_times,
                request.response_time_counter,
                request.min_response_time,
                request.max_response_time
            ),
        );
    }
    if requests.len() > 1 {
        println!(" ------------------------+------------+------------+------------+------------- ");
        if aggregate_response_time_counter == 0 {
            aggregate_response_time_counter = 1;
        }
        println!(
            " {:<23} | {:<10.2} | {:<10.2} | {:<10.2} | {:<10.2}",
            "Aggregated",
            aggregate_total_response_time / aggregate_response_time_counter,
            aggregate_min_response_time,
            aggregate_max_response_time,
            util::median(
                &aggregate_response_times,
                aggregate_response_time_counter,
                aggregate_min_response_time,
                aggregate_max_response_time
            ),
        );
    }

    if display_percentiles {
        println!("-------------------------------------------------------------------------------");
        println!(" Slowest page load within specified percentile of requests (in ms):");
        println!(" ------------------------------------------------------------------------------");
        println!(
            " {:<23} | {:<6} | {:<6} | {:<6} | {:<6} | {:<6} | {:6}",
            "Name", "50%", "75%", "98%", "99%", "99.9%", "99.99%"
        );
        println!(" ----------------------------------------------------------------------------- ");
        for (request_key, request) in requests {
            // Sort response times so we can calculate a mean.
            println!(
                " {:<23} | {:<6.2} | {:<6.2} | {:<6.2} | {:<6.2} | {:<6.2} | {:6.2}",
                util::truncate_string(&request_key, 23),
                calculate_response_time_percentile(
                    &request.response_times,
                    request.response_time_counter,
                    request.min_response_time,
                    request.max_response_time,
                    0.5
                ),
                calculate_response_time_percentile(
                    &request.response_times,
                    request.response_time_counter,
                    request.min_response_time,
                    request.max_response_time,
                    0.75
                ),
                calculate_response_time_percentile(
                    &request.response_times,
                    request.response_time_counter,
                    request.min_response_time,
                    request.max_response_time,
                    0.98
                ),
                calculate_response_time_percentile(
                    &request.response_times,
                    request.response_time_counter,
                    request.min_response_time,
                    request.max_response_time,
                    0.99
                ),
                calculate_response_time_percentile(
                    &request.response_times,
                    request.response_time_counter,
                    request.min_response_time,
                    request.max_response_time,
                    0.999
                ),
                calculate_response_time_percentile(
                    &request.response_times,
                    request.response_time_counter,
                    request.min_response_time,
                    request.max_response_time,
                    0.999
                ),
            );
        }
        if requests.len() > 1 {
            println!(
                " ------------------------+--------+--------+--------+--------+--------+------- "
            );
            println!(
                " {:<23} | {:<6.2} | {:<6.2} | {:<6.2} | {:<6.2} | {:<6.2} | {:6.2}",
                "Aggregated",
                calculate_response_time_percentile(
                    &aggregate_response_times,
                    aggregate_response_time_counter,
                    aggregate_min_response_time,
                    aggregate_max_response_time,
                    0.5
                ),
                calculate_response_time_percentile(
                    &aggregate_response_times,
                    aggregate_response_time_counter,
                    aggregate_min_response_time,
                    aggregate_max_response_time,
                    0.75
                ),
                calculate_response_time_percentile(
                    &aggregate_response_times,
                    aggregate_response_time_counter,
                    aggregate_min_response_time,
                    aggregate_max_response_time,
                    0.98
                ),
                calculate_response_time_percentile(
                    &aggregate_response_times,
                    aggregate_response_time_counter,
                    aggregate_min_response_time,
                    aggregate_max_response_time,
                    0.99
                ),
                calculate_response_time_percentile(
                    &aggregate_response_times,
                    aggregate_response_time_counter,
                    aggregate_min_response_time,
                    aggregate_max_response_time,
                    0.999
                ),
                calculate_response_time_percentile(
                    &aggregate_response_times,
                    aggregate_response_time_counter,
                    aggregate_min_response_time,
                    aggregate_max_response_time,
                    0.9999
                ),
            );
        }
    }
}

fn print_status_codes(requests: &HashMap<String, GooseRequest>) {
    debug!("entering print_status_codes");
    println!("-------------------------------------------------------------------------------");
    println!(" {:<23} | {:<25} ", "Name", "Status codes");
    println!(" ----------------------------------------------------------------------------- ");
    let mut aggregated_status_code_counts: HashMap<u16, usize> = HashMap::new();
    for (request_key, request) in requests {
        let mut codes: String = "".to_string();
        for (status_code, count) in &request.status_code_counts {
            if codes.is_empty() {
                codes = format!(
                    "{} [{}]",
                    count.to_formatted_string(&Locale::en),
                    status_code
                );
            } else {
                codes = format!(
                    "{}, {} [{}]",
                    codes.clone(),
                    count.to_formatted_string(&Locale::en),
                    status_code
                );
            }
            let new_count;
            if let Some(existing_status_code_count) =
                aggregated_status_code_counts.get(&status_code)
            {
                new_count = *existing_status_code_count + *count;
            } else {
                new_count = *count;
            }
            aggregated_status_code_counts.insert(*status_code, new_count);
        }
        println!(
            " {:<23} | {:<25}",
            util::truncate_string(&request_key, 23),
            codes,
        );
    }
    println!("-------------------------------------------------------------------------------");
    let mut codes: String = "".to_string();
    for (status_code, count) in &aggregated_status_code_counts {
        if codes.is_empty() {
            codes = format!(
                "{} [{}]",
                count.to_formatted_string(&Locale::en),
                status_code
            );
        } else {
            codes = format!(
                "{}, {} [{}]",
                codes.clone(),
                count.to_formatted_string(&Locale::en),
                status_code
            );
        }
    }
    println!(" {:<23} | {:<25} ", "Aggregated", codes);
}

/// Display running and ending statistics
pub fn print_final_stats(goose_attack: &GooseAttack, elapsed: usize) {
    if !goose_attack.configuration.worker {
        info!("printing final statistics after {} seconds...", elapsed);
        // 1) print request and fail statistics.
        print_requests_and_fails(&goose_attack.merged_requests, elapsed);
        // 2) print respones time statistics, with percentiles
        print_response_times(&goose_attack.merged_requests, true);
        // 3) print status_codes
        if goose_attack.configuration.status_codes {
            print_status_codes(&goose_attack.merged_requests);
        }
    }
}

pub fn print_running_stats(goose_attack: &GooseAttack, elapsed: usize) {
    if !goose_attack.configuration.worker && !goose_attack.merged_requests.is_empty() {
        info!("printing running statistics after {} seconds...", elapsed);
        // 1) print request and fail statistics.
        print_requests_and_fails(&goose_attack.merged_requests, elapsed);
        // 2) print respones time statistics, without percentiles
        print_response_times(&goose_attack.merged_requests, false);
        println!();
    }
}

#[cfg(test)]
mod test {
    use super::*;

    #[test]
    fn max_response_time() {
        let mut max_response_time = 99;
        // Update max response time to a higher value.
        max_response_time = update_max_response_time(max_response_time, 101);
        assert_eq!(max_response_time, 101);
        // Max response time doesn't update when updating with a lower value.
        max_response_time = update_max_response_time(max_response_time, 1);
        assert_eq!(max_response_time, 101);
    }

    #[test]
    fn min_response_time() {
        let mut min_response_time = 11;
        // Update min response time to a lower value.
        min_response_time = update_min_response_time(min_response_time, 9);
        assert_eq!(min_response_time, 9);
        // Min response time doesn't update when updating with a lower value.
        min_response_time = update_min_response_time(min_response_time, 22);
        assert_eq!(min_response_time, 9);
        // Min response time doesn't update when updating with a 0 value.
        min_response_time = update_min_response_time(min_response_time, 0);
        assert_eq!(min_response_time, 9);
    }

    #[test]
    fn response_time_merge() {
        let mut global_response_times: BTreeMap<usize, usize> = BTreeMap::new();
        let local_response_times: BTreeMap<usize, usize> = BTreeMap::new();
        global_response_times =
            merge_response_times(global_response_times, local_response_times.clone());
        // @TODO: how can we do useful testing of private method and objects?
        assert_eq!(&global_response_times, &local_response_times);
    }

    #[test]
    fn max_response_time_percentile() {
        let mut response_times: BTreeMap<usize, usize> = BTreeMap::new();
        response_times.insert(1, 1);
        response_times.insert(2, 1);
        response_times.insert(3, 1);
        // 3 * .5 = 1.5, rounds to 2.
        assert_eq!(
            calculate_response_time_percentile(&response_times, 3, 1, 3, 0.5),
            2
        );
        response_times.insert(3, 2);
        // 4 * .5 = 2
        assert_eq!(
            calculate_response_time_percentile(&response_times, 4, 1, 3, 0.5),
            2
        );
        // 4 * .25 = 1
        assert_eq!(
            calculate_response_time_percentile(&response_times, 4, 1, 3, 0.25),
            1
        );
        // 4 * .75 = 3
        assert_eq!(
            calculate_response_time_percentile(&response_times, 4, 1, 3, 0.75),
            3
        );
        // 4 * 1 = 4 (and the 4th response time is also 3)
        assert_eq!(
            calculate_response_time_percentile(&response_times, 4, 1, 3, 1.0),
            3
        );

        // 4 * .5 = 2, but uses specified minimum of 2
        assert_eq!(
            calculate_response_time_percentile(&response_times, 4, 2, 3, 0.25),
            2
        );
        // 4 * .75 = 3, but uses specified maximum of 2
        assert_eq!(
            calculate_response_time_percentile(&response_times, 4, 1, 2, 0.75),
            2
        );

        response_times.insert(10, 25);
        response_times.insert(20, 25);
        response_times.insert(30, 25);
        response_times.insert(50, 25);
        response_times.insert(100, 10);
        response_times.insert(200, 1);
        assert_eq!(
            calculate_response_time_percentile(&response_times, 115, 1, 200, 0.9),
            50
        );
        assert_eq!(
            calculate_response_time_percentile(&response_times, 115, 1, 200, 0.99),
            100
        );
        assert_eq!(
            calculate_response_time_percentile(&response_times, 115, 1, 200, 0.999),
            200
        );
    }
}