Skip to content

Commit 8104c13

Browse files
feat(divan_compat): add support for args divan macro argument, add more examples
1 parent d77ff9f commit 8104c13

File tree

21 files changed

+877
-96
lines changed

21 files changed

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

0 commit comments

Comments
 (0)