mirror of
https://github.com/kristoferssolo/project-finder.git
synced 2025-10-21 19:50:35 +00:00
refactor: impl Display and Default
This commit is contained in:
parent
620c274e54
commit
c1e568aa3f
3
benches/common/default.rs
Normal file
3
benches/common/default.rs
Normal file
@ -0,0 +1,3 @@
|
||||
pub fn default<T: Default>() -> T {
|
||||
T::default()
|
||||
}
|
||||
@ -1,2 +1,4 @@
|
||||
mod default;
|
||||
pub mod setup;
|
||||
pub mod utils;
|
||||
pub use default::default;
|
||||
|
||||
@ -4,6 +4,7 @@ use csv::Reader;
|
||||
use regex::Regex;
|
||||
use serde::{Deserialize, Deserializer};
|
||||
use std::{
|
||||
fmt::Display,
|
||||
fs::{self, File, create_dir_all},
|
||||
path::{Path, PathBuf},
|
||||
str::FromStr,
|
||||
@ -17,10 +18,10 @@ pub fn init_temp_dir() {
|
||||
TEMP_DIR.get_or_init(|| setup_entries().expect("Failed to setup test directory"));
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
#[derive(Debug, Clone, Default)]
|
||||
pub struct BenchParams {
|
||||
pub depth: usize,
|
||||
pub max_results: usize,
|
||||
pub depth: Option<usize>,
|
||||
pub max_results: Option<usize>,
|
||||
pub verbose: bool,
|
||||
}
|
||||
|
||||
@ -186,3 +187,15 @@ fn create_dir(path: &Path) -> anyhow::Result<()> {
|
||||
create_dir_all(path)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
impl Display for BenchParams {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
write!(
|
||||
f,
|
||||
"depth: {}, max: {}, verbose: {}",
|
||||
self.depth.unwrap_or_default(),
|
||||
self.max_results.unwrap_or_default(),
|
||||
self.verbose
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,20 +1,18 @@
|
||||
use anyhow::anyhow;
|
||||
use std::{
|
||||
path::{Path, PathBuf},
|
||||
process::Command,
|
||||
};
|
||||
|
||||
use super::setup::BenchParams;
|
||||
|
||||
pub const BASE_DIR: &str = env!("CARGO_MANIFEST_DIR");
|
||||
|
||||
pub fn run_binary_with_args(
|
||||
path: &Path,
|
||||
depth: usize,
|
||||
max_results: usize,
|
||||
verbose: bool,
|
||||
) -> anyhow::Result<()> {
|
||||
pub fn run_binary_with_args(path: &Path, params: &BenchParams) -> anyhow::Result<()> {
|
||||
let binary_path = PathBuf::from(BASE_DIR).join("target/release/project-finder");
|
||||
|
||||
if !binary_path.exists() {
|
||||
return Err(anyhow::anyhow!(
|
||||
return Err(anyhow!(
|
||||
"Binary not found at {}. Did you run `cargo build --release`?",
|
||||
binary_path.display()
|
||||
));
|
||||
@ -25,26 +23,28 @@ pub fn run_binary_with_args(
|
||||
// Add the path to search
|
||||
cmd.arg(path);
|
||||
|
||||
// Add depth parameter
|
||||
cmd.arg("--depth").arg(depth.to_string());
|
||||
if let Some(depth) = params.depth {
|
||||
// Add depth parameter
|
||||
cmd.arg("--depth").arg(depth.to_string());
|
||||
}
|
||||
|
||||
// Add max_results parameter if not zero
|
||||
if max_results > 0 {
|
||||
if let Some(max_results) = params.max_results {
|
||||
cmd.arg("--max-results").arg(max_results.to_string());
|
||||
}
|
||||
|
||||
// Add verbose flag if true
|
||||
if verbose {
|
||||
if params.verbose {
|
||||
cmd.arg("--verbose");
|
||||
}
|
||||
|
||||
let output = cmd.output().map_err(|e| {
|
||||
anyhow::anyhow!("Failed to execute binary {}: {}", binary_path.display(), e)
|
||||
})?;
|
||||
let output = cmd
|
||||
.output()
|
||||
.map_err(|e| anyhow!("Failed to execute binary {}: {}", binary_path.display(), e))?;
|
||||
|
||||
if !output.status.success() {
|
||||
let stderr = String::from_utf8_lossy(&output.stderr);
|
||||
return Err(anyhow::anyhow!(
|
||||
return Err(anyhow!(
|
||||
"Process failed with status: {}\nStderr: {}",
|
||||
output.status,
|
||||
stderr
|
||||
@ -53,6 +53,7 @@ pub fn run_binary_with_args(
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn create_deep_directory(base: &Path, depth: usize) -> anyhow::Result<()> {
|
||||
todo!()
|
||||
}
|
||||
|
||||
@ -1,20 +1,11 @@
|
||||
use std::fmt::format;
|
||||
|
||||
use crate::common::{
|
||||
setup::{BenchParams, TEMP_DIR, init_temp_dir, setup_entries},
|
||||
utils::{BASE_DIR, run_binary_with_args},
|
||||
default,
|
||||
setup::{BenchParams, TEMP_DIR, init_temp_dir},
|
||||
utils::run_binary_with_args,
|
||||
};
|
||||
use criterion::{BenchmarkId, Criterion};
|
||||
use std::{
|
||||
path::{Path, PathBuf},
|
||||
process::Command,
|
||||
};
|
||||
|
||||
fn process_directory(path: &Path) {
|
||||
let binary_path = PathBuf::from(BASE_DIR).join("target/release/project-finder");
|
||||
Command::new(binary_path)
|
||||
.arg(path)
|
||||
.output()
|
||||
.expect("failed to run binary");
|
||||
}
|
||||
|
||||
pub fn benchmark_basic(c: &mut Criterion) {
|
||||
init_temp_dir();
|
||||
@ -22,37 +13,31 @@ pub fn benchmark_basic(c: &mut Criterion) {
|
||||
|
||||
let params = vec![
|
||||
BenchParams {
|
||||
depth: 1,
|
||||
max_results: 0,
|
||||
verbose: false,
|
||||
depth: Some(1),
|
||||
..Default::default()
|
||||
},
|
||||
BenchParams {
|
||||
depth: 5,
|
||||
max_results: 0,
|
||||
verbose: false,
|
||||
depth: Some(5),
|
||||
..default()
|
||||
},
|
||||
BenchParams {
|
||||
depth: Some(10),
|
||||
..default()
|
||||
},
|
||||
BenchParams {
|
||||
depth: Some(10),
|
||||
max_results: Some(10),
|
||||
..default()
|
||||
},
|
||||
];
|
||||
|
||||
let mut group = c.benchmark_group("basic_scenarios");
|
||||
|
||||
group.bench_function("process_directory", |b| {
|
||||
b.iter(|| process_directory(temp_dir))
|
||||
});
|
||||
|
||||
for param in params {
|
||||
let id = BenchmarkId::new(
|
||||
format!(
|
||||
"depth{}_max{}_verbose{}",
|
||||
param.depth, param.max_results, param.verbose
|
||||
),
|
||||
param.depth,
|
||||
);
|
||||
for (idx, param) in params.iter().enumerate() {
|
||||
let id = BenchmarkId::new(format!("with_param_{idx}"), ¶m);
|
||||
|
||||
group.bench_with_input(id, ¶m, |b, param| {
|
||||
b.iter(|| {
|
||||
run_binary_with_args(temp_dir, param.depth, param.max_results, param.verbose)
|
||||
.expect("Failed to run binary")
|
||||
})
|
||||
b.iter(|| run_binary_with_args(temp_dir, param).expect("Failed to run binary"))
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user