cpu_timer_test.cpp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. // boost timer_test.cpp --------------------------------------------------------------//
  2. // Copyright Beman Dawes 2006, 2011
  3. // Distributed under the Boost Software License, Version 1.0.
  4. // See http://www.boost.org/LICENSE_1_0.txt
  5. // See http://www.boost.org/libs/timer for documentation.
  6. #include <boost/timer/timer.hpp>
  7. #include <boost/detail/lightweight_main.hpp>
  8. #include <boost/detail/lightweight_test.hpp>
  9. #include <cstdlib> // for atol()
  10. #include <iostream>
  11. #include <string>
  12. #include <ctime>
  13. using std::string;
  14. using std::cout;
  15. using std::endl;
  16. using boost::timer::default_places;
  17. using boost::timer::nanosecond_type;
  18. using boost::timer::cpu_times;
  19. using boost::timer::format;
  20. using boost::timer::cpu_timer;
  21. using boost::timer::auto_cpu_timer;
  22. namespace
  23. {
  24. void unit_test()
  25. {
  26. cout << "unit test..." << endl;
  27. string default_format(" %ws wall, %us user + %ss system = %ts CPU (%p%)\n");
  28. // each constructor
  29. auto_cpu_timer t1;
  30. BOOST_TEST(!t1.is_stopped());
  31. // the following, and similar below, are giving false failures on MinGW/gcc
  32. // so comment them out for now
  33. //BOOST_TEST(&t1.ostream() == &cout);
  34. BOOST_TEST_EQ(t1.places(), default_places);
  35. BOOST_TEST_EQ(t1.format_string(), default_format);
  36. t1.stop();
  37. BOOST_TEST(t1.is_stopped());
  38. auto_cpu_timer t1a(t1);
  39. BOOST_TEST(t1a.is_stopped());
  40. BOOST_TEST_EQ(t1a.elapsed().wall, t1.elapsed().wall);
  41. BOOST_TEST_EQ(t1a.elapsed().user, t1.elapsed().user);
  42. BOOST_TEST_EQ(t1a.elapsed().system, t1.elapsed().system);
  43. //BOOST_TEST(&t1a.ostream() == &cout);
  44. BOOST_TEST_EQ(t1a.places(), default_places);
  45. BOOST_TEST_EQ(t1a.format_string(), default_format);
  46. auto_cpu_timer t1b;
  47. BOOST_TEST(!t1b.is_stopped());
  48. t1b = t1;
  49. BOOST_TEST(t1b.is_stopped());
  50. BOOST_TEST_EQ(t1b.elapsed().wall, t1.elapsed().wall);
  51. BOOST_TEST_EQ(t1b.elapsed().user, t1.elapsed().user);
  52. BOOST_TEST_EQ(t1b.elapsed().system, t1.elapsed().system);
  53. //BOOST_TEST(&t1b.ostream() == &cout);
  54. BOOST_TEST_EQ(t1b.places(), default_places);
  55. BOOST_TEST_EQ(t1b.format_string(), default_format);
  56. auto_cpu_timer t2(1);
  57. BOOST_TEST(!t2.is_stopped());
  58. //BOOST_TEST(&t2.ostream() == &cout);
  59. BOOST_TEST_EQ(t2.places(), 1);
  60. BOOST_TEST_EQ(t2.format_string(), default_format);
  61. auto_cpu_timer t3("foo");
  62. BOOST_TEST(!t3.is_stopped());
  63. //BOOST_TEST(&t3.ostream() == &cout);
  64. BOOST_TEST_EQ(t3.places(), default_places);
  65. BOOST_TEST_EQ(t3.format_string(), string("foo"));
  66. auto_cpu_timer t4(1, "foo");
  67. BOOST_TEST(!t4.is_stopped());
  68. //BOOST_TEST(&t4.ostream() == &cout);
  69. BOOST_TEST_EQ(t4.places(), 1);
  70. BOOST_TEST_EQ(t4.format_string(), string("foo"));
  71. auto_cpu_timer t5(std::cerr);
  72. BOOST_TEST(!t5.is_stopped());
  73. BOOST_TEST(&t5.ostream() == &std::cerr);
  74. BOOST_TEST_EQ(t5.places(), default_places);
  75. BOOST_TEST_EQ(t5.format_string(), default_format);
  76. auto_cpu_timer t6(std::cerr, 1);
  77. BOOST_TEST(!t6.is_stopped());
  78. BOOST_TEST(&t6.ostream() == &std::cerr);
  79. BOOST_TEST_EQ(t6.places(), 1);
  80. BOOST_TEST_EQ(t6.format_string(), default_format);
  81. auto_cpu_timer t7(std::cerr, "foo");
  82. BOOST_TEST(!t7.is_stopped());
  83. BOOST_TEST(&t7.ostream() == &std::cerr);
  84. BOOST_TEST_EQ(t7.places(), default_places);
  85. BOOST_TEST_EQ(t7.format_string(), string("foo"));
  86. auto_cpu_timer t8(std::cerr, 1, "foo");
  87. BOOST_TEST(!t8.is_stopped());
  88. BOOST_TEST(&t8.ostream() == &std::cerr);
  89. BOOST_TEST_EQ(t8.places(), 1);
  90. BOOST_TEST_EQ(t8.format_string(), string("foo"));
  91. t1.stop();
  92. t1a.stop();
  93. t1b.stop();
  94. t2.stop();
  95. t3.stop();
  96. t4.stop();
  97. t5.stop();
  98. t6.stop();
  99. t7.stop();
  100. t8.stop();
  101. cout << " unit test complete" << endl;
  102. }
  103. void format_test()
  104. {
  105. cout << "format test..." << endl;
  106. cpu_times times;
  107. times.wall = 5123456789LL;
  108. times.user = 2123456789LL;
  109. times.system = 1234567890LL;
  110. cout << " times.wall is " << times.wall << '\n';
  111. cout << " times.user is " << times.user << '\n';
  112. cout << " times.system is " << times.system << '\n';
  113. cout << " user+system is " << times.user + times.system << '\n';
  114. cout << " format(times, 9) output: " << format(times, 9);
  115. BOOST_TEST_EQ(format(times, 9),
  116. string(" 5.123456789s wall, 2.123456789s user + 1.234567890s system = 3.358024679s CPU (65.5%)\n"));
  117. BOOST_TEST_EQ(format(times, 8),
  118. string(" 5.12345679s wall, 2.12345679s user + 1.23456789s system = 3.35802468s CPU (65.5%)\n"));
  119. BOOST_TEST_EQ(format(times, 7),
  120. string(" 5.1234568s wall, 2.1234568s user + 1.2345679s system = 3.3580247s CPU (65.5%)\n"));
  121. BOOST_TEST_EQ(format(times, 6),
  122. string(" 5.123457s wall, 2.123457s user + 1.234568s system = 3.358025s CPU (65.5%)\n"));
  123. BOOST_TEST_EQ(format(times, 5),
  124. string(" 5.12346s wall, 2.12346s user + 1.23457s system = 3.35802s CPU (65.5%)\n"));
  125. BOOST_TEST_EQ(format(times, 4),
  126. string(" 5.1235s wall, 2.1235s user + 1.2346s system = 3.3580s CPU (65.5%)\n"));
  127. BOOST_TEST_EQ(format(times, 3),
  128. string(" 5.123s wall, 2.123s user + 1.235s system = 3.358s CPU (65.5%)\n"));
  129. BOOST_TEST_EQ(format(times, 2),
  130. string(" 5.12s wall, 2.12s user + 1.23s system = 3.36s CPU (65.5%)\n"));
  131. BOOST_TEST_EQ(format(times, 1),
  132. string(" 5.1s wall, 2.1s user + 1.2s system = 3.4s CPU (65.5%)\n"));
  133. BOOST_TEST_EQ(format(times, 0),
  134. string(" 5s wall, 2s user + 1s system = 3s CPU (65.5%)\n"));
  135. BOOST_TEST_EQ(format(times, 10),
  136. string(" 5.123456789s wall, 2.123456789s user + 1.234567890s system = 3.358024679s CPU (65.5%)\n"));
  137. BOOST_TEST_EQ(format(times, -1),
  138. string(" 5.123457s wall, 2.123457s user + 1.234568s system = 3.358025s CPU (65.5%)\n"));
  139. BOOST_TEST_EQ(format(times),
  140. string(" 5.123457s wall, 2.123457s user + 1.234568s system = 3.358025s CPU (65.5%)\n"));
  141. BOOST_TEST_EQ(format(times, 5, " %w, %u, %s, %t, %%p%"),
  142. string(" 5.12346, 2.12346, 1.23457, 3.35802, %65.5%"));
  143. BOOST_TEST_EQ(format(times, 5, "boo"), string("boo"));
  144. cout << " format test complete" << endl;
  145. }
  146. void std_c_consistency_test()
  147. {
  148. cout << "C library consistency test..." << endl;
  149. // This test is designed to account for C timer resolution and for the possibility
  150. // that another active process may take up a lot of time.
  151. cpu_timer t; // calls start(), so ensures any cpu_timer dll loaded
  152. std::time(0); // ensure any system dll's loaded
  153. std::time_t stop_time, start_time = std::time(0);
  154. // wait until the time() clock ticks
  155. while (std::time(0) == start_time) {}
  156. // start both timers
  157. start_time = std::time(0);
  158. t.start();
  159. // wait until the time() clock ticks again
  160. while (std::time(0) == start_time) {}
  161. // stop both timers
  162. stop_time = std::time(0);
  163. t.stop();
  164. cout << " std::time() elapsed is " << (stop_time - start_time) * 1.0L << " seconds\n";
  165. cout << " cpu_timer wall elapsed is " << t.elapsed().wall / 1000000000.0L << " seconds\n";
  166. cout << " The two clocks whose elapsed time is compared by this test are started\n"
  167. " and stopped one right after the other. If the operating system suspends\n"
  168. " the process in the interim, the test may fail. Thus no single failure\n"
  169. " of this test is meaningful.\n";
  170. // These tests allow lots of fuzz to reduce false positives
  171. BOOST_TEST(t.elapsed().wall / 1000000000.0L > (stop_time - start_time) * 0.75L);
  172. BOOST_TEST(t.elapsed().wall / 1000000000.0L < (stop_time - start_time) * 1.25L);
  173. cout << " C library consistency test complete" << endl;
  174. }
  175. } // unnamed namespace
  176. //--------------------------------------------------------------------------------------//
  177. int cpp_main(int, char *[])
  178. {
  179. cout << "---------- timer_test ----------\n";
  180. unit_test();
  181. format_test();
  182. std_c_consistency_test();
  183. return ::boost::report_errors();
  184. }