You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

99 lines
3.1 KiB

4 years ago
  1. #if MYSQL_6_10
  2. // Copyright (c) 2009, 2019, Oracle and/or its affiliates. All rights reserved.
  3. //
  4. // MySQL Connector/NET is licensed under the terms of the GPLv2
  5. // <http://www.gnu.org/licenses/old-licenses/gpl-2.0.html>, like most
  6. // MySQL Connectors. There are special exceptions to the terms and
  7. // conditions of the GPLv2 as it is applied to this software, see the
  8. // FLOSS License Exception
  9. // <http://www.mysql.com/about/legal/licensing/foss-exception.html>.
  10. //
  11. // This program is free software; you can redistribute it and/or modify
  12. // it under the terms of the GNU General Public License as published
  13. // by the Free Software Foundation; version 2 of the License.
  14. //
  15. // This program is distributed in the hope that it will be useful, but
  16. // WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  17. // or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  18. // for more details.
  19. //
  20. // You should have received a copy of the GNU General Public License along
  21. // with this program; if not, write to the Free Software Foundation, Inc.,
  22. // 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  23. using System;
  24. namespace Externals.MySql.Data.Common
  25. {
  26. /// <summary>
  27. /// This class is modeled after .NET Stopwatch. It provides better
  28. /// performance (no system calls).It is however less precise than
  29. /// .NET Stopwatch, measuring in milliseconds. It is adequate to use
  30. /// when high-precision is not required (e.g for measuring IO timeouts),
  31. /// but not for other tasks.
  32. /// </summary>
  33. internal class LowResolutionStopwatch
  34. {
  35. long _startTime;
  36. public static readonly long Frequency = 1000; // measure in milliseconds
  37. public static readonly bool IsHighResolution = false;
  38. public LowResolutionStopwatch()
  39. {
  40. ElapsedMilliseconds = 0;
  41. }
  42. public long ElapsedMilliseconds { get; private set; }
  43. public void Start()
  44. {
  45. _startTime = Environment.TickCount;
  46. }
  47. public void Stop()
  48. {
  49. long now = Environment.TickCount;
  50. long elapsed;
  51. // Calculate time different, handle possible overflow
  52. if (now < _startTime)
  53. {
  54. if (now < 0)
  55. elapsed = 1 + ((long)Int32.MaxValue - _startTime) + (now - (long)Int32.MinValue);
  56. else
  57. elapsed = Int32.MaxValue - _startTime + now;
  58. }
  59. else
  60. elapsed = now - _startTime;
  61. ElapsedMilliseconds += elapsed;
  62. }
  63. public void Reset()
  64. {
  65. ElapsedMilliseconds = 0;
  66. _startTime = 0;
  67. }
  68. public TimeSpan Elapsed => new TimeSpan(0, 0, 0, 0, (int)ElapsedMilliseconds);
  69. public static LowResolutionStopwatch StartNew()
  70. {
  71. LowResolutionStopwatch sw = new LowResolutionStopwatch();
  72. sw.Start();
  73. return sw;
  74. }
  75. public static long GetTimestamp()
  76. {
  77. return Environment.TickCount;
  78. }
  79. bool IsRunning()
  80. {
  81. return (_startTime != 0);
  82. }
  83. }
  84. }
  85. #endif