Skip to content

Commit

Permalink
Extra Benchmarks for Algorithms using Large Generated Graphs (#1344)
Browse files Browse the repository at this point in the history
* hulong bench with critereon

* hulong#

* add rng option to graphgen

* merge bench into algo, add seed to random graph gen, fix issues with new seed, added pagerank bench

* added conn comp bench

* checkin

* format
  • Loading branch information
Haaroon authored Oct 20, 2023
1 parent 7441765 commit fa73868
Show file tree
Hide file tree
Showing 6 changed files with 143 additions and 30 deletions.
1 change: 0 additions & 1 deletion examples/rust/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ name = "crypto"
[[bin]]
name = "pokec"


[target.x86_64-unknown-linux-gnu]
linker = "/usr/bin/clang"
rustflags = ["-Clink-arg=-fuse-ld=lld", "-Clink-arg=-Wl,--no-rosegment"]
Expand Down
88 changes: 83 additions & 5 deletions raphtory-benchmark/benches/algobench.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
use crate::common::bench;
use criterion::{criterion_group, criterion_main, Criterion};
use criterion::{black_box, criterion_group, criterion_main, BenchmarkId, Criterion, SamplingMode};
use raphtory::{
algorithms::metrics::{
local_clustering_coefficient::local_clustering_coefficient,
local_triangle_count::local_triangle_count,
algorithms::{
centrality::pagerank::unweighted_page_rank,
community_detection::connected_components::weakly_connected_components,
metrics::{
clustering_coefficient::clustering_coefficient,
local_clustering_coefficient::local_clustering_coefficient,
},
motifs::local_triangle_count::local_triangle_count,
},
graphgen::random_attachment::random_attachment,
prelude::*,
};
use rayon::prelude::*;
Expand Down Expand Up @@ -88,9 +94,81 @@ pub fn local_clustering_coefficient_analysis(c: &mut Criterion) {
group.finish();
}

pub fn graphgen_large_clustering_coeff(c: &mut Criterion) {
let mut group = c.benchmark_group("graphgen_large_clustering_coeff");
// generate graph
let graph = Graph::new();
let seed: [u8; 32] = [1; 32];
random_attachment(&graph, 500000, 4, Some(seed));

group.sampling_mode(SamplingMode::Flat);
group.measurement_time(std::time::Duration::from_secs(60));
group.sample_size(10);
group.bench_with_input(
BenchmarkId::new("graphgen_large_clustering_coeff", &graph),
&graph,
|b, graph| {
b.iter(|| {
let result = clustering_coefficient(graph);
black_box(result);
});
},
);
group.finish()
}

pub fn graphgen_large_pagerank(c: &mut Criterion) {
let mut group = c.benchmark_group("graphgen_large_pagerank");
// generate graph
let graph = Graph::new();
let seed: [u8; 32] = [1; 32];
random_attachment(&graph, 500000, 4, Some(seed));

group.sampling_mode(SamplingMode::Flat);
group.measurement_time(std::time::Duration::from_secs(20));
group.sample_size(10);
group.bench_with_input(
BenchmarkId::new("graphgen_large_pagerank", &graph),
&graph,
|b, graph| {
b.iter(|| {
let result = unweighted_page_rank(graph, 100, None, None, true);
black_box(result);
});
},
);
group.finish()
}

pub fn graphgen_large_concomp(c: &mut Criterion) {
let mut group = c.benchmark_group("graphgen_large_concomp");
// generate graph
let graph = Graph::new();
let seed: [u8; 32] = [1; 32];
random_attachment(&graph, 500000, 4, Some(seed));

group.sampling_mode(SamplingMode::Flat);
group.measurement_time(std::time::Duration::from_secs(60));
group.sample_size(10);
group.bench_with_input(
BenchmarkId::new("graphgen_large_concomp", &graph),
&graph,
|b, graph| {
b.iter(|| {
let result = weakly_connected_components(graph, 20, None);
black_box(result);
});
},
);
group.finish()
}

criterion_group!(
benches,
local_triangle_count_analysis,
local_clustering_coefficient_analysis
local_clustering_coefficient_analysis,
graphgen_large_clustering_coeff,
graphgen_large_pagerank,
graphgen_large_concomp,
);
criterion_main!(benches);
2 changes: 1 addition & 1 deletion raphtory/src/db/graph/graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -431,7 +431,7 @@ mod db_tests {
assert_eq!(g.latest_time(), Some(20));
assert_eq!(g.earliest_time(), Some(5));

random_attachment(&g, 100, 10);
random_attachment(&g, 100, 10, None);
assert_eq!(g.latest_time(), Some(126));
assert_eq!(g.earliest_time(), Some(5));
}
Expand Down
29 changes: 20 additions & 9 deletions raphtory/src/graphgen/preferential_attachment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
//! use raphtory::graphgen::preferential_attachment::ba_preferential_attachment;
//!
//! let graph = Graph::new();
//! ba_preferential_attachment(&graph, 1000, 10);
//! ba_preferential_attachment(&graph, 1000, 10, None);
//! ```
use crate::{
Expand All @@ -19,7 +19,7 @@ use crate::{
},
prelude::NO_PROPS,
};
use rand::prelude::*;
use rand::{rngs::StdRng, Rng, SeedableRng};
use std::collections::HashSet;

/// Generates a graph using the preferential attachment model.
Expand All @@ -39,17 +39,28 @@ use std::collections::HashSet;
/// * `graph` - The graph you wish to add vertices and edges to
/// * `vertices_to_add` - The amount of vertices you wish to add to the graph (steps)
/// * `edges_per_step` - The amount of edges a joining vertex should add to the graph
/// * `seed` - an optional byte array for the seed used in rng, can be None
/// # Examples
///
/// ```
/// use raphtory::prelude::*;
/// use raphtory::graphgen::preferential_attachment::ba_preferential_attachment;
///
/// let graph = Graph::new();
/// ba_preferential_attachment(&graph, 1000, 10);
/// ba_preferential_attachment(&graph, 1000, 10, None);
/// ```
pub fn ba_preferential_attachment(graph: &Graph, vertices_to_add: usize, edges_per_step: usize) {
let mut rng = thread_rng();
pub fn ba_preferential_attachment(
graph: &Graph,
vertices_to_add: usize,
edges_per_step: usize,
seed: Option<[u8; 32]>,
) {
let mut rng: StdRng;
if let Some(seed_value) = seed {
rng = StdRng::from_seed(seed_value);
} else {
rng = StdRng::from_entropy();
}
let mut latest_time = graph.end().unwrap_or(0);
let view = graph.window(i64::MIN, i64::MAX);
let mut ids: Vec<u64> = view.vertices().id().collect();
Expand Down Expand Up @@ -124,7 +135,7 @@ mod preferential_attachment_tests {
#[test]
fn blank_graph() {
let graph = Graph::new();
ba_preferential_attachment(&graph, 1000, 10);
ba_preferential_attachment(&graph, 1000, 10, None);
assert_eq!(graph.count_edges(), 10009);
assert_eq!(graph.count_vertices(), 1010);
}
Expand All @@ -139,16 +150,16 @@ mod preferential_attachment_tests {
.ok();
}

ba_preferential_attachment(&graph, 1000, 5);
ba_preferential_attachment(&graph, 1000, 5, None);
assert_eq!(graph.count_edges(), 5009);
assert_eq!(graph.count_vertices(), 1010);
}

#[test]
fn prior_graph() {
let graph = Graph::new();
random_attachment(&graph, 1000, 3);
ba_preferential_attachment(&graph, 500, 4);
random_attachment(&graph, 1000, 3, None);
ba_preferential_attachment(&graph, 500, 4, None);
assert_eq!(graph.count_edges(), 5000);
assert_eq!(graph.count_vertices(), 1503);
}
Expand Down
31 changes: 21 additions & 10 deletions raphtory/src/graphgen/random_attachment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
//! use raphtory::prelude::*;
//! use raphtory::graphgen::random_attachment::random_attachment;
//! let graph = Graph::new();
//! random_attachment(&graph, 1000, 10);
//! random_attachment(&graph, 1000, 10, None);
//! ```
use crate::{
Expand All @@ -20,7 +20,7 @@ use crate::{
},
prelude::NO_PROPS,
};
use rand::seq::SliceRandom;
use rand::{rngs::StdRng, seq::SliceRandom, SeedableRng};

/// Given a graph this function will add a user defined number of vertices, each with a
/// user defined number of edges.
Expand All @@ -36,16 +36,27 @@ use rand::seq::SliceRandom;
/// * `graph` - The graph you wish to add vertices and edges to
/// * `vertices_to_add` - The amount of vertices you wish to add to the graph (steps)
/// * `edges_per_step` - The amount of edges a joining vertex should add to the graph
/// * `seed` - (Optional) An array of u8 bytes to be used as the input seed, Default None
/// # Examples
///
/// ```
/// use raphtory::prelude::*;
/// use raphtory::graphgen::random_attachment::random_attachment;
/// let graph = Graph::new();
/// random_attachment(&graph, 1000, 10);
/// random_attachment(&graph, 1000, 10, None);
/// ```
pub fn random_attachment(graph: &Graph, vertices_to_add: usize, edges_per_step: usize) {
let rng = &mut rand::thread_rng();
pub fn random_attachment(
graph: &Graph,
vertices_to_add: usize,
edges_per_step: usize,
seed: Option<[u8; 32]>,
) {
let mut rng: StdRng;
if let Some(seed_value) = seed {
rng = StdRng::from_seed(seed_value);
} else {
rng = StdRng::from_entropy();
}
let mut latest_time = graph.latest_time().unwrap_or(0);
let mut ids: Vec<u64> = graph.vertices().id().collect();
let mut max_id = ids.iter().max().copied().unwrap_or(0);
Expand All @@ -61,7 +72,7 @@ pub fn random_attachment(graph: &Graph, vertices_to_add: usize, edges_per_step:
}

for _ in 0..vertices_to_add {
let edges = ids.choose_multiple(rng, edges_per_step);
let edges = ids.choose_multiple(&mut rng, edges_per_step);
max_id += 1;
latest_time += 1;
edges.for_each(|neighbour| {
Expand All @@ -80,7 +91,7 @@ mod random_graph_test {
#[test]
fn blank_graph() {
let graph = Graph::new();
random_attachment(&graph, 100, 20);
random_attachment(&graph, 100, 20, None);
assert_eq!(graph.count_edges(), 2000);
assert_eq!(graph.count_vertices(), 120);
}
Expand All @@ -95,16 +106,16 @@ mod random_graph_test {
.ok();
}

random_attachment(&graph, 1000, 5);
random_attachment(&graph, 1000, 5, None);
assert_eq!(graph.count_edges(), 5000);
assert_eq!(graph.count_vertices(), 1010);
}

#[test]
fn prior_graph() {
let graph = Graph::new();
ba_preferential_attachment(&graph, 300, 7);
random_attachment(&graph, 4000, 12);
ba_preferential_attachment(&graph, 300, 7, None);
random_attachment(&graph, 4000, 12, None);
assert_eq!(graph.count_edges(), 50106);
assert_eq!(graph.count_vertices(), 4307);
}
Expand Down
22 changes: 18 additions & 4 deletions raphtory/src/python/packages/graph_gen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,19 @@ use pyo3::prelude::*;
/// g: The graph you wish to add vertices and edges to
/// vertices_to_add: The amount of vertices you wish to add to the graph (steps)
/// edges_per_step: The amount of edges a joining vertex should add to the graph
/// seed: The seed used in rng, an array of length 32 containing ints (ints must have a max size of u8)
///
/// Returns:
/// None
#[pyfunction]
pub fn random_attachment(g: &PyGraph, vertices_to_add: usize, edges_per_step: usize) {
ra(&g.graph, vertices_to_add, edges_per_step);
#[pyo3[signature = (g, vertices_to_add, edges_per_step, seed=None)]]
pub fn random_attachment(
g: &PyGraph,
vertices_to_add: usize,
edges_per_step: usize,
seed: Option<[u8; 32]>,
) {
ra(&g.graph, vertices_to_add, edges_per_step, seed);
}

/// Generates a graph using the preferential attachment model.
Expand All @@ -45,11 +52,18 @@ pub fn random_attachment(g: &PyGraph, vertices_to_add: usize, edges_per_step: us
/// g: The graph you wish to add vertices and edges to
/// vertices_to_add: The amount of vertices you wish to add to the graph (steps)
/// edges_per_step: The amount of edges a joining vertex should add to the graph
/// seed: The seed used in rng, an array of length 32 containing ints (ints must have a max size of u8)
///
/// Returns:
///
/// None
#[pyfunction]
pub fn ba_preferential_attachment(g: &PyGraph, vertices_to_add: usize, edges_per_step: usize) {
pa(&g.graph, vertices_to_add, edges_per_step);
#[pyo3[signature = (g, vertices_to_add, edges_per_step, seed=None)]]
pub fn ba_preferential_attachment(
g: &PyGraph,
vertices_to_add: usize,
edges_per_step: usize,
seed: Option<[u8; 32]>,
) {
pa(&g.graph, vertices_to_add, edges_per_step, seed);
}

0 comments on commit fa73868

Please sign in to comment.