From: Russ Combs (rucombs) Date: Tue, 9 Feb 2016 14:04:07 +0000 (-0500) Subject: Merge pull request #245 in SNORT/snort3 from ~JOCORNET/snort3:templatize_stopwatch... X-Git-Tag: 3.0.0-233~625 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=28ec4bc896275a44d2bb91249047a0058090cd23;p=thirdparty%2Fsnort3.git Merge pull request #245 in SNORT/snort3 from ~JOCORNET/snort3:templatize_stopwatch to master Squashed commit of the following: commit 20921dd3f658a2c983e0dbfdc1d25cd5b53232f5 Author: Joel Cornett Date: Mon Feb 8 15:27:35 2016 -0500 removed unneeded include commit bcc9128a73cecc117dca1928ccffb6a76a37b633 Author: Joel Cornett Date: Mon Feb 8 15:25:54 2016 -0500 misc cleanup; commit 69d2cbf8edf011e0fe20d7490b5f4c3aa429c8c7 Author: Joel Cornett Date: Mon Feb 8 15:21:05 2016 -0500 templatized stopwatch to accept arbitrary clocks --- diff --git a/src/profiler/time_profiler_defs.h b/src/profiler/time_profiler_defs.h index a65b6f409..78deb2fea 100644 --- a/src/profiler/time_profiler_defs.h +++ b/src/profiler/time_profiler_defs.h @@ -94,7 +94,7 @@ public: { return sw.active(); } protected: - Stopwatch sw; + Stopwatch sw; bool finished; }; diff --git a/src/time/clock_defs.h b/src/time/clock_defs.h index 856001358..f0f17f5f8 100644 --- a/src/time/clock_defs.h +++ b/src/time/clock_defs.h @@ -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 +struct ClockTraits +{ + using duration = Duration; + using time_point = TimePoint; + using rep = Rep; +}; #endif diff --git a/src/time/packet_time.cc b/src/time/packet_time.cc index 9a71d42de..d268ad3fd 100644 --- a/src/time/packet_time.cc +++ b/src/time/packet_time.cc @@ -32,6 +32,11 @@ #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 diff --git a/src/time/stopwatch.h b/src/time/stopwatch.h index e0c955e0b..c58ade5da 100644 --- a/src/time/stopwatch.h +++ b/src/time/stopwatch.h @@ -20,27 +20,22 @@ #ifndef STOPWATCH_H #define STOPWATCH_H -#include - -#ifdef HAVE_CONFIG_H -#include "config.h" -#endif - -#include "time/clock_defs.h" -#include "main/snort_types.h" - +template 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 diff --git a/src/time/stopwatch_test.cc b/src/time/stopwatch_test.cc index 832b06b31..6cd988411 100644 --- a/src/time/stopwatch_test.cc +++ b/src/time/stopwatch_test.cc @@ -18,15 +18,38 @@ // stopwatch_test.cc author Joel Cornett +#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 { - 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 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() ); }