rust_logic_graph/
bench_helpers.rs

1use async_trait::async_trait;
2use serde_json::json;
3use std::time::Duration;
4
5use crate::core::Context;
6use crate::node::{Node, NodeType};
7use crate::rule::RuleResult;
8
9/// A simple node that simulates an expensive computation by sleeping.
10pub struct ExpensiveComputeNode {
11    pub id: String,
12    pub work_ms: u64,
13}
14
15impl ExpensiveComputeNode {
16    pub fn new(id: &str, work_ms: u64) -> Self {
17        Self {
18            id: id.to_string(),
19            work_ms,
20        }
21    }
22}
23
24#[async_trait]
25impl Node for ExpensiveComputeNode {
26    fn id(&self) -> &str {
27        &self.id
28    }
29
30    fn node_type(&self) -> NodeType {
31        // Treat as a RuleNode-style compute node for benchmarking
32        NodeType::RuleNode
33    }
34
35    async fn run(&self, ctx: &mut Context) -> RuleResult {
36        // Simulate work
37        tokio::time::sleep(Duration::from_millis(self.work_ms)).await;
38        // Put a value into context and also return it
39        let v = json!({ "node": self.id.clone(), "work_ms": self.work_ms });
40        ctx.set(&format!("{}_result", self.id), v.clone());
41        Ok(v)
42    }
43}
44
45/// Helper to create a simple graph/executor pair used by benches.
46pub fn make_simple_expensive_node(id: &str, ms: u64) -> ExpensiveComputeNode {
47    ExpensiveComputeNode::new(id, ms)
48}