StatTimer.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /*----------------------------------------------------------------------------*/
  2. /* */
  3. /* StatTimer.h: interface for the CStatTimer class. */
  4. /* */
  5. /* Author: Mark Carrier (mark@carrierlabs.com) */
  6. /* */
  7. /*----------------------------------------------------------------------------*/
  8. /* Copyright (c) 2006 CarrierLabs, LLC. All rights reserved.
  9. *
  10. * Redistribution and use in source and binary forms, with or without
  11. * modification, are permitted provided that the following conditions
  12. * are met:
  13. *
  14. * 1. Redistributions of source code must retain the above copyright
  15. * notice, this list of conditions and the following disclaimer.
  16. *
  17. * 2. Redistributions in binary form must reproduce the above copyright
  18. * notice, this list of conditions and the following disclaimer in
  19. * the documentation and/or other materials provided with the
  20. * distribution.
  21. *
  22. * 3. The name of the author may not be used to endorse or promote products
  23. * derived from this software without specific prior written permission.
  24. *
  25. * 4. The name "CarrierLabs" must not be used to
  26. * endorse or promote products derived from this software without
  27. * prior written permission. For written permission, please contact
  28. * mark@carrierlabs.com.
  29. *
  30. * THIS SOFTWARE IS PROVIDED BY MARK CARRIER ``AS IS'' AND ANY
  31. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  32. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  33. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MARK CARRIER OR
  34. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  35. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  36. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  37. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  38. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  39. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  40. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  41. * OF THE POSSIBILITY OF SUCH DAMAGE.
  42. *----------------------------------------------------------------------------*/
  43. #ifndef __CSTATTIMER_H__
  44. #define __CSTATTIMER_H__
  45. #include <string.h>
  46. #if WIN32
  47. #include <Winsock2.h>
  48. #include <time.h>
  49. #endif
  50. #ifdef _LINUX
  51. #include <stdio.h>
  52. #include <sys/time.h>
  53. #endif
  54. #include "Host.h"
  55. #if defined(WIN32)
  56. #define GET_CLOCK_COUNT(x) QueryPerformanceCounter((LARGE_INTEGER *)x)
  57. #else
  58. #define GET_CLOCK_COUNT(x) gettimeofday(x, NULL)
  59. #endif
  60. #define MILLISECONDS_CONVERSION 1000
  61. #define MICROSECONDS_CONVERSION 1000000
  62. /// Class to abstract socket communications in a cross platform manner.
  63. /// This class is designed
  64. class EXPORT CStatTimer {
  65. public:
  66. CStatTimer()
  67. {
  68. };
  69. ~CStatTimer()
  70. {
  71. };
  72. void Initialize()
  73. {
  74. memset(&m_startTime, 0, sizeof(struct timeval));
  75. memset(&m_endTime, 0, sizeof(struct timeval));
  76. };
  77. struct timeval GetStartTime() { return m_startTime; };
  78. void SetStartTime() { GET_CLOCK_COUNT(&m_startTime); };
  79. struct timeval GetEndTime() { return m_endTime; };
  80. void SetEndTime() { GET_CLOCK_COUNT(&m_endTime); };
  81. uint32 GetMilliSeconds() { return (CalcTotalUSec() / MILLISECONDS_CONVERSION); };
  82. uint32 GetMicroSeconds() { return (CalcTotalUSec()); };
  83. uint32 GetSeconds() { return (CalcTotalUSec() / MICROSECONDS_CONVERSION); };
  84. uint32 GetCurrentTime()
  85. {
  86. struct timeval tmpTime;
  87. GET_CLOCK_COUNT(&tmpTime);
  88. return ((tmpTime.tv_sec * MICROSECONDS_CONVERSION) + tmpTime.tv_usec);
  89. };
  90. private:
  91. uint32 CalcTotalUSec() { return (((m_endTime.tv_sec - m_startTime.tv_sec) * MICROSECONDS_CONVERSION) +
  92. (m_endTime.tv_usec - m_startTime.tv_usec)); };
  93. private:
  94. struct timeval m_startTime;
  95. struct timeval m_endTime;
  96. };
  97. #endif // __CSTATTIMER_H__