1+ #include < mo_yanxi/adapted_attributes.hpp>
2+
3+ import mo_yanxi.react_flow;
4+ import mo_yanxi.react_flow.common;
5+
6+ import std;
7+
8+ void benchmark_diamond_dag () {
9+ using namespace mo_yanxi ::react_flow;
10+ constexpr std::uint32_t iterations = 1'000'000 ;
11+
12+ // --- 1. React Flow 设置 ---
13+ manager mgr{manager_no_async}; // 禁用异步以单纯测试计算开销
14+
15+ auto & p_a = mgr.add_node <provider_cached<double >>();
16+ auto & p_b = mgr.add_node <provider_cached<double >>();
17+ auto & p_c = mgr.add_node <provider_cached<double >>();
18+
19+ auto & t_x = mgr.add_node (make_transformer ([](double a, double b) {
20+ return a + b;
21+ }));
22+ auto & t_y = mgr.add_node (make_transformer ([](double b, double c) {
23+ return b * c;
24+ }));
25+ auto & t_z = mgr.add_node (make_transformer ([](double x, double y) {
26+ return std::sqrt (x * x + y * y);
27+ }));
28+
29+ // 监听最终结果
30+ volatile double dummy_result = 0.0 ;
31+ auto & listener = mgr.add_node (make_listener ([&](double z) {
32+ dummy_result = z;
33+ }));
34+
35+ // 连接节点 (注意:transformer 的参数顺序对应 slot 的索引)
36+ p_a.connect_successor (0 , t_x);
37+ p_b.connect_successor (1 , t_x);
38+
39+ p_b.connect_successor (0 , t_y);
40+ p_c.connect_successor (1 , t_y);
41+
42+ t_x.connect_successor (0 , t_z);
43+ t_y.connect_successor (1 , t_z);
44+
45+ t_z.connect_successor (0 , listener);
46+
47+ // 初始化基础数据
48+ p_a.update_value (10.0 );
49+ p_c.update_value (5.0 );
50+
51+ // --- 2. Benchmark: Node Flow ---
52+ auto start_node = std::chrono::high_resolution_clock::now ();
53+ for (std::uint32_t i = 0 ; i < iterations; ++i) {
54+ p_b.update_value (static_cast <double >(i)); // 触发整条链路的 update
55+ }
56+ auto end_node = std::chrono::high_resolution_clock::now ();
57+
58+ // --- 3. Benchmark: 直接硬编码 ---
59+ auto start_direct = std::chrono::high_resolution_clock::now ();
60+ double a = 10.0 ;
61+ double c = 5.0 ;
62+ for (std::uint32_t i = 0 ; i < iterations; ++i) {
63+ double b = static_cast <double >(i);
64+ double x = a + b;
65+ double y = b * c;
66+ dummy_result = std::sqrt (x * x + y * y);
67+ }
68+ auto end_direct = std::chrono::high_resolution_clock::now ();
69+
70+ // --- 输出结果 ---
71+ auto duration_node = std::chrono::duration_cast<std::chrono::milliseconds>(end_node - start_node).count ();
72+ auto duration_direct = std::chrono::duration_cast<std::chrono::milliseconds>(end_direct - start_direct).count ();
73+
74+ std::println (" === Diamond DAG Benchmark ({} iterations) ===" , iterations);
75+ std::println (" React Flow Duration: {} ms" , duration_node);
76+ std::println (" Direct Code Duration: {} ms" , duration_direct);
77+ std::println (" Overhead Ratio: {:.2f}x" , static_cast <double >(duration_node) / static_cast <double >(std::max<long long >(duration_direct, 1 )));
78+ }
79+
80+
81+ int main (){
82+ benchmark_diamond_dag ();
83+ }
0 commit comments