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::fmt::Debug;
use uuid::Uuid;
#[derive(Debug, Clone, VertexExt)]
pub enum Vertex {
Person {
name: String,
#[index(range)]
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)
where
G: Graph<Vertex = Vertex, Edge = Edge>,
{
for i in 0..100 {
let age = 20 + i % 60; graph.add_vertex(Vertex::Person {
name: format!("Person{}", i),
age,
unique_id: Uuid::new_v4(),
username: format!("user{}", i),
biography: format!("Bio for person {}", i),
});
}
graph.add_vertex(Vertex::Project {
name: "TestProject".to_string(),
});
graph.add_vertex(Vertex::Rust);
}
#[cfg(feature = "vertex-range-index")]
pub fn bench_lookup<G>(group: &mut BenchmarkGroup<WallTime>, setup: impl Fn() -> G + Clone)
where
G: Graph<Vertex = Vertex, Edge = Edge> + graph_api_lib::SupportsVertexRangeIndex,
{
group.throughput(criterion::Throughput::Elements(1));
group.bench_function("vertex_range_age_lookup", |b| {
let mut graph = setup();
populate_test_data(&mut graph);
b.iter(|| {
let results = graph
.walk()
.vertices(Vertex::person_by_age_range(30..50))
.collect::<Vec<_>>();
assert!(!results.is_empty());
results
})
});
}
#[cfg(not(feature = "vertex-range-index"))]
pub fn bench_lookup<G>(_group: &mut BenchmarkGroup<WallTime>, _setup: impl Fn() -> G + Clone)
where
G: Graph<Vertex = Vertex, Edge = Edge>,
{
}
#[cfg(feature = "vertex-range-index")]
pub fn bench_insertion<G>(group: &mut BenchmarkGroup<WallTime>, setup: impl Fn() -> G + Clone)
where
G: Graph<Vertex = Vertex, Edge = Edge> + graph_api_lib::SupportsVertexRangeIndex,
{
group.bench_function("vertex_range_insertion", |b| {
let mut graph = setup();
let mut age = 25;
b.iter(|| {
age = (age + 1) % 100;
graph.add_vertex(Vertex::Person {
name: format!("Range{}", age),
age,
unique_id: Uuid::new_v4(),
username: format!("range_user{}", age),
biography: "Test biography for range index".to_string(),
})
})
});
}
#[cfg(not(feature = "vertex-range-index"))]
pub fn bench_insertion<G>(_group: &mut BenchmarkGroup<WallTime>, _setup: impl Fn() -> G + Clone)
where
G: Graph<Vertex = Vertex, Edge = Edge>,
{
}
#[cfg(all(feature = "vertex-range-index", feature = "element-removal"))]
pub fn bench_removal<G>(group: &mut BenchmarkGroup<WallTime>, setup: impl Fn() -> G + Clone)
where
G: Graph<Vertex = Vertex, Edge = Edge>
+ graph_api_lib::SupportsVertexRangeIndex
+ SupportsElementRemoval,
{
group.bench_function("vertex_range_removal", |b| {
b.iter_with_setup(
|| {
let mut graph = setup();
let vertex_id = graph.add_vertex(Vertex::Person {
name: "RangeRemoveMe".to_string(),
age: 42,
unique_id: Uuid::new_v4(),
username: "range_remove_user".to_string(),
biography: "To be removed from range index".to_string(),
});
(graph, vertex_id)
},
|(mut graph, vertex_id)| {
graph.remove_vertex(vertex_id);
},
)
});
}
#[cfg(not(all(feature = "vertex-range-index", feature = "element-removal")))]
pub fn bench_removal<G>(_group: &mut BenchmarkGroup<WallTime>, _setup: impl Fn() -> G + Clone)
where
G: Graph<Vertex = Vertex, Edge = Edge>,
{
}