From 2b80c54e594a721acc915a0dc57b4a0015fe34bb Mon Sep 17 00:00:00 2001 From: Tony Redondo Date: Wed, 28 Aug 2024 10:01:52 +0200 Subject: [PATCH] - Add Debug Mode --- src/Directory.Build.props | 2 +- .../Assertors/DefaultAssertor.cs | 2 +- .../Configuration/Builder/ConfigBuilder.cs | 10 ++++++ .../Configuration/Config.cs | 5 +++ src/TimeItSharp.Common/ScenarioProcessor.cs | 32 +++++++++++++++++-- src/TimeItSharp/Program.cs | 8 +++++ 6 files changed, 54 insertions(+), 5 deletions(-) diff --git a/src/Directory.Build.props b/src/Directory.Build.props index c66c71a..8248541 100644 --- a/src/Directory.Build.props +++ b/src/Directory.Build.props @@ -1,6 +1,6 @@ - 0.1.18 + 0.1.19 Tony Redondo, Grégory Léocadie net6.0;net7.0;net8.0 enable diff --git a/src/TimeItSharp.Common/Assertors/DefaultAssertor.cs b/src/TimeItSharp.Common/Assertors/DefaultAssertor.cs index a8fc454..5e94fc0 100644 --- a/src/TimeItSharp.Common/Assertors/DefaultAssertor.cs +++ b/src/TimeItSharp.Common/Assertors/DefaultAssertor.cs @@ -72,7 +72,7 @@ public override AssertResponse ExecutionAssertion(in AssertionData data) _sbuilder.Clear(); _consecutiveErrorCount++; - if (Options.Configuration.ProcessFailedDataPoints) + if (Options.Configuration.ProcessFailedDataPoints || Options.Configuration.DebugMode) { return new AssertResponse( status: Status.Failed, diff --git a/src/TimeItSharp.Common/Configuration/Builder/ConfigBuilder.cs b/src/TimeItSharp.Common/Configuration/Builder/ConfigBuilder.cs index e624364..489b563 100644 --- a/src/TimeItSharp.Common/Configuration/Builder/ConfigBuilder.cs +++ b/src/TimeItSharp.Common/Configuration/Builder/ConfigBuilder.cs @@ -193,6 +193,16 @@ public ConfigBuilder ShowStdOutForFirstRun() return this; } + /// + /// Sets timeit to run in debug mode + /// + /// Configuration builder instance + public ConfigBuilder WithDebugMode() + { + _configuration.DebugMode = true; + return this; + } + #region WithExporter /// diff --git a/src/TimeItSharp.Common/Configuration/Config.cs b/src/TimeItSharp.Common/Configuration/Config.cs index ff92d2e..4d734e1 100644 --- a/src/TimeItSharp.Common/Configuration/Config.cs +++ b/src/TimeItSharp.Common/Configuration/Config.cs @@ -53,6 +53,9 @@ public class Config : ProcessData [JsonPropertyName("showStdOutForFirstRun")] public bool ShowStdOutForFirstRun { get; set; } + [JsonPropertyName("debugMode")] + public bool DebugMode { get; set; } + public Config() { FilePath = string.Empty; @@ -71,6 +74,7 @@ public Config() Services = new(); ProcessFailedDataPoints = false; ShowStdOutForFirstRun = false; + DebugMode = false; } public static Config LoadConfiguration(string filePath) @@ -128,5 +132,6 @@ public static Config LoadConfiguration(string filePath) Tags = new Dictionary(Tags), ProcessFailedDataPoints = ProcessFailedDataPoints, ShowStdOutForFirstRun = ShowStdOutForFirstRun, + DebugMode = DebugMode, }; } \ No newline at end of file diff --git a/src/TimeItSharp.Common/ScenarioProcessor.cs b/src/TimeItSharp.Common/ScenarioProcessor.cs index eac8977..b9e6e88 100644 --- a/src/TimeItSharp.Common/ScenarioProcessor.cs +++ b/src/TimeItSharp.Common/ScenarioProcessor.cs @@ -266,7 +266,10 @@ await RunScenarioAsync(repeat.Count, index, scenario, TimeItPhase.ExtraRun, fals lastStandardOutput = item.StandardOutput; } - if (item.Status == Status.Passed || _configuration.ProcessFailedDataPoints || !anyPassedDataPoint) + if (item.Status == Status.Passed || + _configuration.ProcessFailedDataPoints || + _configuration.DebugMode || + !anyPassedDataPoint) { #if NET7_0_OR_GREATER durations.Add(item.Duration.TotalNanoseconds); @@ -426,6 +429,10 @@ private async Task> RunScenarioAsync(int count, int index, Scena dataPoints.Add(currentRun); AnsiConsole.Markup(currentRun.Status == Status.Failed ? "[red]x[/]" : "[green].[/]"); + if (_configuration.DebugMode) + { + AnsiConsole.WriteLine(); + } if (checkShouldContinue && !currentRun.ShouldContinue) { @@ -481,9 +488,24 @@ private async Task RunCommandAsync(int index, Scenario scenario, Time cmd = cmd.WithArguments(cmdArguments); } - if (executionId == 0 && _configuration.ShowStdOutForFirstRun) + if ((executionId == 0 && _configuration.ShowStdOutForFirstRun) || _configuration.DebugMode) { AnsiConsole.WriteLine(); + if (_configuration.DebugMode) + { + AnsiConsole.Markup(" [aqua]{0}. Running:[/] ", executionId + 1); + } + else + { + AnsiConsole.Markup(" [aqua]Running:[/] "); + } + AnsiConsole.WriteLine("{0} {1}", cmdString, cmdArguments); + if (!string.IsNullOrWhiteSpace(workingDirectory)) + { + AnsiConsole.Markup(" [aqua]Working Folder:[/] "); + AnsiConsole.WriteLine(workingDirectory); + } + AnsiConsole.WriteLine(new string('-', 80)); cmd = cmd.WithStandardOutputPipe(PipeTarget.Merge(cmd.StandardOutputPipe, PipeTarget.ToStream(Console.OpenStandardOutput()))); @@ -735,10 +757,14 @@ mainEndDate is not null && _callbacksTriggers.ExecutionEnd(dataPoint, phase); - if (executionId == 0 && _configuration.ShowStdOutForFirstRun) + if ((executionId == 0 && _configuration.ShowStdOutForFirstRun) || _configuration.DebugMode) { AnsiConsole.WriteLine(new string('-', 80)); AnsiConsole.Write(" "); + if (_configuration.DebugMode) + { + AnsiConsole.Markup(" [aqua]Result:[/] "); + } } return dataPoint; diff --git a/src/TimeItSharp/Program.cs b/src/TimeItSharp/Program.cs index f889d9d..7e27788 100644 --- a/src/TimeItSharp/Program.cs +++ b/src/TimeItSharp/Program.cs @@ -57,6 +57,7 @@ var datadogProfiler = new Option("--datadog-profiler", () => false, "Enable Datadog profiler"); var showStdOutForFistRun = new Option("--first-run-stdout", () => false, "Show the StdOut and StdErr for the first run"); var processFailedExecutions = new Option("--process-failed-executions", () => false, "Include failed executions in the final results"); +var debugMode = new Option("--debug", () => false, "Run timeit in debug mode"); var root = new RootCommand { @@ -70,6 +71,7 @@ datadogProfiler, showStdOutForFistRun, processFailedExecutions, + debugMode, }; root.SetHandler(async (context) => @@ -84,6 +86,7 @@ var datadogProfilerValue = GetValueForHandlerParameter(datadogProfiler, context); var showStdOutForFistRunValue = GetValueForHandlerParameter(showStdOutForFistRun, context); var processFailedExecutionsValue = GetValueForHandlerParameter(processFailedExecutions, context); + var debugModeValue = GetValueForHandlerParameter(debugMode, context); var isConfigFile = false; if (File.Exists(argumentValue)) @@ -155,6 +158,11 @@ configBuilder = configBuilder.ProcessFailedDataPoints(); } + if (debugModeValue) + { + configBuilder = configBuilder.WithDebugMode(); + } + var timeitOption = new TimeItOptions(templateVariablesValue); if (jsonExporterValue)