Skip to content

Commit dc286be

Browse files
committed
add unit tests
1 parent c1af52b commit dc286be

File tree

4 files changed

+54
-5
lines changed

4 files changed

+54
-5
lines changed

common/performancetimer.cpp

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,12 @@ void PerformanceTimer::stop()
4949
m_stop = std::chrono::steady_clock::now();
5050
}
5151

52-
void PerformanceTimer::inc(uint64_t count)
52+
std::string PerformanceTimer::inc(uint64_t count)
5353
{
5454
SWSS_LOG_ENTER();
5555

56+
std::string output = "";
57+
5658
m_calls += 1;
5759

5860
m_tasks += count;
@@ -66,7 +68,7 @@ void PerformanceTimer::inc(uint64_t count)
6668
if (count == 0) {
6769
m_gaps.pop_back();
6870
m_calls -= 1;
69-
return;
71+
return output;
7072
}
7173

7274
if (m_incs.size() <= LIMIT) {
@@ -82,16 +84,19 @@ void PerformanceTimer::inc(uint64_t count)
8284

8385
if (m_enable && mseconds > 0)
8486
{
87+
output = getTimerState();
8588
std::ifstream indicator("/var/log/syslog_notice_flag");
8689
if (indicator.good()) {
87-
SWSS_LOG_NOTICE("%s", getTimerState().c_str());
90+
SWSS_LOG_NOTICE("%s", output.c_str());
8891
} else {
89-
SWSS_LOG_INFO("%s", getTimerState().c_str());
92+
SWSS_LOG_INFO("%s", output.c_str());
9093
}
9194
}
9295

9396
reset();
9497
}
98+
99+
return output;
95100
}
96101

97102
std::string PerformanceTimer::getTimerState()

common/performancetimer.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ namespace swss
2727

2828
void stop();
2929

30-
void inc(uint64_t count = 1);
30+
std::string inc(uint64_t count = 1);
3131

3232
void reset();
3333

tests/Makefile.am

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ tests_tests_SOURCES = tests/redis_ut.cpp \
4242
tests/binary_serializer_ut.cpp \
4343
tests/zmq_state_ut.cpp \
4444
tests/profileprovider_ut.cpp \
45+
tests/performancetimer_ut.cpp \
4546
tests/main.cpp
4647

4748
tests_tests_CFLAGS = $(DBGFLAGS) $(AM_CFLAGS) $(CFLAGS_COMMON) $(CFLAGS_GTEST) $(LIBNL_CFLAGS)

tests/performancetimer_ut.cpp

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#include "common/performancetimer.h"
2+
#include <nlohmann/json.hpp>
3+
#include "gtest/gtest.h"
4+
#include <thread>
5+
6+
using namespace std;
7+
8+
#define PRINT_ALL 1
9+
10+
TEST(PerformancetimerTest, basic)
11+
{
12+
std::string expected;
13+
14+
static swss::PerformanceTimer timer("basic", PRINT_ALL);
15+
timer.start();
16+
this_thread::sleep_for(chrono::milliseconds(100));
17+
timer.stop();
18+
std::string output = timer.inc(1000);
19+
20+
expected = R"({"API":"basic","RPS[k]":10.0,"Tasks":1000,"Total[ms]":100,"busy[ms]":100,"idle[ms]":0})";
21+
EXPECT_EQ(output, expected);
22+
23+
timer.setTimerName("basic_set_name");
24+
timer.setTimerVerbose(true);
25+
timer.setTimerThreshold(3000);
26+
27+
timer.start();
28+
this_thread::sleep_for(chrono::milliseconds(100));
29+
timer.stop();
30+
output = timer.inc(1000);
31+
EXPECT_EQ(output, "");
32+
33+
this_thread::sleep_for(chrono::milliseconds(200));
34+
35+
timer.start();
36+
this_thread::sleep_for(chrono::milliseconds(300));
37+
timer.stop();
38+
output = timer.inc(2000);
39+
40+
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]})";
41+
42+
EXPECT_EQ(output, expected);
43+
}

0 commit comments

Comments
 (0)