Skip to content

Commit

Permalink
DYN-1899: Node Execution Events (#9823)
Browse files Browse the repository at this point in the history
* Update changes from librarie.js to fix QNTM-3710

* Add new events and tests

* Remove unnecessary arguments

* Remove unneeded arguments
  • Loading branch information
ColinDayOrg authored and QilongTang committed Jul 3, 2019
1 parent b9e38bd commit f43f372
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 1 deletion.
2 changes: 2 additions & 0 deletions src/DynamoCore/Engine/Profiling/NodeProfilingData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,12 @@ private void RecordEvaluationState(object data)
if (!startTime.HasValue)
{
startTime = DateTime.Now;
node.OnNodeExecutionBegin();
return;
}

endTime = DateTime.Now;
node.OnNodeExecutionEnd();
}

internal TimeSpan? ExecutionTime
Expand Down
22 changes: 22 additions & 0 deletions src/DynamoCore/Graph/Nodes/NodeModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,28 @@ private void OnDispatchedToUI(object sender, UIDispatcherEventArgs e)
/// </summary>
public event Action<PortModel> PortDisconnected;

/// <summary>
/// Event triggered before a node is executed.
/// Note: This event will only be triggered when profiling is active.
/// </summary>
public event Action<NodeModel> NodeExecutionBegin;

internal void OnNodeExecutionBegin()
{
NodeExecutionBegin?.Invoke(this);
}

/// <summary>
/// Event triggered after a node is executed.
/// Note: This event will only be triggered when profiling is active.
/// </summary>
public event Action<NodeModel> NodeExecutionEnd;

internal void OnNodeExecutionEnd()
{
NodeExecutionEnd?.Invoke(this);
}

#endregion

#region public properties
Expand Down
57 changes: 56 additions & 1 deletion test/DynamoCoreTests/ProfilingTest.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;

using Dynamo.Graph.Workspaces;
using Dynamo.Models;
using Dynamo.Graph.Nodes;
using NUnit.Framework;

namespace Dynamo.Tests
Expand Down Expand Up @@ -94,5 +96,58 @@ public void TestProfilingSingleNodePublicMethodsOnly()
Assert.IsNotNull(profilingData.NodeExecutionTime(node));
Assert.Greater(profilingData.NodeExecutionTime(node)?.Ticks, 0);
}

private static List<Guid> executingNodes = new List<Guid>();
private static int nodeExecutionBeginCount = 0;
private static int nodeExecutionEndCount = 0;

private static void onNodeExectuionBegin(NodeModel model)
{
// Assert that the node has ot been executed more than once
CollectionAssert.DoesNotContain(executingNodes, model.GUID);

executingNodes.Add(model.GUID);
nodeExecutionBeginCount++;
}

private static void onNodeExectuionEnd(NodeModel model)
{
// Assert that the node ending execution had the begin exectuion event fired previously
CollectionAssert.Contains(executingNodes, model.GUID);

nodeExecutionEndCount++;
}

[Test]
public void TestNodeExecutionEvents()
{
// Note: This test file is saved in manual run mode
string openPath = Path.Combine(TestDirectory, @"core\profiling\createSomePoints.dyn");
OpenModel(openPath);

var nodes = CurrentDynamoModel.CurrentWorkspace.Nodes;
foreach(var node in nodes)
{
node.NodeExecutionBegin += onNodeExectuionBegin;
node.NodeExecutionEnd += onNodeExectuionEnd;
}

// Currently the node execution begin/end events are only enabled when profiling is enabled
var engineController = CurrentDynamoModel.EngineController;
var homeWorkspace = CurrentDynamoModel.Workspaces.OfType<HomeWorkspaceModel>().FirstOrDefault();
engineController.EnableProfiling(true, homeWorkspace, nodes);

BeginRun();

// Assert that all nodes were executed and the begin and end events were fired as expectd
Assert.AreEqual(4, nodeExecutionBeginCount);
Assert.AreEqual(4, nodeExecutionEndCount);

foreach (var node in nodes)
{
node.NodeExecutionBegin -= onNodeExectuionBegin;
node.NodeExecutionEnd -= onNodeExectuionEnd;
}
}
}
}

0 comments on commit f43f372

Please sign in to comment.