use gumdrop::Options;
use httpmock::MockServer;
use std::io::{self, BufRead};
use goose::goose::{GooseTask, GooseTaskSet};
use goose::metrics::GooseMetrics;
use goose::{GooseAttack, GooseConfiguration};
type WorkerHandles = Vec<std::thread::JoinHandle<()>>;
pub fn build_configuration(server: &MockServer, custom: Vec<&str>) -> GooseConfiguration {
let mut configuration: Vec<&str> = vec![];
let server_url = server.url("/");
configuration.extend_from_slice(&custom);
if !configuration.contains(&"--worker") {
if !configuration.contains(&"--host") {
configuration.extend_from_slice(&["--host", &server_url]);
}
if !configuration.contains(&"--users") {
configuration.extend_from_slice(&["--users", "1"]);
}
if !configuration.contains(&"--hatch-rate") {
configuration.extend_from_slice(&["--hatch-rate", "1"]);
}
if !configuration.contains(&"--run-time") {
configuration.extend_from_slice(&["--run-time", "1"]);
}
}
GooseConfiguration::parse_args_default(&configuration)
.expect("failed to parse options and generate a configuration")
}
#[allow(dead_code)]
pub fn launch_gaggle_workers(
goose_attack: GooseAttack,
expect_workers: usize,
) -> WorkerHandles {
let mut worker_handles = Vec::new();
for _ in 0..expect_workers {
let worker_goose_attack = goose_attack.clone();
worker_handles.push(std::thread::spawn(move || {
run_load_test(worker_goose_attack, None);
}));
}
worker_handles
}
#[allow(dead_code)]
pub fn build_load_test(
configuration: GooseConfiguration,
taskset: &GooseTaskSet,
start_task: Option<&GooseTask>,
stop_task: Option<&GooseTask>,
) -> GooseAttack {
let mut goose = crate::GooseAttack::initialize_with_config(configuration)
.unwrap()
.register_taskset(taskset.clone());
if let Some(task) = start_task {
goose = goose.test_start(task.clone());
}
if let Some(task) = stop_task {
goose = goose.test_stop(task.clone());
}
goose
}
pub fn run_load_test(
goose_attack: GooseAttack,
worker_handles: Option<WorkerHandles>,
) -> GooseMetrics {
let goose_metrics = goose_attack.execute().unwrap();
if let Some(handles) = worker_handles {
for handle in handles {
let _ = handle.join();
}
}
goose_metrics
}
#[allow(dead_code)]
pub fn file_length(file_name: &str) -> usize {
if let Ok(file) = std::fs::File::open(std::path::Path::new(file_name)) {
io::BufReader::new(file).lines().count()
} else {
0
}
}
#[allow(dead_code)]
pub fn cleanup_files(files: Vec<&str>) {
for file in files {
if std::path::Path::new(file).exists() {
std::fs::remove_file(file).expect("failed to remove file");
}
}
}