Leaked source code of windows server 2003
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.

137 lines
3.1 KiB

  1. //+--------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1996.
  5. //
  6. // File: B E N C H M R K . C P P
  7. //
  8. // Contents: Benchmarking class
  9. //
  10. // Notes:
  11. //
  12. // Author: billbe 13 Oct 1997
  13. //
  14. //---------------------------------------------------------------------------
  15. #include "pch.h"
  16. #pragma hdrstop
  17. #include "benchmrk.h"
  18. CBenchmark::CBenchmark()
  19. : m_i64Frequency(1000),
  20. m_sznDescription(NULL),
  21. m_i64TotalTime(0),
  22. m_fStarted(FALSE)
  23. {
  24. LARGE_INTEGER li1;
  25. // Check if QueryPerformanceCounter is supported
  26. if (QueryPerformanceCounter(&li1))
  27. {
  28. // Now get # of ticks per second
  29. QueryPerformanceFrequency(reinterpret_cast<LARGE_INTEGER*>
  30. (&m_i64Frequency));
  31. //wprintf(L"QueryPerformanceFrequency: %I64d\n", m_i64Frequency);
  32. m_fSupported = TRUE;
  33. }
  34. else
  35. {
  36. // TraceTag(ttidBenchmark, "High performance counter is not supported.");
  37. m_fSupported = FALSE;
  38. wprintf(L"QueryPerformanceFrequency: not supported!!\n");
  39. }
  40. }
  41. CBenchmark::~CBenchmark()
  42. {
  43. delete [] m_sznDescription;
  44. }
  45. void
  46. CBenchmark::Start(PCWSTR sznDescription)
  47. {
  48. // If QueryPerformanceCounter is supported
  49. if (m_fSupported)
  50. {
  51. // replace with new one if specified
  52. if (sznDescription)
  53. {
  54. // delete the old description
  55. delete [] m_sznDescription;
  56. m_sznDescription = new WCHAR[lstrlen(sznDescription) + 1];
  57. if (m_sznDescription)
  58. {
  59. lstrcpy(m_sznDescription, sznDescription);
  60. }
  61. }
  62. else
  63. {
  64. // no description specified clear the member variable
  65. m_sznDescription = NULL;
  66. }
  67. m_fStarted = TRUE;
  68. m_i64TotalTime = 0;
  69. // Record our start time
  70. QueryPerformanceCounter(reinterpret_cast<LARGE_INTEGER*>
  71. (&m_i64StartTime));
  72. }
  73. }
  74. void
  75. CBenchmark::Stop()
  76. {
  77. __int64 i64Stop;
  78. // Record our stop time
  79. QueryPerformanceCounter(reinterpret_cast<LARGE_INTEGER*>(&i64Stop));
  80. // If start was called prior to stop, then record the total time and
  81. // reset our m_fStarted flag
  82. //
  83. if (m_fStarted)
  84. {
  85. m_fStarted = FALSE;
  86. m_i64TotalTime = i64Stop - m_i64StartTime;
  87. }
  88. else
  89. {
  90. // invalidate previous benchmark since stop was called before start
  91. m_i64TotalTime = 0;
  92. }
  93. }
  94. PCWSTR
  95. CBenchmark::SznBenchmarkSeconds(unsigned short usPrecision)
  96. {
  97. WCHAR sznFmt[10];
  98. swprintf(sznFmt, L"%%.%df", usPrecision);
  99. swprintf(m_sznSeconds, sznFmt, DblBenchmarkSeconds());
  100. return m_sznSeconds;
  101. }
  102. CBenchmark g_timer;
  103. void timer_start()
  104. {
  105. g_timer.Start(NULL);
  106. }
  107. void timer_stop()
  108. {
  109. g_timer.Stop();
  110. }
  111. PCWSTR timer_secs()
  112. {
  113. return g_timer.SznBenchmarkSeconds(5);
  114. }
  115. double timer_time()
  116. {
  117. return g_timer.DblBenchmarkSeconds();
  118. }