Skip to content

Commit

Permalink
Code polishing according to the latest clippy
Browse files Browse the repository at this point in the history
  • Loading branch information
xnuter committed Jul 18, 2021
1 parent 6644051 commit 4d9b724
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 23 deletions.
32 changes: 15 additions & 17 deletions src/configuration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ pub struct BenchmarkConfig {
pub rate_ladder: RateLadder,
pub mode: BenchmarkMode,
#[builder(default)]
pub reporters: Vec<Arc<Box<dyn ExternalMetricsServiceReporter + Send + Sync + 'static>>>,
pub reporters: Vec<Arc<dyn ExternalMetricsServiceReporter + Send + Sync + 'static>>,
}

impl BenchmarkConfig {
Expand Down Expand Up @@ -141,40 +141,38 @@ impl BenchmarkConfig {
fn build_metric_destinations(
test_case_name: Option<String>,
matches: ArgMatches,
) -> Vec<Arc<Box<dyn ExternalMetricsServiceReporter + Send + Sync>>> {
) -> Vec<Arc<dyn ExternalMetricsServiceReporter + Send + Sync>> {
if matches.value_of("PROMETHEUS_ADDR").is_some() {
println!("Prometheus is not supported in this configuration");
exit(-1);
}

vec![Arc::new(Box::new(DefaultConsoleReporter::new(
test_case_name,
)))]
vec![Arc::new(DefaultConsoleReporter::new(test_case_name))]
}

#[cfg(feature = "report-to-prometheus")]
fn build_metric_destinations(
test_case_name: Option<String>,
matches: ArgMatches,
) -> Vec<Arc<Box<dyn ExternalMetricsServiceReporter + Send + Sync>>> {
) -> Vec<Arc<dyn ExternalMetricsServiceReporter + Send + Sync>> {
use crate::prometheus_reporter::PrometheusReporter;
use std::net::SocketAddr;

let mut metrics_destinations: Vec<
Arc<Box<dyn ExternalMetricsServiceReporter + Send + Sync + 'static>>,
> = vec![Arc::new(Box::new(DefaultConsoleReporter::new(
Arc<dyn ExternalMetricsServiceReporter + Send + Sync + 'static>,
> = vec![Arc::new(DefaultConsoleReporter::new(
test_case_name.clone(),
)))];
))];

if let Some(prometheus_addr) = matches.value_of("PROMETHEUS_ADDR") {
if SocketAddr::from_str(prometheus_addr).is_err() {
panic!("Illegal Prometheus Gateway addr `{}`", prometheus_addr);
}
metrics_destinations.push(Arc::new(Box::new(PrometheusReporter::new(
metrics_destinations.push(Arc::new(PrometheusReporter::new(
test_case_name.clone(),
prometheus_addr.to_string(),
matches.value_of("PROMETHEUS_JOB"),
))));
)));
}

metrics_destinations
Expand Down Expand Up @@ -218,12 +216,12 @@ impl BenchmarkConfig {
const FILE_PREFIX: &str = "file://";

if let Some(body_value) = config.value_of("BODY") {
if body_value.starts_with(RANDOM_PREFIX) {
BenchmarkConfig::generate_random_vec(&body_value[RANDOM_PREFIX.len()..])
} else if body_value.starts_with(BASE64_PREFIX) {
base64::decode(&body_value[BASE64_PREFIX.len()..]).expect("Invalid base64")
} else if body_value.starts_with(FILE_PREFIX) {
BenchmarkConfig::read_file_as_vec(&body_value[FILE_PREFIX.len()..])
if let Some(body_size) = body_value.strip_prefix(RANDOM_PREFIX) {
BenchmarkConfig::generate_random_vec(body_size)
} else if let Some(base64) = body_value.strip_prefix(BASE64_PREFIX) {
base64::decode(base64).expect("Invalid base64")
} else if let Some(filename) = body_value.strip_prefix(FILE_PREFIX) {
BenchmarkConfig::read_file_as_vec(filename)
} else {
panic!("Unsupported format: {}", body_value);
}
Expand Down
11 changes: 5 additions & 6 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,8 @@ async fn main() -> io::Result<()> {

info!("Starting with configuration {}", benchmark_config);

let (reporter_task, batch_metric_sender) = create_async_metrics_channel(
benchmark_config.reporters.clone(),
benchmark_config.continuous,
);
let (reporter_task, batch_metric_sender) =
create_async_metrics_channel(&benchmark_config.reporters, benchmark_config.continuous);
let bench_session = benchmark_config.new_bench_session();

for batch in bench_session {
Expand Down Expand Up @@ -76,14 +74,15 @@ fn shutdown(reporter_task: JoinHandle<()>, batch_metric_sender: Sender<BenchRunM
}

fn create_async_metrics_channel(
metric_reporters: Vec<Arc<Box<dyn ExternalMetricsServiceReporter + Send + Sync + 'static>>>,
metric_reporters: &[Arc<dyn ExternalMetricsServiceReporter + Send + Sync + 'static>],
continuous: bool,
) -> (JoinHandle<()>, Sender<BenchRunMetrics>) {
// We need to report metrics in a separate threads,
// as at the moment of writing this code not all major metric client libraries
// had `async` APIs.
// We can replace it with `tokio::sync::mpsc` and `tokio::spawn` at any time
let (sender, receiver) = std::sync::mpsc::channel();
let metric_reporters = metric_reporters.to_owned();
let reporter_task = thread::spawn(move || {
while let Ok(stats) = receiver.recv() {
// broadcast to all metrics reporters
Expand All @@ -96,7 +95,7 @@ fn create_async_metrics_channel(
// for continuous runs we don't want to reset metrics
// to avoid saw-like graphs
if !continuous {
for reporter in &metric_reporters {
for reporter in metric_reporters {
reporter.reset_metrics();
}
}
Expand Down

0 comments on commit 4d9b724

Please sign in to comment.