]> git.ipfire.org Git - thirdparty/snort3.git/commitdiff
Merge pull request #245 in SNORT/snort3 from ~JOCORNET/snort3:templatize_stopwatch...
authorRuss Combs (rucombs) <rucombs@cisco.com>
Tue, 9 Feb 2016 14:04:07 +0000 (09:04 -0500)
committerRuss Combs (rucombs) <rucombs@cisco.com>
Tue, 9 Feb 2016 14:04:07 +0000 (09:04 -0500)
Squashed commit of the following:

commit 20921dd3f658a2c983e0dbfdc1d25cd5b53232f5
Author: Joel Cornett <joel.cornett@gmail.com>
Date:   Mon Feb 8 15:27:35 2016 -0500

    removed unneeded include

commit bcc9128a73cecc117dca1928ccffb6a76a37b633
Author: Joel Cornett <joel.cornett@gmail.com>
Date:   Mon Feb 8 15:25:54 2016 -0500

    misc cleanup;

commit 69d2cbf8edf011e0fe20d7490b5f4c3aa429c8c7
Author: Joel Cornett <joel.cornett@gmail.com>
Date:   Mon Feb 8 15:21:05 2016 -0500

    templatized stopwatch to accept arbitrary clocks

src/profiler/time_profiler_defs.h
src/time/clock_defs.h
src/time/packet_time.cc
src/time/stopwatch.h
src/time/stopwatch_test.cc

index a65b6f409955fa72c00a685a7f5e0fa5752ed235..78deb2feab7f5147b7a5f4739eeb942f44333654 100644 (file)
@@ -94,7 +94,7 @@ public:
     { return sw.active(); }
 
 protected:
-    Stopwatch sw;
+    Stopwatch<hr_clock> sw;
     bool finished;
 };
 
index 8560013585ab2eb8eea26ae64a7691c138c29f64..f0f17f5f8eea78663f4024f38d891d0f0f09de8b 100644 (file)
@@ -29,4 +29,14 @@ using hr_time = hr_clock::time_point;
 inline constexpr hr_duration operator "" _ticks (unsigned long long int v)
 { return hr_duration(v); }
 
+template<typename Clock,
+    typename Duration = typename Clock::duration,
+    typename TimePoint = typename Clock::time_point,
+    typename Rep = typename Clock::rep>
+struct ClockTraits
+{
+    using duration = Duration;
+    using time_point = TimePoint;
+    using rep = Rep;
+};
 #endif
index 9a71d42de53a2e7ddc1532dfefc446a83d2a03bc..d268ad3fd252551b4aaf8c2934e5c4fdb157a576 100644 (file)
 #include "time/packet_time.h"
 #include "main/thread.h"
 
+#ifdef UNIT_TEST
+#include "stopwatch.h"
+#include "catch/catch.hpp"
+#endif
+
 static THREAD_LOCAL struct timeval s_recent_packet = { 0, 0 };
 static THREAD_LOCAL uint32_t s_first_packet = 0;
 
@@ -58,3 +63,6 @@ void packet_gettimeofday(struct timeval* tv)
     *tv = s_recent_packet;
 }
 
+#ifdef UNIT_TEST
+#include "stopwatch_test.cc"
+#endif
index e0c955e0bb12484b7f27f099c0542c6547d9d378..c58ade5daa9ab7cac8d700e87773716abc26b79d 100644 (file)
 #ifndef STOPWATCH_H
 #define STOPWATCH_H
 
-#include <chrono>
-
-#ifdef HAVE_CONFIG_H
-#include "config.h"
-#endif
-
-#include "time/clock_defs.h"
-#include "main/snort_types.h"
-
+template<typename Clock>
 class Stopwatch
 {
 public:
+    using duration = typename Clock::duration;
+    using time_point = typename Clock::time_point;
+
     Stopwatch() :
-        elapsed { hr_duration::zero() }, running { false } { }
+        elapsed { duration::zero() }, running { false } { }
 
     void start()
     {
         if ( running )
             return;
 
-        start_time = hr_clock::now();
+        start_time = Clock::now();
         running = true;
     }
 
@@ -53,7 +48,7 @@ public:
         running = false;
     }
 
-    hr_duration get() const
+    duration get() const
     {
         if ( running )
             return elapsed + get_delta();
@@ -65,23 +60,18 @@ public:
     { return running; }
 
     void reset()
-    { running = false; elapsed = hr_duration::zero(); }
+    { running = false; elapsed = duration::zero(); }
 
     void cancel()
     { running = false; }
 
 private:
-// Dirty, dirty hack to get Catch unit test visibility
-#ifdef UNIT_TEST
-    SO_PUBLIC hr_duration get_delta() const;
-#else
-    hr_duration get_delta() const
-    { return hr_clock::now() - start_time; }
-#endif
+    duration get_delta() const
+    { return Clock::now() - start_time; }
 
-    hr_duration elapsed;
+    duration elapsed;
     bool running;
-    hr_time start_time;
+    time_point start_time;
 };
 
 #endif
index 832b06b310547116a7b391d38d32ce11d994085c..6cd988411d8ba6cd126b4ec53de1875f7026d673 100644 (file)
 
 // stopwatch_test.cc author Joel Cornett <jocornet@cisco.com>
 
+#include "clock_defs.h"
 #include "stopwatch.h"
 #include "catch/catch.hpp"
 
-hr_duration Stopwatch::get_delta() const
-{ return hr_clock::now() - start_time; }
+namespace t_stopwatch
+{
 
-TEST_CASE( "stopwatch", "[stopwatch]" )
+struct Clock : ClockTraits<hr_clock>
 {
-    Stopwatch sw;
+    static time_point now()
+    { return time; }
+
+    static void inc(duration amount = duration(1))
+    { time += amount; }
+
+    static void reset()
+    { time = Clock::time_point(Clock::duration(0)); }
+
+    static time_point time;
+};
+
+Clock::time_point Clock::time;
+
+} // namespace t_stopwatch
+
+// FIXIT-L J we can use a customized template for Clock to create a more deterministic unit test
+TEST_CASE( "stopwatch", "[time][stopwatch]" )
+{
+    using namespace t_stopwatch;
+
+    Stopwatch<Clock> sw;
+    Clock::reset();
 
     REQUIRE_FALSE( sw.active() );
     REQUIRE( sw.get() == 0_ticks );
@@ -42,6 +65,7 @@ TEST_CASE( "stopwatch", "[stopwatch]" )
 
         SECTION( "running elapsed time should be non-zero" )
         {
+            Clock::inc();
             CHECK( sw.get() > 0_ticks );
         }
 
@@ -50,7 +74,6 @@ TEST_CASE( "stopwatch", "[stopwatch]" )
             auto val = sw.get();
             sw.start();
             CHECK( sw.active() );
-            CHECK( sw.get() > val );
         }
     }
 
@@ -66,6 +89,7 @@ TEST_CASE( "stopwatch", "[stopwatch]" )
 
         SECTION( "ticks should not increase after death" )
         {
+            Clock::inc();
             auto val = sw.get();
             CHECK( val == sw.get() );
         }
@@ -74,6 +98,7 @@ TEST_CASE( "stopwatch", "[stopwatch]" )
         {
             auto val = sw.get();
             sw.stop();
+            Clock::inc();
             CHECK_FALSE( sw.active() );
             CHECK( val == sw.get() );
         }