Skip to content

Commit

Permalink
Generics and more
Browse files Browse the repository at this point in the history
  • Loading branch information
buildMapi committed Feb 5, 2024
1 parent db2fd06 commit 4e79e0b
Show file tree
Hide file tree
Showing 26 changed files with 1,151 additions and 792 deletions.
214 changes: 108 additions & 106 deletions benches/main.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use std::ops::Range;

use criterion::{criterion_group, criterion_main, Criterion, BenchmarkId, BatchSize, Throughput};
use criterion::{criterion_group, criterion_main, BatchSize, BenchmarkId, Criterion, Throughput};
use fake::*;
use rand::seq::IteratorRandom;
use typed_graph::generic_graph::{GenericGraph, GenericResult};
use fake::*;

type TestGraph = GenericGraph<usize, usize, usize, usize>;
type TestResult<T> = GenericResult<T, usize, usize, usize, usize>;
Expand All @@ -17,29 +17,31 @@ pub struct CompleteGraph {
/// Number of node types (types start at 0)
pub node_types: usize,
/// Number of edge types (types start at 0)
pub edge_types: usize
pub edge_types: usize,
}

impl Dummy<CompleteGraph> for TestGraph {
fn dummy_with_rng<R: Rng + ?Sized>(config: &CompleteGraph, rng: &mut R) -> Self {
let mut g = TestGraph::default();
let node_count = config.width * config.height;
for i in 0..node_count {
g.add_node((i, rng.gen_range(0..config.node_types))).unwrap();
g.add_node((i, rng.gen_range(0..config.node_types)))
.unwrap();
}

if config.edge_types != 0 {
for x in 0..config.width {
for y in 0..config.height {
if x == y {
if x == y {
continue;
}

g.add_edge(
x,
y,
(x + y * config.width, rng.gen_range(0..config.edge_types))
).unwrap();
x,
y,
(x + y * config.width, rng.gen_range(0..config.edge_types)),
)
.unwrap();
}
}
}
Expand All @@ -59,19 +61,24 @@ pub struct SparseGraph {
/// Number of node types (types start at 0)
pub node_types: usize,
/// Number of edge types (types start at 0)
pub edge_types: usize
pub edge_types: usize,
}

impl Dummy<SparseGraph> for TestGraph {
fn dummy_with_rng<R: Rng + ?Sized>(config: &SparseGraph, rng: &mut R) -> Self {
let mut g = TestGraph::default();
let node_count = config.width * config.height;
for i in 0..node_count {
g.add_node((i, rng.gen_range(0..config.node_types))).unwrap();
g.add_node((i, rng.gen_range(0..config.node_types)))
.unwrap();
}

for i in 0..node_count {
let edge_count = config.node_connections.clone().choose(rng).unwrap_or_default();
let edge_count = config
.node_connections
.clone()
.choose(rng)
.unwrap_or_default();

for _ in 0..edge_count {
// Figure out which node to connect to
Expand All @@ -83,7 +90,12 @@ impl Dummy<SparseGraph> for TestGraph {
continue;
}

g.add_edge(i, target, (g.edge_count(), rng.gen_range(0..config.edge_types))).unwrap();
g.add_edge(
i,
target,
(g.edge_count(), rng.gen_range(0..config.edge_types)),
)
.unwrap();
}
}

Expand All @@ -93,145 +105,135 @@ impl Dummy<SparseGraph> for TestGraph {

fn add_node(c: &mut Criterion) {
let mut group = c.benchmark_group("Node+");

for side_length in (0..=100).step_by(25) {
group.throughput(Throughput::Elements((side_length*side_length) as u64));
group.throughput(Throughput::Elements((side_length * side_length) as u64));
group.bench_with_input::<_, _, usize>(
BenchmarkId::from_parameter(side_length*side_length),
&side_length,
BenchmarkId::from_parameter(side_length * side_length),
&side_length,
|b, side_length| {

b.iter_batched_ref(
|| {
TestGraph::default()
},
|g| {
for i in 0..(side_length*side_length) {
g.add_node((i, i))?;
}
b.iter_batched_ref(
|| TestGraph::default(),
|g| {
for i in 0..(side_length * side_length) {
g.add_node((i, i))?;
}

TestResult::Ok(())
},
BatchSize::SmallInput
)
});
TestResult::Ok(())
},
BatchSize::SmallInput,
)
},
);
}
group.finish();
}

fn remove_node(c: &mut Criterion) {
let mut group = c.benchmark_group("Node-");

for side_length in (0..=100).step_by(25) {
group.throughput(Throughput::Elements((side_length*side_length) as u64));
group.throughput(Throughput::Elements((side_length * side_length) as u64));
group.bench_with_input::<_, _, usize>(
BenchmarkId::from_parameter(side_length*side_length),
&side_length,
BenchmarkId::from_parameter(side_length * side_length),
&side_length,
|b, side_length| {

b.iter_batched_ref(
|| {
CompleteGraph {
width: *side_length,
height: *side_length,
node_types: side_length*side_length,
edge_types: side_length*side_length
}.fake::<TestGraph>()
},
|g| {
let node_count = g.node_count();
for i in 0..node_count {
g.remove_node(i)?;
}
b.iter_batched_ref(
|| {
CompleteGraph {
width: *side_length,
height: *side_length,
node_types: side_length * side_length,
edge_types: side_length * side_length,
}
.fake::<TestGraph>()
},
|g| {
let node_count = g.node_count();
for i in 0..node_count {
g.remove_node(i)?;
}

TestResult::Ok(())
},
BatchSize::SmallInput
)
});
TestResult::Ok(())
},
BatchSize::SmallInput,
)
},
);
}
group.finish();
}

fn add_edge(c: &mut Criterion) {
let mut group = c.benchmark_group("Edge+");

for side_length in (0..=100).step_by(25) {
group.throughput(Throughput::Elements((side_length*side_length) as u64));
group.throughput(Throughput::Elements((side_length * side_length) as u64));
group.bench_with_input::<_, _, usize>(
BenchmarkId::from_parameter(side_length*side_length),
&side_length,
BenchmarkId::from_parameter(side_length * side_length),
&side_length,
|b, side_length| {

b.iter_batched_ref(
|| {
CompleteGraph {
width: *side_length,
height: *side_length,
node_types: side_length*side_length,
edge_types: 0
}.fake::<TestGraph>()
},
|g| {
for x in 0..*side_length {
for y in 0..*side_length {
g.add_edge(
x,
y,
(x + y * 100, 0)
)?;
b.iter_batched_ref(
|| {
CompleteGraph {
width: *side_length,
height: *side_length,
node_types: side_length * side_length,
edge_types: 0,
}
.fake::<TestGraph>()
},
|g| {
for x in 0..*side_length {
for y in 0..*side_length {
g.add_edge(x, y, (x + y * 100, 0))?;
}
}
}

TestResult::Ok(())
},
BatchSize::SmallInput
)
});
TestResult::Ok(())
},
BatchSize::SmallInput,
)
},
);
}
group.finish();
}

fn remove_edge(c: &mut Criterion) {
let mut group = c.benchmark_group("Edge-");

for side_length in (0..=100).step_by(25) {
group.throughput(Throughput::Elements((side_length*side_length) as u64));
group.throughput(Throughput::Elements((side_length * side_length) as u64));
group.bench_with_input::<_, _, usize>(
BenchmarkId::from_parameter(side_length*side_length),
&side_length,
BenchmarkId::from_parameter(side_length * side_length),
&side_length,
|b, side_length| {
b.iter_batched_ref(
|| {
CompleteGraph {
width: *side_length,
height: *side_length,
node_types: side_length*side_length,
edge_types: side_length*side_length
}.fake::<TestGraph>()
},
node_types: side_length * side_length,
edge_types: side_length * side_length,
}
.fake::<TestGraph>()
},
|g| {
let edge_count = g.edge_count();
for i in 0..edge_count {
g.remove_edge(i)?;
}

TestResult::Ok(())
},
BatchSize::SmallInput
},
BatchSize::SmallInput,
)
}
},
);

}
group.finish();
}

criterion_group!(
benches,
add_node,
remove_node,
add_edge,
remove_edge
);
criterion_main!(benches);
criterion_group!(benches, add_node, remove_node, add_edge, remove_edge);
criterion_main!(benches);
Loading

0 comments on commit 4e79e0b

Please sign in to comment.