Skip to content

Commit

Permalink
Bench/ci continue profiling (#35)
Browse files Browse the repository at this point in the history
* add: bench
  • Loading branch information
shenxiangzhuang authored Apr 29, 2024
1 parent 78b85d0 commit 9479127
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 0 deletions.
25 changes: 25 additions & 0 deletions .github/workflows/bench.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
name: Rust Bencher

on:
push:
branches: [ "master" ]
pull_request:
branches: [ "master" ]

jobs:
benchmark_base_branch:
name: Continuous Benchmarking with Bencher
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: bencherdev/bencher@main
- name: Track base branch benchmarks with Bencher
run: |
bencher run \
--project bleuscore \
--token '${{ secrets.BENCHER_API_TOKEN }}' \
--branch master \
--testbed ubuntu-latest \
--adapter rust \
--err \
cargo bench --all
2 changes: 2 additions & 0 deletions rust-toolchain.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[toolchain]
channel = "nightly"
20 changes: 20 additions & 0 deletions src/bleu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,3 +131,23 @@ mod test {
assert!((res.bleu - 0.668740304976422).abs() < 1e-10);
}
}

#[cfg(test)]
mod benchmark {
use crate::bleu::compute_score;
use test::Bencher;
#[bench]
fn bench_bleu(b: &mut Bencher) {
let max_order: usize = 4;
let smooth: bool = true;
let references: Vec<Vec<String>> = vec![vec!["Hello, World!".to_string()]];
let predictions: Vec<String> = vec!["Yellow, World!".to_string()];

let iter_num: usize = 100;
b.iter(|| {
std::hint::black_box(for _ in 1..=iter_num {
compute_score(&references, &predictions, max_order, smooth);
});
});
}
}
3 changes: 3 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ println!("result: {:?}", res);
// brevity_penalty: 1.0, length_ratio: 1.0, translation_length: 4, reference_length: 4 }
```
!*/
#![feature(test)]
extern crate test;

mod tokenizer;
pub use crate::tokenizer::{Tokenizer, Tokenizer13a, TokenizerRegex};
mod bleu;
Expand Down
20 changes: 20 additions & 0 deletions src/ngram.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,3 +95,23 @@ mod test {
assert_eq!(counter[&"aabc"], 1);
}
}

#[cfg(test)]
mod benchmark {
use crate::ngram::{get_ngram_counter, get_token_ngram_counter};
use std::cmp::max;
use test::Bencher;

#[bench]
fn bench_ngram(b: &mut Bencher) {
let line = "aabc";
let max_order = 4;

let iter_num: usize = 100;
b.iter(|| {
std::hint::black_box(for _ in 1..=iter_num {
get_ngram_counter(line, max_order);
});
});
}
}
19 changes: 19 additions & 0 deletions src/tokenizer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,3 +143,22 @@ mod test {
)
}
}

#[cfg(test)]
mod benchmark {
use crate::tokenizer;
use crate::tokenizer::Tokenizer;
use test::Bencher;
#[bench]
fn bench_tokenizer(b: &mut Bencher) {
let tokenizer_regex = tokenizer::Tokenizer13a::new();
let line = "Hello, &quot;World!<skipped>";

let iter_num: usize = 100;
b.iter(|| {
std::hint::black_box(for _ in 1..=iter_num {
tokenizer_regex.tokenize(line);
});
});
}
}

0 comments on commit 9479127

Please sign in to comment.