WallClockStopwatch.cs 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. #region Copyright notice and license
  2. // Copyright 2015 gRPC authors.
  3. //
  4. // Licensed under the Apache License, Version 2.0 (the "License");
  5. // you may not use this file except in compliance with the License.
  6. // You may obtain a copy of the License at
  7. //
  8. // http://www.apache.org/licenses/LICENSE-2.0
  9. //
  10. // Unless required by applicable law or agreed to in writing, software
  11. // distributed under the License is distributed on an "AS IS" BASIS,
  12. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. // See the License for the specific language governing permissions and
  14. // limitations under the License.
  15. #endregion
  16. using System;
  17. using System.Collections.Generic;
  18. using System.Diagnostics;
  19. using System.IO;
  20. using System.Linq;
  21. using System.Text.RegularExpressions;
  22. using System.Threading;
  23. using System.Threading.Tasks;
  24. using Google.Protobuf;
  25. using Grpc.Core;
  26. using Grpc.Core.Utils;
  27. using NUnit.Framework;
  28. using Grpc.Testing;
  29. namespace Grpc.IntegrationTesting
  30. {
  31. /// <summary>
  32. /// Snapshottable wall clock stopwatch.
  33. /// </summary>
  34. public class WallClockStopwatch
  35. {
  36. long startTicks;
  37. public WallClockStopwatch()
  38. {
  39. this.startTicks = DateTime.UtcNow.Ticks;
  40. }
  41. public TimeSpan GetElapsedSnapshot(bool reset)
  42. {
  43. var utcNow = DateTime.UtcNow;
  44. long oldStartTicks;
  45. if (reset)
  46. {
  47. oldStartTicks = Interlocked.Exchange(ref this.startTicks, utcNow.Ticks);
  48. }
  49. else
  50. {
  51. oldStartTicks = this.startTicks;
  52. }
  53. return utcNow - new DateTime(oldStartTicks, DateTimeKind.Utc);
  54. }
  55. }
  56. }