From ce900ada49d07db27445fa91eb9774ab73f32f8c Mon Sep 17 00:00:00 2001 From: Tig Date: Fri, 25 Oct 2024 15:15:02 -0600 Subject: [PATCH] Benchmark cleanup --- UICatalog/Scenario.cs | 12 ++++------ UICatalog/UICatalog.cs | 46 +++++++++++++++++++++++++------------ bench.json | 52 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 89 insertions(+), 21 deletions(-) create mode 100644 bench.json diff --git a/UICatalog/Scenario.cs b/UICatalog/Scenario.cs index 71af17793..3936ca1a6 100644 --- a/UICatalog/Scenario.cs +++ b/UICatalog/Scenario.cs @@ -151,14 +151,12 @@ public class Scenario : IDisposable /// 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 (); diff --git a/UICatalog/UICatalog.cs b/UICatalog/UICatalog.cs index 247b69526..6af90b8e6 100644 --- a/UICatalog/UICatalog.cs +++ b/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 resultsFile = new Option ("--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 scenarioArgument = new Argument ( "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. */ } } diff --git a/bench.json b/bench.json new file mode 100644 index 000000000..52a38240e --- /dev/null +++ b/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 + } +] \ No newline at end of file