PageRenderTime 47ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/mysql-connector-c++-1.1.0/test/framework/test_timer.cpp

#
C++ | 246 lines | 156 code | 52 blank | 38 comment | 33 complexity | 963885faec5b34c04d76563d740a3b72 MD5 | raw file
Possible License(s): GPL-2.0
  1. /*
  2. Copyright (c) 2009, 2010, Oracle and/or its affiliates. All rights reserved.
  3. The MySQL Connector/C++ is licensed under the terms of the GPLv2
  4. <http://www.gnu.org/licenses/old-licenses/gpl-2.0.html>, like most
  5. MySQL Connectors. There are special exceptions to the terms and
  6. conditions of the GPLv2 as it is applied to this software, see the
  7. FLOSS License Exception
  8. <http://www.mysql.com/about/legal/licensing/foss-exception.html>.
  9. This program is free software; you can redistribute it and/or modify
  10. it under the terms of the GNU General Public License as published
  11. by the Free Software Foundation; version 2 of the License.
  12. This program is distributed in the hope that it will be useful, but
  13. WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  14. or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  15. for more details.
  16. You should have received a copy of the GNU General Public License along
  17. with this program; if not, write to the Free Software Foundation, Inc.,
  18. 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. #include "test_timer.h"
  21. namespace testsuite
  22. {
  23. Timer::Timer()
  24. {
  25. }
  26. clock_t Timer::startTest(const String & test)
  27. {
  28. std::map<String, test_timer>::const_iterator cit=theInstance().timeRecorder.find(test);
  29. if (cit == theInstance().timeRecorder.end())
  30. {
  31. theInstance().currentTest=test;
  32. test_timer t=test_timer();
  33. theInstance().timeRecorder[test]=t;
  34. return theInstance().timeRecorder[test].cpu;
  35. }
  36. return static_cast<clock_t> (-1);
  37. }
  38. clock_t Timer::stopTest(const String & test)
  39. {
  40. std::map<String, test_timer>::const_iterator cit=theInstance().timeRecorder.find(test);
  41. if (cit != theInstance().timeRecorder.end())
  42. {
  43. clock_t now=clock();
  44. theInstance().timeRecorder[test].cpu=now - theInstance().timeRecorder[test].cpu;
  45. return theInstance().timeRecorder[test].cpu;
  46. }
  47. return static_cast<clock_t> (-1);
  48. }
  49. clock_t Timer::startTimer(const String & name, const String & file, const unsigned int line)
  50. {
  51. // HACK
  52. return startTimer(theInstance().currentTest, name, file, line);
  53. }
  54. clock_t Timer::startTimer(const String & test, const String & name, const String & file, const unsigned int line)
  55. {
  56. std::map<String, test_timer>::const_iterator cit=theInstance().timeRecorder.find(test);
  57. if (cit == theInstance().timeRecorder.end())
  58. // unknown test - must not happen
  59. return static_cast<clock_t> (-1);
  60. std::map<String, timer>::const_iterator it=theInstance().timeRecorder[test].timers.find(name);
  61. clock_t now=clock();
  62. if (it == theInstance().timeRecorder[test].timers.end())
  63. {
  64. timer t=timer(now, file, line);
  65. theInstance().timeRecorder[test].timers[name]=t;
  66. }
  67. else
  68. {
  69. /* TODO: API semantics are somewhat unclear - not sure what is best */
  70. theInstance().timeRecorder[test].timers[name].start=now;
  71. theInstance().timeRecorder[name].timers[name].stopped=false;
  72. }
  73. return theInstance().timeRecorder[name].timers[name].start;
  74. }
  75. clock_t Timer::stopTimer(const String & name)
  76. {
  77. // hack
  78. return stopTimer(theInstance().currentTest, name);
  79. }
  80. clock_t Timer::stopTimer(const String & test, const String & name)
  81. {
  82. std::map<String, test_timer>::const_iterator cit=theInstance().timeRecorder.find(test);
  83. if (cit == theInstance().timeRecorder.end())
  84. // unknown test - must not happen
  85. return static_cast<clock_t> (-1);
  86. std::map<String, timer>::const_iterator it=theInstance().timeRecorder[test].timers.find(name);
  87. if (it == theInstance().timeRecorder[test].timers.end())
  88. // unknown timer
  89. return static_cast<clock_t> (-1);
  90. if (theInstance().timeRecorder[test].timers[name].stopped)
  91. // has been stopped before
  92. return static_cast<clock_t> (-1);
  93. clock_t runtime=clock() - theInstance().timeRecorder[test].timers[name].start;
  94. theInstance().timeRecorder[test].timers[name].stopped=true;
  95. theInstance().timeRecorder[test].timers[name].total_cpu+=runtime;
  96. theInstance().timeRecorder[test].timers[name].start=static_cast<clock_t> (0);
  97. return runtime;
  98. }
  99. clock_t Timer::getTime(const String &name)
  100. {
  101. return getTime(theInstance().currentTest, name);
  102. }
  103. clock_t Timer::getTime(const String & test, const String & name)
  104. {
  105. std::map<String, test_timer>::const_iterator cit=theInstance().timeRecorder.find(test);
  106. if (cit == theInstance().timeRecorder.end())
  107. // unknown test - must not happen
  108. return static_cast<clock_t> (-1);
  109. std::map<String, timer>::const_iterator it=theInstance().timeRecorder[test].timers.find(name);
  110. if (it == theInstance().timeRecorder[test].timers.end())
  111. // unknown timer
  112. return static_cast<clock_t> (-1);
  113. return theInstance().timeRecorder[test].timers[name].total_cpu;
  114. }
  115. float Timer::getSeconds(const String & name)
  116. {
  117. return translate2seconds(getTime(name));
  118. }
  119. float Timer::translate2seconds(clock_t inWallClocks)
  120. {
  121. #ifdef CLOCKS_PER_SEC
  122. /* it looks like CLOCKS_PER_SEC should be defined on all platforms... just to feel safe.
  123. Maybe use sysconf(_SC_CLK_TCK) */
  124. return static_cast<float> (inWallClocks) / CLOCKS_PER_SEC;
  125. #else
  126. return static_cast<float> (inWallClocks);
  127. #endif
  128. }
  129. unsigned int Timer::getLine(const String &name)
  130. {
  131. return getLine(theInstance().currentTest, name);
  132. }
  133. unsigned int Timer::getLine(const String & test, const String & name)
  134. {
  135. std::map<String, test_timer>::const_iterator cit=theInstance().timeRecorder.find(test);
  136. if (cit == theInstance().timeRecorder.end())
  137. // unknown test - must not happen
  138. return 0;
  139. std::map<String, timer>::const_iterator it=theInstance().timeRecorder[test].timers.find(name);
  140. if (it == theInstance().timeRecorder[test].timers.end())
  141. // unknown timer
  142. return 0;
  143. return theInstance().timeRecorder[test].timers[name].line;
  144. }
  145. const String Timer::getFile(const String &name)
  146. {
  147. return getFile(theInstance().currentTest, name);
  148. }
  149. const String Timer::getFile(const String & test, const String & name)
  150. {
  151. std::map<String, test_timer>::const_iterator cit=theInstance().timeRecorder.find(test);
  152. if (cit == theInstance().timeRecorder.end())
  153. // unknown test - must not happen
  154. return 0;
  155. std::map<String, timer>::const_iterator it=theInstance().timeRecorder[test].timers.find(name);
  156. if (it == theInstance().timeRecorder[test].timers.end())
  157. // unknown timer
  158. return 0;
  159. return theInstance().timeRecorder[test].timers[name].file;
  160. }
  161. const List & Timer::getNames()
  162. {
  163. static List names;
  164. std::map<String, test_timer>::const_iterator cit=theInstance().timeRecorder.find("perf_statement::anonymousSelect");
  165. std::map<String, timer>::const_iterator it;
  166. for (; cit != theInstance().timeRecorder.end(); ++cit)
  167. {
  168. for (it=theInstance().timeRecorder[cit->first].timers.begin(); it != theInstance().timeRecorder[cit->first].timers.end(); ++it)
  169. {
  170. names.push_back(it->first);
  171. }
  172. }
  173. return names;
  174. }
  175. const List & Timer::getNames(const String & test)
  176. {
  177. static List names;
  178. std::map<String, test_timer>::const_iterator cit=theInstance().timeRecorder.find(test);
  179. if (cit == theInstance().timeRecorder.end())
  180. {
  181. return names;
  182. }
  183. std::map<String, timer>::const_iterator it;
  184. for (it=theInstance().timeRecorder[cit->first].timers.begin(); it != theInstance().timeRecorder[cit->first].timers.end(); ++it)
  185. {
  186. names.push_back(it->first);
  187. }
  188. return names;
  189. }
  190. }