diff --git a/src/core/system-wall-clock-ms.h b/src/core/system-wall-clock-ms.h index 0c512d3ad..9cca3ca38 100644 --- a/src/core/system-wall-clock-ms.h +++ b/src/core/system-wall-clock-ms.h @@ -25,6 +25,9 @@ namespace ns3 { /** * \brief measure wall-clock time in milliseconds + + * \todo This class exists also in non-unix systems but is not + * implemented and always return zero as measurd time. */ class SystemWallClockMs { public: @@ -36,13 +39,31 @@ public: */ void Start (void); /** - * \returns the measured elapsed wall clock time since - * ns3::SystemWallClockMs::start was invoked. + * \brief Stop measuring the time since Start() was called. + * \returns the measured elapsed wall clock time (in milliseconds) since + * ns3::SystemWallClockMs::Start was invoked. * - * It is possible to start a new measurement with ns3::SystemWallClockMs::start + * It is possible to start a new measurement with ns3::SystemWallClockMs::Start * after this method returns. */ unsigned long long End (void); + + /** + * \returns the measured elapsed wall clock time (in milliseconds) since + * ns3::SystemWallClockMs::Start was invoked. + */ + double GetElapsedReal (void) const; + /** + * \returns the measured elapsed 'user' wall clock time (in milliseconds) since + * ns3::SystemWallClockMs::Start was invoked. + */ + double GetElapsedUser (void) const; + /** + * \returns the measured elapsed 'system' wall clock time (in milliseconds) since + * ns3::SystemWallClockMs::Start was invoked. + */ + double GetElapsedSystem (void) const; + private: class SystemWallClockMsPrivate *m_priv; }; diff --git a/src/core/test.cc b/src/core/test.cc index 57d285ad0..3b3a2db24 100644 --- a/src/core/test.cc +++ b/src/core/test.cc @@ -262,7 +262,7 @@ TestCase::ContinueOnFailure (void) void TestCase::DoReportStart (void) { - m_startTime = times (&m_startTimes); + m_clock.Start (); if (m_ofs == 0) { @@ -319,26 +319,18 @@ TestCase::DoReportTestFailure ( void TestCase::DoReportEnd (void) { - static long ticksPerSecond = sysconf (_SC_CLK_TCK); - + m_clock.End (); if (m_ofs == 0) { return; } - struct tms endTimes; - clock_t endTime = times (&endTimes); - - clock_t elapsed = endTime - m_startTime; - clock_t elapsedUsr = endTimes.tms_utime - m_startTimes.tms_utime; - clock_t elapsedSys = endTimes.tms_stime - m_startTimes.tms_stime; - (*m_ofs).precision (2); *m_ofs << std::fixed; - *m_ofs << " " << "real " << static_cast (elapsed) / ticksPerSecond - << " user " << static_cast (elapsedUsr) / ticksPerSecond - << " system " << static_cast (elapsedSys) / ticksPerSecond + *m_ofs << " " << "real " << m_clock.GetElapsedReal () * 1e-3 + << " user " << m_clock.GetElapsedUser () * 1e-3 + << " system " << m_clock.GetElapsedSystem () * 1e-3 << "" << std::endl; *m_ofs << " " << std::endl; @@ -523,8 +515,8 @@ TestSuite::ContinueOnFailure (void) void TestSuite::DoReportStart (void) { - m_startTime = times (&m_startTimes); - + m_clock.Start (); + if (m_ofs == 0) { return; @@ -556,25 +548,19 @@ TestSuite::DoReportSuccess (void) void TestSuite::DoReportEnd (void) { - static long ticksPerSecond = sysconf (_SC_CLK_TCK); - + m_clock.End (); + if (m_ofs == 0) { return; } - struct tms endTimes; - clock_t endTime = times (&endTimes); - - clock_t elapsed = endTime - m_startTime; - clock_t elapsedUsr = endTimes.tms_utime - m_startTimes.tms_utime; - clock_t elapsedSys = endTimes.tms_stime - m_startTimes.tms_stime; (*m_ofs).precision (2); *m_ofs << std::fixed; - *m_ofs << " " << "real " << static_cast (elapsed) / ticksPerSecond - << " user " << static_cast (elapsedUsr) / ticksPerSecond - << " system " << static_cast (elapsedSys) / ticksPerSecond + *m_ofs << " " << "real " << m_clock.GetElapsedReal () * 1e-3 + << " user " << m_clock.GetElapsedUser () * 1e-3 + << " system " << m_clock.GetElapsedSystem () * 1e-3 << "" << std::endl; *m_ofs << "" << std::endl; diff --git a/src/core/test.h b/src/core/test.h index 58db025f9..1a6e77950 100644 --- a/src/core/test.h +++ b/src/core/test.h @@ -27,7 +27,10 @@ #include #include #include -#include + +#include "ns3/system-wall-clock-ms.h" + + // // Note on below macros: // @@ -821,6 +824,7 @@ private: TestCase (TestCase& tc); TestCase& operator= (TestCase& tc); + SystemWallClockMs m_clock; std::string m_name; bool m_verbose; bool m_continueOnFailure; @@ -828,8 +832,6 @@ private: std::string m_basedir; std::ofstream *m_ofs; bool m_error; - clock_t m_startTime; - struct tms m_startTimes; }; /** @@ -1057,6 +1059,7 @@ private: TestSuite (TestSuite& ts); TestSuite& operator= (TestSuite& ts); + SystemWallClockMs m_clock; std::string m_name; bool m_verbose; bool m_continueOnFailure; @@ -1064,10 +1067,7 @@ private: std::ofstream *m_ofs; bool m_error; TestType m_type; - - clock_t m_startTime; - struct tms m_startTimes; - + typedef std::vector TestCaseVector_t; TestCaseVector_t m_tests; }; diff --git a/src/core/unix-system-wall-clock-ms.cc b/src/core/unix-system-wall-clock-ms.cc index 14f1fbb5b..cfe53932f 100644 --- a/src/core/unix-system-wall-clock-ms.cc +++ b/src/core/unix-system-wall-clock-ms.cc @@ -19,7 +19,9 @@ */ #include "system-wall-clock-ms.h" -#include +#include +#include +#include namespace ns3 { @@ -27,28 +29,57 @@ class SystemWallClockMsPrivate { public: void Start (void); unsigned long long End (void); + double GetElapsedReal (void) const; + double GetElapsedUser (void) const; + double GetElapsedSystem (void) const; + private: - struct timeval m_startTv; - struct timeval m_endTv; + struct tms m_startTimes; + clock_t m_startTime; + double m_elapsedReal; + double m_elapsedUser; + double m_elapsedSystem; }; void SystemWallClockMsPrivate::Start (void) { - struct timezone tz; - gettimeofday (&m_startTv, &tz); + m_startTime = times (&m_startTimes); } unsigned long long SystemWallClockMsPrivate::End (void) { - struct timezone tz; - gettimeofday (&m_endTv, &tz); - unsigned long long end = m_endTv.tv_sec *1000 + m_endTv.tv_usec / 1000; - unsigned long long start = m_startTv.tv_sec *1000 + m_startTv.tv_usec / 1000; - return end - start; + static long ticksPerSecond = sysconf (_SC_CLK_TCK); + + struct tms endTimes; + clock_t endTime = times (&endTimes); + + m_elapsedReal = 1e3 * static_cast (endTime - m_startTime) / ticksPerSecond; + m_elapsedUser = 1e3 * static_cast (endTimes.tms_utime - m_startTimes.tms_utime) / ticksPerSecond; + m_elapsedSystem = 1e3 * static_cast (endTimes.tms_stime - m_startTimes.tms_stime) / ticksPerSecond; + + return m_elapsedReal; } +double +SystemWallClockMsPrivate::GetElapsedReal (void) const +{ + return m_elapsedReal; +} + +double +SystemWallClockMsPrivate::GetElapsedUser (void) const +{ + return m_elapsedUser; +} + +double +SystemWallClockMsPrivate::GetElapsedSystem (void) const +{ + return m_elapsedSystem; +} + SystemWallClockMs::SystemWallClockMs () : m_priv (new SystemWallClockMsPrivate ()) {} @@ -70,4 +101,22 @@ SystemWallClockMs::End (void) return m_priv->End (); } +double +SystemWallClockMs::GetElapsedReal (void) const +{ + return m_priv->GetElapsedReal (); +} + +double +SystemWallClockMs::GetElapsedUser (void) const +{ + return m_priv->GetElapsedUser (); +} + +double +SystemWallClockMs::GetElapsedSystem (void) const +{ + return m_priv->GetElapsedSystem (); +} + }; // namespace ns3 diff --git a/src/core/win32-system-wall-clock-ms.cc b/src/core/win32-system-wall-clock-ms.cc index 1cc9fc2c5..c02f0f268 100644 --- a/src/core/win32-system-wall-clock-ms.cc +++ b/src/core/win32-system-wall-clock-ms.cc @@ -61,4 +61,22 @@ SystemWallClockMs::End (void) return m_priv->End (); } +double +SystemWallClockMs::GetElapsedReal (void) const +{ + return 0; +} + +double +SystemWallClockMs::GetElapsedUser (void) const +{ + return 0; +} + +double +SystemWallClockMs::GetElapsedSystem (void) const +{ + return 0; +} + }; // namespace ns3