From 98fde663cb36c97eec9f7972709d43b860973f48 Mon Sep 17 00:00:00 2001 From: asl Date: Thu, 7 Sep 2017 07:53:47 +0300 Subject: [PATCH] the tests execution time is measured --- src/HYDRO_tests/TestLib_Listener.cxx | 63 ++++++++++++++++++++++++- src/HYDRO_tests/TestLib_Listener.h | 9 +++- src/HYDRO_tests/test_HYDROData_Main.cxx | 4 ++ 3 files changed, 74 insertions(+), 2 deletions(-) diff --git a/src/HYDRO_tests/TestLib_Listener.cxx b/src/HYDRO_tests/TestLib_Listener.cxx index e2586dce..593c8d6e 100644 --- a/src/HYDRO_tests/TestLib_Listener.cxx +++ b/src/HYDRO_tests/TestLib_Listener.cxx @@ -20,10 +20,56 @@ #include #include +#ifdef WIN32 + #include +#endif + +#ifdef WIN32 +/** + Convert the given time structure to milliseconds number + @param theTime the time structure to convert + @return the calculated number of milliseconds +*/ +INT64 GetMilliseconds( const FILETIME& theTime ) +{ + SYSTEMTIME aTime; + FileTimeToSystemTime( &theTime, &aTime ); + INT64 aRes = 0; + aRes += aTime.wHour * ( 3600 * 1000 ); + aRes += aTime.wMinute * ( 60 * 1000 ); + aRes += aTime.wSecond * ( 1000 ); + aRes += aTime.wMilliseconds; + return aRes; +} +#endif + +/** + Calculate the complete execution time for the current thread + @return the calculated time as the number of milliseconds +*/ +INT64 GetTimeForCurrentThreadInMs() +{ +#ifdef WIN32 + FILETIME aCreationTime, anExitTime, aKernelTime, aUserTime; + GetThreadTimes( GetCurrentThread(), &aCreationTime, &anExitTime, &aKernelTime, &aUserTime ); + INT64 aKernelMs = GetMilliseconds( aKernelTime ); + INT64 aUserMs = GetMilliseconds( aUserTime ); + return aKernelMs + aUserMs; +#else + struct timespec aTime; + clock_gettime( CLOCK_MONOTONIC, &aTime ); + INT64 aSec = aTime.tv_sec; + aSec *= 1000; + aSec += aTime.tv_nsec / 1000000; + return aSec; +#endif +} + /** \brief Constructor */ TestLib_Listener::TestLib_Listener() +: myStart( 0 ), myComplete( 0 ) { } @@ -39,6 +85,17 @@ TestLib_Listener::~TestLib_Listener() */ void TestLib_Listener::Clear() { + myStart = 0; + myComplete = 0; +} + +/** + Get complete time of all tests execution in milliseconds + @return complete time in milliseconds +*/ +INT64 TestLib_Listener::GetCompleteTimeInMS() const +{ + return myComplete; } /** @@ -52,6 +109,7 @@ void TestLib_Listener::startTest( CppUnit::Test* theTest ) aName = theTest->getName(); std::cout << aName << "..."; + myStart = GetTimeForCurrentThreadInMs(); } /** @@ -60,7 +118,10 @@ void TestLib_Listener::startTest( CppUnit::Test* theTest ) */ void TestLib_Listener::endTest( CppUnit::Test* theTest ) { - std::cout << std::endl; + INT64 aCurTime = GetTimeForCurrentThreadInMs(); + INT64 anExecTimeMS = aCurTime - myStart; + myComplete += anExecTimeMS; + std::cout << " " << anExecTimeMS << " ms" << std::endl; } /** diff --git a/src/HYDRO_tests/TestLib_Listener.h b/src/HYDRO_tests/TestLib_Listener.h index b8e125d1..8953a29d 100644 --- a/src/HYDRO_tests/TestLib_Listener.h +++ b/src/HYDRO_tests/TestLib_Listener.h @@ -20,6 +20,8 @@ #include +typedef long long INT64; ///< the cross-platform type definition for 64-bits integer + /** \class TestLib_Listener \brief Implementation of the custom listener printing the time of test execution @@ -31,7 +33,8 @@ public: virtual ~TestLib_Listener(); void Clear(); - + INT64 GetCompleteTimeInMS() const; + virtual void startTest( CppUnit::Test* ); virtual void endTest( CppUnit::Test* ); @@ -39,5 +42,9 @@ public: virtual void endSuite( CppUnit::Test* ); virtual void addFailure( const CppUnit::TestFailure& ); + +private: + INT64 myStart; ///< start time in milliseconds + INT64 myComplete; ///< complete time of all tests execution in milliseconds }; diff --git a/src/HYDRO_tests/test_HYDROData_Main.cxx b/src/HYDRO_tests/test_HYDROData_Main.cxx index 05f6b9b8..fe430bfd 100644 --- a/src/HYDRO_tests/test_HYDROData_Main.cxx +++ b/src/HYDRO_tests/test_HYDROData_Main.cxx @@ -95,5 +95,9 @@ int main( int argc, char* argv[] ) aSession.closeSession(); anApp.exit(!isOK); DEBTRACE("--- TODO: exception on exit..."); // TODO: exception on exit... + + int ms = progress.GetCompleteTimeInMS(); + printf( "COMPLETE TESTS TIME: %i ms\n", ms ); + return result.wasSuccessful() ? 0 : 1; } -- 2.39.2