-
Notifications
You must be signed in to change notification settings - Fork 774
Fixes #5310. Add ConfigurationManager / Scheme / Theme benchmark baseline #5312
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: develop
Are you sure you want to change the base?
Changes from all commits
810628d
4753931
8c099fb
0ae15fe
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| using BenchmarkDotNet.Attributes; | ||
| using Terminal.Gui.Configuration; | ||
|
|
||
| namespace Terminal.Gui.Benchmarks.Configuration; | ||
|
|
||
| /// <summary> | ||
| /// Measures the cold-start cost of loading the embedded library configuration: | ||
| /// <c>ConfigurationManager.Disable (true)</c> → <c>Enable (ConfigLocations.LibraryResources)</c> → <c>Apply ()</c>. | ||
| /// This is the app-startup hot path. | ||
| /// </summary> | ||
| /// <remarks> | ||
| /// <para> | ||
| /// Run: | ||
| /// <code>dotnet run --project Tests/Benchmarks -c Release -- --filter '*ConfigurationManagerLoad*'</code> | ||
| /// </para> | ||
| /// </remarks> | ||
| [MemoryDiagnoser] | ||
| [BenchmarkCategory ("Configuration")] | ||
| public class ConfigurationManagerLoadBenchmark | ||
| { | ||
| /// <summary>Resets ConfigurationManager to a clean state before each iteration.</summary> | ||
| [IterationSetup] | ||
| public void IterationSetup () | ||
| { | ||
| ConfigurationManager.Disable (true); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Loads the embedded library configuration from scratch and applies it. | ||
| /// Captures the full deserialize + merge + apply path. | ||
| /// </summary> | ||
| [Benchmark] | ||
| public void LoadAndApply () | ||
| { | ||
| ConfigurationManager.Enable (ConfigLocations.LibraryResources); | ||
| ConfigurationManager.Apply (); | ||
| } | ||
|
|
||
| /// <summary>Ensures ConfigurationManager is disabled after all iterations.</summary> | ||
| [GlobalCleanup] | ||
| public void Cleanup () | ||
| { | ||
| ConfigurationManager.Disable (true); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| using BenchmarkDotNet.Attributes; | ||
| using Terminal.Gui.Drawing; | ||
| using TgAttribute = Terminal.Gui.Drawing.Attribute; | ||
|
|
||
| namespace Terminal.Gui.Benchmarks.Configuration; | ||
|
|
||
| /// <summary> | ||
| /// Measures <see cref="Scheme.GetAttributeForRole"/> for roles at different depths of the derivation chain: | ||
| /// <list type="bullet"> | ||
| /// <item><see cref="VisualRole.Normal"/> — explicitly set (O(1) lookup)</item> | ||
| /// <item><see cref="VisualRole.HotFocus"/> — derived from <see cref="VisualRole.Focus"/></item> | ||
| /// <item><see cref="VisualRole.Code"/> — deepest derivation (<c>Code → Editable → Normal</c>)</item> | ||
| /// </list> | ||
| /// No <see cref="Terminal.Gui.Configuration.ConfigurationManager"/> required; operates on a standalone | ||
| /// <see cref="Scheme"/> instance. | ||
| /// </summary> | ||
| /// <remarks> | ||
| /// <para> | ||
| /// Run: | ||
| /// <code>dotnet run --project Tests/Benchmarks -c Release -- --filter '*SchemeAttribute*'</code> | ||
| /// </para> | ||
| /// </remarks> | ||
| [MemoryDiagnoser] | ||
| [BenchmarkCategory ("Configuration", "Scheme")] | ||
| public class SchemeAttributeBenchmark | ||
| { | ||
| private Scheme _scheme = null!; | ||
|
|
||
| /// <summary>Creates a scheme with only <see cref="VisualRole.Normal"/> explicitly set.</summary> | ||
| [GlobalSetup] | ||
| public void Setup () | ||
| { | ||
| _scheme = new Scheme { Normal = new TgAttribute (Color.White, Color.Black) }; | ||
| } | ||
|
|
||
| /// <summary>Lookup for an explicitly-set role — the fastest path.</summary> | ||
| [Benchmark (Baseline = true)] | ||
| public TgAttribute GetNormal () => _scheme.GetAttributeForRole (VisualRole.Normal); | ||
|
|
||
| /// <summary>Lookup for a role derived from Focus (which itself is derived from Normal).</summary> | ||
| [Benchmark] | ||
| public TgAttribute GetHotFocus () => _scheme.GetAttributeForRole (VisualRole.HotFocus); | ||
|
|
||
| /// <summary>Lookup for the deepest derivation path: Code → Editable → Normal.</summary> | ||
| [Benchmark] | ||
| public TgAttribute GetCode () => _scheme.GetAttributeForRole (VisualRole.Code); | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| using System.Text.Json; | ||
| using BenchmarkDotNet.Attributes; | ||
| using Terminal.Gui.Configuration; | ||
| using Terminal.Gui.Drawing; | ||
| using TgAttribute = Terminal.Gui.Drawing.Attribute; | ||
|
|
||
| namespace Terminal.Gui.Benchmarks.Configuration; | ||
|
|
||
| /// <summary> | ||
| /// Measures serialize-then-deserialize of a representative <c>Base</c> <see cref="Scheme"/> via | ||
| /// <see cref="JsonSerializer"/> and <see cref="SchemeJsonConverter"/>. Catches regressions in the JSON | ||
| /// code paths when future PRs add fields to <see cref="Scheme"/>. | ||
| /// </summary> | ||
| /// <remarks> | ||
| /// <para> | ||
| /// Run: | ||
| /// <code>dotnet run --project Tests/Benchmarks -c Release -- --filter '*SchemeSerialization*'</code> | ||
| /// </para> | ||
| /// </remarks> | ||
| [MemoryDiagnoser] | ||
| [BenchmarkCategory ("Configuration", "Scheme")] | ||
| public class SchemeSerializationBenchmark | ||
| { | ||
| private Scheme _scheme = null!; | ||
| private string _json = null!; | ||
| private JsonSerializerOptions _options = null!; | ||
|
|
||
| /// <summary> | ||
| /// Creates a representative <c>Base</c> scheme with only <see cref="VisualRole.Normal"/> explicitly set | ||
| /// and prepares serialization options with the <see cref="SchemeJsonConverter"/>. | ||
| /// </summary> | ||
| [GlobalSetup] | ||
| public void Setup () | ||
| { | ||
| _scheme = new Scheme { Normal = new TgAttribute (Color.White, Color.Black) }; | ||
|
|
||
| _options = new JsonSerializerOptions | ||
| { | ||
| Converters = { new SchemeJsonConverter () }, | ||
| PropertyNameCaseInsensitive = true | ||
| }; | ||
|
|
||
| // Pre-serialize to have a stable JSON string for deserialization benchmarks. | ||
| _json = JsonSerializer.Serialize (_scheme, _options); | ||
| } | ||
|
|
||
| /// <summary>Serializes a <see cref="Scheme"/> to JSON.</summary> | ||
| [Benchmark] | ||
| public string Serialize () => JsonSerializer.Serialize (_scheme, _options); | ||
|
|
||
| /// <summary>Deserializes a <see cref="Scheme"/> from JSON.</summary> | ||
| [Benchmark] | ||
| public Scheme? Deserialize () => JsonSerializer.Deserialize<Scheme> (_json, _options); | ||
|
|
||
| /// <summary>Full round-trip: serialize then immediately deserialize.</summary> | ||
| [Benchmark (Baseline = true)] | ||
| public Scheme? RoundTrip () | ||
| { | ||
| string json = JsonSerializer.Serialize (_scheme, _options); | ||
|
|
||
| return JsonSerializer.Deserialize<Scheme> (json, _options); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| using BenchmarkDotNet.Attributes; | ||
| using Terminal.Gui.Configuration; | ||
|
|
||
| namespace Terminal.Gui.Benchmarks.Configuration; | ||
|
|
||
| /// <summary> | ||
| /// Measures the cost of switching the active theme via | ||
| /// <c>ThemeManager.Theme = "X"; ConfigurationManager.Apply ()</c>. | ||
| /// Parametric over every built-in theme name shipped in the embedded <c>config.json</c>. | ||
| /// </summary> | ||
| /// <remarks> | ||
| /// <para> | ||
| /// Run: | ||
| /// <code>dotnet run --project Tests/Benchmarks -c Release -- --filter '*ThemeSwitch*'</code> | ||
| /// </para> | ||
| /// </remarks> | ||
| [MemoryDiagnoser] | ||
| [BenchmarkCategory ("Configuration", "Theme")] | ||
| public class ThemeSwitchBenchmark | ||
| { | ||
| /// <summary>The built-in theme to switch to during each benchmark invocation.</summary> | ||
| [ParamsSource (nameof (ThemeNames))] | ||
| public string ThemeName { get; set; } = ThemeManager.DEFAULT_THEME_NAME; | ||
|
|
||
| /// <summary>Returns the set of built-in theme names available after loading library resources.</summary> | ||
| public static IEnumerable<string> ThemeNames | ||
| { | ||
| get | ||
| { | ||
| ConfigurationManager.Disable (true); | ||
| ConfigurationManager.Enable (ConfigLocations.LibraryResources); | ||
|
|
||
| IEnumerable<string> names = ThemeManager.GetThemeNames (); | ||
|
|
||
| ConfigurationManager.Disable (true); | ||
|
|
||
| return names; | ||
| } | ||
| } | ||
|
|
||
| /// <summary>Loads the embedded configuration so all built-in themes are available.</summary> | ||
| [GlobalSetup] | ||
| public void Setup () | ||
| { | ||
| ConfigurationManager.Disable (true); | ||
| ConfigurationManager.Enable (ConfigLocations.LibraryResources); | ||
| } | ||
|
|
||
| /// <summary>Resets the theme to Default before each iteration for a stable starting point.</summary> | ||
| [IterationSetup] | ||
| public void IterationSetup () | ||
| { | ||
| ThemeManager.Theme = ThemeManager.DEFAULT_THEME_NAME; | ||
| ConfigurationManager.Apply (); | ||
| } | ||
|
Comment on lines
+50
to
+55
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Using Useful? React with 👍 / 👎. |
||
|
|
||
| /// <summary> | ||
| /// Switches the active theme and applies the change. | ||
| /// This is the user-facing hot path when cycling themes via a <see cref="Views.Shortcut"/>. | ||
| /// </summary> | ||
| [Benchmark] | ||
| public void SwitchTheme () | ||
| { | ||
| ThemeManager.Theme = ThemeName; | ||
| ConfigurationManager.Apply (); | ||
| } | ||
|
|
||
| /// <summary>Ensures ConfigurationManager is disabled after all iterations.</summary> | ||
| [GlobalCleanup] | ||
| public void Cleanup () | ||
| { | ||
| ConfigurationManager.Disable (true); | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This benchmark claims to measure cold-start load/apply, but
[IterationSetup]runs only once per iteration while BenchmarkDotNet executes the benchmark method many times per iteration. After the first invocation,ConfigurationManager.Enable(...)short-circuits because it is already enabled, so most samples measure a warmApply()path instead of the documented cold-start path, skewing the baseline used by the perf gate.Useful? React with 👍 / 👎.