From fbd458219040546856c5c114f7e792909c129190 Mon Sep 17 00:00:00 2001 From: tznind Date: Sun, 2 Mar 2025 08:34:34 +0000 Subject: [PATCH 1/3] Do not repeatedly raise new sets of keystrokes when modals pop and trigger run state changes --- UICatalog/Scenario.cs | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/UICatalog/Scenario.cs b/UICatalog/Scenario.cs index e45e269f7d..431cc56b7c 100644 --- a/UICatalog/Scenario.cs +++ b/UICatalog/Scenario.cs @@ -229,13 +229,20 @@ private void OnApplicationOnIteration (object? s, IterationEventArgs a) } } + private HashSet _scenariosRun = new HashSet (); private void OnApplicationNotifyNewRunState (object? sender, RunStateEventArgs e) { + // We are just returning to the same scenario + if (!_scenariosRun.Add (this)) + { + return; + } + SubscribeAllSubviews (Application.Top!); _currentDemoKey = 0; _demoKeys = GetDemoKeyStrokes (); - + Application.AddTimeout ( new TimeSpan (0, 0, 0, 0, BENCHMARK_KEY_PACING), () => From 5d190735479d640bb7ef50e9d226fbbe0a940a9d Mon Sep 17 00:00:00 2001 From: tznind Date: Sun, 2 Mar 2025 08:59:08 +0000 Subject: [PATCH 2/3] Remove iteration limit for benchmarking in v2 --- Terminal.Gui/Application/Application.cs | 9 +++++++++ Terminal.Gui/ConsoleDrivers/V2/MainLoop.cs | 3 ++- 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/Terminal.Gui/Application/Application.cs b/Terminal.Gui/Application/Application.cs index a493f30569..91dd82f966 100644 --- a/Terminal.Gui/Application/Application.cs +++ b/Terminal.Gui/Application/Application.cs @@ -26,6 +26,15 @@ public static partial class Application /// Gets all cultures supported by the application without the invariant language. public static List? SupportedCultures { get; private set; } = GetSupportedCultures (); + /// + /// Maximum number of iterations of the main loop (and hence draws) + /// to allow to occur per second. Defaults to 25 which is a 40ms sleep + /// after iteration (factoring in how long iteration took to run). + /// Note that not ever iteration draws (see ). + /// Only affects v2 drivers. + /// + public static ushort MaximumIterationsPerSecond = 25; + /// /// Gets a string representation of the Application as rendered by . /// diff --git a/Terminal.Gui/ConsoleDrivers/V2/MainLoop.cs b/Terminal.Gui/ConsoleDrivers/V2/MainLoop.cs index 8c830aaa36..1bbd2e38fd 100644 --- a/Terminal.Gui/ConsoleDrivers/V2/MainLoop.cs +++ b/Terminal.Gui/ConsoleDrivers/V2/MainLoop.cs @@ -99,11 +99,12 @@ public void Initialize (ITimedEvents timedEvents, ConcurrentQueue inputBuffer public void Iteration () { DateTime dt = Now (); + int timeAllowed = 1000 / Math.Max(1,(int)Application.MaximumIterationsPerSecond); IterationImpl (); TimeSpan took = Now () - dt; - TimeSpan sleepFor = TimeSpan.FromMilliseconds (50) - took; + TimeSpan sleepFor = TimeSpan.FromMilliseconds (timeAllowed) - took; Logging.TotalIterationMetric.Record (took.Milliseconds); From 30f8d26b2a3cabea6f27fb2bc1c6f78980454c24 Mon Sep 17 00:00:00 2001 From: tznind Date: Sun, 2 Mar 2025 17:55:16 +0000 Subject: [PATCH 3/3] Raise iteration event --- Terminal.Gui/Application/Application.Run.cs | 5 +++++ Terminal.Gui/ConsoleDrivers/V2/MainLoop.cs | 2 ++ UICatalog/UICatalog.cs | 1 + 3 files changed, 8 insertions(+) diff --git a/Terminal.Gui/Application/Application.Run.cs b/Terminal.Gui/Application/Application.Run.cs index 6906efd903..347744a5f2 100644 --- a/Terminal.Gui/Application/Application.Run.cs +++ b/Terminal.Gui/Application/Application.Run.cs @@ -516,6 +516,11 @@ public static bool RunIteration (ref RunState state, bool firstIteration = false return firstIteration; } + internal static void RaiseIteration () + { + Iteration?.Invoke (null, new IterationEventArgs ()); + } + /// Stops the provided , causing or the if provided. /// The to stop. /// diff --git a/Terminal.Gui/ConsoleDrivers/V2/MainLoop.cs b/Terminal.Gui/ConsoleDrivers/V2/MainLoop.cs index 49cc807272..4a9351ab14 100644 --- a/Terminal.Gui/ConsoleDrivers/V2/MainLoop.cs +++ b/Terminal.Gui/ConsoleDrivers/V2/MainLoop.cs @@ -108,6 +108,8 @@ public void Iteration () Logging.TotalIterationMetric.Record (took.Milliseconds); + Application.RaiseIteration (); + if (sleepFor.Milliseconds > 0) { Task.Delay (sleepFor).Wait (); diff --git a/UICatalog/UICatalog.cs b/UICatalog/UICatalog.cs index 3ff3035ddc..ae13054178 100644 --- a/UICatalog/UICatalog.cs +++ b/UICatalog/UICatalog.cs @@ -527,6 +527,7 @@ void ApplicationOnInitializedChanged (object? sender, EventArgs e) private static void BenchmarkAllScenarios () { + Application.MaximumIterationsPerSecond = ushort.MaxValue; List resultsList = new (); var maxScenarios = 5;