21 #define BOOST_TEST_MODULE libjmmcg_tests
22 #include <boost/test/included/unit_test.hpp>
24 #include <boost/mpl/list.hpp>
26 #include "core/ave_deviation_meter.hpp"
27 #include "core/config.h"
28 #include "core/hp_timer.hpp"
29 #include "core/stats_output.hpp"
31 #include <boost/chrono.hpp>
38 #if defined(i386) || defined(__i386__) || defined(__x86_64__
)
39 # include <x86intrin.h>
42 using namespace libjmmcg;
47 using result_type=
unsigned long long;
49 static constexpr char const name[]=
"__rdtsc";
58 using result_type=
unsigned long long;
60 static constexpr char const name[]=
"__rdtscp";
64 return __rdtscp(&cpuid);
70 using result_type=
unsigned long long;
72 static constexpr char const name[]=
"gettimeofday";
76 gettimeofday(&tv,
nullptr);
83 using result_type=
unsigned long long;
85 static constexpr char const name[]=
"clock_gettime(REALTIME)";
89 clock_gettime(CLOCK_REALTIME, &tv);
96 using result_type=
unsigned long long;
98 static constexpr char const name[]=
"clock_gettime(REALTIME_COARSE)";
102 clock_gettime(CLOCK_REALTIME_COARSE, &tv);
109 using result_type=
unsigned long long;
111 static constexpr char const name[]=
"clock_gettime(MONOTONIC)";
115 clock_gettime(CLOCK_MONOTONIC, &tv);
122 using result_type=
unsigned long long;
124 static constexpr char const name[]=
"clock_gettime(MONOTONIC_COARSE)";
128 clock_gettime(CLOCK_MONOTONIC_COARSE, &tv);
135 using result_type=
unsigned long long;
137 static constexpr char const name[]=
"clock_gettime(MONOTONIC_RAW)";
141 clock_gettime(CLOCK_MONOTONIC_RAW, &tv);
148 using result_type=
unsigned long long;
150 static constexpr char const name[]=
"clock_gettime(PROCESS_CPUTIME_ID)";
154 clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &tv);
161 using result_type=
unsigned long long;
163 static constexpr char const name[]=
"clock_gettime(THREAD_CPUTIME_ID)";
167 clock_gettime(CLOCK_THREAD_CPUTIME_ID, &tv);
174 using result_type=
unsigned long long;
176 static constexpr char const name[]=
"std::chrono::high_resolution_clock";
179 return std::chrono::high_resolution_clock::now().time_since_epoch().count();
185 using result_type=
unsigned long long;
187 static constexpr char const name[]=
"std::chrono::steady_clock";
190 return std::chrono::steady_clock::now().time_since_epoch().count();
196 using result_type=
unsigned long long;
198 static constexpr char const name[]=
"std::chrono::system_clock";
201 return std::chrono::system_clock::now().time_since_epoch().count();
207 using result_type=
unsigned long long;
209 static constexpr char const name[]=
"boost::chrono::high_resolution_clock";
212 return boost::chrono::high_resolution_clock::now().time_since_epoch().count();
218 using result_type=
unsigned long long;
220 static constexpr char const name[]=
"boost::chrono::steady_clock";
223 return boost::chrono::steady_clock::now().time_since_epoch().count();
229 using result_type=
unsigned long long;
231 static constexpr char const name[]=
"boost::chrono::system_clock";
234 return boost::chrono::system_clock::now().time_since_epoch().count();
258 BOOST_AUTO_TEST_SUITE(timers)
260 BOOST_AUTO_TEST_SUITE(performance, *stats_to_csv::make_fixture(
"timers.csv"))
268 BOOST_AUTO_TEST_CASE_TEMPLATE(time, test, timer_types)
277 #ifdef JMMCG_PERFORMANCE_TESTS
278 const std::size_t num_loops=500000;
279 const unsigned short loops_for_conv=500;
281 const std::size_t num_loops=10;
282 const unsigned short loops_for_conv=5;
284 const double perc_conv_estimate=5.0;
286 const std::pair<timed_results_t,
bool> timed_results(compute_average_deviation<timed_results_t::value_type>(
290 typename test::result_type time=
typename test::result_type();
291 cpu_timer::element_type time_to_measure;
293 cpu_timer::out_of_order timer(time_to_measure);
294 for (
unsigned i=0; i<num_loops; ++i) {
295 time+=test::result();
298 BOOST_CHECK_GT(time_to_measure, 0U);
299 return static_cast<timed_results_t::value_type>(time_to_measure)/num_loops;
302 std::cout<<test::name<<
" time in CPU ticks="<<timed_results.first<<std::endl;
303 #ifdef JMMCG_PERFORMANCE_TESTS
304 stats_to_csv::handle->stats<<timed_results.first.to_csv()<<std::flush;
305 BOOST_CHECK(!timed_results.second);
309 BOOST_AUTO_TEST_SUITE_END()
311 BOOST_AUTO_TEST_SUITE_END()