rust_logic_graph/
bench_helpers.rs

1use std::time::Duration;
2use async_trait::async_trait;
3use serde_json::json;
4
5use crate::node::{Node, NodeType};
6use crate::core::Context;
7use crate::rule::{RuleResult, RuleError};
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 { id: id.to_string(), work_ms }
18    }
19}
20
21#[async_trait]
22impl Node for ExpensiveComputeNode {
23    fn id(&self) -> &str {
24        &self.id
25    }
26
27    fn node_type(&self) -> NodeType {
28        // Treat as a RuleNode-style compute node for benchmarking
29        NodeType::RuleNode
30    }
31
32    async fn run(&self, ctx: &mut Context) -> RuleResult {
33        // Simulate work
34        tokio::time::sleep(Duration::from_millis(self.work_ms)).await;
35        // Put a value into context and also return it
36        let v = json!({ "node": self.id.clone(), "work_ms": self.work_ms });
37        ctx.set(&format!("{}_result", self.id), v.clone())
38            .map_err(|e| RuleError::Eval(format!("bench ctx set failed: {}", e)))?;
39        Ok(v)
40    }
41}
42
43/// Helper to create a simple graph/executor pair used by benches.
44pub fn make_simple_expensive_node(id: &str, ms: u64) -> ExpensiveComputeNode {
45    ExpensiveComputeNode::new(id, ms)
46}