| | 1 | | #pragma warning disable SA1636 |
| | 2 | | // Copyright (c) .NET Foundation. All rights reserved. |
| | 3 | | // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. |
| | 4 | | #pragma warning restore SA1636 |
| | 5 | |
|
| | 6 | | using System; |
| | 7 | | using System.Diagnostics; |
| | 8 | |
|
| | 9 | | namespace Azure.Core.Diagnostics |
| | 10 | | { |
| | 11 | | // https://github.com/dotnet/aspnetcore/blob/master/src/Shared/ValueStopwatch/ValueStopwatch.cs |
| | 12 | | internal struct ValueStopwatch |
| | 13 | | { |
| 0 | 14 | | private static readonly double TimestampToTicks = TimeSpan.TicksPerSecond / (double)Stopwatch.Frequency; |
| | 15 | |
|
| | 16 | | private long _startTimestamp; |
| | 17 | |
|
| 0 | 18 | | public bool IsActive => _startTimestamp != 0; |
| | 19 | |
|
| | 20 | | private ValueStopwatch(long startTimestamp) |
| | 21 | | { |
| 0 | 22 | | _startTimestamp = startTimestamp; |
| 0 | 23 | | } |
| | 24 | |
|
| 0 | 25 | | public static ValueStopwatch StartNew() => new ValueStopwatch(Stopwatch.GetTimestamp()); |
| | 26 | |
|
| | 27 | | public TimeSpan GetElapsedTime() |
| | 28 | | { |
| | 29 | | // Start timestamp can't be zero in an initialized ValueStopwatch. It would have to be literally the first t |
| | 30 | | // So it being 0 is a clear indication of default(ValueStopwatch) |
| 0 | 31 | | if (!IsActive) |
| | 32 | | { |
| 0 | 33 | | throw new InvalidOperationException("An uninitialized, or 'default', ValueStopwatch cannot be used to ge |
| | 34 | | } |
| | 35 | |
|
| 0 | 36 | | var end = Stopwatch.GetTimestamp(); |
| 0 | 37 | | var timestampDelta = end - _startTimestamp; |
| 0 | 38 | | var ticks = (long)(TimestampToTicks * timestampDelta); |
| 0 | 39 | | return new TimeSpan(ticks); |
| | 40 | | } |
| | 41 | | } |
| | 42 | | } |