Skip to content

Commit

Permalink
Fix bug in MeasureDurationResult (#5035)
Browse files Browse the repository at this point in the history
* Fix millisecond measurement

* Add some unit tests

---------

Co-authored-by: Bogdan Gavril <[email protected]>
  • Loading branch information
Ugonnaak1 and bgavrilMS authored Dec 31, 2024
1 parent 19bf0f3 commit 3787cbd
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ internal struct MeasureDurationResult<TResult>
public MeasureDurationResult(TResult result, long ticks)
{
Result = result;
Milliseconds = (long)(ticks / s_tickFrequency / TimeSpan.TicksPerMillisecond);
Microseconds = (long)(ticks * s_tickFrequency / TicksPerMicrosecond % 1000);
Milliseconds = (long)(ticks * s_tickFrequency / TimeSpan.TicksPerMillisecond);
Microseconds = (long)(ticks * s_tickFrequency / TicksPerMicrosecond);
Ticks = ticks;
}

Expand Down Expand Up @@ -51,8 +51,8 @@ internal struct MeasureDurationResult

public MeasureDurationResult(long ticks)
{
Milliseconds = (long)(ticks * s_tickFrequency / TimeSpan.TicksPerMillisecond % 1000);
Microseconds = (long)(ticks * s_tickFrequency / TicksPerMicrosecond % 1000);
Milliseconds = (long)(ticks * s_tickFrequency / TimeSpan.TicksPerMillisecond);
Microseconds = (long)(ticks * s_tickFrequency / TicksPerMicrosecond);
Ticks = ticks;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ namespace Microsoft.Identity.Client.Utils
/// Singleton timer used to measure the duration tasks.
/// </summary>
internal static class StopwatchService

{
/// <summary>
/// Singleton stopwatch.
Expand Down Expand Up @@ -85,7 +84,6 @@ internal static async Task<MeasureDurationResult> MeasureAsync(this Task task)
await task.ConfigureAwait(false);

return new MeasureDurationResult(Watch.ElapsedTicks - startTicks);
;
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
using System;
using System.Diagnostics;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Identity.Client.Utils;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Microsoft.Identity.Test.Unit.UtilTests
{
[TestClass]
public class StopWatchServiceTests
{
[TestMethod]
public void MeasureCodeBlock()
{
MeasureDurationResult result = StopwatchService.MeasureCodeBlock(() => Thread.Sleep(50));

Assert.IsTrue(result.Milliseconds >= 50, "Measured time is less than expected.");
Assert.IsTrue(result.Milliseconds < 100, "Measured time is too high.");

long diff = result.Microseconds - (result.Milliseconds * 1000);
Assert.IsTrue(diff >= -1000, "Microseconds is less than expected.");
Assert.IsTrue(diff < 1000, "Microseconds is too high.");
}

[TestMethod]
public async Task MeasureCodeBlockAsync()
{
MeasureDurationResult result = await StopwatchService.MeasureCodeBlockAsync(async () =>
{
await Task.Delay(50).ConfigureAwait(true);
}).ConfigureAwait(true);

Assert.IsTrue(result.Milliseconds >= 50, "Measured time is less than expected.");
Assert.IsTrue(result.Milliseconds < 100, "Measured time is too high.");

long diff = result.Microseconds - (result.Milliseconds * 1000);
Assert.IsTrue(diff >= -1000, "Microseconds is less than expected.");
Assert.IsTrue(diff < 1000, "Microseconds is too high.");
}

[TestMethod]
public async Task MeasureCodeBlockAsyncWithResult()
{
MeasureDurationResult<int> result = await StopwatchService.MeasureCodeBlockAsync(async () =>
{
await Task.Delay(50).ConfigureAwait(true);
return 42;
}).ConfigureAwait(true);

Assert.AreEqual(42, result.Result, "Result is not as expected.");
Assert.IsTrue(result.Milliseconds >= 50, "Measured time is less than expected.");
Assert.IsTrue(result.Milliseconds < 100, "Measured time is too high.");

long diff = result.Microseconds - (result.Milliseconds * 1000);
Assert.IsTrue(diff >= -1000, "Microseconds is less than expected.");
Assert.IsTrue(diff < 1000, "Microseconds is too high.");
}
}
}

0 comments on commit 3787cbd

Please sign in to comment.