Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.");
}
}
}
Loading