Scheduler: Add scheduler timer latency test
g0dil [Fri, 15 May 2009 15:17:47 +0000 (15:17 +0000)]
git-svn-id: https://svn.berlios.de/svnroot/repos/senf/trunk@1207 270642c3-0616-0410-b53a-bc976706d245

Scheduler/FIFORunner.cc
Scheduler/TimerEvent.hh
Scheduler/TimerEvent.test.cc

index 2f37b91..b57c66b 100644 (file)
@@ -89,6 +89,8 @@ prefix_ void senf::scheduler::detail::FIFORunner::startWatchdog()
 
         watchdogRunning_ = true;
     }
+    else
+        stopWatchdog();
 }
 
 prefix_ void senf::scheduler::detail::FIFORunner::stopWatchdog()
index 148fdcc..d44cd50 100644 (file)
@@ -116,6 +116,7 @@ namespace scheduler {
                                              \param[in] initiallyEnabled if set \c false, do not
                                                  enable callback automatically. */
         ClockService::clock_type timeout() const;
+                                        ///< Get current/last timeout value
 
     private:
         virtual void v_run();
index e7b6141..ff410e9 100644 (file)
 // Custom includes
 #include "TimerEvent.hh"
 #include "Scheduler.hh"
+#include <boost/bind.hpp>
 
 #include "../Utils//auto_unit_test.hh"
 #include <boost/test/test_tools.hpp>
+#include <boost/random.hpp>
 
 #define prefix_
 ///////////////////////////////cc.p////////////////////////////////////////
@@ -83,6 +85,67 @@ BOOST_AUTO_UNIT_TEST(timerDispatcher)
     }
 }
 
+namespace {
+
+    senf::ClockService::clock_type randomDelay()
+    {
+        static boost::uniform_smallint<> random (100,300);
+        static boost::mt19937 generator;
+        return senf::scheduler::now() + senf::ClockService::milliseconds(random(generator));
+    }
+
+    unsigned count (0);
+    senf::ClockService::clock_type delay (0);
+
+    void jitterCb(senf::scheduler::TimerEvent & tm)
+    {
+        std::cerr << senf::scheduler::now() << ' ' << tm.timeout() << '\n';
+        count ++;
+        delay += senf::scheduler::now() - tm.timeout();
+        tm.timeout(randomDelay());
+    }
+
+    void logSchedulerTime()
+    {
+        std::cerr << senf::scheduler::now() << '\n';
+    }
+
+    void jitterTest()
+    {
+        count = 0;
+        delay = 0;
+        senf::scheduler::TimerEvent tm1 ("jitterTest::tm1", boost::bind(&jitterCb, boost::ref(tm1)),
+                                         randomDelay());
+        senf::scheduler::TimerEvent tm2 ("jitterTest::tm2", boost::bind(&jitterCb, boost::ref(tm2)),
+                                         randomDelay());
+        senf::scheduler::TimerEvent tm3 ("jitterTest::tm3", boost::bind(&jitterCb, boost::ref(tm3)),
+                                         randomDelay());
+        
+        senf::scheduler::TimerEvent timeout("jitterTest::timeout", &senf::scheduler::terminate,
+                                            senf::scheduler::now() + senf::ClockService::seconds(5));
+
+        senf::scheduler::EventHook timerCalled ("jitterTest::logSchedulerTime", &logSchedulerTime,
+                                                senf::scheduler::EventHook::PRE);
+
+        senf::scheduler::process();
+
+        std::cerr << "Average scheduling delay: " << delay/count << "\n";
+    }
+
+}
+
+BOOST_AUTO_UNIT_TEST(timerJitter)
+{
+    senf::scheduler::watchdogTimeout(0);
+    std::cerr << "Epoll timers\n";
+    senf::scheduler::loresTimers();
+    jitterTest();
+    std::cerr << "Hires timers\n";
+    senf::scheduler::hiresTimers();
+    jitterTest();
+    senf::scheduler::watchdogTimeout(1000);
+}
+
 ///////////////////////////////cc.e////////////////////////////////////////
 #undef prefix_