diff --git a/.github/workflows/criterion-rs.yml b/.github/workflows/criterion-rs.yml new file mode 100644 index 000000000..17a599a75 --- /dev/null +++ b/.github/workflows/criterion-rs.yml @@ -0,0 +1,29 @@ +name: Criterion.rs Example +on: + push: + branches: + - master + +jobs: + benchmark: + name: Run Criterion.rs benchmark example + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + - run: rustup toolchain update + - name: Run benchmark + run: cd examples/criterion-rs && cargo bench -- --output-format bencher | tee output.txt + - name: Store benchmark result + uses: rhysd/github-action-benchmark@v1 + with: + name: Rust Benchmark + tool: 'cargo' + output-file-path: examples/criterion-rs/output.txt + # Use personal access token instead of GITHUB_TOKEN due to https://github.community/t5/GitHub-Actions/Github-action-not-triggering-gh-pages-upon-push/td-p/26869/highlight/false + github-token: ${{ secrets.PERSONAL_GITHUB_TOKEN }} + auto-push: true + # Show alert with commit comment on detecting possible performance regression + alert-threshold: '200%' + comment-on-alert: true + fail-on-alert: true + alert-comment-cc-users: '@rhysd' diff --git a/.gitignore b/.gitignore index 163dbb49b..6f7ba1563 100644 --- a/.gitignore +++ b/.gitignore @@ -3,6 +3,8 @@ /src/*.js.map /examples/rust/Cargo.lock /examples/rust/target +/examples/criterion-rs/Cargo.lock +/examples/criterion-rs/target /test/*.js /test/*.js.map /scripts/*.js diff --git a/examples/criterion-rs/Cargo.toml b/examples/criterion-rs/Cargo.toml new file mode 100644 index 000000000..9f9adcb08 --- /dev/null +++ b/examples/criterion-rs/Cargo.toml @@ -0,0 +1,17 @@ +[package] +name = "criterion_example" +version = "0.1.0" +authors = ["rhysd "] +edition = "2018" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +criterion = "*" + +[lib] +bench = false + +[[bench]] +name = "bench" +harness = false diff --git a/examples/criterion-rs/README.md b/examples/criterion-rs/README.md new file mode 100644 index 000000000..70c7fa903 --- /dev/null +++ b/examples/criterion-rs/README.md @@ -0,0 +1,37 @@ +# Rust Criterion example for benchmarking with `cargo bench` + +- [Workflow for this example](../../.github/workflows/criterion-rs.yml) +- [Benchmark results on GitHub pages](https://rhysd.github.io/github-action-benchmark/dev/bench/) + +This directory shows how to use [`github-action-benchmark`](https://github.com/rhysd/github-action-benchmark) +with [`criterion`](https://github.com/bheisler/criterion.rs). + +## Run benchmarks + +Official documentation for usage of `cargo bench` with Criterion: + +https://github.com/bheisler/criterion.rs + +e.g. + +```yaml +- name: Run benchmark + run: cargo bench -- --output-format bencher | tee output.txt +``` + +Note that you should run the benchmarks using the bencher output format. + + +## Process benchmark results + +Store the benchmark results with step using the action. Please set `cargo` to `tool` input. + +```yaml +- name: Store benchmark result + uses: rhysd/github-action-benchmark@v1 + with: + tool: 'cargo' + output-file-path: output.txt +``` + +Please read ['How to use' section](https://github.com/rhysd/github-action-benchmark#how-to-use) for common usage. diff --git a/examples/criterion-rs/benches/bench.rs b/examples/criterion-rs/benches/bench.rs new file mode 100644 index 000000000..420f852a3 --- /dev/null +++ b/examples/criterion-rs/benches/bench.rs @@ -0,0 +1,35 @@ +#[macro_use] +extern crate criterion; +use criterion::{black_box, Criterion, BenchmarkId}; +use criterion_example::{fib, fast_fib}; + +fn bench_fib_10(c: &mut Criterion) { + c.bench_function("BenchFib10", move |b| { + b.iter(|| { + let _ = fib(black_box(10)); + }); + }); +} + +fn bench_fib_20(c: &mut Criterion) { + c.bench_function("BenchFib20", move |b| { + b.iter(|| { + let _ = fib(20); + }); + }); +} + +fn bench_fibs(c: &mut Criterion) { + let mut group = c.benchmark_group("Fibonacci"); + for i in [20, 21].iter() { + group.bench_with_input(BenchmarkId::new("Recursive", i), i, + |b, i| b.iter(|| fib(*i))); + group.bench_with_input(BenchmarkId::new("Iterative", i), i, + |b, i| b.iter(|| fast_fib(*i))); + } + group.finish(); +} + + +criterion_group!(benches, bench_fib_10, bench_fib_20, bench_fibs); +criterion_main!(benches); diff --git a/examples/criterion-rs/src/lib.rs b/examples/criterion-rs/src/lib.rs new file mode 100644 index 000000000..a42246eb6 --- /dev/null +++ b/examples/criterion-rs/src/lib.rs @@ -0,0 +1,25 @@ +pub fn fib(u: u32) -> u32 { + if u <= 1 { + 1 + } else { + fib(u - 2) + fib(u - 1) + } +} + +pub fn fast_fib(n: u32) -> u32 { + let mut a = 0; + let mut b = 1; + + match n { + 0 => b, + _ => { + for _ in 0..n { + let c = a + b; + a = b; + b = c; + } + b + } + } +} + diff --git a/src/extract.ts b/src/extract.ts index a8d2c4ee9..84a1e9b8b 100644 --- a/src/extract.ts +++ b/src/extract.ts @@ -178,7 +178,7 @@ function extractCargoResult(output: string): BenchmarkResult[] { const ret = []; // Example: // test bench_fib_20 ... bench: 37,174 ns/iter (+/- 7,527) - const reExtract = /^test (\w+)\s+\.\.\. bench:\s+([0-9,]+) ns\/iter \(\+\/- ([0-9,]+)\)$/; + const reExtract = /^test ([\w/]+)\s+\.\.\. bench:\s+([0-9,]+) ns\/iter \(\+\/- ([0-9,]+)\)$/; const reComma = /,/g; for (const line of lines) {