diff options
author | Liudmila Molkova <lmolkova@microsoft.com> | 2017-04-20 05:24:49 +0300 |
---|---|---|
committer | Liudmila Molkova <lmolkova@microsoft.com> | 2017-04-20 05:59:40 +0300 |
commit | 0bedd4c9b8b8cbbcfbd6cf65ef7a29be9f7fdd88 (patch) | |
tree | f506202c069e926c07e2a03a2dd9b73d74caecfd /src | |
parent | 0ad947709f6629cf965a17ab94d9a67c3faf85b2 (diff) |
Use precise StartTime and duration on .NET Desktop
Diffstat (limited to 'src')
-rw-r--r-- | src/System.Diagnostics.DiagnosticSource/src/AssemblyInfo.netfx.cs (renamed from src/System.Diagnostics.DiagnosticSource/src/AssemblyInfo.Net46.cs) | 0 | ||||
-rw-r--r-- | src/System.Diagnostics.DiagnosticSource/src/System.Diagnostics.DiagnosticSource.csproj | 6 | ||||
-rw-r--r-- | src/System.Diagnostics.DiagnosticSource/src/System/Diagnostics/Activity.DateTime.corefx.cs | 14 | ||||
-rw-r--r-- | src/System.Diagnostics.DiagnosticSource/src/System/Diagnostics/Activity.DateTime.netfx.cs | 35 | ||||
-rw-r--r-- | src/System.Diagnostics.DiagnosticSource/src/System/Diagnostics/Activity.cs | 8 | ||||
-rw-r--r-- | src/System.Diagnostics.DiagnosticSource/tests/ActivityDateTimeTests.cs | 24 | ||||
-rw-r--r-- | src/System.Diagnostics.DiagnosticSource/tests/System.Diagnostics.DiagnosticSource.Tests.csproj | 1 |
7 files changed, 85 insertions, 3 deletions
diff --git a/src/System.Diagnostics.DiagnosticSource/src/AssemblyInfo.Net46.cs b/src/System.Diagnostics.DiagnosticSource/src/AssemblyInfo.netfx.cs index bfb744e1bc..bfb744e1bc 100644 --- a/src/System.Diagnostics.DiagnosticSource/src/AssemblyInfo.Net46.cs +++ b/src/System.Diagnostics.DiagnosticSource/src/AssemblyInfo.netfx.cs diff --git a/src/System.Diagnostics.DiagnosticSource/src/System.Diagnostics.DiagnosticSource.csproj b/src/System.Diagnostics.DiagnosticSource/src/System.Diagnostics.DiagnosticSource.csproj index 3fc1281f86..19cf56deb9 100644 --- a/src/System.Diagnostics.DiagnosticSource/src/System.Diagnostics.DiagnosticSource.csproj +++ b/src/System.Diagnostics.DiagnosticSource/src/System.Diagnostics.DiagnosticSource.csproj @@ -59,6 +59,9 @@ <TargetingPackReference Include="System" /> <TargetingPackReference Include="System.Runtime.Remoting" /> </ItemGroup> + <ItemGroup Condition=" '$(TargetGroup)' == 'netcoreapp' OR '$(TargetGroup)' == 'uap' OR '$(TargetGroup)' == 'netstandard1.1' OR '$(TargetGroup)' == 'netstandard1.3' OR '$(TargetGroup)' == 'netstandard'"> + <Compile Include="System\Diagnostics\Activity.DateTime.corefx.cs" /> + </ItemGroup> <ItemGroup Condition=" '$(TargetGroup)' == 'netstandard1.3' OR '$(TargetGroup)' == 'uap'"> <Compile Include="System\Diagnostics\Activity.Id.Random.cs" /> </ItemGroup> @@ -78,7 +81,8 @@ <Reference Include="System.Threading" /> </ItemGroup> <ItemGroup Condition="'$(TargetGroup)' == 'net45' OR '$(TargetGroup)' == 'net46' OR '$(TargetGroup)' == 'netfx'"> - <Compile Include="AssemblyInfo.Net46.cs" /> + <Compile Include="AssemblyInfo.netfx.cs" /> + <Compile Include="System\Diagnostics\Activity.DateTime.netfx.cs" /> <Reference Include="mscorlib" /> <Reference Include="System" /> </ItemGroup> diff --git a/src/System.Diagnostics.DiagnosticSource/src/System/Diagnostics/Activity.DateTime.corefx.cs b/src/System.Diagnostics.DiagnosticSource/src/System/Diagnostics/Activity.DateTime.corefx.cs new file mode 100644 index 0000000000..c485b41b7b --- /dev/null +++ b/src/System.Diagnostics.DiagnosticSource/src/System/Diagnostics/Activity.DateTime.corefx.cs @@ -0,0 +1,14 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +namespace System.Diagnostics +{ + partial class Activity + { + private DateTime GetUtcNow() + { + return DateTime.UtcNow; + } + } +}
\ No newline at end of file diff --git a/src/System.Diagnostics.DiagnosticSource/src/System/Diagnostics/Activity.DateTime.netfx.cs b/src/System.Diagnostics.DiagnosticSource/src/System/Diagnostics/Activity.DateTime.netfx.cs new file mode 100644 index 0000000000..d7e7ec349e --- /dev/null +++ b/src/System.Diagnostics.DiagnosticSource/src/System/Diagnostics/Activity.DateTime.netfx.cs @@ -0,0 +1,35 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System.Threading; + +namespace System.Diagnostics +{ + partial class Activity + { + private DateTime GetUtcNow() + { + // Timer ticks need to be converted to DateTime ticks + long dateTimeTicksDiff = (long)((Stopwatch.GetTimestamp() - syncStopwatchTicks) * 10000000L / + (double)Stopwatch.Frequency); + + // DateTime.AddSeconds (or Milliseconds) rounds value to 1 ms, use AddTicks to prevent it + return syncUtcNow.AddTicks(dateTimeTicksDiff); + } + + private static void Sync() + { + // wait for DateTime.UtcNow update to the next granular value + Thread.Sleep(1); + syncStopwatchTicks = Stopwatch.GetTimestamp(); + syncUtcNow = DateTime.UtcNow; + } + + private static DateTime syncUtcNow; + private static long syncStopwatchTicks; + + // sync DateTime and Stopwatch ticks every 2 hours + private static Timer syncTimeUpdater = new Timer(s => { Sync(); }, null, 0, 7200000); + } +}
\ No newline at end of file diff --git a/src/System.Diagnostics.DiagnosticSource/src/System/Diagnostics/Activity.cs b/src/System.Diagnostics.DiagnosticSource/src/System/Diagnostics/Activity.cs index 6342582d28..35f7083e30 100644 --- a/src/System.Diagnostics.DiagnosticSource/src/System/Diagnostics/Activity.cs +++ b/src/System.Diagnostics.DiagnosticSource/src/System/Diagnostics/Activity.cs @@ -303,7 +303,9 @@ namespace System.Diagnostics } if (StartTimeUtc == default(DateTime)) - StartTimeUtc = DateTime.UtcNow; + { + StartTimeUtc = GetUtcNow(); + } Id = GenerateId(); Current = this; @@ -331,7 +333,9 @@ namespace System.Diagnostics isFinished = true; if (Duration == TimeSpan.Zero) - SetEndTime(DateTime.UtcNow); + { + SetEndTime(GetUtcNow()); + } Current = Parent; } diff --git a/src/System.Diagnostics.DiagnosticSource/tests/ActivityDateTimeTests.cs b/src/System.Diagnostics.DiagnosticSource/tests/ActivityDateTimeTests.cs new file mode 100644 index 0000000000..feb17e2f13 --- /dev/null +++ b/src/System.Diagnostics.DiagnosticSource/tests/ActivityDateTimeTests.cs @@ -0,0 +1,24 @@ +using System.Threading; +using Xunit; + +namespace System.Diagnostics.Tests +{ + public class ActivityDateTimeTests + { + [Fact] + public void StartStopReturnsPreciseDuration() + { + var activity = new Activity("test"); + + var sw = Stopwatch.StartNew(); + + activity.Start(); + SpinWait.SpinUntil(() => sw.ElapsedMilliseconds > 1, 3); + activity.Stop(); + + sw.Stop(); + + Assert.True(activity.Duration.TotalMilliseconds > 1 && activity.Duration.TotalMilliseconds <= sw.ElapsedMilliseconds); + } + } +}
\ No newline at end of file diff --git a/src/System.Diagnostics.DiagnosticSource/tests/System.Diagnostics.DiagnosticSource.Tests.csproj b/src/System.Diagnostics.DiagnosticSource/tests/System.Diagnostics.DiagnosticSource.Tests.csproj index 10c2589941..b02dba964f 100644 --- a/src/System.Diagnostics.DiagnosticSource/tests/System.Diagnostics.DiagnosticSource.Tests.csproj +++ b/src/System.Diagnostics.DiagnosticSource/tests/System.Diagnostics.DiagnosticSource.Tests.csproj @@ -31,6 +31,7 @@ </ItemGroup> <ItemGroup Condition=" '$(TargetGroup)' == 'netfx'"> <Compile Include="HttpHandlerDiagnosticListenerTests.cs" /> + <Compile Include="ActivityDateTimeTests.cs" /> </ItemGroup> <Import Project="$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildThisFileDirectory), dir.targets))\dir.targets" /> </Project>
\ No newline at end of file |