Skip to content

Commit

Permalink
add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
a114j0y committed Jul 29, 2024
1 parent 2eb4fe1 commit f9bc905
Show file tree
Hide file tree
Showing 7 changed files with 136 additions and 5 deletions.
Binary file added .DS_Store
Binary file not shown.
19 changes: 19 additions & 0 deletions .vscode/c_cpp_properties.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"configurations": [
{
"name": "Mac",
"includePath": [
"${workspaceFolder}/**"
],
"defines": [],
"macFrameworkPath": [
"/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/System/Library/Frameworks"
],
"compilerPath": "/usr/bin/clang",
"cStandard": "c17",
"cppStandard": "c++17",
"intelliSenseMode": "macos-clang-x64"
}
],
"version": 4
}
63 changes: 63 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
{
"files.associations": {
"__bit_reference": "cpp",
"__config": "cpp",
"__hash_table": "cpp",
"__locale": "cpp",
"__node_handle": "cpp",
"__split_buffer": "cpp",
"__threading_support": "cpp",
"__tree": "cpp",
"__verbose_abort": "cpp",
"array": "cpp",
"bitset": "cpp",
"cctype": "cpp",
"charconv": "cpp",
"clocale": "cpp",
"cmath": "cpp",
"complex": "cpp",
"condition_variable": "cpp",
"cstdarg": "cpp",
"cstddef": "cpp",
"cstdint": "cpp",
"cstdio": "cpp",
"cstdlib": "cpp",
"cstring": "cpp",
"ctime": "cpp",
"cwchar": "cpp",
"cwctype": "cpp",
"deque": "cpp",
"execution": "cpp",
"fstream": "cpp",
"future": "cpp",
"initializer_list": "cpp",
"iomanip": "cpp",
"ios": "cpp",
"iosfwd": "cpp",
"iostream": "cpp",
"istream": "cpp",
"limits": "cpp",
"list": "cpp",
"locale": "cpp",
"map": "cpp",
"mutex": "cpp",
"new": "cpp",
"optional": "cpp",
"ostream": "cpp",
"queue": "cpp",
"ratio": "cpp",
"regex": "cpp",
"set": "cpp",
"sstream": "cpp",
"stdexcept": "cpp",
"streambuf": "cpp",
"string": "cpp",
"string_view": "cpp",
"tuple": "cpp",
"typeinfo": "cpp",
"unordered_map": "cpp",
"variant": "cpp",
"vector": "cpp",
"algorithm": "cpp"
}
}
13 changes: 9 additions & 4 deletions common/performancetimer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,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 @@ -66,7 +68,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 @@ -82,16 +84,19 @@ void PerformanceTimer::inc(uint64_t count)

if (m_enable && mseconds > 0)
{
output = getTimerState();
std::ifstream indicator("/var/log/syslog_notice_flag");
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 f9bc905

Please sign in to comment.