Skip to content

Commit

Permalink
Add filtering benchmarks using a command-line option.
Browse files Browse the repository at this point in the history
  • Loading branch information
bheisler committed Dec 11, 2017
1 parent 98fd1c9 commit b6a8162
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 5 deletions.
46 changes: 41 additions & 5 deletions src/lib.rs
Expand Up @@ -306,6 +306,7 @@ pub struct Criterion {
sample_size: usize,
significance_level: f64,
warm_up_time: Duration,
filter: Option<String>,
}

impl Default for Criterion {
Expand All @@ -320,6 +321,7 @@ impl Default for Criterion {
/// - Confidence level: 0.95
/// - Significance level: 0.05
/// - Plotting: enabled (if gnuplot is available)
/// - No filter
fn default() -> Criterion {
let plotting = if simplot::version().is_ok() {
Plotting::Enabled
Expand All @@ -338,6 +340,7 @@ impl Default for Criterion {
plotting: plotting,
significance_level: 0.05,
warm_up_time: Duration::new(3, 0),
filter: None,
}
}
}
Expand Down Expand Up @@ -482,6 +485,29 @@ impl Criterion {
}
}

/// Filters the benchmarks. Only benchmarks with names that contain the
/// given string will be executed.
pub fn with_filter<S: Into<String>>(&mut self, filter: S) -> &mut Criterion {
self.filter = Some(filter.into());

self
}

/// Configure this criterion struct based on the command-line arguments to
/// this process.
pub fn configure_from_args(&mut self) {
if let Some(arg) = ::std::env::args().skip(1).find(|arg| *arg != "--bench") {
self.with_filter(arg);
}
}

fn filter_matches(&self, id: &str) -> bool {
match &self.filter {
&Some(ref string) => id.contains(string),
&None => true,
}
}

/// Benchmarks a function
///
/// The function under test must follow the setup - bench - teardown pattern:
Expand All @@ -504,7 +530,9 @@ impl Criterion {
pub fn bench_function<F>(&mut self, id: &str, f: F) -> &mut Criterion where
F: FnMut(&mut Bencher),
{
analysis::function(id, f, self);
if self.filter_matches(id) {
analysis::function(id, f, self);
}

self
}
Expand Down Expand Up @@ -543,7 +571,9 @@ impl Criterion {
input: &I) -> &mut Criterion
where I: fmt::Display
{
analysis::functions(id, funs, input, self);
if self.filter_matches(id) {
analysis::functions(id, funs, input, self);
}
self
}

Expand All @@ -570,7 +600,9 @@ impl Criterion {
I::Item: fmt::Display,
F: FnMut(&mut Bencher, &I::Item),
{
analysis::function_over_inputs(id, f, inputs, self);
if self.filter_matches(id) {
analysis::function_over_inputs(id, f, inputs, self);
}

self
}
Expand Down Expand Up @@ -613,7 +645,9 @@ impl Criterion {
/// }
/// ```
pub fn bench_program(&mut self, id: &str, mut program: Command) -> &mut Criterion {
analysis::program(id, &mut program, self);
if self.filter_matches(id) {
analysis::program(id, &mut program, self);
}

self
}
Expand All @@ -632,7 +666,9 @@ impl Criterion {
I: IntoIterator,
I::Item: fmt::Display,
{
analysis::program_over_inputs(id, program, inputs, self);
if self.filter_matches(id) {
analysis::program_over_inputs(id, program, inputs, self);
}

self
}
Expand Down
1 change: 1 addition & 0 deletions src/macros.rs
Expand Up @@ -63,6 +63,7 @@ macro_rules! criterion_group {
pub fn $name() {
$(
let mut criterion: Criterion = $config;
criterion.configure_from_args();
$target(&mut criterion);
)+
}
Expand Down

0 comments on commit b6a8162

Please sign in to comment.