use criterion::{BenchmarkGroup, measurement::WallTime};
use graph_api_derive::{EdgeExt, VertexExt};
use graph_api_lib::Graph;
#[cfg(feature = "element-removal")]
use graph_api_lib::SupportsElementRemoval;
use std::collections::HashSet;
use std::fmt::Debug;
use uuid::Uuid;
#[derive(Debug, Clone, VertexExt)]
pub enum Vertex {
Person {
name: String,
age: u64,
unique_id: Uuid,
username: String,
biography: String,
},
Project {
name: String,
},
Rust,
}
#[derive(Debug, Clone, EdgeExt)]
pub enum Edge {
Knows { since: i32 },
Created,
Language { name: String },
}
pub fn populate_test_data<G>(graph: &mut G) -> (HashSet<G::VertexId>, G::VertexId, G::VertexId)
where
G: Graph<Vertex = Vertex, Edge = Edge>,
{
let mut person_ids = HashSet::new();
for i in 0..100 {
let id = graph.add_vertex(Vertex::Person {
name: format!("Person{}", i),
age: 25 + (i % 50) as u64,
unique_id: Uuid::new_v4(),
username: format!("user{}", i),
biography: format!("Bio for person {}", i),
});
person_ids.insert(id);
}
let project = graph.add_vertex(Vertex::Project {
name: "TestProject".to_string(),
});
let rust = graph.add_vertex(Vertex::Rust);
(person_ids, project, rust)
}
#[cfg(feature = "vertex-label-index")]
pub fn bench_label_lookup<G>(group: &mut BenchmarkGroup<WallTime>, setup: impl Fn() -> G + Clone)
where
G: Graph<Vertex = Vertex, Edge = Edge> + graph_api_lib::SupportsVertexLabelIndex,
{
group.throughput(criterion::Throughput::Elements(1));
group.bench_function("vertex_label_lookup", |b| {
let mut graph = setup();
populate_test_data(&mut graph);
b.iter(|| {
let results = graph
.walk()
.vertices(Vertex::person())
.take(10)
.collect::<Vec<_>>();
assert!(!results.is_empty());
results
})
});
}
#[cfg(not(feature = "vertex-label-index"))]
pub fn bench_label_lookup<G>(_group: &mut BenchmarkGroup<WallTime>, _setup: impl Fn() -> G + Clone)
where
G: Graph<Vertex = Vertex, Edge = Edge>,
{
}
#[cfg(feature = "vertex-label-index")]
pub fn bench_label_insertion<G>(group: &mut BenchmarkGroup<WallTime>, setup: impl Fn() -> G + Clone)
where
G: Graph<Vertex = Vertex, Edge = Edge> + graph_api_lib::SupportsVertexLabelIndex,
{
group.bench_function("vertex_label_insertion", |b| {
let mut graph = setup();
let mut counter = 0;
b.iter(|| {
counter += 1;
graph.add_vertex(Vertex::Person {
name: format!("Test{}", counter),
age: 30,
unique_id: Uuid::new_v4(),
username: format!("test_user{}", counter),
biography: "Test biography".to_string(),
})
})
});
}
#[cfg(not(feature = "vertex-label-index"))]
pub fn bench_label_insertion<G>(
_group: &mut BenchmarkGroup<WallTime>,
_setup: impl Fn() -> G + Clone,
) where
G: Graph<Vertex = Vertex, Edge = Edge>,
{
}
#[cfg(all(feature = "vertex-label-index", feature = "element-removal"))]
pub fn bench_label_removal<G>(group: &mut BenchmarkGroup<WallTime>, setup: impl Fn() -> G + Clone)
where
G: Graph<Vertex = Vertex, Edge = Edge>
+ graph_api_lib::SupportsVertexLabelIndex
+ SupportsElementRemoval,
{
group.bench_function("vertex_label_removal", |b| {
b.iter_with_setup(
|| {
let mut graph = setup();
let vertex_id = graph.add_vertex(Vertex::Person {
name: "RemoveMe".to_string(),
age: 25,
unique_id: Uuid::new_v4(),
username: "remove_user".to_string(),
biography: "To be removed".to_string(),
});
(graph, vertex_id)
},
|(mut graph, vertex_id)| {
graph.remove_vertex(vertex_id);
},
)
});
}
#[cfg(not(all(feature = "vertex-label-index", feature = "element-removal")))]
pub fn bench_label_removal<G>(_group: &mut BenchmarkGroup<WallTime>, _setup: impl Fn() -> G + Clone)
where
G: Graph<Vertex = Vertex, Edge = Edge>,
{
}