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