Salome HOME
the tests execution time is measured
[modules/hydro.git] / src / HYDRO_tests / TestLib_Listener.cxx
index e2586dcea120ab716ee2aeba88ce92a78fc345d9..593c8d6e29b3404c4531c09bbf312c39fb9692e8 100644 (file)
 #include <cppunit/Test.h>
 #include <iostream>
 
+#ifdef WIN32
+  #include <windows.h>
+#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;
 }
 
 /**