Skip to content

Commit

Permalink
Default to 2 decimal places for timings and add options to control it (
Browse files Browse the repository at this point in the history
…#580)

This adds a new `PopupDecimalPlaces` option to control, but defaults to 2 now that things are getting so fast we need more precision. For anyone who finds this cluttered options to control it have been added and the old behavior is 1 setting away. Added to samples and examples.

Example from sample:
![example](https://user-images.githubusercontent.com/454813/138612824-09450896-080a-49ef-bc91-71f1ef031aea.png)
  • Loading branch information
NickCraver authored Oct 24, 2021
1 parent f9b191b commit 29e886e
Show file tree
Hide file tree
Showing 17 changed files with 64 additions and 14 deletions.
1 change: 1 addition & 0 deletions docs/AspDotNet.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ protected void Application_Start()
// specified position in the .RenderIncludes() call.
PopupRenderPosition = RenderPosition.Right, // defaults to left
PopupMaxTracesToShow = 10, // defaults to 15
PopupDecimalPlaces = 1, // defaults to 2
ColorScheme = ColorScheme.Auto, // defaults to light
// ResultsAuthorize (optional - open to all by default):
Expand Down
4 changes: 4 additions & 0 deletions docs/AspDotNetCore.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ public void ConfigureServices(IServiceCollection services)
// (Optional) Use something other than the "light" color scheme.
// (defaults to "light")
options.ColorScheme = StackExchange.Profiling.ColorScheme.Auto;

// Optionally change the number of decimal places shown for millisecond timings.
// (defaults to 2)
options.PopupDecimalPlaces = 1;

// The below are newer options, available in .NET Core 3.0 and above:
Expand Down
1 change: 1 addition & 0 deletions docs/Releases.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ This page tracks major changes included in any update starting with version 4.0.
- **New**:
- Added an option to control `SpacesAfterCommas` to `InlineSqlFormatter` and `SqlServerFormatter` ([#549](https://github.com/MiniProfiler/dotnet/pull/549) - thanks [Turnerj](https://github.com/Turnerj))
- Fixed `nonce` attribute propagation to generated `<link>` style element for full CSP support ([#565](https://github.com/MiniProfiler/dotnet/pull/565))
- Changed popup to show 2 decimal places for timings by default, with a new `PopupDecimalPlaces` option to control this (present everywhere settings are) ([#580](https://github.com/MiniProfiler/dotnet/pull/580))

#### Version 4.2.1
- **New**:
Expand Down
3 changes: 3 additions & 0 deletions samples/Samples.AspNet5/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,9 @@ public void ConfigureServices(IServiceCollection services)
// Optionally use something other than the "light" color scheme.
options.ColorScheme = StackExchange.Profiling.ColorScheme.Auto;

// Optionally change the number of decimal places shown for millisecond timings.
options.PopupDecimalPlaces = 2;

// Enabled sending the Server-Timing header on responses
options.EnableServerTimingHeader = true;

Expand Down
2 changes: 1 addition & 1 deletion samples/Samples.AspNet5/Views/Shared/_Layout.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
@RenderSection("scripts", required: false)

@* Simple options are exposed...or make a full options class for customizing. *@
<mini-profiler position="@RenderPosition.Right" max-traces="5" color-scheme="ColorScheme.Auto" nonce="45" />
<mini-profiler position="@RenderPosition.Right" max-traces="5" color-scheme="ColorScheme.Auto" nonce="45" decimal-places="2" />
@*<mini-profiler options="new RenderOptions { Position = RenderPosition.Right, MaxTracesToShow = 5, ColorScheme = ColorScheme.Auto }" />*@
</body>
</html>
1 change: 1 addition & 0 deletions samples/Samples.Mvc5/Global.asax.cs
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ private void InitProfilerSettings()
// specified position in the .RenderIncludes() call.
PopupRenderPosition = RenderPosition.Right, // defaults to left
PopupMaxTracesToShow = 10, // defaults to 15
PopupDecimalPlaces = 1, // defaults to 2
ColorScheme = ColorScheme.Auto, // defaults to light

// ResultsAuthorize (optional - open to all by default):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,16 @@ public class MiniProfilerScriptTagHelper : TagHelper
public bool? StartHidden { get; set; }

/// <summary>
/// The color scheme to start with:
/// The color scheme to start with.
/// </summary>
[HtmlAttributeName("color-scheme")]
public ColorScheme? ColorScheme { get; set; }

/// <summary>
/// The number of decimal places to show on timings (which are in miliseconds).
/// </summary>
public int? DecimalPlaces { get; set;}

/// <summary>
/// The JavaScript nonce (if any) to use on this script tag render.
/// </summary>
Expand All @@ -83,6 +88,7 @@ private RenderOptions GetOptions()
if (ShowControls.HasValue) options.ShowControls = ShowControls;
if (StartHidden.HasValue) options.StartHidden = StartHidden;
if (ColorScheme.HasValue) options.ColorScheme = ColorScheme;
if (DecimalPlaces.HasValue) options.DecimalPlaces = DecimalPlaces;
if (Nonce.HasValue()) options.Nonce = Nonce;

return options;
Expand Down
2 changes: 1 addition & 1 deletion src/MiniProfiler.Providers.MySql/MySqlStorage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public override void Save(MiniProfiler profiler)
Name = profiler.Name.Truncate(200),
User = profiler.User.Truncate(100),
RootTimingId = profiler.Root?.Id,
profiler.DurationMilliseconds,
DurationMilliseconds = decimal.Round(profiler.DurationMilliseconds, 1),
profiler.HasUserViewed,
MachineName = profiler.MachineName.Truncate(100),
profiler.CustomLinksJson,
Expand Down
2 changes: 1 addition & 1 deletion src/MiniProfiler.Providers.PostgreSql/PostgreSqlStorage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public override void Save(MiniProfiler profiler)
Name = profiler.Name.Truncate(200),
User = profiler.User.Truncate(100),
RootTimingId = profiler.Root?.Id,
profiler.DurationMilliseconds,
DurationMilliseconds = decimal.Round(profiler.DurationMilliseconds, 1),
profiler.HasUserViewed,
MachineName = profiler.MachineName.Truncate(100),
profiler.CustomLinksJson,
Expand Down
2 changes: 1 addition & 1 deletion src/MiniProfiler.Providers.SqlServer/SqlServerStorage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public override void Save(MiniProfiler profiler)
Name = profiler.Name.Truncate(200),
User = profiler.User.Truncate(100),
RootTimingId = profiler.Root?.Id,
profiler.DurationMilliseconds,
DurationMilliseconds = decimal.Round(profiler.DurationMilliseconds, 1),
profiler.HasUserViewed,
MachineName = profiler.MachineName.Truncate(100),
profiler.CustomLinksJson,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ public override async Task SaveAsync(MiniProfiler profiler)
User = profiler.User.Truncate(100),
Name = profiler.Name.Truncate(200),
RootTimingId = profiler.Root?.Id,
profiler.DurationMilliseconds,
DurationMilliseconds = decimal.Round(profiler.DurationMilliseconds, 1),
profiler.HasUserViewed,
MachineName = profiler.MachineName.Truncate(100),
profiler.CustomLinksJson,
Expand Down
6 changes: 6 additions & 0 deletions src/MiniProfiler.Shared/Internal/MiniProfilerBaseOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,12 @@ public class MiniProfilerBaseOptions
/// </summary>
public RenderPosition PopupRenderPosition { get; set; } = RenderPosition.Left;

/// <summary>
/// Determines how many decimal places to show in popup timings.
/// Note: precision maxes out at 2 decimal places in recorded data.
/// </summary>
public int PopupDecimalPlaces { get; set; } = 2;

/// <summary>
/// Determines which color scheme to use when rendering (the class to apply for CSS styling).
/// </summary>
Expand Down
3 changes: 3 additions & 0 deletions src/MiniProfiler.Shared/Internal/Render.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ public static string Includes(

sb.Append("\" data-scheme=\"");
sb.Append((renderOptions?.ColorScheme ?? options.ColorScheme).ToString());

sb.Append("\" data-decimal-places=\"");
sb.Append((renderOptions?.DecimalPlaces ?? options.PopupDecimalPlaces).ToString());
sb.Append('"');

if (isAuthorized)
Expand Down
7 changes: 3 additions & 4 deletions src/MiniProfiler.Shared/MiniProfiler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -355,14 +355,13 @@ internal Timing StepImpl(string name, decimal? minSaveMs = null, bool? includeCh
}

/// <summary>
/// Returns milliseconds based on Stopwatch's Frequency, rounded to one decimal place.
/// Returns milliseconds based on Stopwatch's Frequency, rounded to two decimal places.
/// </summary>
/// <param name="ticks">The tick count to round.</param>
internal decimal GetRoundedMilliseconds(long ticks)
{
long z = 10000 * ticks;
decimal timesTen = (int)(z / Stopwatch.Frequency);
return timesTen / 10;
long times100 = ticks * 100 / TimeSpan.TicksPerMillisecond;
return times100 / 100m;
}

/// <summary>
Expand Down
6 changes: 6 additions & 0 deletions src/MiniProfiler.Shared/RenderOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,12 @@ public class RenderOptions
/// </summary>
public ColorScheme? ColorScheme { get; set; }

/// <summary>
/// The number of decimal places to show on timings (which are in miliseconds).
/// Defaults to <see cref="MiniProfilerBaseOptions.PopupDecimalPlaces"/>.
/// </summary>
public int? DecimalPlaces { get; set; }

/// <summary>
/// A one-time-use nonce to render in the script tag.
/// </summary>
Expand Down
4 changes: 3 additions & 1 deletion src/MiniProfiler.Shared/ui/lib/MiniProfiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ namespace StackExchange.Profiling {
authorized: boolean;
colorScheme: ColorScheme;
currentId: string;
decimalPlaces: number;
ids: string[];
ignoredDuplicateExecuteTypes: string[];
maxTracesToShow: number;
Expand Down Expand Up @@ -289,6 +290,7 @@ namespace StackExchange.Profiling {
version: data.version,
renderPosition: data.position as RenderPosition,
colorScheme: data.scheme as ColorScheme,
decimalPlaces: parseInt(data.decimalPlaces || '2', 10),
showTrivial: bool(data.trivial),
trivialMilliseconds: parseFloat(data.trivialMilliseconds),
showChildrenTime: bool(data.children),
Expand Down Expand Up @@ -689,7 +691,7 @@ namespace StackExchange.Profiling {
if (milliseconds === undefined) {
return '';
}
return (milliseconds || 0).toFixed(decimalPlaces === undefined ? 1 : decimalPlaces);
return (milliseconds || 0).toFixed(decimalPlaces === undefined ? this.options.decimalPlaces : decimalPlaces);
};
const renderDebugInfo = (timing: ITiming) => {
if (timing.DebugInfo) {
Expand Down
24 changes: 21 additions & 3 deletions tests/MiniProfiler.Tests/RenderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public void DefaultRender()
Assert.NotNull(result);
Assert.Contains("id=\"mini-profiler\"", result);

var expected = $@"<script async id=""mini-profiler"" src=""/includes.min.js?v={Options.VersionHash}"" data-version=""{Options.VersionHash}"" data-path=""/"" data-current-id=""{profiler.Id}"" data-ids=""{profiler.Id}"" data-position=""Left"" data-scheme=""Light"" data-authorized=""true"" data-max-traces=""15"" data-toggle-shortcut=""Alt+P"" data-trivial-milliseconds=""2.0"" data-ignored-duplicate-execute-types=""Open,OpenAsync,Close,CloseAsync""></script>";
var expected = $@"<script async id=""mini-profiler"" src=""/includes.min.js?v={Options.VersionHash}"" data-version=""{Options.VersionHash}"" data-path=""/"" data-current-id=""{profiler.Id}"" data-ids=""{profiler.Id}"" data-position=""Left"" data-scheme=""Light"" data-decimal-places=""2"" data-authorized=""true"" data-max-traces=""15"" data-toggle-shortcut=""Alt+P"" data-trivial-milliseconds=""2.0"" data-ignored-duplicate-execute-types=""Open,OpenAsync,Close,CloseAsync""></script>";
Assert.Equal(expected, result);
}

Expand All @@ -40,15 +40,16 @@ public void OptionsSet()
ShowTimeWithChildren = true,
ShowTrivial = true,
StartHidden = true,
TrivialDurationThresholdMilliseconds = 23
TrivialDurationThresholdMilliseconds = 23,
DecimalPlaces = 1,
};
var result = Render.Includes(profiler, "/", true, renderOptions, new List<Guid>() { profiler.Id });
Output.WriteLine("Result: " + result);

Assert.NotNull(result);
Assert.Contains("id=\"mini-profiler\"", result);

var expected = $@"<script async id=""mini-profiler"" src=""/includes.min.js?v={Options.VersionHash}"" data-version=""{Options.VersionHash}"" data-path=""/"" data-current-id=""{profiler.Id}"" data-ids=""{profiler.Id}"" data-position=""Right"" data-scheme=""Auto"" data-authorized=""true"" data-trivial=""true"" data-children=""true"" data-controls=""true"" data-start-hidden=""true"" nonce=""myNonce"" data-max-traces=""12"" data-toggle-shortcut=""Alt+Q"" data-trivial-milliseconds=""23"" data-ignored-duplicate-execute-types=""Open,OpenAsync,Close,CloseAsync""></script>";
var expected = $@"<script async id=""mini-profiler"" src=""/includes.min.js?v={Options.VersionHash}"" data-version=""{Options.VersionHash}"" data-path=""/"" data-current-id=""{profiler.Id}"" data-ids=""{profiler.Id}"" data-position=""Right"" data-scheme=""Auto"" data-decimal-places=""1"" data-authorized=""true"" data-trivial=""true"" data-children=""true"" data-controls=""true"" data-start-hidden=""true"" nonce=""myNonce"" data-max-traces=""12"" data-toggle-shortcut=""Alt+Q"" data-trivial-milliseconds=""23"" data-ignored-duplicate-execute-types=""Open,OpenAsync,Close,CloseAsync""></script>";
Assert.Equal(expected, result);
}

Expand Down Expand Up @@ -87,6 +88,23 @@ public void Positions(RenderPosition? position, string expected)
Assert.Contains(expected, result);
}

[Theory]
[InlineData(null, @"data-decimal-places=""2""")]
[InlineData(0, @"data-decimal-places=""0""")]
[InlineData(1, @"data-decimal-places=""1""")]
[InlineData(2, @"data-decimal-places=""2""")]
public void DecimalPlaces(int? decimalPlaces, string expected)
{
var profiler = GetBasicProfiler();
var renderOptions = new RenderOptions() { DecimalPlaces = decimalPlaces };

var result = Render.Includes(profiler, " / ", true, renderOptions);
Output.WriteLine("Result: " + result);

Assert.NotNull(result);
Assert.Contains(expected, result);
}

[Fact]
public void Nonce()
{
Expand Down

0 comments on commit 29e886e

Please sign in to comment.