add divan benchmarks and CodSpeed workflow

This commit is contained in:
Sylvestre Ledru
2026-03-07 23:06:16 +01:00
parent 1bebe9c401
commit 7c87fb4afb
3 changed files with 121 additions and 0 deletions
+37
View File
@@ -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
+8
View File
@@ -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
+76
View File
@@ -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<String> {
(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();
}