Skip to content

Commit 54bfae9

Browse files
feat(divan_compat): add support for args divan macro argument, add more examples
1 parent 8995959 commit 54bfae9

File tree

21 files changed

+865
-90
lines changed

21 files changed

+865
-90
lines changed

Cargo.lock

Lines changed: 66 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
[workspace]
22
members = [
3-
"crates/codspeed",
4-
"crates/bencher_compat",
5-
"crates/criterion_compat",
6-
"crates/cargo-codspeed",
7-
"crates/divan_compat",
8-
"crates/divan_compat/macros",
9-
"crates/divan_compat/divan_fork",
3+
"crates/codspeed",
4+
"crates/bencher_compat",
5+
"crates/criterion_compat",
6+
"crates/cargo-codspeed",
7+
8+
"crates/divan_compat",
9+
"crates/divan_compat/macros",
10+
"crates/divan_compat/divan_fork",
11+
"crates/divan_compat/examples",
1012
]
1113
resolver = "2"
1214

crates/divan_compat/Cargo.toml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ repository = "https://github.com/CodSpeedHQ/codspeed-rust"
1111
homepage = "https://codspeed.io"
1212
license = "MIT OR Apache-2.0"
1313
categories = [
14-
"development-tools",
15-
"development-tools::profiling",
16-
"development-tools::testing",
14+
"development-tools",
15+
"development-tools::profiling",
16+
"development-tools::testing",
1717
]
1818
keywords = ["codspeed", "benchmark", "divan"]
1919

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
use codspeed_divan_compat as divan;
2-
31
fn fibo(n: i32) -> i32 {
42
let mut a = 0;
53
let mut b = 1;
@@ -13,17 +11,16 @@ fn fibo(n: i32) -> i32 {
1311
a
1412
}
1513

16-
#[divan::bench]
14+
#[codspeed_divan_compat::bench]
1715
fn fibo_500() -> i32 {
18-
divan::black_box(fibo(500))
16+
codspeed_divan_compat::black_box(fibo(500))
1917
}
2018

21-
#[divan::bench]
22-
fn fibo_100() -> i32 {
23-
divan::black_box(fibo(10))
19+
#[codspeed_divan_compat::bench]
20+
fn fibo_10() -> i32 {
21+
codspeed_divan_compat::black_box(fibo(10))
2422
}
2523

2624
fn main() {
27-
// Run `add` benchmark:
28-
divan::main();
25+
codspeed_divan_compat::main();
2926
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
[package]
2+
name = "codspeed-divan-compat-examples"
3+
version = "0.0.0"
4+
edition = "2021"
5+
description = "Examples for Divan, a comfy benchmarking framework."
6+
publish = false
7+
license = "MIT OR Apache-2.0"
8+
9+
[dependencies]
10+
fastrand = "2.3.0"
11+
divan = { package = "codspeed-divan-compat", path = ".." }
12+
13+
[[bench]]
14+
name = "math"
15+
harness = false
16+
17+
[[bench]]
18+
name = "sort"
19+
harness = false
20+
21+
[[bench]]
22+
name = "time"
23+
harness = false
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Example benchmarks
2+
3+
Mostly copied from [divan's examples](https://github.com/nvzqz/divan/tree/main/examples).
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
use divan::black_box;
2+
use std::collections::{BTreeMap, HashMap};
3+
4+
fn main() {
5+
divan::main();
6+
}
7+
8+
#[divan::bench]
9+
fn add() -> i32 {
10+
black_box(2) + black_box(1)
11+
}
12+
13+
#[divan::bench]
14+
#[ignore]
15+
fn sub() -> i32 {
16+
black_box(2) - black_box(1)
17+
}
18+
19+
#[divan::bench]
20+
fn mul() -> i32 {
21+
black_box(2) * black_box(1)
22+
}
23+
24+
#[divan::bench]
25+
fn div() -> i32 {
26+
black_box(2) / black_box(1)
27+
}
28+
29+
#[divan::bench]
30+
fn rem() -> i32 {
31+
black_box(2) % black_box(1)
32+
}
33+
34+
// 1, 1, 2, 3, 5, ...
35+
mod fibonacci {
36+
use super::*;
37+
38+
const VALUES: &[u64] = &[0, 5, 10, 20, 30];
39+
40+
// O(n)
41+
#[divan::bench(args = VALUES)]
42+
fn iterative(n: u64) -> u64 {
43+
let mut previous = 1;
44+
let mut current = 1;
45+
46+
for _ in 2..=n {
47+
let next = previous + current;
48+
previous = current;
49+
current = next;
50+
}
51+
52+
current
53+
}
54+
55+
// O(2^n)
56+
#[divan::bench(args = VALUES, max_time = 1)]
57+
fn recursive(n: u64) -> u64 {
58+
if n <= 1 {
59+
1
60+
} else {
61+
recursive(n - 2) + recursive(n - 1)
62+
}
63+
}
64+
65+
#[allow(dead_code)]
66+
trait Map: Default {
67+
fn get(&self, key: u64) -> Option<u64>;
68+
fn set(&mut self, key: u64, value: u64);
69+
}
70+
71+
impl Map for HashMap<u64, u64> {
72+
fn get(&self, key: u64) -> Option<u64> {
73+
self.get(&key).copied()
74+
}
75+
76+
fn set(&mut self, key: u64, value: u64) {
77+
self.insert(key, value);
78+
}
79+
}
80+
81+
impl Map for BTreeMap<u64, u64> {
82+
fn get(&self, key: u64) -> Option<u64> {
83+
self.get(&key).copied()
84+
}
85+
86+
fn set(&mut self, key: u64, value: u64) {
87+
self.insert(key, value);
88+
}
89+
}
90+
91+
// Will be ignored in instrumented mode as we do not support type generics yet
92+
// O(n)
93+
#[cfg(not(codspeed))]
94+
#[divan::bench(
95+
types = [BTreeMap<u64, u64>, HashMap<u64, u64>],
96+
args = VALUES,
97+
)]
98+
fn recursive_memoized<M: Map>(n: u64) -> u64 {
99+
fn fibonacci<M: Map>(n: u64, cache: &mut M) -> u64 {
100+
if let Some(result) = cache.get(n) {
101+
return result;
102+
}
103+
104+
if n <= 1 {
105+
return 1;
106+
}
107+
108+
let result = fibonacci(n - 2, cache) + fibonacci(n - 1, cache);
109+
cache.set(n, result);
110+
result
111+
}
112+
113+
fibonacci(n, &mut M::default())
114+
}
115+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
use divan::{AllocProfiler, Bencher};
2+
3+
#[global_allocator]
4+
static ALLOC: AllocProfiler = AllocProfiler::system();
5+
6+
fn main() {
7+
divan::main();
8+
}
9+
10+
/// Functions that generate deterministic values.
11+
mod gen {
12+
pub const LEN: usize = 100_000;
13+
14+
pub fn rand_int_generator() -> impl FnMut() -> i32 {
15+
let mut rng = fastrand::Rng::with_seed(42);
16+
move || rng.i32(..)
17+
}
18+
19+
pub fn rand_int_vec_generator() -> impl FnMut() -> Vec<i32> {
20+
let mut rand_int_generator = rand_int_generator();
21+
move || (0..LEN).map(|_| rand_int_generator()).collect()
22+
}
23+
24+
pub fn sorted_int_vec_generator() -> impl FnMut() -> Vec<i32> {
25+
move || (0..LEN).map(|i| i as i32).collect()
26+
}
27+
}
28+
29+
mod random {
30+
use super::*;
31+
32+
#[divan::bench]
33+
fn sort(bencher: Bencher) {
34+
bencher
35+
.with_inputs(gen::rand_int_vec_generator())
36+
.bench_local_refs(|v| v.sort());
37+
}
38+
39+
#[divan::bench]
40+
fn sort_unstable(bencher: Bencher) {
41+
bencher
42+
.with_inputs(gen::rand_int_vec_generator())
43+
.bench_local_refs(|v| v.sort_unstable());
44+
}
45+
}
46+
47+
mod sorted {
48+
use super::*;
49+
50+
#[divan::bench]
51+
fn sort(bencher: Bencher) {
52+
bencher
53+
.with_inputs(gen::sorted_int_vec_generator())
54+
.bench_local_refs(|v| v.sort());
55+
}
56+
57+
#[divan::bench]
58+
fn sort_unstable(bencher: Bencher) {
59+
bencher
60+
.with_inputs(gen::sorted_int_vec_generator())
61+
.bench_local_refs(|v| v.sort_unstable());
62+
}
63+
}

0 commit comments

Comments
 (0)