]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/chrono/example/chrono_accuracy_test.cpp
1 // boost run_timer_test.cpp -----------------------------------------------------//
3 // Copyright Beman Dawes 2006, 2008
4 // Copyright 2009 Vicente J. Botet Escriba
6 // Distributed under the Boost Software License, Version 1.0.
7 // See http://www.boost.org/LICENSE_1_0.txt
9 // See http://www.boost.org/libs/chrono for documentation.
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()
20 #include <cmath> // for sqrt(), used to burn time
22 //using boost::chrono::run_timer;
23 using boost::system::error_code
;
25 #include <boost/detail/lightweight_test.hpp>
29 typedef boost::chrono::nanoseconds ns
;
32 void accuracy_test( int argc
, char * argv
[] )
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)...";
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...";
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
;
47 boost::chrono::simple_stopwatch
<> hires
;
48 boost::chrono::simple_stopwatch
<boost::chrono::process_cpu_clock
> process
;
54 } while ( now
< timeout_in_clock_t
);
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();
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...";
65 std::cout
<< std::endl
;
67 ns
timeout_in_nanoseconds( static_cast<long long>(timeout_in_secs
) * 1000000000LL );
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 ) );
73 std::cout
<< timeout_in_nanoseconds
.count() << " timeout_in_nanoseconds\n";
74 std::cout
<< maximum_delta
.count() << " maximum_delta\n";
76 std::cout
<< sys_dur
.count() << " sys_dur\n";
78 BOOST_TEST( sys_dur
> timeout_in_nanoseconds
- maximum_delta
79 && sys_dur
< timeout_in_nanoseconds
+ maximum_delta
);
81 #ifdef BOOST_CHRONO_HAS_CLOCK_STEADY
82 std::cout
<< steady_dur
.count() << " steady_dur\n";
84 BOOST_TEST( steady_dur
> timeout_in_nanoseconds
- maximum_delta
85 && steady_dur
< timeout_in_nanoseconds
+ maximum_delta
);
88 std::cout
<< hires_dur
.count() << " hires_dur\n";
90 BOOST_TEST( hires_dur
> timeout_in_nanoseconds
- maximum_delta
91 && hires_dur
< timeout_in_nanoseconds
+ maximum_delta
);
93 std::cout
<< times
.count().real
<< " times.real\n";
95 BOOST_TEST( ns(times
.count().real
) > timeout_in_nanoseconds
- maximum_delta
96 && ns(times
.count().real
) < timeout_in_nanoseconds
+ maximum_delta
);
101 int main( int argc
, char * argv
[] )
103 accuracy_test( argc
, argv
);
105 return boost::report_errors();