Skip to content

Commit

Permalink
feat: benchmark github action
Browse files Browse the repository at this point in the history
  • Loading branch information
Dan Alin committed Dec 2, 2024
1 parent 810cfc4 commit 2d3ddba
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 21 deletions.
51 changes: 51 additions & 0 deletions .github/workflows/bench.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
name: Bench

on:
push:
branches:
- main

jobs:
build-and-test:
name: Build and Test
runs-on: ubuntu-latest

steps:
- name: Checkout Code
uses: actions/checkout@v4

- name: Set up Rust
uses: actions-rust-lang/setup-rust-toolchain@v1

- name: Cache Cargo Registry
uses: actions/cache@v4
with:
path: ~/.cargo/registry
key: ${{ runner.os }}-cargo-registry-${{ hashFiles('**/Cargo.lock') }}
restore-keys: |
${{ runner.os }}-cargo-registry-
- name: Cache Cargo Build
uses: actions/cache@v4
with:
path: target
key: ${{ runner.os }}-cargo-build-${{ hashFiles('**/Cargo.lock') }}
restore-keys: |
${{ runner.os }}-cargo-build-
- name: Build Project
run: cargo build --workspace --release

- name: Run Tests
run: cargo test --workspace --all-features -- --nocapture

- name: Run Benchmarks and Save Output
run: |
mkdir -p benchmarks_output
cargo bench --workspace --all-features | grep -A 100 'benchmarks' > benchmarks_output/cargo_bench_output.txt
- name: Upload Benchmarks Output
uses: actions/upload-artifact@v4
with:
name: benchmarks_output
path: benchmarks_output/cargo_bench_output.txt
24 changes: 8 additions & 16 deletions src/day_one/mod.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@

fn prepare(file:&str) -> (Vec<i32>, Vec<i32>){

let mut left:Vec<i32> = Vec::new();
let mut right:Vec<i32> = Vec::new();
fn prepare(file: &str) -> (Vec<i32>, Vec<i32>) {
let mut left: Vec<i32> = Vec::new();
let mut right: Vec<i32> = Vec::new();

for line in file.lines() {
if let [left_value, right_value] = line.split_whitespace().collect::<Vec<&str>>()[..] {
Expand All @@ -12,16 +10,13 @@ fn prepare(file:&str) -> (Vec<i32>, Vec<i32>){
}

(left, right)

}



pub fn part_one(file:&str)-> i32 {
pub fn part_one(file: &str) -> i32 {
let (mut left, mut right) = prepare(file);

let mut count = 0;

left.sort();
right.sort();

Expand All @@ -30,15 +25,12 @@ pub fn part_one(file:&str)-> i32 {
}

count

}


pub fn part_two(file:&str)-> i32 {
let ( left, right) = prepare(file);
pub fn part_two(file: &str) -> i32 {
let (left, right) = prepare(file);

let mut similarity = 0;


let mut right_map: std::collections::HashMap<i32, i32> = std::collections::HashMap::new();
for value in right {
Expand All @@ -52,4 +44,4 @@ pub fn part_two(file:&str)-> i32 {
}

similarity
}
}
6 changes: 1 addition & 5 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
mod day_one;
mod day_two;



fn main() {

let d1_input = include_str!("../assets/input_day_one.txt");
let d2_input = include_str!("../assets/input_day_two.txt");


println!("day one - part one: {}", day_one::part_one(&d1_input));
println!("day one - part one: {}", day_one::part_one(&d1_input));
println!("day one - part two: {}", day_one::part_two(&d1_input));
println!("day two - part one: {}", day_two::part_one(&d2_input));
println!("day two - part two: {}", day_two::part_two(&d2_input));
Expand Down

0 comments on commit 2d3ddba

Please sign in to comment.