Skip to content

Commit a231651

Browse files
author
Bart Koelman
committed
Align results in columns
1 parent fa3a3e2 commit a231651

File tree

2 files changed

+69
-16
lines changed

2 files changed

+69
-16
lines changed

src/Examples/JsonApiDotNetCoreExample/Startups/Startup.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,10 @@ public override void ConfigureServices(IServiceCollection services)
6767
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
6868
public override void Configure(IApplicationBuilder app, IWebHostEnvironment environment, ILoggerFactory loggerFactory)
6969
{
70+
ILogger<Startup> logger = loggerFactory.CreateLogger<Startup>();
71+
7072
using (CodeTimingSessionManager.Current.Measure("Initialize other (startup)"))
7173
{
72-
ILogger<Startup> logger = loggerFactory.CreateLogger<Startup>();
73-
7474
CreateTestData(app);
7575

7676
app.UseRouting();
@@ -81,11 +81,11 @@ public override void Configure(IApplicationBuilder app, IWebHostEnvironment envi
8181
}
8282

8383
app.UseEndpoints(endpoints => endpoints.MapControllers());
84-
85-
string result = CodeTimingSessionManager.Current.GetResult();
86-
logger.LogWarning($"Measurement results for application startup:{Environment.NewLine}{result}");
8784
}
8885

86+
string result = CodeTimingSessionManager.Current.GetResult();
87+
logger.LogWarning($"Measurement results for application startup:{Environment.NewLine}{result}");
88+
8989
_codeTimingSession.Dispose();
9090
}
9191

src/JsonApiDotNetCore/Diagnostics/CascadingCodeTimer.cs

Lines changed: 64 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -70,20 +70,46 @@ private void Close(MeasureScope scope)
7070
/// <inheritdoc />
7171
public string GetResult()
7272
{
73+
int paddingLength = GetPaddingLength();
74+
7375
var builder = new StringBuilder();
76+
WriteResult(builder, paddingLength);
77+
78+
return builder.ToString();
79+
}
80+
81+
private int GetPaddingLength()
82+
{
83+
int maxLength = 0;
7484

7585
foreach (MeasureScope scope in _completedScopes)
7686
{
77-
scope.WriteResult(builder, 0);
87+
int nextLength = scope.GetPaddingLength();
88+
maxLength = Math.Max(maxLength, nextLength);
7889
}
7990

8091
if (_activeScopeStack.Any())
8192
{
8293
MeasureScope scope = _activeScopeStack.Peek();
83-
scope.WriteResult(builder, 0);
94+
int nextLength = scope.GetPaddingLength();
95+
maxLength = Math.Max(maxLength, nextLength);
8496
}
8597

86-
return builder.ToString();
98+
return maxLength + 3;
99+
}
100+
101+
private void WriteResult(StringBuilder builder, int paddingLength)
102+
{
103+
foreach (MeasureScope scope in _completedScopes)
104+
{
105+
scope.WriteResult(builder, 0, paddingLength);
106+
}
107+
108+
if (_activeScopeStack.Any())
109+
{
110+
MeasureScope scope = _activeScopeStack.Peek();
111+
scope.WriteResult(builder, 0, paddingLength);
112+
}
87113
}
88114

89115
public void Dispose()
@@ -132,6 +158,25 @@ public MeasureScope SpawnChild(CascadingCodeTimer owner, string name, bool exclu
132158
return childScope;
133159
}
134160

161+
public int GetPaddingLength()
162+
{
163+
return GetPaddingLength(0);
164+
}
165+
166+
private int GetPaddingLength(int indent)
167+
{
168+
int selfLength = indent * 2 + Name.Length;
169+
int maxChildrenLength = 0;
170+
171+
foreach (MeasureScope child in _children)
172+
{
173+
int nextLength = child.GetPaddingLength(indent + 1);
174+
maxChildrenLength = Math.Max(nextLength, maxChildrenLength);
175+
}
176+
177+
return Math.Max(selfLength, maxChildrenLength);
178+
}
179+
135180
private TimeSpan GetElapsedInSelf()
136181
{
137182
return GetElapsedInTotal() - GetElapsedInChildren();
@@ -175,26 +220,26 @@ private TimeSpan GetSkippedInChildren()
175220
return skippedInChildren;
176221
}
177222

178-
public void WriteResult(StringBuilder builder, int indent)
223+
public void WriteResult(StringBuilder builder, int indent, int paddingLength)
179224
{
180225
TimeSpan timeElapsedGlobal = GetElapsedInTotal() - GetSkippedInTotal();
181-
WriteResult(builder, indent, timeElapsedGlobal);
226+
WriteResult(builder, indent, timeElapsedGlobal, paddingLength);
182227
}
183228

184-
private void WriteResult(StringBuilder builder, int indent, TimeSpan timeElapsedGlobal)
229+
private void WriteResult(StringBuilder builder, int indent, TimeSpan timeElapsedGlobal, int paddingLength)
185230
{
186231
TimeSpan timeElapsedInSelf = GetElapsedInSelf();
187232
double scaleElapsedInSelf = timeElapsedGlobal != TimeSpan.Zero ? timeElapsedInSelf / timeElapsedGlobal : 0;
188233

189234
WriteIndent(builder, indent);
190235
builder.Append(Name);
191-
builder.Append(": ");
192-
builder.Append(timeElapsedInSelf.ToString("G", CultureInfo.InvariantCulture));
236+
WritePadding(builder, indent, paddingLength);
237+
builder.AppendFormat(CultureInfo.InvariantCulture, "{0,19:G}", timeElapsedInSelf);
193238

194239
if (!_excludeInRelativeCost)
195240
{
196-
builder.Append(" - ");
197-
builder.Append(scaleElapsedInSelf.ToString("P", CultureInfo.InvariantCulture));
241+
builder.Append(" ... ");
242+
builder.AppendFormat(CultureInfo.InvariantCulture, "{0,7:#0.00%}", scaleElapsedInSelf);
198243
}
199244

200245
if (_stoppedAt == null)
@@ -206,7 +251,7 @@ private void WriteResult(StringBuilder builder, int indent, TimeSpan timeElapsed
206251

207252
foreach (MeasureScope child in _children)
208253
{
209-
child.WriteResult(builder, indent + 1, timeElapsedGlobal);
254+
child.WriteResult(builder, indent + 1, timeElapsedGlobal, paddingLength);
210255
}
211256
}
212257

@@ -215,6 +260,14 @@ private static void WriteIndent(StringBuilder builder, int indent)
215260
builder.Append(new string(' ', indent * 2));
216261
}
217262

263+
private void WritePadding(StringBuilder builder, int indent, int paddingLength)
264+
{
265+
string padding = new string('.', paddingLength - Name.Length - indent * 2);
266+
builder.Append(' ');
267+
builder.Append(padding);
268+
builder.Append(' ');
269+
}
270+
218271
public void Dispose()
219272
{
220273
if (_stoppedAt == null)

0 commit comments

Comments
 (0)