diff --git a/common/performancetimer.cpp b/common/performancetimer.cpp index 0ac33c9f..400a55c8 100644 --- a/common/performancetimer.cpp +++ b/common/performancetimer.cpp @@ -51,10 +51,12 @@ void PerformanceTimer::stop() m_stop = std::chrono::steady_clock::now(); } -void PerformanceTimer::inc(uint64_t count) +std::string PerformanceTimer::inc(uint64_t count) { SWSS_LOG_ENTER(); + std::string output = ""; + m_calls += 1; m_tasks += count; @@ -68,7 +70,7 @@ void PerformanceTimer::inc(uint64_t count) if (count == 0) { m_gaps.pop_back(); m_calls -= 1; - return; + return output; } if (m_incs.size() <= LIMIT) { @@ -84,16 +86,19 @@ void PerformanceTimer::inc(uint64_t count) if (m_enable && mseconds > 0) { + output = getTimerState(); std::ifstream indicator(INDICATOR); if (indicator.good()) { - SWSS_LOG_NOTICE("%s", getTimerState().c_str()); + SWSS_LOG_NOTICE("%s", output.c_str()); } else { - SWSS_LOG_INFO("%s", getTimerState().c_str()); + SWSS_LOG_INFO("%s", output.c_str()); } } reset(); } + + return output; } std::string PerformanceTimer::getTimerState() diff --git a/common/performancetimer.h b/common/performancetimer.h index 46a06bfb..545aeeae 100644 --- a/common/performancetimer.h +++ b/common/performancetimer.h @@ -27,7 +27,7 @@ namespace swss void stop(); - void inc(uint64_t count = 1); + std::string inc(uint64_t count = 1); void reset(); diff --git a/tests/Makefile.am b/tests/Makefile.am index a07fadc2..aece12cc 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -42,6 +42,7 @@ tests_tests_SOURCES = tests/redis_ut.cpp \ tests/binary_serializer_ut.cpp \ tests/zmq_state_ut.cpp \ tests/profileprovider_ut.cpp \ + tests/performancetimer_ut.cpp \ tests/main.cpp tests_tests_CFLAGS = $(DBGFLAGS) $(AM_CFLAGS) $(CFLAGS_COMMON) $(CFLAGS_GTEST) $(LIBNL_CFLAGS) diff --git a/tests/performancetimer_ut.cpp b/tests/performancetimer_ut.cpp new file mode 100644 index 00000000..4bdaf74e --- /dev/null +++ b/tests/performancetimer_ut.cpp @@ -0,0 +1,43 @@ +#include "common/performancetimer.h" +#include +#include "gtest/gtest.h" +#include + +using namespace std; + +#define PRINT_ALL 1 + +TEST(PerformancetimerTest, basic) +{ + std::string expected; + + static swss::PerformanceTimer timer("basic", PRINT_ALL); + timer.start(); + this_thread::sleep_for(chrono::milliseconds(100)); + timer.stop(); + std::string output = timer.inc(1000); + + expected = R"({"API":"basic","RPS[k]":10.0,"Tasks":1000,"Total[ms]":100,"busy[ms]":100,"idle[ms]":0})"; + EXPECT_EQ(output, expected); + + timer.setTimerName("basic_set_name"); + timer.setTimerVerbose(true); + timer.setTimerThreshold(3000); + + timer.start(); + this_thread::sleep_for(chrono::milliseconds(100)); + timer.stop(); + output = timer.inc(1000); + EXPECT_EQ(output, ""); + + this_thread::sleep_for(chrono::milliseconds(200)); + + timer.start(); + this_thread::sleep_for(chrono::milliseconds(300)); + timer.stop(); + output = timer.inc(2000); + + expected = R"({"API":"basic_set_name","RPS[k]":5.0,"Tasks":3000,"Total[ms]":600,"busy[ms]":400,"idle[ms]":200,"m_gaps":[0,200],"m_incs":[1000,2000],"m_intervals":[100,300]})"; + + EXPECT_EQ(output, expected); +}