From 0d8e8d835b786c105f8d1ccf219959ce9e51b120 Mon Sep 17 00:00:00 2001 From: Ivo Petrov <48355182+ivaylo-matov@users.noreply.github.com> Date: Wed, 17 Jul 2024 10:33:21 +0100 Subject: [PATCH] [DYN-2476] First graph run does not show execution time in TuneUp + (#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 --- TuneUp/ProfiledNodeViewModel.cs | 8 +++++ TuneUp/TuneUpViewExtension.cs | 10 +++++-- TuneUp/TuneUpWindow.xaml.cs | 3 +- TuneUp/TuneUpWindowViewModel.cs | 53 +++++++++++++++++++++------------ 4 files changed, 52 insertions(+), 22 deletions(-) diff --git a/TuneUp/ProfiledNodeViewModel.cs b/TuneUp/ProfiledNodeViewModel.cs index 8c52c92..0647e8c 100644 --- a/TuneUp/ProfiledNodeViewModel.cs +++ b/TuneUp/ProfiledNodeViewModel.cs @@ -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 @@ -130,6 +132,11 @@ public string StateDescription } } + /// + /// The Stopwatch to measure execution time of this node + /// + internal Stopwatch Stopwatch { get; set; } + internal NodeModel NodeModel { get; set; } #endregion @@ -142,6 +149,7 @@ public ProfiledNodeViewModel(NodeModel node) { NodeModel = node; State = ProfiledNodeState.NotExecuted; + Stopwatch = new Stopwatch(); } /// diff --git a/TuneUp/TuneUpViewExtension.cs b/TuneUp/TuneUpViewExtension.cs index 47c9f67..57fe602 100644 --- a/TuneUp/TuneUpViewExtension.cs +++ b/TuneUp/TuneUpViewExtension.cs @@ -1,4 +1,5 @@ -using System.Linq; +using System; +using System.Linq; using System.Windows.Controls; using Dynamo.Wpf.Extensions; @@ -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(); } }; @@ -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; } } } diff --git a/TuneUp/TuneUpWindow.xaml.cs b/TuneUp/TuneUpWindow.xaml.cs index 1fd729e..839e7e2 100644 --- a/TuneUp/TuneUpWindow.xaml.cs +++ b/TuneUp/TuneUpWindow.xaml.cs @@ -1,4 +1,5 @@ -using System.Collections.Generic; +using System; +using System.Collections.Generic; using System.ComponentModel; using System.Linq; using System.Windows; diff --git a/TuneUp/TuneUpWindowViewModel.cs b/TuneUp/TuneUpWindowViewModel.cs index 5a4ace5..b65eaf0 100644 --- a/TuneUp/TuneUpWindowViewModel.cs +++ b/TuneUp/TuneUpWindowViewModel.cs @@ -61,7 +61,6 @@ public class TuneUpWindowViewModel : NotificationObject, IDisposable private ListSortDirection sortDirection; private string sortingOrder; - /// /// Name of the row to display current execution time /// @@ -213,6 +212,7 @@ public string TotalGraphExecutiontime return (PreviousExecutionTimeRow?.ExecutionMilliseconds + CurrentExecutionTimeRow?.ExecutionMilliseconds).ToString() + "ms"; } } + #endregion #region Constructor @@ -230,6 +230,7 @@ public TuneUpWindowViewModel(ViewLoadedParams p) CurrentWorkspace = p.CurrentWorkspaceModel as HomeWorkspaceModel; } } + #endregion #region ProfilingMethods @@ -240,12 +241,16 @@ public TuneUpWindowViewModel(ViewLoadedParams p) /// 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(); + var newNodeDictionary = new Dictionary(); + + // Assign the new collection + ProfiledNodes = newProfiledNodes; + nodeDictionary = newNodeDictionary; + foreach (var node in CurrentWorkspace.Nodes) { var profiledNode = new ProfiledNodeViewModel(node); @@ -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 @@ -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(); @@ -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 @@ -481,7 +496,7 @@ private void ManageWorkspaceEvents(HomeWorkspaceModel workspace, bool subscribe) node.NodeExecutionBegin += OnNodeExecutionBegin; node.NodeExecutionEnd += OnNodeExecutionEnd; } - ResetProfiledNodes(); + ResetProfiledNodes(); } // Unsubscribe to workspace events else @@ -496,7 +511,7 @@ private void ManageWorkspaceEvents(HomeWorkspaceModel workspace, bool subscribe) node.NodeExecutionBegin -= OnNodeExecutionBegin; node.NodeExecutionEnd -= OnNodeExecutionEnd; } - } + } executedNodesNum = 0; } @@ -539,4 +554,4 @@ public void ExportToCsv() #endregion } -} +} \ No newline at end of file