Skip to content

Commit

Permalink
chore: Add benchmarks
Browse files Browse the repository at this point in the history
  • Loading branch information
leo91000 committed Oct 21, 2024
1 parent 16382d5 commit 0f9d0e0
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 4 deletions.
28 changes: 28 additions & 0 deletions .github/workflows/codespeed.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
name: CodSpeed
on:
push:
branches:
- "main"
pull_request:
# `workflow_dispatch` allows CodSpeed to trigger backtest
# performance analysis in order to generate initial data.
workflow_dispatch:
jobs:
benchmarks:
name: Run benchmarks
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Setup rust toolchain, cache and cargo-codspeed binary
uses: moonrepo/setup-rust@v0
with:
channel: stable
cache-target: release
bins: cargo-codspeed
- name: Build the benchmark target(s)
run: cargo codspeed build -p subset_sum
- name: Run the benchmarks
uses: CodSpeedHQ/action@v3
with:
run: cargo codspeed run -p subset_sum
token: ${{ secrets.CODSPEED_TOKEN }}
5 changes: 2 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
[workspace]
resolver = "2"

members = [
"crates/*",
]
members = ["crates/*"]

[profile.release]
codegen-units = 1
Expand Down
17 changes: 16 additions & 1 deletion crates/subset-sum/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,24 @@ strum_macros = "0.26.4"
num-traits = "0.2.19"
fn-memo = { version = "1.2", default-features = false }
wasm-bindgen = { version = "^0.2.95", optional = true }
instant = { version = "^0.1.13", features = ["wasm-bindgen", "inaccurate"], optional = true }
instant = { version = "^0.1.13", features = [
"wasm-bindgen",
"inaccurate",
], optional = true }
napi = { version = "^2.16.12", optional = true }

[features]
wasm-js = ["wasm-bindgen", "instant"]
napi-types = ["napi"]

[dev-dependencies]
codspeed-criterion-compat = "2.7.2"
criterion = "0.5.1"

[[bench]]
name = "get_all_subset_sum"
harness = false

[[bench]]
name = "get_subset_sum"
harness = false
21 changes: 21 additions & 0 deletions crates/subset-sum/benches/get_all_subset_sum.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
use codspeed_criterion_compat::{criterion_group, criterion_main, Criterion};
use subset_sum::{get_all_subset_sums, SubsetSumAllResult};

fn benchmark_all_subset_sums(c: &mut Criterion) {
// Define a sample list and sum for benchmarking
let list = vec![3, 34, 4, 12, 5, 2];
let sum = 9;
let timeout_in_ms = None;

// Benchmark the get_all_subset_sums function
c.bench_function("get_all_subset_sums", |b| {
b.iter(|| {
let result: SubsetSumAllResult<i32> =
get_all_subset_sums(list.clone(), sum, timeout_in_ms);
criterion::black_box(result)
});
});
}

criterion_group!(benches, benchmark_all_subset_sums);
criterion_main!(benches);
20 changes: 20 additions & 0 deletions crates/subset-sum/benches/get_subset_sum.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
use codspeed_criterion_compat::{criterion_group, criterion_main, Criterion};
use subset_sum::{get_subset_sum, SubsetSumResult};

fn benchmark_subset_sum(c: &mut Criterion) {
// Define a sample list and sum for benchmarking
let list = vec![3, 34, 4, 12, 5, 2];
let sum = 9;
let timeout_in_ms = None;

// Benchmark the get_subset_sum function
c.bench_function("get_subset_sum", |b| {
b.iter(|| {
let result: SubsetSumResult<i32> = get_subset_sum(list.clone(), sum, timeout_in_ms);
criterion::black_box(result)
});
});
}

criterion_group!(benches, benchmark_subset_sum);
criterion_main!(benches);

0 comments on commit 0f9d0e0

Please sign in to comment.