Skip to content

Commit

Permalink
Use iai-callgrind instead of iai
Browse files Browse the repository at this point in the history
  • Loading branch information
frengor committed Nov 7, 2023
1 parent 848cad6 commit 70194a5
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 32 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ pedantic-debug-assertions = []
thiserror = "1.0.37"

[dev-dependencies]
iai = "0.1.1"
iai-callgrind = "0.7.3"
rand = "0.8.3"

[[bench]]
Expand Down
54 changes: 48 additions & 6 deletions benches/bench.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
///! Same benchmarks of [rust-cc-benchmarks](https://github.com/frengor/rust-cc-benchmarks), but run with [iai](https://github.com/bheisler/iai).
//! Same benchmarks of [rust-cc-benchmarks](https://github.com/frengor/rust-cc-benchmarks), but run with [iai-callgrind](https://github.com/iai-callgrind/iai-callgrind).

mod benches {
pub(super) mod stress_test;
Expand All @@ -7,9 +7,51 @@ mod benches {
pub(super) mod large_linked_list;
}

use benches::stress_test::benchmark_stress_test;
use benches::binary_trees::benchmark_count_binary_trees;
use benches::binary_trees_with_parent_pointers::benchmark_count_binary_trees_with_parent;
use benches::large_linked_list::benchmark_large_linked_list;
use std::hint::black_box;
use iai_callgrind::{library_benchmark, library_benchmark_group, main};
use crate::benches::binary_trees::count_binary_trees;
use crate::benches::binary_trees_with_parent_pointers::count_binary_trees_with_parent;
use crate::benches::large_linked_list::large_linked_list;
use crate::benches::stress_test::stress_test;

iai::main!(benchmark_stress_test, benchmark_count_binary_trees, benchmark_count_binary_trees_with_parent, benchmark_large_linked_list);
#[library_benchmark]
#[bench::seed(0xCAFE)]
fn stress_test_bench(seed: u64) -> Vec<usize> {
black_box(stress_test(seed))
}

#[library_benchmark]
#[bench::depth(11)]
fn count_binary_trees_bench(depth: usize) -> Vec<usize> {
black_box(count_binary_trees(depth))
}

#[library_benchmark]
#[bench::depth(11)]
fn count_binary_trees_with_parent_bench(depth: usize) -> Vec<usize> {
black_box(count_binary_trees_with_parent(depth))
}

#[library_benchmark]
#[bench::small(1024)]
#[bench::big(4096)]
fn linked_list_bench(size: usize) -> Vec<usize> {
black_box(large_linked_list(size))
}

library_benchmark_group!(
name = stress_tests_group;
benchmarks = stress_test_bench
);

library_benchmark_group!(
name = binary_trees_group;
benchmarks = count_binary_trees_bench, count_binary_trees_with_parent_bench
);

library_benchmark_group!(
name = linked_lists_group;
benchmarks = linked_list_bench
);

main!(library_benchmark_groups = stress_tests_group, binary_trees_group, linked_lists_group);
8 changes: 1 addition & 7 deletions benches/benches/binary_trees.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
//! Benchmark adapted from the shredder crate, released under MIT license. Src: https://github.com/Others/shredder/blob/266de5a3775567463ee82febc42eed1c9a8b6197/benches/shredder_benchmark.rs

use std::hint::black_box;

use rust_cc::*;

// BENCHMARK 2: It's binary-trees from the benchmarks game!

fn count_binary_trees(max_size: usize) -> Vec<usize> {
pub fn count_binary_trees(max_size: usize) -> Vec<usize> {
let mut res = Vec::new();
{
let min_size = 4;
Expand Down Expand Up @@ -64,7 +62,3 @@ impl TreeNode {
}
}
}

pub fn benchmark_count_binary_trees() {
count_binary_trees(black_box(11));
}
7 changes: 1 addition & 6 deletions benches/benches/binary_trees_with_parent_pointers.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
//! Benchmark adapted from the shredder crate, released under MIT license. Src: https://github.com/Others/shredder/blob/266de5a3775567463ee82febc42eed1c9a8b6197/benches/shredder_benchmark.rs

use std::cell::RefCell;
use std::hint::black_box;

use rust_cc::*;

// BENCHMARK 3: Same as benchmark 2, but with parent pointers. Added by rust-cc

fn count_binary_trees_with_parent(max_size: usize) -> Vec<usize> {
pub fn count_binary_trees_with_parent(max_size: usize) -> Vec<usize> {
let mut res = Vec::new();
{
let min_size = 4;
Expand Down Expand Up @@ -115,7 +114,3 @@ impl TreeNodeWithParent {
}
}
}

pub fn benchmark_count_binary_trees_with_parent() {
count_binary_trees_with_parent(black_box(11));
}
7 changes: 1 addition & 6 deletions benches/benches/large_linked_list.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
use std::cell::RefCell;
use std::hint::black_box;

use rust_cc::*;

fn large_linked_list(size: usize) -> Vec<usize> {
pub fn large_linked_list(size: usize) -> Vec<usize> {
let mut res = Vec::new();
for _ in 0..30 {
let mut list = List::new();
Expand Down Expand Up @@ -72,7 +71,3 @@ unsafe impl Trace for Node {
}

impl Finalize for Node {}

pub fn benchmark_large_linked_list() {
large_linked_list(black_box(4096));
}
8 changes: 2 additions & 6 deletions benches/benches/stress_test.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
//! Benchmark adapted from the shredder crate, released under MIT license. Src: https://github.com/Others/shredder/blob/266de5a3775567463ee82febc42eed1c9a8b6197/benches/shredder_benchmark.rs

use std::cell::RefCell;
use std::hint::black_box;
use rand::rngs::StdRng;
use rand::SeedableRng;
use rand::seq::SliceRandom;
Expand All @@ -27,7 +26,8 @@ const NODE_COUNT: usize = 1 << 15;
const EDGE_COUNT: usize = 1 << 15;
const SHRINK_DIV: usize = 1 << 10;

fn stress_test(rng: &mut StdRng) -> Vec<usize> {
pub fn stress_test(seed: u64) -> Vec<usize> {
let rng = &mut StdRng::seed_from_u64(seed);
let mut res = Vec::new();
{
let mut nodes = Vec::new();
Expand Down Expand Up @@ -57,7 +57,3 @@ fn stress_test(rng: &mut StdRng) -> Vec<usize> {
collect_cycles();
res
}

pub fn benchmark_stress_test() {
stress_test(black_box(&mut StdRng::seed_from_u64(0xCAFE)));
}

0 comments on commit 70194a5

Please sign in to comment.