1 #include <libgeodecomp/config.h> 2 #ifdef LIBGEODECOMP_WITH_HPX 3 #include <hpx/config.hpp> 4 #endif 5 6 #include <libgeodecomp/misc/chronometer.h> 7 #include <libgeodecomp/misc/stringops.h> 8 9 #include <cxxtest/TestSuite.h> 10 11 using namespace LibGeoDecomp; 12 13 namespace LibGeoDecomp { 14 15 class ChronometerTest : public CxxTest::TestSuite 16 { 17 public: setUp()18 void setUp() 19 { 20 c = new Chronometer(); 21 } 22 tearDown()23 void tearDown() 24 { 25 delete c; 26 } 27 testWorkRatio1()28 void testWorkRatio1() 29 { 30 { 31 TimeTotal t(c); 32 { 33 TimeCompute t(c); 34 ScopedTimer::busyWait(100000); 35 } 36 37 ScopedTimer::busyWait(200000); 38 } 39 40 double ratio = c->ratio<TimeCompute, TimeTotal>(); 41 TS_ASSERT_DELTA(1.0/3.0, ratio, 0.1); 42 } 43 testWorkRatio2()44 void testWorkRatio2() 45 { 46 { 47 TimeTotal t(c); 48 { 49 TimeComputeGhost t(c); 50 ScopedTimer::busyWait(100000); 51 } 52 53 ScopedTimer::busyWait(300000); 54 { 55 TimeComputeInner t(c); 56 ScopedTimer::busyWait(200000); 57 } 58 ScopedTimer::busyWait(300000); 59 } 60 61 double ratio; 62 63 ratio = c->ratio<TimeCompute, TimeTotal>(); 64 TS_ASSERT_DELTA(1.0/3.0, ratio, 0.05); 65 66 ratio = c->ratio<TimeComputeGhost, TimeTotal>(); 67 TS_ASSERT_DELTA(1.0/9.0, ratio, 0.05); 68 69 ratio = c->ratio<TimeComputeInner, TimeTotal>(); 70 TS_ASSERT_DELTA(2.0/9.0, ratio, 0.05); 71 } 72 testAddition()73 void testAddition() 74 { 75 Chronometer c1; 76 Chronometer c2; 77 Chronometer c3; 78 79 { 80 TimePatchAccepters t1(&c1); 81 TimePatchProviders t2(&c2); 82 83 { 84 TimeCompute t(&c1); 85 ScopedTimer::busyWait(15000); 86 } 87 { 88 TimeCompute t(&c2); 89 ScopedTimer::busyWait(15000); 90 } 91 } 92 93 c3 = c1 + c2; 94 TS_ASSERT_EQUALS(c3.interval<TimeCompute>(), 95 c1.interval<TimeCompute>() + 96 c2.interval<TimeCompute>()); 97 98 TS_ASSERT_EQUALS(c3.interval<TimePatchAccepters>(), 99 c1.interval<TimePatchAccepters>()); 100 101 TS_ASSERT_EQUALS(c3.interval<TimePatchProviders>(), 102 c2.interval<TimePatchProviders>()); 103 } 104 testReport()105 void testReport() 106 { 107 { 108 TimeTotal t(c); 109 { 110 TimeCompute t(c); 111 ScopedTimer::busyWait(15000); 112 } 113 114 ScopedTimer::busyWait(30000); 115 { 116 TimeCompute t(c); 117 ScopedTimer::busyWait(15000); 118 } 119 ScopedTimer::busyWait(30000); 120 } 121 122 std::vector<std::string> lines = StringOps::tokenize(c->report(), "\n"); 123 TS_ASSERT_EQUALS(lines.size() , Chronometer::NUM_INTERVALS); 124 TS_ASSERT_LESS_THAN_EQUALS(std::size_t(6), Chronometer::NUM_INTERVALS); 125 126 for (std::size_t i = 0; i < Chronometer::NUM_INTERVALS; ++i) { 127 std::vector<std::string> tokens = StringOps::tokenize(lines[i], ":"); 128 TS_ASSERT_EQUALS(std::size_t(2), tokens.size()); 129 TS_ASSERT_LESS_THAN_EQUALS(0, StringOps::atof(tokens[1])); 130 } 131 } 132 testAddTime()133 void testAddTime() 134 { 135 TS_ASSERT_EQUALS(0, c->interval<TimeCompute>()); 136 c->addTime<TimeCompute>(5); 137 TS_ASSERT_EQUALS(5, c->interval<TimeCompute>()); 138 c->addTime<TimeCompute>(4); 139 TS_ASSERT_EQUALS(9, c->interval<TimeCompute>()); 140 } 141 testTock()142 void testTock() 143 { 144 double t = ScopedTimer::time(); 145 ScopedTimer::busyWait(15000); 146 c->tock<TimeComputeInner>(t); 147 TS_ASSERT_LESS_THAN_EQUALS(0.015, c->interval<TimeComputeInner>()); 148 } 149 150 private: 151 Chronometer *c; 152 }; 153 154 } 155