Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r49819 - in sandbox/chrono: boost/chrono libs/chrono/src libs/chrono/test libs/chrono/test/chrono_msvc/run_timer_test
From: bdawes_at_[hidden]
Date: 2008-11-17 17:29:07


Author: bemandawes
Date: 2008-11-17 17:29:06 EST (Mon, 17 Nov 2008)
New Revision: 49819
URL: http://svn.boost.org/trac/boost/changeset/49819

Log:
Add run_timer::test_report and test cases using it, add timer tests, clock accuracy tests.
Text files modified:
   sandbox/chrono/boost/chrono/process_times.hpp | 28 ++++++---
   sandbox/chrono/boost/chrono/timer.hpp | 6 +
   sandbox/chrono/libs/chrono/src/run_timer.cpp | 12 ++++
   sandbox/chrono/libs/chrono/test/chrono_msvc/run_timer_test/run_timer_test.vcproj | 4 +
   sandbox/chrono/libs/chrono/test/run_timer_test.cpp | 105 ++++++++++++++++++++++++++++++++++++++-
   5 files changed, 140 insertions(+), 15 deletions(-)

Modified: sandbox/chrono/boost/chrono/process_times.hpp
==============================================================================
--- sandbox/chrono/boost/chrono/process_times.hpp (original)
+++ sandbox/chrono/boost/chrono/process_times.hpp 2008-11-17 17:29:06 EST (Mon, 17 Nov 2008)
@@ -23,16 +23,7 @@
   namespace chrono
   {
 
-//---------------------------------------------------------------------------------//
-// process_times //
-//---------------------------------------------------------------------------------//
-
- struct process_times
- {
- nanoseconds real; // real time
- nanoseconds user; // user cpu time
- nanoseconds system; // system cpu time
- };
+ struct process_times;
 
 //---------------------------------------------------------------------------------//
 // process_clock //
@@ -52,6 +43,17 @@
     };
 
 //---------------------------------------------------------------------------------//
+// process_times //
+//---------------------------------------------------------------------------------//
+
+ struct process_times
+ {
+ process_clock::duration real; // real time
+ process_clock::duration user; // user cpu time
+ process_clock::duration system; // system cpu time
+ };
+
+//---------------------------------------------------------------------------------//
 // process_timer //
 //---------------------------------------------------------------------------------//
 
@@ -59,6 +61,10 @@
     {
     public:
 
+ typedef process_clock clock;
+ typedef process_clock::duration duration;
+ typedef process_clock::time_point time_point;
+
       explicit process_timer( system::error_code & ec = system::throws )
       {
         start(ec);
@@ -144,6 +150,8 @@
 
       void report( system::error_code & ec = system::throws );
 
+ void test_report( duration real_, duration user_, duration system_ );
+
       bool reported() const { return m_reported; }
 
       static int default_places() { return m_default_places; }

Modified: sandbox/chrono/boost/chrono/timer.hpp
==============================================================================
--- sandbox/chrono/boost/chrono/timer.hpp (original)
+++ sandbox/chrono/boost/chrono/timer.hpp 2008-11-17 17:29:06 EST (Mon, 17 Nov 2008)
@@ -29,8 +29,8 @@
     {
     public:
       typedef Clock clock;
- typedef typename Clock::time_point time_point;
       typedef typename Clock::duration duration;
+ typedef typename Clock::time_point time_point;
 
       explicit timer( system::error_code & ec = system::throws )
         { start(ec); }
@@ -47,6 +47,10 @@
       time_point m_start;
     };
 
+ typedef boost::chrono::timer< boost::chrono::system_clock > system_timer;
+ typedef boost::chrono::timer< boost::chrono::monotonic_clock > monotonic_timer;
+ typedef boost::chrono::timer< boost::chrono::high_resolution_clock > high_resolution_timer;
+
   } // namespace chrono
 } // namespace boost
 

Modified: sandbox/chrono/libs/chrono/src/run_timer.cpp
==============================================================================
--- sandbox/chrono/libs/chrono/src/run_timer.cpp (original)
+++ sandbox/chrono/libs/chrono/src/run_timer.cpp 2008-11-17 17:29:06 EST (Mon, 17 Nov 2008)
@@ -140,5 +140,17 @@
       }
     }
 
+ void run_timer::test_report( duration real_, duration user_, duration system_ )
+ {
+ if ( m_format.empty() ) m_format = default_format;
+
+ process_times times;
+ times.real = real_;
+ times.user = user_;
+ times.system = system_;
+
+ show_time( times, m_format.c_str(), m_places, m_os );
+ }
+
   } // namespace chrono
 } // namespace boost

Modified: sandbox/chrono/libs/chrono/test/chrono_msvc/run_timer_test/run_timer_test.vcproj
==============================================================================
--- sandbox/chrono/libs/chrono/test/chrono_msvc/run_timer_test/run_timer_test.vcproj (original)
+++ sandbox/chrono/libs/chrono/test/chrono_msvc/run_timer_test/run_timer_test.vcproj 2008-11-17 17:29:06 EST (Mon, 17 Nov 2008)
@@ -84,6 +84,8 @@
                         />
                         <Tool
                                 Name="VCPostBuildEventTool"
+ Description="Executing test $(TargetName).exe..."
+ CommandLine="&quot;$(TargetDir)\$(TargetName).exe&quot;"
                         />
                 </Configuration>
                 <Configuration
@@ -157,6 +159,8 @@
                         />
                         <Tool
                                 Name="VCPostBuildEventTool"
+ Description="Executing test $(TargetName).exe..."
+ CommandLine="&quot;$(TargetDir)\$(TargetName).exe&quot;"
                         />
                 </Configuration>
         </Configurations>

Modified: sandbox/chrono/libs/chrono/test/run_timer_test.cpp
==============================================================================
--- sandbox/chrono/libs/chrono/test/run_timer_test.cpp (original)
+++ sandbox/chrono/libs/chrono/test/run_timer_test.cpp 2008-11-17 17:29:06 EST (Mon, 17 Nov 2008)
@@ -11,13 +11,37 @@
 #include <boost/chrono/timer.hpp>
 #include <cstdlib> // for atol()
 #include <iostream>
+#include <sstream>
 #include <locale>
+#include <ctime>
 
 using boost::chrono::run_timer;
 using boost::system::error_code;
 
+#include <boost/test/minimal.hpp>
+
+#define CHECK_REPORT(Timer,String_Stream,R,U,S,Expected_String) \
+ check_report(Timer, String_Stream, R, U, S, Expected_String, __LINE__)
+
+
 namespace
 {
+ typedef boost::chrono::nanoseconds ns;
+
+ bool check_report( run_timer & tmr, std::stringstream & ss,
+ run_timer::duration r, run_timer::duration u, run_timer::duration s,
+ const std::string & expected, int line )
+ {
+ tmr.test_report(r,u,s);
+ bool result(true);
+ if ( ss.str() != expected )
+ {
+ std::cout << "run_timer_test.cpp(" << line << ") : error: actual output \""
+ << ss.str() << "\" != expected \"" << expected << "\"\n";
+ result = false;
+ }
+ return result;
+ }
   
   void run_timer_constructor_overload_test()
   {
@@ -58,17 +82,90 @@
   }
 }
 
-int main( int argc, char * argv[] )
+int test_main( int argc, char * argv[] )
 {
   std::locale loc( "" ); // test with appropriate locale
   std::cout.imbue( loc );
 
+ {
+ std::stringstream ss;
+ run_timer t(ss);
+ BOOST_CHECK( CHECK_REPORT(t, ss, ns(0), ns(0), ns(0),
+ "\nreal 0.000s, cpu 0.000s (0.0%), user 0.000s, system 0.000s\n" ) );
+ }
+
+ {
+ std::stringstream ss;
+ run_timer t(ss);
+ BOOST_CHECK( CHECK_REPORT(t, ss, ns(3000000000), ns(2000000000), ns(1000000000),
+ "\nreal 3.000s, cpu 3.000s (100.0%), user 2.000s, system 1.000s\n" ) );
+ }
+
+ {
+ std::stringstream ss;
+ run_timer t( ss, "9 places: r %r, c %c, p %p, u %u, s %s", 9 );
+ BOOST_CHECK( CHECK_REPORT(t, ss, ns(3000000003), ns(2000000002), ns(1000000001),
+ "9 places: "
+ "r 3.000000003, c 3.000000003, p 100.0, u 2.000000002, s 1.000000001" ) );
+ }
+
   run_timer_constructor_overload_test();
+
+ // accuracy test
+
+ long timeout_in_secs = 1;
+ if ( argc > 1 ) timeout_in_secs = std::atol( argv[1] );
+
+ std::clock_t timeout_in_clock_t = std::clock();
+ timeout_in_clock_t += (timeout_in_secs * CLOCKS_PER_SEC);
+
+ std::cout << "accuracy test for " << timeout_in_secs << " second(s)...";
+
+ boost::chrono::system_timer sys;
+ boost::chrono::monotonic_timer mono;
+ boost::chrono::high_resolution_timer hires;
+ boost::chrono::process_timer process;
   
- //run_timer timer(6);
- //run_timer timer2("\nrtc %r sec, utilization %p%\n");
- //run_timer timer3("\nrtc %r sec, total cpu %c sec, utilization %p%\n", 3);
+ std::clock_t now;
+ do
+ {
+ now = std::clock();
+ } while ( now < timeout_in_clock_t );
+
+ boost::chrono::system_timer::duration sys_dur = sys.elapsed();
+ boost::chrono::monotonic_timer::duration mono_dur = mono.elapsed();
+ boost::chrono::high_resolution_timer::duration hires_dur = hires.elapsed();
+ boost::chrono::process_times times;
+ process.elapsed( times );
+
+ std::cout << std::endl;
+
+ ns timeout_in_nanoseconds( static_cast<long long>(timeout_in_secs) * 1000000000LL );
+ ns maximum_delta ( static_cast<long long>(timeout_in_nanoseconds.count() * 0.02 ) ); // 2% leeway
+
+ std::cout << timeout_in_nanoseconds.count() << " timeout_in_nanoseconds\n";
+ std::cout << maximum_delta.count() << " maximum_delta\n";
 
+ std::cout << sys_dur.count() << " sys_dur\n";
+
+ BOOST_CHECK( sys_dur > timeout_in_nanoseconds - maximum_delta
+ && sys_dur < timeout_in_nanoseconds + maximum_delta );
+
+ std::cout << mono_dur.count() << " mono_dur\n";
+
+ BOOST_CHECK( mono_dur > timeout_in_nanoseconds - maximum_delta
+ && mono_dur < timeout_in_nanoseconds + maximum_delta );
+
+ std::cout << hires_dur.count() << " hires_dur\n";
+
+ BOOST_CHECK( hires_dur > timeout_in_nanoseconds - maximum_delta
+ && hires_dur < timeout_in_nanoseconds + maximum_delta );
+
+ std::cout << times.real.count() << " times.real\n";
+
+ BOOST_CHECK( times.real > timeout_in_nanoseconds - maximum_delta
+ && times.real < timeout_in_nanoseconds + maximum_delta );
+
   //long count = 0;
   //times_t times;
   //times.real = 0;


Boost-Commit list run by bdawes at acm.org, david.abrahams at rcn.com, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk