Skip to content

Commit

Permalink
[DYN-2476] First graph run does not show execution time in TuneUp + (D…
Browse files Browse the repository at this point in the history
…ynamoDS#47)

* imptived stopwatch

* Optimized

* keeps sorting order

TODO:
- Remove mid-run update
-Keep Execution time on current tun on top or bottom when sorting by name

* holds total times at the bottom

* for PR

* clean code fot PR

* internal class stopwatch

* conflict resolved
  • Loading branch information
ivaylo-matov authored Jul 17, 2024
1 parent ce03050 commit 0d8e8d8
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 22 deletions.
8 changes: 8 additions & 0 deletions TuneUp/ProfiledNodeViewModel.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
using System;
using System.ComponentModel.DataAnnotations;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using Dynamo.Core;
using Dynamo.Graph.Nodes;

namespace TuneUp
{
public class ProfiledNodeViewModel : NotificationObject
Expand Down Expand Up @@ -130,6 +132,11 @@ public string StateDescription
}
}

/// <summary>
/// The Stopwatch to measure execution time of this node
/// </summary>
internal Stopwatch Stopwatch { get; set; }

internal NodeModel NodeModel { get; set; }

#endregion
Expand All @@ -142,6 +149,7 @@ public ProfiledNodeViewModel(NodeModel node)
{
NodeModel = node;
State = ProfiledNodeState.NotExecuted;
Stopwatch = new Stopwatch();
}

/// <summary>
Expand Down
10 changes: 8 additions & 2 deletions TuneUp/TuneUpViewExtension.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Linq;
using System;
using System.Linq;
using System.Windows.Controls;
using Dynamo.Wpf.Extensions;

Expand Down Expand Up @@ -45,11 +46,12 @@ public override void Loaded(ViewLoadedParams p)
{
p.AddToExtensionsSideBar(this, TuneUpView);
ViewModel.SwitchToManualMode();
ViewModel.EnableProfiling();
ViewModel.EnableProfiling();
}
else
{
p.CloseExtensioninInSideBar(this);
ViewModel.DisableProfiling();
}
};

Expand Down Expand Up @@ -110,6 +112,10 @@ public override void Closed()
if (this.TuneUpMenuItem != null)
{
this.TuneUpMenuItem.IsChecked = false;

// Reset DataGrid sorting order & direction
ViewModel.SortingOrder = "number";
ViewModel.SortDirection = System.ComponentModel.ListSortDirection.Ascending;
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion TuneUp/TuneUpWindow.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Windows;
Expand Down
53 changes: 34 additions & 19 deletions TuneUp/TuneUpWindowViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ public class TuneUpWindowViewModel : NotificationObject, IDisposable
private ListSortDirection sortDirection;
private string sortingOrder;


/// <summary>
/// Name of the row to display current execution time
/// </summary>
Expand Down Expand Up @@ -213,6 +212,7 @@ public string TotalGraphExecutiontime
return (PreviousExecutionTimeRow?.ExecutionMilliseconds + CurrentExecutionTimeRow?.ExecutionMilliseconds).ToString() + "ms";
}
}

#endregion

#region Constructor
Expand All @@ -230,6 +230,7 @@ public TuneUpWindowViewModel(ViewLoadedParams p)
CurrentWorkspace = p.CurrentWorkspaceModel as HomeWorkspaceModel;
}
}

#endregion

#region ProfilingMethods
Expand All @@ -240,12 +241,16 @@ public TuneUpWindowViewModel(ViewLoadedParams p)
/// </summary>
internal void ResetProfiledNodes()
{
if (CurrentWorkspace == null)
{
return;
}
nodeDictionary.Clear();
ProfiledNodes.Clear();
if (CurrentWorkspace == null) return;

// Use temporary collections to minimize UI updates
var newProfiledNodes = new ObservableCollection<ProfiledNodeViewModel>();
var newNodeDictionary = new Dictionary<Guid, ProfiledNodeViewModel>();

// Assign the new collection
ProfiledNodes = newProfiledNodes;
nodeDictionary = newNodeDictionary;

foreach (var node in CurrentWorkspace.Nodes)
{
var profiledNode = new ProfiledNodeViewModel(node);
Expand Down Expand Up @@ -309,6 +314,15 @@ internal void EnableProfiling()
RaisePropertyChanged(nameof(ProfiledNodesCollection));
}

internal void DisableProfiling()
{
if (isProfilingEnabled && CurrentWorkspace != null)
{
CurrentWorkspace.EngineController.EnableProfiling(false, CurrentWorkspace, CurrentWorkspace.Nodes);
isProfilingEnabled = false;
}
}

#endregion

#region ExecutionEvents
Expand Down Expand Up @@ -339,7 +353,7 @@ private void CurrentWorkspaceModel_EvaluationCompleted(object sender, Dynamo.Mod
RaisePropertyChanged(nameof(ProfiledNodesCollection));
RaisePropertyChanged(nameof(ProfiledNodes));

ProfiledNodesCollection.Dispatcher.Invoke(() =>
ProfiledNodesCollection.Dispatcher.InvokeAsync(() =>
{
ApplySorting();
ProfiledNodesCollection.View.Refresh();
Expand Down Expand Up @@ -392,28 +406,29 @@ public void ApplySorting()
internal void OnNodeExecutionBegin(NodeModel nm)
{
var profiledNode = nodeDictionary[nm.GUID];
profiledNode.Stopwatch.Start();
profiledNode.State = ProfiledNodeState.Executing;
RaisePropertyChanged(nameof(ProfiledNodesCollection));
}

internal void OnNodeExecutionEnd(NodeModel nm)
{
var profiledNode = nodeDictionary[nm.GUID];
if (executionTimeData != null)
profiledNode.Stopwatch.Stop();
var executionTime = profiledNode.Stopwatch.Elapsed;

if (executionTime > TimeSpan.Zero)
{
var executionTime = executionTimeData.NodeExecutionTime(nm);
if (executionTime != null)
{
profiledNode.ExecutionTime = (TimeSpan)executionTime;
}
profiledNode.ExecutionTime = executionTime;

if (!profiledNode.WasExecutedOnLastRun)
{
profiledNode.ExecutionOrderNumber = executedNodesNum++;
}
}

profiledNode.Stopwatch.Reset();
profiledNode.WasExecutedOnLastRun = true;
profiledNode.State = ProfiledNodeState.ExecutedOnCurrentRun;
RaisePropertyChanged(nameof(ProfiledNodesCollection));
}

#endregion
Expand Down Expand Up @@ -481,7 +496,7 @@ private void ManageWorkspaceEvents(HomeWorkspaceModel workspace, bool subscribe)
node.NodeExecutionBegin += OnNodeExecutionBegin;
node.NodeExecutionEnd += OnNodeExecutionEnd;
}
ResetProfiledNodes();
ResetProfiledNodes();
}
// Unsubscribe to workspace events
else
Expand All @@ -496,7 +511,7 @@ private void ManageWorkspaceEvents(HomeWorkspaceModel workspace, bool subscribe)
node.NodeExecutionBegin -= OnNodeExecutionBegin;
node.NodeExecutionEnd -= OnNodeExecutionEnd;
}
}
}
executedNodesNum = 0;
}

Expand Down Expand Up @@ -539,4 +554,4 @@ public void ExportToCsv()

#endregion
}
}
}

0 comments on commit 0d8e8d8

Please sign in to comment.