Skip to content

Commit

Permalink
improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
tonyredondo committed Nov 24, 2024
1 parent ded34e7 commit b3be62f
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 15 deletions.
21 changes: 11 additions & 10 deletions src/TimeItSharp.Common/Exporters/ConsoleExporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,7 @@ public void Export(TimeitResult results)
"[dodgerblue1 bold]StdErr[/]",
"[dodgerblue1 bold]Median[/]",
"[dodgerblue1 bold]CI 100%[/]",
"[dodgerblue1 bold]P95[/]",
"[dodgerblue1 bold]P90[/]",
"[dodgerblue1 bold]CI 95%[/]",
"[dodgerblue1 bold]Outliers[/]"
};

Expand Down Expand Up @@ -150,8 +149,9 @@ public void Export(TimeitResult results)
Math.Abs(result.Min - result.Max) > 0.0001 ?
$"[aqua][[{Math.Round(Utils.FromNanosecondsToMilliseconds(result.Min), 3)} - {Math.Round(Utils.FromNanosecondsToMilliseconds(result.Max), 3)}]] ms[/]" :
$"[aqua]{Math.Round(Utils.FromNanosecondsToMilliseconds(result.Min), 3)}ms[/]",
$"[aqua]{Math.Round(Utils.FromNanosecondsToMilliseconds(result.P95), 3)}ms[/]",
$"[aqua]{Math.Round(Utils.FromNanosecondsToMilliseconds(result.P90), 3)}ms[/]",
Math.Abs(result.Ci95[0] - result.Ci95[1]) > 0.0001 ?
$"[aqua][[{Math.Round(Utils.FromNanosecondsToMilliseconds(result.Ci95[0]), 3)} - {Math.Round(Utils.FromNanosecondsToMilliseconds(result.Ci95[1]), 3)}]] ms[/]" :
$"[aqua]{Math.Round(Utils.FromNanosecondsToMilliseconds(result.Ci95[0]), 3)}ms[/]",
$"[aqua]{outliersValue}[/]"
};

Expand Down Expand Up @@ -191,8 +191,7 @@ public void Export(TimeitResult results)
var mStdErr = mStdDev / Math.Sqrt(itemResult.Count);
var mMin = itemResult.Min();
var mMax = itemResult.Max();
var mP95 = itemResult.Percentile(95);
var mP90 = itemResult.Percentile(90);
double[] ci95 = [mMean - 1.96 * mStdErr, mMean + 1.96 * mStdErr];

string name;
if (i < totalNum - 1)
Expand All @@ -214,8 +213,9 @@ public void Export(TimeitResult results)
Math.Abs(mMin - mMax) > 0.0001 ?
$"[[{Math.Round(mMin, 3).ToString(CultureInfo.InvariantCulture)} - {Math.Round(mMax, 3).ToString(CultureInfo.InvariantCulture)}]]" :
Math.Round(mMin, 3).ToString(CultureInfo.InvariantCulture),
Math.Round(mP95, 3).ToString(CultureInfo.InvariantCulture),
Math.Round(mP90, 3).ToString(CultureInfo.InvariantCulture),
Math.Abs(ci95[0] - ci95[1]) > 0.0001 ?
$"[[{Math.Round(ci95[0], 3).ToString(CultureInfo.InvariantCulture)} - {Math.Round(ci95[1], 3).ToString(CultureInfo.InvariantCulture)}]]" :
Math.Round(ci95[0], 3).ToString(CultureInfo.InvariantCulture),
(metricsOutliers.Count == 0 ? "0" : metricsOutliers.Count + " {" + Math.Round(metricsThreshold, 3) + "}"));
}
}
Expand All @@ -233,8 +233,9 @@ public void Export(TimeitResult results)
Math.Abs(result.Min - result.Max) > 0.0001 ?
$"[[{Math.Round(Utils.FromNanosecondsToMilliseconds(result.Min), 3)} - {Math.Round(Utils.FromNanosecondsToMilliseconds(result.Max), 3)}]] ms" :
$"{Math.Round(Utils.FromNanosecondsToMilliseconds(result.Min), 3)}ms",
$"{Math.Round(Utils.FromNanosecondsToMilliseconds(result.P95), 3)}ms",
$"{Math.Round(Utils.FromNanosecondsToMilliseconds(result.P90), 3)}ms",
Math.Abs(result.Ci95[0] - result.Ci95[1]) > 0.0001 ?
$"[[{Math.Round(Utils.FromNanosecondsToMilliseconds(result.Ci95[0]), 3)} - {Math.Round(Utils.FromNanosecondsToMilliseconds(result.Ci95[1]), 3)}]] ms" :
$"{Math.Round(Utils.FromNanosecondsToMilliseconds(result.Ci95[0]), 3)}ms",
$"{outliersValue}"
};

Expand Down
11 changes: 10 additions & 1 deletion src/TimeItSharp.Common/Results/ScenarioResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public sealed class ScenarioResult : Scenario

[JsonPropertyName("stderr")]
public double StdErr { get; set; }

[JsonPropertyName("p99")]
public double P99 { get; set; }

Expand All @@ -61,6 +61,15 @@ public sealed class ScenarioResult : Scenario

[JsonPropertyName("p90")]
public double P90 { get; set; }

[JsonPropertyName("ci99")]
public double[] Ci99 { get; set; }

[JsonPropertyName("ci95")]
public double[] Ci95 { get; set; }

[JsonPropertyName("ci90")]
public double[] Ci90 { get; set; }

[JsonPropertyName("isBimodal")]
public bool IsBimodal { get; set; }
Expand Down
12 changes: 9 additions & 3 deletions src/TimeItSharp.Common/ScenarioProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ await RunScenarioAsync(_configuration.WarmUpCount, index, scenario, TimeItPhase.
return null;
}

AnsiConsole.MarkupLine(" Duration: {0}", watch.Elapsed);
AnsiConsole.MarkupLine(" Duration: {0}", watch.Elapsed.ToDurationString());
}

AnsiConsole.Markup(" [green3]Run[/]");
Expand All @@ -240,7 +240,7 @@ await RunScenarioAsync(_configuration.WarmUpCount, index, scenario, TimeItPhase.
}

watch.Stop();
AnsiConsole.MarkupLine(" Duration: {0}", watch.Elapsed);
AnsiConsole.MarkupLine(" Duration: {0}", watch.Elapsed.ToDurationString());

foreach (var repeat in scenarioStartArgs.GetRepeats())
{
Expand All @@ -255,7 +255,7 @@ await RunScenarioAsync(repeat.Count, index, scenario, TimeItPhase.ExtraRun, fals
return null;
}

AnsiConsole.MarkupLine(" Duration: {0}", watch.Elapsed);
AnsiConsole.MarkupLine(" Duration: {0}", watch.Elapsed.ToDurationString());
}

scenario.ParentService = null;
Expand Down Expand Up @@ -326,6 +326,9 @@ await RunScenarioAsync(repeat.Count, index, scenario, TimeItPhase.ExtraRun, fals
var p95 = newDurations.Percentile(95);
var p90 = newDurations.Percentile(90);
var stderr = stdev / Math.Sqrt(newDurations.Count);
double[] ci99 = [mean - 2.576 * stderr, mean + 2.576 * stderr];
double[] ci95 = [mean - 1.96 * stderr, mean + 1.96 * stderr];
double[] ci90 = [mean - 1.645 * stderr, mean + 1.645 * stderr];

// Calculate metrics stats
var metricsStats = new Dictionary<string, double>();
Expand Down Expand Up @@ -392,6 +395,9 @@ await RunScenarioAsync(repeat.Count, index, scenario, TimeItPhase.ExtraRun, fals
P99 = p99,
P95 = p95,
P90 = p90,
Ci99 = ci99,
Ci95 = ci95,
Ci90 = ci90,
IsBimodal = isBimodal,
PeakCount = peakCount,
Metrics = metricsStats,
Expand Down
35 changes: 34 additions & 1 deletion src/TimeItSharp.Common/Utils.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
using TimeItSharp.Common.Results;
using System.Text;
using TimeItSharp.Common.Results;

namespace TimeItSharp.Common;

internal static class Utils
{
[ThreadStatic]
private static StringBuilder? _strBuilder;

/// <summary>
/// Calculates the standard deviation of a sequence of double-precision floating-point numbers.
/// </summary>
Expand Down Expand Up @@ -244,4 +248,33 @@ public static int GetSafeWidth(int defaultValue = 280)
return defaultValue;
}
}

/// <summary>
/// Converts a TimeSpan object to a human-readable string.
/// </summary>
/// <param name="timeSpan">Timespan</param>
/// <returns>human-readable string</returns>
public static string ToDurationString(this TimeSpan timeSpan)
{
_strBuilder ??= new StringBuilder();
if (timeSpan.Hours > 0)
{
_strBuilder.Append($"{timeSpan.Hours} h ");
}

if (timeSpan.Minutes > 0)
{
_strBuilder.Append($"{timeSpan.Minutes} min ");
}

if (timeSpan.Seconds > 0)
{
_strBuilder.Append($"{timeSpan.Seconds} sec ");
}

_strBuilder.Append($"{timeSpan.Milliseconds} ms");
var value = _strBuilder.ToString();
_strBuilder.Clear();
return value;
}
}

0 comments on commit b3be62f

Please sign in to comment.