use codenexus::model::{Edge, EdgeType, Graph, Node, NodeLabel};
use codenexus::trace::{TraceFacade, TraceType};
use criterion::{black_box, criterion_group, criterion_main, Criterion};
fn build_call_chain(size: usize) -> Graph {
let mut g = Graph::new();
for i in 0..size {
let name = format!("func_{i}");
let node = Node::builder(NodeLabel::Function, name.clone(), format!("bench.{name}"))
.id(format!("f{i}"))
.project("bench")
.file_path(format!("src/{name}.rs"))
.start_line(10)
.build();
g.add_node(node);
}
for i in 0..size.saturating_sub(1) {
g.add_edge(Edge::new(
format!("f{i}"),
format!("f{}", i + 1),
EdgeType::Calls,
"bench",
));
}
g
}
fn build_call_chain_with_back_edges(size: usize) -> Graph {
let mut g = Graph::new();
for i in 0..size {
let name = format!("func_{i}");
let node = Node::builder(NodeLabel::Function, name.clone(), format!("bench.{name}"))
.id(format!("f{i}"))
.project("bench")
.file_path(format!("src/{name}.rs"))
.start_line(10)
.build();
g.add_node(node);
}
for i in 0..size.saturating_sub(1) {
g.add_edge(Edge::new(
format!("f{i}"),
format!("f{}", i + 1),
EdgeType::Calls,
"bench",
));
}
for i in 1..size {
g.add_edge(Edge::new(format!("f{i}"), "f0", EdgeType::Calls, "bench"));
}
g
}
fn bench_trace(c: &mut Criterion) {
let graph = build_call_chain(100);
let facade = TraceFacade::new(&graph);
let mut group = c.benchmark_group("trace");
group.sample_size(100);
group.bench_function("trace_calls_depth_5", |b| {
b.iter(|| {
let result = facade.trace("func_0", TraceType::Calls, 5).unwrap();
black_box(result);
});
});
group.bench_function("trace_calls_depth_10", |b| {
b.iter(|| {
let result = facade.trace("func_0", TraceType::Calls, 10).unwrap();
black_box(result);
});
});
group.bench_function("trace_calls_depth_50", |b| {
b.iter(|| {
let result = facade.trace("func_0", TraceType::Calls, 50).unwrap();
black_box(result);
});
});
group.bench_function("trace_all_depth_10", |b| {
b.iter(|| {
let result = facade.trace("func_0", TraceType::All, 10).unwrap();
black_box(result);
});
});
group.finish();
}
fn bench_trace_path_contains(c: &mut Criterion) {
let graph = build_call_chain_with_back_edges(120);
let facade = TraceFacade::new(&graph);
let mut group = c.benchmark_group("trace_path_contains");
group.sample_size(50);
group.bench_function("deep_chain_with_back_edges_depth_100", |b| {
b.iter(|| {
let result = facade.trace("func_0", TraceType::Calls, 100).unwrap();
black_box(result);
});
});
group.finish();
}
criterion_group!(benches, bench_trace, bench_trace_path_contains);
criterion_main!(benches);