From 7c87fb4afb9c1e97c8ec8ce8a804741dc1741005 Mon Sep 17 00:00:00 2001 From: Sylvestre Ledru Date: Sat, 7 Mar 2026 23:02:34 +0100 Subject: [PATCH] add divan benchmarks and CodSpeed workflow --- .github/workflows/codspeed.yml | 37 +++++++++++++++++ Cargo.toml | 8 ++++ benches/bench-grid.rs | 76 ++++++++++++++++++++++++++++++++++ 3 files changed, 121 insertions(+) create mode 100644 .github/workflows/codspeed.yml create mode 100644 benches/bench-grid.rs diff --git a/.github/workflows/codspeed.yml b/.github/workflows/codspeed.yml new file mode 100644 index 0000000..a1ffc51 --- /dev/null +++ b/.github/workflows/codspeed.yml @@ -0,0 +1,37 @@ +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: + +permissions: + contents: read + id-token: write + +jobs: + codspeed: + name: Run benchmarks + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v6 + + - 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 -m simulation + + - name: Run the benchmarks + uses: CodSpeedHQ/action@v4 + with: + mode: simulation + run: cargo codspeed run diff --git a/Cargo.toml b/Cargo.toml index f21ed07..38d9017 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -16,3 +16,11 @@ name = "term_grid" [dependencies] ansi-width = "0.1.0" + +[dev-dependencies] +divan = { version = "4.3.0", package = "codspeed-divan-compat" } + +[[bench]] +name = "bench_grid" +path = "benches/bench-grid.rs" +harness = false diff --git a/benches/bench-grid.rs b/benches/bench-grid.rs new file mode 100644 index 0000000..b170700 --- /dev/null +++ b/benches/bench-grid.rs @@ -0,0 +1,76 @@ +// For the full copyright and license information, please view the LICENSE +// file that was distributed with this source code. + +use std::hint::black_box; + +use divan::Bencher; +use term_grid::{Direction, Filling, Grid, GridOptions}; + +const CELL_COUNTS: [usize; 4] = [10, 100, 1_000, 10_000]; + +fn generate_cells(n: usize) -> Vec { + (0..n).map(|i| format!("file-{i:04}.txt")).collect() +} + +#[divan::bench(args = CELL_COUNTS)] +fn grid_top_to_bottom(bencher: Bencher, n: usize) { + let cells = generate_cells(n); + bencher.with_inputs(|| cells.clone()).bench_values(|cells| { + black_box(Grid::new( + cells, + GridOptions { + direction: Direction::TopToBottom, + filling: Filling::Spaces(2), + width: 80, + }, + )) + }); +} + +#[divan::bench(args = CELL_COUNTS)] +fn grid_left_to_right(bencher: Bencher, n: usize) { + let cells = generate_cells(n); + bencher.with_inputs(|| cells.clone()).bench_values(|cells| { + black_box(Grid::new( + cells, + GridOptions { + direction: Direction::LeftToRight, + filling: Filling::Spaces(2), + width: 80, + }, + )) + }); +} + +#[divan::bench(args = CELL_COUNTS)] +fn grid_display(bencher: Bencher, n: usize) { + let cells = generate_cells(n); + let grid = Grid::new( + cells, + GridOptions { + direction: Direction::TopToBottom, + filling: Filling::Spaces(2), + width: 80, + }, + ); + bencher.bench(|| black_box(grid.to_string())); +} + +#[divan::bench(args = [20, 80, 160, 320])] +fn grid_varying_width(bencher: Bencher, width: usize) { + let cells = generate_cells(500); + bencher.with_inputs(|| cells.clone()).bench_values(|cells| { + black_box(Grid::new( + cells, + GridOptions { + direction: Direction::TopToBottom, + filling: Filling::Spaces(2), + width, + }, + )) + }); +} + +fn main() { + divan::main(); +}