Skip to content

Commit

Permalink
add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
a114j0y committed Sep 23, 2024
1 parent 1772ba3 commit f7b0a1d
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 5 deletions.
13 changes: 9 additions & 4 deletions common/performancetimer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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) {
Expand All @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion common/performancetimer.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ namespace swss

void stop();

void inc(uint64_t count = 1);
std::string inc(uint64_t count = 1);

void reset();

Expand Down
1 change: 1 addition & 0 deletions tests/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
43 changes: 43 additions & 0 deletions tests/performancetimer_ut.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#include "common/performancetimer.h"
#include <nlohmann/json.hpp>
#include "gtest/gtest.h"
#include <thread>

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);
}

0 comments on commit f7b0a1d

Please sign in to comment.