Browse Source

Use static methods StopWatch where it makes sense (#8898)

Bond-009 2 years ago
parent
commit
a455e580be

+ 7 - 7
Jellyfin.Server/Middleware/ResponseTimeMiddleware.cs

@@ -40,25 +40,25 @@ namespace Jellyfin.Server.Middleware
         /// <returns>Task.</returns>
         /// <returns>Task.</returns>
         public async Task Invoke(HttpContext context, IServerConfigurationManager serverConfigurationManager)
         public async Task Invoke(HttpContext context, IServerConfigurationManager serverConfigurationManager)
         {
         {
-            var watch = new Stopwatch();
-            watch.Start();
+            var startTimestamp = Stopwatch.GetTimestamp();
+
             var enableWarning = serverConfigurationManager.Configuration.EnableSlowResponseWarning;
             var enableWarning = serverConfigurationManager.Configuration.EnableSlowResponseWarning;
             var warningThreshold = serverConfigurationManager.Configuration.SlowResponseThresholdMs;
             var warningThreshold = serverConfigurationManager.Configuration.SlowResponseThresholdMs;
             context.Response.OnStarting(() =>
             context.Response.OnStarting(() =>
             {
             {
-                watch.Stop();
-                var responseTimeForCompleteRequest = watch.ElapsedMilliseconds;
-                if (enableWarning && responseTimeForCompleteRequest > warningThreshold && _logger.IsEnabled(LogLevel.Debug))
+                var responseTime = Stopwatch.GetElapsedTime(startTimestamp);
+                var responseTimeMs = responseTime.TotalMilliseconds;
+                if (enableWarning && responseTimeMs > warningThreshold && _logger.IsEnabled(LogLevel.Debug))
                 {
                 {
                     _logger.LogDebug(
                     _logger.LogDebug(
                         "Slow HTTP Response from {Url} to {RemoteIp} in {Elapsed:g} with Status Code {StatusCode}",
                         "Slow HTTP Response from {Url} to {RemoteIp} in {Elapsed:g} with Status Code {StatusCode}",
                         context.Request.GetDisplayUrl(),
                         context.Request.GetDisplayUrl(),
                         context.GetNormalizedRemoteIp(),
                         context.GetNormalizedRemoteIp(),
-                        watch.Elapsed,
+                        responseTime,
                         context.Response.StatusCode);
                         context.Response.StatusCode);
                 }
                 }
 
 
-                context.Response.Headers[ResponseHeaderResponseTime] = responseTimeForCompleteRequest.ToString(CultureInfo.InvariantCulture);
+                context.Response.Headers[ResponseHeaderResponseTime] = responseTimeMs.ToString(CultureInfo.InvariantCulture);
                 return Task.CompletedTask;
                 return Task.CompletedTask;
             });
             });
 
 

+ 2 - 5
Jellyfin.Server/Program.cs

@@ -94,8 +94,7 @@ namespace Jellyfin.Server
 
 
         private static async Task StartApp(StartupOptions options)
         private static async Task StartApp(StartupOptions options)
         {
         {
-            var stopWatch = new Stopwatch();
-            stopWatch.Start();
+            var startTimestamp = Stopwatch.GetTimestamp();
 
 
             // Log all uncaught exceptions to std error
             // Log all uncaught exceptions to std error
             static void UnhandledExceptionToConsole(object sender, UnhandledExceptionEventArgs e) =>
             static void UnhandledExceptionToConsole(object sender, UnhandledExceptionEventArgs e) =>
@@ -217,9 +216,7 @@ namespace Jellyfin.Server
 
 
                 await appHost.RunStartupTasksAsync(_tokenSource.Token).ConfigureAwait(false);
                 await appHost.RunStartupTasksAsync(_tokenSource.Token).ConfigureAwait(false);
 
 
-                stopWatch.Stop();
-
-                _logger.LogInformation("Startup complete {Time:g}", stopWatch.Elapsed);
+                _logger.LogInformation("Startup complete {Time:g}", Stopwatch.GetElapsedTime(startTimestamp));
 
 
                 // Block main thread until shutdown
                 // Block main thread until shutdown
                 await Task.Delay(-1, _tokenSource.Token).ConfigureAwait(false);
                 await Task.Delay(-1, _tokenSource.Token).ConfigureAwait(false);