Skip to content

Commit

Permalink
Store desired jobs and threads in LoadtestOptions struct
Browse files Browse the repository at this point in the history
  • Loading branch information
rustworthy committed Dec 2, 2023
1 parent ccc25ee commit b695db4
Showing 1 changed file with 21 additions and 28 deletions.
49 changes: 21 additions & 28 deletions src/bin/loadtest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ use clap::ArgMatches;
use clap::{Arg, Command};
use faktory::{ConsumerBuilder, Error, Job, Producer};
use rand::prelude::*;
use std::collections::HashMap;
use std::io;
use std::process;
use std::sync::{self, atomic};
Expand Down Expand Up @@ -45,28 +44,30 @@ fn setup_parser() -> Command<'static> {
)
}

struct LoadtestOptions {
jobs: usize,
threads: usize,
}

fn parse_command_line_args() -> ArgMatches {
let parser = setup_parser();
parser.get_matches()
}

Check warning on line 55 in src/bin/loadtest.rs

View check run for this annotation

Codecov / codecov/patch

src/bin/loadtest.rs#L52-L55

Added lines #L52 - L55 were not covered by tests

fn get_opts(parse: Option<Box<dyn FnOnce() -> ArgMatches>>) -> HashMap<&'static str, usize> {
fn get_opts(parse: Option<Box<dyn FnOnce() -> ArgMatches>>) -> LoadtestOptions {
let matches = parse.unwrap_or(Box::new(parse_command_line_args))();
let jobs_count = matches
let jobs = matches
.get_one::<String>("jobs")
.unwrap()
.parse::<usize>()
.expect("Number of jobs to run");
let threads_count = matches
let threads = matches
.get_one::<String>("threads")
.unwrap()
.parse::<usize>()
.expect("Number of consumers/producers to run");

let mut opts: HashMap<&'static str, usize> = HashMap::new();
opts.insert("jobs", jobs_count);
opts.insert("threads", threads_count);
opts
LoadtestOptions { jobs, threads }
}

#[derive(Clone, Default)]
Expand Down Expand Up @@ -152,32 +153,30 @@ fn calc_secs_elapsed(elapsed: &time::Duration) -> f64 {
elapsed_nanos as f64 / 1_000_000_000.0
}

fn run_loadtest(jobs: usize, threads: usize) {
fn run_loadtest(opts: &LoadtestOptions) {
println!(
"Running loadtest with {} jobs and {} threads",
jobs, threads
opts.jobs, opts.threads
);

ping!();

let start = time::Instant::now();
let (_ops_count, pushed, popped) = load_with_jobs(jobs, threads);
let (_ops_count, pushed, popped) = load_with_jobs(opts.jobs, opts.threads);
let stop = calc_secs_elapsed(&start.elapsed());

println!(
"Processed {} pushes and {} pops in {:.2} seconds, rate: {} jobs/s",
pushed.get(),
popped.get(),
stop,
jobs as f64 / stop,
opts.jobs as f64 / stop,
);
}

fn main() {
let opts = get_opts(None);
let jobs = opts.get("jobs").unwrap();
let threads = opts.get("threads").unwrap();
run_loadtest(*jobs, *threads)
run_loadtest(&opts)
}

Check warning on line 180 in src/bin/loadtest.rs

View check run for this annotation

Codecov / codecov/patch

src/bin/loadtest.rs#L177-L180

Added lines #L177 - L180 were not covered by tests

#[cfg(test)]
Expand Down Expand Up @@ -230,11 +229,9 @@ mod test {
let argv = ["./target/release/loadtest"];
let parse_fn = prepare_parse_fn(&argv);
let opts = get_opts(Some(Box::new(parse_fn)));
let jobs_count = opts.get("jobs").unwrap().to_owned();
let threads_count = opts.get("threads").unwrap().to_owned();
assert_eq!(jobs_count, DEFAULT_JOBS_COUNT.parse::<usize>().unwrap());
assert_eq!(opts.jobs, DEFAULT_JOBS_COUNT.parse::<usize>().unwrap());
assert_eq!(
threads_count,
opts.threads,
DEFAULT_THREADS_COUNT.parse::<usize>().unwrap()
);
}
Expand All @@ -244,10 +241,8 @@ mod test {
let argv = ["./target/release/loadtest", "20000", "8"];
let parse_fn = prepare_parse_fn(&argv);
let opts = get_opts(Some(Box::new(parse_fn)));
let jobs_count = opts.get("jobs").unwrap().to_owned();
let threads_count = opts.get("threads").unwrap().to_owned();
assert_eq!(jobs_count, 20_000);
assert_eq!(threads_count, 8);
assert_eq!(opts.jobs, 20_000);
assert_eq!(opts.threads, 8);
}

#[test]
Expand Down Expand Up @@ -280,13 +275,11 @@ mod test {
}

#[test]
fn test_loadtest_flow() {
skip_test_if_faktory_url_not_provided!();
fn test_loadtest_flow() {
skip_test_if_faktory_url_not_provided!();
let argv = ["./target/release/loadtest", "21000", "10"];
let parse_fn = prepare_parse_fn(&argv);
let opts = get_opts(Some(Box::new(parse_fn)));
let jobs = opts.get("jobs").unwrap();
let threads = opts.get("threads").unwrap();
run_loadtest(*jobs, *threads);
run_loadtest(&opts);
}
}

0 comments on commit b695db4

Please sign in to comment.