-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add utility for performance measurement, tracking the duration of sco…
…ped code block. (#894) * Add common performance measurement utility. * Update unit test.
- Loading branch information
Showing
6 changed files
with
141 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
....Health.MeasurementUtility.UnitTests/Microsoft.Health.MeasurementUtility.UnitTests.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<Description>Unit tests for common utilities used by Microsoft Health.</Description> | ||
<TargetFrameworks>$(SupportedFrameworks)</TargetFrameworks> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="MediatR" /> | ||
<PackageReference Include="Microsoft.NET.Test.Sdk" /> | ||
<PackageReference Include="Newtonsoft.Json" /> | ||
<PackageReference Include="NSubstitute" /> | ||
<PackageReference Include="xunit" /> | ||
<PackageReference Include="xunit.runner.visualstudio" PrivateAssets="All" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\Microsoft.Health.MeasurementUtility\Microsoft.Health.MeasurementUtility.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
28 changes: 28 additions & 0 deletions
28
src/Microsoft.Health.MeasurementUtility.UnitTests/PerformanceTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
// ------------------------------------------------------------------------------------------------- | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License (MIT). See LICENSE in the repo root for license information. | ||
// ------------------------------------------------------------------------------------------------- | ||
|
||
using System.Threading; | ||
using Xunit; | ||
|
||
namespace Microsoft.Health.MeasurementUtility.UnitTests; | ||
|
||
public class PerformanceTests | ||
{ | ||
[Fact] | ||
public void GivenTheITimed_WhenBeingDisposed_ThenHandlerShouldBeInvoked() | ||
{ | ||
bool hasHandlerInvoked = false; | ||
using (ITimed timedHandler = Performance.TrackDuration(duration => | ||
{ | ||
hasHandlerInvoked = true; | ||
Assert.True(duration > 1000); | ||
})) | ||
{ | ||
Thread.Sleep(1000); | ||
} | ||
|
||
Assert.True(hasHandlerInvoked); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
// ------------------------------------------------------------------------------------------------- | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License (MIT). See LICENSE in the repo root for license information. | ||
// ------------------------------------------------------------------------------------------------- | ||
|
||
using System; | ||
|
||
namespace Microsoft.Health.MeasurementUtility; | ||
|
||
public interface ITimed : IDisposable | ||
{ | ||
TimeSpan Elapsed { get; } | ||
|
||
ITimed Record(); | ||
} |
12 changes: 12 additions & 0 deletions
12
src/Microsoft.Health.MeasurementUtility/Microsoft.Health.MeasurementUtility.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<Description>Common utilities used by Microsoft Health.</Description> | ||
<TargetFrameworks>$(SupportedFrameworks)</TargetFrameworks> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Ensure.That" /> | ||
</ItemGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
// ------------------------------------------------------------------------------------------------- | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License (MIT). See LICENSE in the repo root for license information. | ||
// ------------------------------------------------------------------------------------------------- | ||
|
||
using System; | ||
using System.Diagnostics; | ||
using EnsureThat; | ||
|
||
namespace Microsoft.Health.MeasurementUtility; | ||
|
||
public static class Performance | ||
{ | ||
/// <summary> | ||
/// Track the duration of the execution time in the life cycle of the ITimed. | ||
/// </summary> | ||
/// <param name="handler">The callback to handle the duration time.</param> | ||
/// <returns></returns> | ||
public static ITimed TrackDuration(Action<double> handler) | ||
{ | ||
EnsureArg.IsNotNull(handler, nameof(handler)); | ||
|
||
return new TimedHandler | ||
{ | ||
Stopwatch = Stopwatch.StartNew(), | ||
Handler = handler, | ||
}; | ||
} | ||
|
||
[System.Diagnostics.CodeAnalysis.SuppressMessage("Performance", "CA1815:Override equals and operator equals on value types", Justification = "The struct is Private.")] | ||
private struct TimedHandler : ITimed | ||
{ | ||
public Stopwatch Stopwatch { get; set; } | ||
|
||
public Action<double> Handler { get; set; } | ||
|
||
public TimeSpan Elapsed => Stopwatch.Elapsed; | ||
|
||
public ITimed Record() | ||
{ | ||
Handler(Elapsed.TotalMilliseconds); | ||
return this; | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
Stopwatch.Stop(); | ||
Record(); | ||
} | ||
} | ||
} |