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.

98 lines
2.7 KiB

4 years ago
  1. #if MYSQL_6_9
  2. // Copyright (c) 2009 Sun Microsystems, Inc.
  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. class LowResolutionStopwatch
  34. {
  35. long millis;
  36. long startTime;
  37. public static readonly long Frequency = 1000; // measure in milliseconds
  38. public static readonly bool isHighResolution = false;
  39. public LowResolutionStopwatch()
  40. {
  41. millis = 0;
  42. }
  43. public long ElapsedMilliseconds
  44. {
  45. get { return millis; }
  46. }
  47. public void Start()
  48. {
  49. startTime = Environment.TickCount;
  50. }
  51. public void Stop()
  52. {
  53. long now = Environment.TickCount;
  54. // Calculate time different, handle possible overflow
  55. long elapsed = (now < startTime) ? Int32.MaxValue - startTime + now : now - startTime;
  56. millis += elapsed;
  57. }
  58. public void Reset()
  59. {
  60. millis = 0;
  61. startTime = 0;
  62. }
  63. public TimeSpan Elapsed
  64. {
  65. get
  66. {
  67. return new TimeSpan(0, 0, 0, 0, (int)millis);
  68. }
  69. }
  70. public static LowResolutionStopwatch StartNew()
  71. {
  72. LowResolutionStopwatch sw = new LowResolutionStopwatch();
  73. sw.Start();
  74. return sw;
  75. }
  76. public static long GetTimestamp()
  77. {
  78. return Environment.TickCount;
  79. }
  80. bool IsRunning()
  81. {
  82. return (startTime != 0);
  83. }
  84. }
  85. }
  86. #endif