Skip to content

Add codspeed<>divan compat layer #65

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Feb 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 66 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 9 additions & 7 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
[workspace]
members = [
"crates/codspeed",
"crates/bencher_compat",
"crates/criterion_compat",
"crates/cargo-codspeed",
"crates/divan_compat",
"crates/divan_compat/macros",
"crates/divan_compat/divan_fork",
"crates/codspeed",
"crates/bencher_compat",
"crates/criterion_compat",
"crates/cargo-codspeed",

"crates/divan_compat",
"crates/divan_compat/macros",
"crates/divan_compat/divan_fork",
"crates/divan_compat/examples",
]
resolver = "2"

Expand Down
6 changes: 3 additions & 3 deletions crates/divan_compat/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ repository = "https://github.com/CodSpeedHQ/codspeed-rust"
homepage = "https://codspeed.io"
license = "MIT OR Apache-2.0"
categories = [
"development-tools",
"development-tools::profiling",
"development-tools::testing",
"development-tools",
"development-tools::profiling",
"development-tools::testing",
]
keywords = ["codspeed", "benchmark", "divan"]

Expand Down
15 changes: 6 additions & 9 deletions crates/divan_compat/benches/basic_example.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use codspeed_divan_compat as divan;

fn fibo(n: i32) -> i32 {
let mut a = 0;
let mut b = 1;
Expand All @@ -13,17 +11,16 @@ fn fibo(n: i32) -> i32 {
a
}

#[divan::bench]
#[codspeed_divan_compat::bench]
fn fibo_500() -> i32 {
divan::black_box(fibo(500))
codspeed_divan_compat::black_box(fibo(500))
}

#[divan::bench]
fn fibo_100() -> i32 {
divan::black_box(fibo(10))
#[codspeed_divan_compat::bench]
fn fibo_10() -> i32 {
codspeed_divan_compat::black_box(fibo(10))
}

fn main() {
// Run `add` benchmark:
divan::main();
codspeed_divan_compat::main();
}
3 changes: 3 additions & 0 deletions crates/divan_compat/divan_fork/src/divan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,9 @@ impl Divan {
if should_compute_stats {
let stats = bench_context.compute_stats();
{
// WARNING: Keep in sync with `codspeed-divan-compat::uri::generate`
// Not worth doing the work of actually using the same code since this fork
// is temporary
let name = bench_entry.display_name().to_string();
let file = bench_entry.meta().location.file;
let mut module_path = bench_entry
Expand Down
23 changes: 23 additions & 0 deletions crates/divan_compat/examples/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
[package]
name = "codspeed-divan-compat-examples"
version = "0.0.0"
edition = "2021"
description = "Examples for Divan, a comfy benchmarking framework."
publish = false
license = "MIT OR Apache-2.0"

[dependencies]
fastrand = "2.3.0"
divan = { package = "codspeed-divan-compat", path = ".." }

[[bench]]
name = "math"
harness = false

[[bench]]
name = "sort"
harness = false

[[bench]]
name = "time"
harness = false
3 changes: 3 additions & 0 deletions crates/divan_compat/examples/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Example benchmarks

Mostly copied from [divan's examples](https://github.com/nvzqz/divan/tree/main/examples).
116 changes: 116 additions & 0 deletions crates/divan_compat/examples/benches/math.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
//! Copied from divan's exmaples
use divan::black_box;
use std::collections::{BTreeMap, HashMap};

fn main() {
divan::main();
}

#[divan::bench]
fn add() -> i32 {
black_box(2) + black_box(1)
}

#[divan::bench]
#[ignore]
fn sub() -> i32 {
black_box(2) - black_box(1)
}

#[divan::bench]
fn mul() -> i32 {
black_box(2) * black_box(1)
}

#[divan::bench]
fn div() -> i32 {
black_box(2) / black_box(1)
}

#[divan::bench]
fn rem() -> i32 {
black_box(2) % black_box(1)
}

// 1, 1, 2, 3, 5, ...
mod fibonacci {
use super::*;

const VALUES: &[u64] = &[0, 5, 10, 20, 30];

// O(n)
#[divan::bench(args = VALUES)]
fn iterative(n: u64) -> u64 {
let mut previous = 1;
let mut current = 1;

for _ in 2..=n {
let next = previous + current;
previous = current;
current = next;
}

current
}

// O(2^n)
#[divan::bench(args = VALUES, max_time = 1)]
fn recursive(n: u64) -> u64 {
if n <= 1 {
1
} else {
recursive(n - 2) + recursive(n - 1)
}
}

#[allow(dead_code)]
trait Map: Default {
fn get(&self, key: u64) -> Option<u64>;
fn set(&mut self, key: u64, value: u64);
}

impl Map for HashMap<u64, u64> {
fn get(&self, key: u64) -> Option<u64> {
self.get(&key).copied()
}

fn set(&mut self, key: u64, value: u64) {
self.insert(key, value);
}
}

impl Map for BTreeMap<u64, u64> {
fn get(&self, key: u64) -> Option<u64> {
self.get(&key).copied()
}

fn set(&mut self, key: u64, value: u64) {
self.insert(key, value);
}
}

// Will be ignored in instrumented mode as we do not support type generics yet
// O(n)
#[cfg(not(codspeed))]
#[divan::bench(
types = [BTreeMap<u64, u64>, HashMap<u64, u64>],
args = VALUES,
)]
fn recursive_memoized<M: Map>(n: u64) -> u64 {
fn fibonacci<M: Map>(n: u64, cache: &mut M) -> u64 {
if let Some(result) = cache.get(n) {
return result;
}

if n <= 1 {
return 1;
}

let result = fibonacci(n - 2, cache) + fibonacci(n - 1, cache);
cache.set(n, result);
result
}

fibonacci(n, &mut M::default())
}
}
Loading