1 //  boost run_timer_test.cpp  -----------------------------------------------------//
2 
3 //  Copyright Beman Dawes 2006, 2008
4 //  Copyright 2009 Vicente J. Botet Escriba
5 
6 //  Distributed under the Boost Software License, Version 1.0.
7 //  See http://www.boost.org/LICENSE_1_0.txt
8 
9 //  See http://www.boost.org/libs/chrono for documentation.
10 
11 #include <boost/chrono/chrono.hpp>
12 #include <boost/chrono/process_cpu_clocks.hpp>
13 #include <boost/chrono/thread_clock.hpp>
14 #include "boost/chrono/stopwatches/simple_stopwatch.hpp"
15 #include <cstdlib> // for atol()
16 #include <iostream>
17 #include <sstream>
18 #include <locale>
19 #include <ctime>
20 #include <cmath>  // for sqrt(), used to burn time
21 
22 //using boost::chrono::run_timer;
23 using boost::system::error_code;
24 
25 #include <boost/detail/lightweight_test.hpp>
26 
27 namespace
28 {
29   typedef boost::chrono::nanoseconds ns;
30 
31   // accuracy test
accuracy_test(int argc,char * argv[])32   void accuracy_test( int argc, char * argv[] )
33   {
34     long timeout_in_secs = 1;
35     if ( argc > 1 ) timeout_in_secs = std::atol( argv[1] );
36     std::cout << "accuracy test for " << timeout_in_secs << " second(s)...";
37 
38     std::clock_t timeout_in_clock_t = std::clock();
39     std::cout << "accuracy test. Now=" << timeout_in_clock_t << " ticks...";
40     timeout_in_clock_t += (timeout_in_secs * CLOCKS_PER_SEC);
41     std::cout << "accuracy test. Timeout=" << timeout_in_clock_t << " ticks...";
42 
43     boost::chrono::simple_stopwatch<boost::chrono::system_clock>          sys;
44 #ifdef BOOST_CHRONO_HAS_CLOCK_STEADY
45     boost::chrono::simple_stopwatch<boost::chrono::steady_clock>          steady;
46 #endif
47     boost::chrono::simple_stopwatch<>  hires;
48     boost::chrono::simple_stopwatch<boost::chrono::process_cpu_clock>          process;
49 
50     std::clock_t now;
51     do
52     {
53       now = std::clock();
54     } while ( now < timeout_in_clock_t );
55 
56     boost::chrono::simple_stopwatch<boost::chrono::system_clock>::duration sys_dur = sys.elapsed();
57 #ifdef BOOST_CHRONO_HAS_CLOCK_STEADY
58     boost::chrono::simple_stopwatch<boost::chrono::steady_clock>::duration steady_dur = steady.elapsed();
59 #endif
60     boost::chrono::simple_stopwatch<>::duration hires_dur = hires.elapsed();
61     boost::chrono::simple_stopwatch<boost::chrono::process_cpu_clock>::duration times;
62     times = process.elapsed();
63     std::cout << "accuracy test. Now=" << now << " ticks...";
64 
65     std::cout << std::endl;
66 
67     ns timeout_in_nanoseconds( static_cast<long long>(timeout_in_secs) * 1000000000LL );
68 
69     //  Allow 20% leeway. Particularly on Linux, there seems to be a large discrepancy
70     //  between std::clock() and higher resolution clocks.
71     ns maximum_delta ( static_cast<long long>(timeout_in_nanoseconds.count() * 0.20 ) );
72 
73     std::cout << timeout_in_nanoseconds.count() << " timeout_in_nanoseconds\n";
74     std::cout << maximum_delta.count() << " maximum_delta\n";
75 
76     std::cout << sys_dur.count() << " sys_dur\n";
77 
78     BOOST_TEST( sys_dur > timeout_in_nanoseconds - maximum_delta
79       && sys_dur < timeout_in_nanoseconds + maximum_delta );
80 
81 #ifdef BOOST_CHRONO_HAS_CLOCK_STEADY
82     std::cout << steady_dur.count() << " steady_dur\n";
83 
84     BOOST_TEST( steady_dur > timeout_in_nanoseconds - maximum_delta
85       && steady_dur < timeout_in_nanoseconds + maximum_delta );
86 #endif
87 
88     std::cout << hires_dur.count() << " hires_dur\n";
89 
90     BOOST_TEST( hires_dur > timeout_in_nanoseconds - maximum_delta
91       && hires_dur < timeout_in_nanoseconds + maximum_delta );
92 
93     std::cout << times.count().real << " times.real\n";
94 
95     BOOST_TEST( ns(times.count().real) > timeout_in_nanoseconds - maximum_delta
96       && ns(times.count().real) < timeout_in_nanoseconds + maximum_delta );
97   }
98 
99 }
100 
main(int argc,char * argv[])101 int main( int argc, char * argv[] )
102 {
103   accuracy_test( argc, argv );
104 
105   return boost::report_errors();
106 }
107 
108