From 4b8dad55879eeb4b5c5baedc194c94f4250016d9 Mon Sep 17 00:00:00 2001 From: Haipz Date: Tue, 12 Nov 2024 15:46:47 +0800 Subject: [PATCH] Cache current process object to avoid performance hit (#5597) * Read working set from Environment in ProcessInfo since it has better performance. * Add unit test for ProcessInfo. * Remove OSSkipCondition tag from process info unit test since it's cross-platform. * Use Environment.WorkingSet in GetMemoryUsageInBytes. --- .../Windows/Interop/ProcessInfo.cs | 4 ++-- .../Windows/WindowsSnapshotProvider.cs | 3 +-- .../Windows/ProcessInfoTests.cs | 23 +++++++++++++++++++ 3 files changed, 26 insertions(+), 4 deletions(-) create mode 100644 test/Libraries/Microsoft.Extensions.Diagnostics.ResourceMonitoring.Tests/Windows/ProcessInfoTests.cs diff --git a/src/Libraries/Microsoft.Extensions.Diagnostics.ResourceMonitoring/Windows/Interop/ProcessInfo.cs b/src/Libraries/Microsoft.Extensions.Diagnostics.ResourceMonitoring/Windows/Interop/ProcessInfo.cs index cb5febeff55..fb5223f3d02 100644 --- a/src/Libraries/Microsoft.Extensions.Diagnostics.ResourceMonitoring/Windows/Interop/ProcessInfo.cs +++ b/src/Libraries/Microsoft.Extensions.Diagnostics.ResourceMonitoring/Windows/Interop/ProcessInfo.cs @@ -1,6 +1,7 @@ // Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. +using System; using System.Diagnostics; using System.Diagnostics.CodeAnalysis; @@ -41,7 +42,6 @@ public ulong GetMemoryUsage() public ulong GetCurrentProcessMemoryUsage() { - using Process process = Process.GetCurrentProcess(); - return (ulong)process.WorkingSet64; + return (ulong)Environment.WorkingSet; } } diff --git a/src/Libraries/Microsoft.Extensions.Diagnostics.ResourceMonitoring/Windows/WindowsSnapshotProvider.cs b/src/Libraries/Microsoft.Extensions.Diagnostics.ResourceMonitoring/Windows/WindowsSnapshotProvider.cs index 7197499afd9..da828a2d064 100644 --- a/src/Libraries/Microsoft.Extensions.Diagnostics.ResourceMonitoring/Windows/WindowsSnapshotProvider.cs +++ b/src/Libraries/Microsoft.Extensions.Diagnostics.ResourceMonitoring/Windows/WindowsSnapshotProvider.cs @@ -109,8 +109,7 @@ internal static long GetCpuTicks() internal static long GetMemoryUsageInBytes() { - using var process = Process.GetCurrentProcess(); - return process.WorkingSet64; + return Environment.WorkingSet; } internal static ulong GetTotalMemoryInBytes() diff --git a/test/Libraries/Microsoft.Extensions.Diagnostics.ResourceMonitoring.Tests/Windows/ProcessInfoTests.cs b/test/Libraries/Microsoft.Extensions.Diagnostics.ResourceMonitoring.Tests/Windows/ProcessInfoTests.cs new file mode 100644 index 00000000000..ab83f2677df --- /dev/null +++ b/test/Libraries/Microsoft.Extensions.Diagnostics.ResourceMonitoring.Tests/Windows/ProcessInfoTests.cs @@ -0,0 +1,23 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +using Microsoft.Extensions.Diagnostics.ResourceMonitoring.Windows.Interop; +using Microsoft.TestUtilities; +using Xunit; + +namespace Microsoft.Extensions.Diagnostics.ResourceMonitoring.Windows.Test; + +/// +/// Process Info Interop Tests. +/// +/// These tests are added for coverage reasons, but the code doesn't have +/// the necessary environment predictability to really test it. +public sealed class ProcessInfoTests +{ + [ConditionalFact] + public void GetCurrentProcessMemoryUsage() + { + var workingSet64 = new ProcessInfo().GetCurrentProcessMemoryUsage(); + Assert.True(workingSet64 > 0); + } +}