Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix some lints #194

Merged
merged 13 commits into from
Jul 17, 2019
8 changes: 4 additions & 4 deletions src/hyperfine/benchmark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ pub fn time_shell_command(
/// Measure the average shell spawning time
pub fn mean_shell_spawning_time(
shell: &str,
style: &OutputStyleOption,
style: OutputStyleOption,
show_output: bool,
) -> io::Result<TimingResult> {
const COUNT: u64 = 200;
Expand Down Expand Up @@ -150,7 +150,7 @@ fn run_intermediate_command(
show_output: bool,
error_output: &'static str,
) -> io::Result<TimingResult> {
if let &Some(ref cmd) = command {
if let Some(ref cmd) = command {
let res = time_shell_command(shell, cmd, show_output, CmdFailureAction::RaiseError, None);
if res.is_err() {
return Err(io::Error::new(io::ErrorKind::Other, error_output));
Expand Down Expand Up @@ -210,7 +210,7 @@ pub fn run_benchmark(
let progress_bar = get_progress_bar(
options.warmup_count,
"Performing warmup runs",
&options.output_style,
options.output_style,
);

for _ in 0..options.warmup_count {
Expand All @@ -230,7 +230,7 @@ pub fn run_benchmark(
let progress_bar = get_progress_bar(
options.runs.min,
"Initial time measurement",
&options.output_style,
options.output_style,
);

// Run init command
Expand Down
4 changes: 2 additions & 2 deletions src/hyperfine/export/asciidoc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use std::io::Result;
pub struct AsciidocExporter {}

impl Exporter for AsciidocExporter {
fn serialize(&self, results: &Vec<BenchmarkResult>, unit: Option<Unit>) -> Result<Vec<u8>> {
fn serialize(&self, results: &[BenchmarkResult], unit: Option<Unit>) -> Result<Vec<u8>> {
let unit = if let Some(unit) = unit {
// Use the given unit for all entries.
unit
Expand All @@ -27,7 +27,7 @@ impl Exporter for AsciidocExporter {
res.append(&mut table_startend());
res.append(&mut table_header(unit));
for result in results {
res.push('\n' as u8);
res.push(b'\n');
res.append(&mut table_row(result, unit));
}
res.append(&mut table_startend());
Expand Down
2 changes: 1 addition & 1 deletion src/hyperfine/export/csv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use csv::WriterBuilder;
pub struct CsvExporter {}

impl Exporter for CsvExporter {
fn serialize(&self, results: &Vec<BenchmarkResult>, _unit: Option<Unit>) -> Result<Vec<u8>> {
fn serialize(&self, results: &[BenchmarkResult], _unit: Option<Unit>) -> Result<Vec<u8>> {
let mut writer = WriterBuilder::new().from_writer(vec![]);
for res in results {
// The list of times cannot be exported to the CSV file - remove it:
Expand Down
4 changes: 2 additions & 2 deletions src/hyperfine/export/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ use serde_json::to_vec_pretty;

#[derive(Serialize, Debug)]
struct HyperfineSummary<'a> {
results: &'a Vec<BenchmarkResult>,
results: &'a [BenchmarkResult],
}

#[derive(Default)]
pub struct JsonExporter {}

impl Exporter for JsonExporter {
fn serialize(&self, results: &Vec<BenchmarkResult>, _unit: Option<Unit>) -> Result<Vec<u8>> {
fn serialize(&self, results: &[BenchmarkResult], _unit: Option<Unit>) -> Result<Vec<u8>> {
let mut output = to_vec_pretty(&HyperfineSummary { results });
for content in output.iter_mut() {
content.push(b'\n');
Expand Down
4 changes: 2 additions & 2 deletions src/hyperfine/export/markdown.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use std::io::Result;
pub struct MarkdownExporter {}

impl Exporter for MarkdownExporter {
fn serialize(&self, results: &Vec<BenchmarkResult>, unit: Option<Unit>) -> Result<Vec<u8>> {
fn serialize(&self, results: &[BenchmarkResult], unit: Option<Unit>) -> Result<Vec<u8>> {
let unit = if let Some(unit) = unit {
// Use the given unit for all entries.
unit
Expand All @@ -23,7 +23,7 @@ impl Exporter for MarkdownExporter {
Unit::Second
};

let annotated_results = compute_relative_speed(&results);
let annotated_results = compute_relative_speed(results);

let mut destination = start_table(unit);

Expand Down
4 changes: 2 additions & 2 deletions src/hyperfine/export/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ pub enum ExportType {
/// Interface for different exporters.
trait Exporter {
/// Export the given entries in the serialized form.
fn serialize(&self, results: &Vec<BenchmarkResult>, unit: Option<Unit>) -> Result<Vec<u8>>;
fn serialize(&self, results: &[BenchmarkResult], unit: Option<Unit>) -> Result<Vec<u8>>;
}

struct ExporterWithFilename {
Expand Down Expand Up @@ -79,7 +79,7 @@ impl ExportManager {
}

/// Write the given content to a file with the specified name
fn write_to_file(filename: &String, content: &Vec<u8>) -> Result<()> {
fn write_to_file(filename: &str, content: &[u8]) -> Result<()> {
let mut file = File::create(filename)?;
file.write_all(content)
}
12 changes: 6 additions & 6 deletions src/hyperfine/internal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@ use std::iter::Iterator;
pub const MIN_EXECUTION_TIME: Second = 5e-3;

/// Return a pre-configured progress bar
pub fn get_progress_bar(length: u64, msg: &str, option: &OutputStyleOption) -> ProgressBar {
let progressbar_style = match *option {
pub fn get_progress_bar(length: u64, msg: &str, option: OutputStyleOption) -> ProgressBar {
let progressbar_style = match option {
OutputStyleOption::Basic | OutputStyleOption::Color => ProgressStyle::default_bar(),
_ => ProgressStyle::default_spinner()
.tick_chars("⠋⠙⠹⠸⠼⠴⠦⠧⠇⠏")
.template(" {spinner} {msg:<30} {wide_bar} ETA {eta_precise}"),
};

let progress_bar = match *option {
let progress_bar = match option {
OutputStyleOption::Basic | OutputStyleOption::Color => ProgressBar::hidden(),
_ => ProgressBar::new(length),
};
Expand Down Expand Up @@ -57,7 +57,7 @@ fn compare_mean_time(l: &BenchmarkResult, r: &BenchmarkResult) -> Ordering {
}

pub fn compute_relative_speed<'a>(
results: &'a Vec<BenchmarkResult>,
results: &'a [BenchmarkResult],
) -> Vec<BenchmarkResultWithRelativeSpeed<'a>> {
let fastest: &BenchmarkResult = results
.iter()
Expand All @@ -84,7 +84,7 @@ pub fn compute_relative_speed<'a>(
.collect()
}

pub fn write_benchmark_comparison(results: &Vec<BenchmarkResult>) {
pub fn write_benchmark_comparison(results: &[BenchmarkResult]) {
if results.len() < 2 {
return;
}
Expand Down Expand Up @@ -123,7 +123,7 @@ fn test_compute_relative_speed() {

let create_result = |name: &str, mean| BenchmarkResult {
command: name.into(),
mean: mean,
mean,
stddev: 1.0,
median: mean,
user: mean,
Expand Down
2 changes: 1 addition & 1 deletion src/hyperfine/timer/wallclocktimer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,6 @@ impl TimerStop for WallClockTimer {

fn stop(&self) -> Second {
let duration = self.start.elapsed();
duration.as_secs() as f64 + (duration.subsec_nanos() as f64) * 1e-9
duration.as_secs() as f64 + f64::from(duration.subsec_nanos()) * 1e-9
}
}
4 changes: 2 additions & 2 deletions src/hyperfine/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@ pub struct Command<'a> {
impl<'a> Command<'a> {
pub fn new(expression: &'a str) -> Command<'a> {
Command {
expression: expression,
expression,
parameter: None,
}
}

pub fn new_parametrized(expression: &'a str, parameter: &'a str, value: i32) -> Command<'a> {
Command {
expression: expression,
expression,
parameter: Some((parameter, value)),
}
}
Expand Down
8 changes: 4 additions & 4 deletions src/hyperfine/units.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,16 @@ pub enum Unit {

impl Unit {
/// The abbreviation of the Unit.
pub fn short_name(&self) -> String {
match *self {
pub fn short_name(self) -> String {
match self {
Unit::Second => String::from("s"),
Unit::MilliSecond => String::from("ms"),
}
}

/// Returns the Second value formatted for the Unit.
pub fn format(&self, value: Second) -> String {
match *self {
pub fn format(self, value: Second) -> String {
match self {
Unit::Second => format!("{:.3}", value),
Unit::MilliSecond => format!("{:.1}", value * 1e3),
}
Expand Down
29 changes: 13 additions & 16 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@ pub fn error(message: &str) -> ! {

/// Runs the benchmark for the given commands
fn run(
commands: &Vec<Command<'_>>,
commands: &[Command<'_>],
options: &HyperfineOptions,
) -> io::Result<Vec<BenchmarkResult>> {
let shell_spawning_time =
mean_shell_spawning_time(&options.shell, &options.output_style, options.show_output)?;
mean_shell_spawning_time(&options.shell, options.output_style, options.show_output)?;

let mut timing_results = vec![];

Expand All @@ -52,7 +52,7 @@ fn parse_parameter_scan_args<'a>(
let param_min: i32 = vals.next().unwrap().parse()?;
let param_max: i32 = vals.next().unwrap().parse()?;

const MAX_PARAMETERS: i32 = 100000;
const MAX_PARAMETERS: i32 = 100_000;
if param_max - param_min > MAX_PARAMETERS {
return Err(ParameterScanError::TooLarge);
}
Expand All @@ -61,7 +61,7 @@ fn parse_parameter_scan_args<'a>(
return Err(ParameterScanError::EmptyRange);
}

return Ok((param_name, param_min..(param_max + 1)));
Ok((param_name, param_min..(param_max + 1)))
}

fn main() {
Expand Down Expand Up @@ -93,17 +93,15 @@ fn main() {
/// Build the HyperfineOptions that correspond to the given ArgMatches
fn build_hyperfine_options(matches: &ArgMatches<'_>) -> Result<HyperfineOptions, OptionsError> {
let mut options = HyperfineOptions::default();
let str_to_u64 = |n| u64::from_str_radix(n, 10).ok();
let param_to_u64 = |param| matches.value_of(param).and_then(|n| u64::from_str_radix(n, 10).ok());

options.warmup_count = matches
.value_of("warmup")
.and_then(&str_to_u64)
options.warmup_count = param_to_u64("warmup")
.unwrap_or(options.warmup_count);

let mut min_runs = matches.value_of("min-runs").and_then(&str_to_u64);
let mut max_runs = matches.value_of("max-runs").and_then(&str_to_u64);
let mut min_runs = param_to_u64("min-runs");
let mut max_runs = param_to_u64("max-runs");

if let Some(runs) = matches.value_of("runs").and_then(&str_to_u64) {
if let Some(runs) = param_to_u64("runs") {
min_runs = Some(runs);
max_runs = Some(runs);
}
Expand Down Expand Up @@ -207,13 +205,13 @@ fn build_export_manager(matches: &ArgMatches<'_>) -> ExportManager {
fn build_commands<'a>(matches: &'a ArgMatches<'_>) -> Vec<Command<'a>> {
let command_strings = matches.values_of("command").unwrap();

let commands = if let Some(args) = matches.values_of("parameter-scan") {
if let Some(args) = matches.values_of("parameter-scan") {
match parse_parameter_scan_args(args) {
Ok((param_name, param_range)) => {
let mut commands = vec![];
let command_strings = command_strings.collect::<Vec<&str>>();
for value in param_range.start..param_range.end {
for ref cmd in &command_strings {
for cmd in &command_strings {
commands.push(Command::new_parametrized(cmd, param_name, value));
}
}
Expand All @@ -222,7 +220,6 @@ fn build_commands<'a>(matches: &'a ArgMatches<'_>) -> Vec<Command<'a>> {
Err(e) => error(e.description()),
}
} else {
command_strings.map(|c| Command::new(c)).collect()
};
commands
command_strings.map(Command::new).collect()
}
}