Skip to content

Commit fb4916a

Browse files
authored
DYN-1899: Node Execution Events (#9823)
* Update changes from librarie.js to fix QNTM-3710 * Add new events and tests * Remove unnecessary arguments * Remove unneeded arguments
1 parent ffed170 commit fb4916a

File tree

3 files changed

+80
-1
lines changed

3 files changed

+80
-1
lines changed

src/DynamoCore/Engine/Profiling/NodeProfilingData.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,12 @@ private void RecordEvaluationState(object data)
3333
if (!startTime.HasValue)
3434
{
3535
startTime = DateTime.Now;
36+
node.OnNodeExecutionBegin();
3637
return;
3738
}
3839

3940
endTime = DateTime.Now;
41+
node.OnNodeExecutionEnd();
4042
}
4143

4244
internal TimeSpan? ExecutionTime

src/DynamoCore/Graph/Nodes/NodeModel.cs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,28 @@ private void OnDispatchedToUI(object sender, UIDispatcherEventArgs e)
135135
/// </summary>
136136
public event Action<PortModel> PortDisconnected;
137137

138+
/// <summary>
139+
/// Event triggered before a node is executed.
140+
/// Note: This event will only be triggered when profiling is active.
141+
/// </summary>
142+
public event Action<NodeModel> NodeExecutionBegin;
143+
144+
internal void OnNodeExecutionBegin()
145+
{
146+
NodeExecutionBegin?.Invoke(this);
147+
}
148+
149+
/// <summary>
150+
/// Event triggered after a node is executed.
151+
/// Note: This event will only be triggered when profiling is active.
152+
/// </summary>
153+
public event Action<NodeModel> NodeExecutionEnd;
154+
155+
internal void OnNodeExecutionEnd()
156+
{
157+
NodeExecutionEnd?.Invoke(this);
158+
}
159+
138160
#endregion
139161

140162
#region public properties

test/DynamoCoreTests/ProfilingTest.cs

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1-
using System.Collections.Generic;
1+
using System;
2+
using System.Collections.Generic;
23
using System.IO;
34
using System.Linq;
45

56
using Dynamo.Graph.Workspaces;
67
using Dynamo.Models;
8+
using Dynamo.Graph.Nodes;
79
using NUnit.Framework;
810

911
namespace Dynamo.Tests
@@ -94,5 +96,58 @@ public void TestProfilingSingleNodePublicMethodsOnly()
9496
Assert.IsNotNull(profilingData.NodeExecutionTime(node));
9597
Assert.Greater(profilingData.NodeExecutionTime(node)?.Ticks, 0);
9698
}
99+
100+
private static List<Guid> executingNodes = new List<Guid>();
101+
private static int nodeExecutionBeginCount = 0;
102+
private static int nodeExecutionEndCount = 0;
103+
104+
private static void onNodeExectuionBegin(NodeModel model)
105+
{
106+
// Assert that the node has ot been executed more than once
107+
CollectionAssert.DoesNotContain(executingNodes, model.GUID);
108+
109+
executingNodes.Add(model.GUID);
110+
nodeExecutionBeginCount++;
111+
}
112+
113+
private static void onNodeExectuionEnd(NodeModel model)
114+
{
115+
// Assert that the node ending execution had the begin exectuion event fired previously
116+
CollectionAssert.Contains(executingNodes, model.GUID);
117+
118+
nodeExecutionEndCount++;
119+
}
120+
121+
[Test]
122+
public void TestNodeExecutionEvents()
123+
{
124+
// Note: This test file is saved in manual run mode
125+
string openPath = Path.Combine(TestDirectory, @"core\profiling\createSomePoints.dyn");
126+
OpenModel(openPath);
127+
128+
var nodes = CurrentDynamoModel.CurrentWorkspace.Nodes;
129+
foreach(var node in nodes)
130+
{
131+
node.NodeExecutionBegin += onNodeExectuionBegin;
132+
node.NodeExecutionEnd += onNodeExectuionEnd;
133+
}
134+
135+
// Currently the node execution begin/end events are only enabled when profiling is enabled
136+
var engineController = CurrentDynamoModel.EngineController;
137+
var homeWorkspace = CurrentDynamoModel.Workspaces.OfType<HomeWorkspaceModel>().FirstOrDefault();
138+
engineController.EnableProfiling(true, homeWorkspace, nodes);
139+
140+
BeginRun();
141+
142+
// Assert that all nodes were executed and the begin and end events were fired as expectd
143+
Assert.AreEqual(4, nodeExecutionBeginCount);
144+
Assert.AreEqual(4, nodeExecutionEndCount);
145+
146+
foreach (var node in nodes)
147+
{
148+
node.NodeExecutionBegin -= onNodeExectuionBegin;
149+
node.NodeExecutionEnd -= onNodeExectuionEnd;
150+
}
151+
}
97152
}
98153
}

0 commit comments

Comments
 (0)