File indexing completed on 2024-04-06 12:13:14
0001
0002
0003
0004
0005
0006
0007
0008 #include <cassert>
0009 #include <iostream>
0010 #include <string>
0011 #include <unistd.h>
0012 #include <sys/resource.h>
0013 #include <cppunit/extensions/HelperMacros.h>
0014 #include "FWCore/Utilities/interface/CPUTimer.h"
0015
0016 class testCPUTimer : public CppUnit::TestFixture {
0017 CPPUNIT_TEST_SUITE(testCPUTimer);
0018
0019 CPPUNIT_TEST(testTiming);
0020
0021 CPPUNIT_TEST_SUITE_END();
0022
0023 public:
0024 void setUp() {}
0025 void tearDown() {}
0026
0027 void testTiming();
0028 };
0029
0030
0031 CPPUNIT_TEST_SUITE_REGISTRATION(testCPUTimer);
0032
0033 using std::cerr;
0034 using std::endl;
0035
0036 void testCPUTimer::testTiming()
0037
0038 {
0039 edm::CPUTimer timer;
0040 CPPUNIT_ASSERT(timer.realTime() == 0.0);
0041 CPPUNIT_ASSERT(timer.cpuTime() == 0.0);
0042
0043 timer.start();
0044 sleep(2);
0045 timer.stop();
0046
0047 if ((timer.realTime() <= 2.0) or (timer.cpuTime() + 2.0 - 0.02 > timer.realTime())) {
0048 std::cerr << "real " << timer.realTime() << " cpu " << timer.cpuTime() << std::endl;
0049 }
0050 CPPUNIT_ASSERT(timer.realTime() > 2.0);
0051 CPPUNIT_ASSERT(timer.cpuTime() + 2.0 - 0.02 <= timer.realTime());
0052
0053 timer.start();
0054 sleep(2);
0055 if (timer.realTime() <= 4.0) {
0056 std::cerr << "real " << timer.realTime() << " cpu " << timer.cpuTime() << std::endl;
0057 }
0058 CPPUNIT_ASSERT(timer.realTime() > 4.0);
0059
0060 timer.start();
0061 CPPUNIT_ASSERT(timer.realTime() > 4.0);
0062
0063 sleep(2);
0064
0065 timer.stop();
0066
0067 double real = timer.realTime();
0068 double cpu = timer.cpuTime();
0069
0070
0071 timer.stop();
0072 CPPUNIT_ASSERT(timer.realTime() == real);
0073 CPPUNIT_ASSERT(timer.cpuTime() == cpu);
0074
0075 timer.reset();
0076 CPPUNIT_ASSERT(timer.realTime() == 0.0);
0077 CPPUNIT_ASSERT(timer.cpuTime() == 0.0);
0078
0079 rusage theUsage;
0080 getrusage(RUSAGE_SELF, &theUsage);
0081 struct timeval startTime;
0082 startTime.tv_sec = theUsage.ru_utime.tv_sec;
0083 startTime.tv_usec = theUsage.ru_utime.tv_usec;
0084
0085 timer.start();
0086 struct timeval nowTime;
0087 do {
0088 rusage theUsage2;
0089 getrusage(RUSAGE_SELF, &theUsage2);
0090 nowTime.tv_sec = theUsage2.ru_utime.tv_sec;
0091 nowTime.tv_usec = theUsage2.ru_utime.tv_usec;
0092 } while (nowTime.tv_sec - startTime.tv_sec + 1E-6 * (nowTime.tv_usec - startTime.tv_usec) < 1);
0093 timer.stop();
0094
0095 if ((timer.realTime() < 1.0) or (timer.cpuTime() < 1.0)) {
0096 std::cerr << "real " << timer.realTime() << " cpu " << timer.cpuTime() << std::endl;
0097 }
0098 CPPUNIT_ASSERT(timer.realTime() >= 1.0);
0099 CPPUNIT_ASSERT(timer.cpuTime() >= 1.0);
0100 }