Skip to content

Commit 3787cbd

Browse files
Ugonnaak1bgavrilMS
andauthored
Fix bug in MeasureDurationResult (#5035)
* Fix millisecond measurement * Add some unit tests --------- Co-authored-by: Bogdan Gavril <[email protected]>
1 parent 19bf0f3 commit 3787cbd

File tree

3 files changed

+64
-6
lines changed

3 files changed

+64
-6
lines changed

src/client/Microsoft.Identity.Client/Utils/MeasureDurationResult.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ internal struct MeasureDurationResult<TResult>
1818
public MeasureDurationResult(TResult result, long ticks)
1919
{
2020
Result = result;
21-
Milliseconds = (long)(ticks / s_tickFrequency / TimeSpan.TicksPerMillisecond);
22-
Microseconds = (long)(ticks * s_tickFrequency / TicksPerMicrosecond % 1000);
21+
Milliseconds = (long)(ticks * s_tickFrequency / TimeSpan.TicksPerMillisecond);
22+
Microseconds = (long)(ticks * s_tickFrequency / TicksPerMicrosecond);
2323
Ticks = ticks;
2424
}
2525

@@ -51,8 +51,8 @@ internal struct MeasureDurationResult
5151

5252
public MeasureDurationResult(long ticks)
5353
{
54-
Milliseconds = (long)(ticks * s_tickFrequency / TimeSpan.TicksPerMillisecond % 1000);
55-
Microseconds = (long)(ticks * s_tickFrequency / TicksPerMicrosecond % 1000);
54+
Milliseconds = (long)(ticks * s_tickFrequency / TimeSpan.TicksPerMillisecond);
55+
Microseconds = (long)(ticks * s_tickFrequency / TicksPerMicrosecond);
5656
Ticks = ticks;
5757
}
5858

src/client/Microsoft.Identity.Client/Utils/StopWatchService.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ namespace Microsoft.Identity.Client.Utils
1414
/// Singleton timer used to measure the duration tasks.
1515
/// </summary>
1616
internal static class StopwatchService
17-
1817
{
1918
/// <summary>
2019
/// Singleton stopwatch.
@@ -85,7 +84,6 @@ internal static async Task<MeasureDurationResult> MeasureAsync(this Task task)
8584
await task.ConfigureAwait(false);
8685

8786
return new MeasureDurationResult(Watch.ElapsedTicks - startTicks);
88-
;
8987
}
9088

9189
/// <summary>
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
using System;
2+
using System.Diagnostics;
3+
using System.Threading;
4+
using System.Threading.Tasks;
5+
using Microsoft.Identity.Client.Utils;
6+
using Microsoft.VisualStudio.TestTools.UnitTesting;
7+
8+
namespace Microsoft.Identity.Test.Unit.UtilTests
9+
{
10+
[TestClass]
11+
public class StopWatchServiceTests
12+
{
13+
[TestMethod]
14+
public void MeasureCodeBlock()
15+
{
16+
MeasureDurationResult result = StopwatchService.MeasureCodeBlock(() => Thread.Sleep(50));
17+
18+
Assert.IsTrue(result.Milliseconds >= 50, "Measured time is less than expected.");
19+
Assert.IsTrue(result.Milliseconds < 100, "Measured time is too high.");
20+
21+
long diff = result.Microseconds - (result.Milliseconds * 1000);
22+
Assert.IsTrue(diff >= -1000, "Microseconds is less than expected.");
23+
Assert.IsTrue(diff < 1000, "Microseconds is too high.");
24+
}
25+
26+
[TestMethod]
27+
public async Task MeasureCodeBlockAsync()
28+
{
29+
MeasureDurationResult result = await StopwatchService.MeasureCodeBlockAsync(async () =>
30+
{
31+
await Task.Delay(50).ConfigureAwait(true);
32+
}).ConfigureAwait(true);
33+
34+
Assert.IsTrue(result.Milliseconds >= 50, "Measured time is less than expected.");
35+
Assert.IsTrue(result.Milliseconds < 100, "Measured time is too high.");
36+
37+
long diff = result.Microseconds - (result.Milliseconds * 1000);
38+
Assert.IsTrue(diff >= -1000, "Microseconds is less than expected.");
39+
Assert.IsTrue(diff < 1000, "Microseconds is too high.");
40+
}
41+
42+
[TestMethod]
43+
public async Task MeasureCodeBlockAsyncWithResult()
44+
{
45+
MeasureDurationResult<int> result = await StopwatchService.MeasureCodeBlockAsync(async () =>
46+
{
47+
await Task.Delay(50).ConfigureAwait(true);
48+
return 42;
49+
}).ConfigureAwait(true);
50+
51+
Assert.AreEqual(42, result.Result, "Result is not as expected.");
52+
Assert.IsTrue(result.Milliseconds >= 50, "Measured time is less than expected.");
53+
Assert.IsTrue(result.Milliseconds < 100, "Measured time is too high.");
54+
55+
long diff = result.Microseconds - (result.Milliseconds * 1000);
56+
Assert.IsTrue(diff >= -1000, "Microseconds is less than expected.");
57+
Assert.IsTrue(diff < 1000, "Microseconds is too high.");
58+
}
59+
}
60+
}

0 commit comments

Comments
 (0)