Browse Source

Benchmark cleanup

Tig 9 months ago
parent
commit
ce900ada49
3 changed files with 89 additions and 21 deletions
  1. 5 7
      UICatalog/Scenario.cs
  2. 32 14
      UICatalog/UICatalog.cs
  3. 52 0
      bench.json

+ 5 - 7
UICatalog/Scenario.cs

@@ -151,14 +151,12 @@ public class Scenario : IDisposable
     /// </summary>
     public virtual void Main () { }
 
-    private readonly object _timeoutLock = new ();
-    private object? _timeout;
     private const uint ABORT_TIME = 1000;
-
-    private Stopwatch? _stopwatch;
-
     private const uint MAX_ITERATIONS = 500;
 
+    private readonly object _timeoutLock = new ();
+    private object? _timeout;
+    private Stopwatch? _stopwatch;
     private readonly BenchmarkResults _benchmarkResults = new BenchmarkResults ();
 
     public void StartBenchmark ()
@@ -178,7 +176,7 @@ public class Scenario : IDisposable
                 _timeout = null;
             }
         }
-        
+
         return _benchmarkResults;
     }
 
@@ -252,7 +250,7 @@ public class Scenario : IDisposable
             }
         }
 
-        Console.WriteLine ($@"  Failed to Quit with {Application.QuitKey} after {ABORT_TIME}ms and {BenchmarkResults.IterationCount} iterations. Force quit.");
+        Debug.WriteLine ($@"  Failed to Quit with {Application.QuitKey} after {ABORT_TIME}ms and {BenchmarkResults.IterationCount} iterations. Force quit.");
 
         Application.RequestStop ();
 

+ 32 - 14
UICatalog/UICatalog.cs

@@ -136,7 +136,6 @@ public class UICatalogApp
                         .Select (d => d!.Name)
                         .ToArray ()
             );
-
         driverOption.AddAlias ("-d");
         driverOption.AddAlias ("--d");
 
@@ -144,6 +143,10 @@ public class UICatalogApp
         benchmarkFlag.AddAlias ("-b");
         benchmarkFlag.AddAlias ("--b");
 
+        Option<string> resultsFile = new Option<string> ("--file", "The file to save benchmark results to. If not specified with --benchmark, the results will be displayed in a TableView.");
+        resultsFile.AddAlias ("-f");
+        resultsFile.AddAlias ("--f");
+
         Argument<string> scenarioArgument = new Argument<string> (
                                                                   "scenario",
                                                                   description: "The name of the scenario to run.",
@@ -155,17 +158,20 @@ public class UICatalogApp
                                                                              );
 
 
-        var rootCommand =
-            new RootCommand ("A comprehensive sample library for Terminal.Gui") { scenarioArgument, benchmarkFlag, driverOption };
+        var rootCommand = new RootCommand ("A comprehensive sample library for Terminal.Gui")
+        {
+            scenarioArgument, benchmarkFlag, resultsFile, driverOption,
+        };
 
         rootCommand.SetHandler (
                                 context =>
                                 {
                                     var options = new Options
                                     {
+                                        Scenario = context.ParseResult.GetValueForArgument (scenarioArgument),
                                         Driver = context.ParseResult.GetValueForOption (driverOption) ?? string.Empty,
                                         Benchmark = context.ParseResult.GetValueForOption (benchmarkFlag),
-                                        Scenario = context.ParseResult.GetValueForArgument (scenarioArgument),
+                                        ResultsFile = context.ParseResult.GetValueForOption (resultsFile) ?? string.Empty,
                                         /* etc. */
                                     };
 
@@ -176,6 +182,7 @@ public class UICatalogApp
 
         rootCommand.Invoke (args);
 
+
         UICatalogMain (_options);
 
         return 0;
@@ -404,7 +411,7 @@ public class UICatalogApp
         Application.Init (driverName: _forceDriver);
         scenario.TopLevelColorScheme = _topLevelColorScheme;
 
-        Application.Screen = new (0, 0,120, 50);
+        Application.Screen = new (0, 0, 120, 50);
         scenario.Main ();
 
         BenchmarkResults? results = null;
@@ -435,19 +442,26 @@ public class UICatalogApp
 
             if (maxScenarios == 0)
             {
-                //break;
+                break;
             }
         }
 
         if (resultsList.Count > 0)
         {
-            var output = JsonSerializer.Serialize (
-                                                   resultsList,
-                                                   new JsonSerializerOptions ()
-                                                   {
-                                                       WriteIndented = true
-                                                   });
-            Console.WriteLine (output);
+            if (!string.IsNullOrEmpty (_options.ResultsFile))
+            {
+                var output = JsonSerializer.Serialize (
+                                                       resultsList,
+                                                       new JsonSerializerOptions ()
+                                                       {
+                                                           WriteIndented = true
+                                                       });
+
+                using var file = File.CreateText (_options.ResultsFile);
+                file.Write (output);
+                file.Close ();
+                return;
+            }
 
             Application.Init ();
 
@@ -542,7 +556,7 @@ public class UICatalogApp
                 UpdatedCount = resultsList.Sum (r => r.UpdatedCount),
                 IterationCount = resultsList.Sum (r => r.IterationCount),
             };
-            dt.Rows.Add(
+            dt.Rows.Add (
                         totalRow.Scenario,
                         totalRow.Duration,
                         totalRow.RefreshedCount,
@@ -1316,11 +1330,15 @@ public class UICatalogApp
 
     private struct Options
     {
+        public bool Version;
+
         public string Driver;
 
         public string Scenario;
 
         public bool Benchmark;
+
+        public string ResultsFile;
         /* etc. */
     }
 }

+ 52 - 0
bench.json

@@ -0,0 +1,52 @@
+[
+  {
+    "Scenario": "Adornments Demo",
+    "Duration": "00:00:00.1805368",
+    "IterationCount": 501,
+    "ClearedContentCount": 0,
+    "RefreshedCount": 503,
+    "UpdatedCount": 1,
+    "DrawCompleteCount": 82,
+    "LaidOutCount": 82
+  },
+  {
+    "Scenario": "All Views Tester",
+    "Duration": "00:00:00.1070009",
+    "IterationCount": 501,
+    "ClearedContentCount": 0,
+    "RefreshedCount": 503,
+    "UpdatedCount": 1,
+    "DrawCompleteCount": 103,
+    "LaidOutCount": 182
+  },
+  {
+    "Scenario": "Animation",
+    "Duration": "00:00:00.0675802",
+    "IterationCount": 501,
+    "ClearedContentCount": 0,
+    "RefreshedCount": 503,
+    "UpdatedCount": 1,
+    "DrawCompleteCount": 4,
+    "LaidOutCount": 4
+  },
+  {
+    "Scenario": "Arrangement",
+    "Duration": "00:00:00.1284709",
+    "IterationCount": 501,
+    "ClearedContentCount": 0,
+    "RefreshedCount": 503,
+    "UpdatedCount": 1,
+    "DrawCompleteCount": 123,
+    "LaidOutCount": 123
+  },
+  {
+    "Scenario": "ASCIICustomButtonTest",
+    "Duration": "00:00:01.0613372",
+    "IterationCount": 30,
+    "ClearedContentCount": 0,
+    "RefreshedCount": 32,
+    "UpdatedCount": 31,
+    "DrawCompleteCount": 4185,
+    "LaidOutCount": 2852
+  }
+]