Skip to content

Commit

Permalink
chore: set up benchmarks
Browse files Browse the repository at this point in the history
  • Loading branch information
pbillaut authored Sep 14, 2024
1 parent a33b314 commit c6c29f5
Show file tree
Hide file tree
Showing 8 changed files with 143,117 additions and 1 deletion.
2 changes: 1 addition & 1 deletion .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,4 @@ jobs:
steps:
- uses: actions/checkout@v4
- name: Run tests
run: cargo test --verbose --all-targets --all-features
run: cargo test --verbose --lib --bins --tests --examples
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,6 @@ tracing-subscriber = { version = "0.3.18", features = ["env-filter"] }
criterion = { version = "0.5.1", features = ["html_reports"] }
test-log = { version = "0.2.16", features = ["trace"] }

[[bench]]
name = "bench_main"
harness = false
43 changes: 43 additions & 0 deletions benches/bench_main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
use criterion::criterion_main;

mod benchmarks;

pub type ParseResult = Vec<CsvProcessorResult<AccountActivity>>;

#[allow(unused_macros)]
macro_rules! open_file {
($str_arg:expr) => {{
let mut path = PathBuf::from(file!());
path.pop();
path.pop();
path.push("data/");
path.push($str_arg);
File::open(path).expect("Benchmark setup: unable to open file")
}};
}
#[allow(unused_imports)]
pub(crate) use open_file;
use payment_processor::account_activity::AccountActivity;
use payment_processor::processors::csv::CsvProcessorResult;

#[allow(unused_macros)]
macro_rules! read_file {
($str_arg:expr) => {{
let mut file = open_file!($str_arg);
let mut buffer = Vec::new();
file.read_to_end(&mut buffer).expect("Benchmark setup: unable to read file");
buffer
}};
}
#[allow(unused_imports)]
pub(crate) use read_file;

pub const SCENARIOS: [(&str, u64); 2] = [
("activities_1K.csv", 1_000),
("activities_10K.csv", 10_000),
];

criterion_main!(
benchmarks::parsing::benches,
benchmarks::processing::benches,
);
2 changes: 2 additions & 0 deletions benches/benchmarks/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pub mod parsing;
pub mod processing;
25 changes: 25 additions & 0 deletions benches/benchmarks/parsing.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
use crate::{open_file, read_file, ParseResult, SCENARIOS};
use criterion::{black_box, criterion_group, BenchmarkId, Criterion};
use payment_processor::processors::csv::reader::CsvReader;
use std::fs::File;
use std::io::Read;
use std::path::PathBuf;

fn bench_parsing(c: &mut Criterion) {
let mut group = c.benchmark_group("CsvReader::iter");
for (filename, num_elements) in SCENARIOS {
let buffer = read_file!(filename);
group.throughput(criterion::Throughput::Elements(num_elements));
group.bench_with_input(
BenchmarkId::from_parameter(num_elements), &buffer,
|b, buffer| b.iter(|| {
let mut reader = CsvReader::try_new(black_box(buffer.as_slice()))
.expect("Benchmark: unable to create csv reader");
reader.iter().collect::<ParseResult>()
}),
);
}
group.finish();
}

criterion_group!(benches, bench_parsing);
41 changes: 41 additions & 0 deletions benches/benchmarks/processing.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
use crate::ParseResult;
use criterion::{black_box, criterion_group, BatchSize, Criterion};
use payment_processor::account_activity::AccountActivity;
use payment_processor::processor::Processor;
use payment_processor::processors::csv::CsvProcessor;
use payment_processor::transaction::TransactionID;
use payment_processor::ClientID;

fn bench_process_transactions(c: &mut Criterion) {
let client_id_1 = ClientID(1);
let client_id_2 = ClientID(2);
let transactions = vec![
AccountActivity::deposit(TransactionID(1), client_id_1, 100.0),
AccountActivity::dispute(TransactionID(1), client_id_1),
AccountActivity::deposit(TransactionID(2), client_id_1, 100.0),
AccountActivity::withdrawal(TransactionID(3), client_id_1, 50.0),
AccountActivity::resolve(TransactionID(1), client_id_1),
AccountActivity::dispute(TransactionID(2), client_id_1),
AccountActivity::chargeback(TransactionID(2), client_id_1),
AccountActivity::withdrawal(TransactionID(4), client_id_1, 100.0),
AccountActivity::deposit(TransactionID(1), client_id_2, 100.0),
AccountActivity::dispute(TransactionID(1), client_id_2),
AccountActivity::deposit(TransactionID(2), client_id_2, 100.0),
AccountActivity::withdrawal(TransactionID(3), client_id_2, 50.0),
AccountActivity::resolve(TransactionID(1), client_id_2),
AccountActivity::dispute(TransactionID(2), client_id_2),
AccountActivity::chargeback(TransactionID(2), client_id_2),
AccountActivity::withdrawal(TransactionID(4), client_id_2, 100.0),
];
let processor = CsvProcessor::new();

c.bench_function("CsvProcessor::process_account_activity [dispute process]", move |b| {
b.iter_batched(
|| transactions.clone().into_iter().map(Ok).collect::<ParseResult>(),
|transactions| processor.process_account_activity(black_box(transactions.into_iter())),
BatchSize::SmallInput,
)
});
}

criterion_group!(benches, bench_process_transactions);
Loading

0 comments on commit c6c29f5

Please sign in to comment.