Skip to content

Commit 25994fb

Browse files
committed
Use new Stopwatch methods for better performance
1 parent 1f0a0b3 commit 25994fb

File tree

1 file changed

+11
-3
lines changed

1 file changed

+11
-3
lines changed

Source/Boxed.AspNetCore/Middleware/ServerTimingMiddleware.cs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,21 @@ public async Task InvokeAsync(HttpContext context, RequestDelegate next)
2424
if (context.Response.SupportsTrailers())
2525
{
2626
context.Response.DeclareTrailer(ServerTimingHttpHeader);
27-
var stopWatch = new Stopwatch();
28-
stopWatch.Start();
27+
#if NET7_0_OR_GREATER
28+
var startingTimestamp = Stopwatch.GetTimestamp();
29+
#else
30+
var stopWatch = Stopwatch.StartNew();
31+
#endif
2932

3033
await next(context).ConfigureAwait(false);
3134

35+
#if NET7_0_OR_GREATER
36+
var elapsedMilliseconds = Stopwatch.GetElapsedTime(startingTimestamp).TotalMilliseconds;
37+
#else
3238
stopWatch.Stop();
33-
context.Response.AppendTrailer(ServerTimingHttpHeader, $"app;dur={stopWatch.ElapsedMilliseconds}.0");
39+
var elapsedMilliseconds = stopWatch.ElapsedMilliseconds;
40+
#endif
41+
context.Response.AppendTrailer(ServerTimingHttpHeader, $"app;dur={elapsedMilliseconds}.0");
3442
}
3543
else
3644
{

0 commit comments

Comments
 (0)