@@ -9,35 +9,46 @@ using namespace std;
99
1010TEST (PerformancetimerTest, basic)
1111{
12- std::string expected;
13-
1412 static swss::PerformanceTimer timer (" basic" , PRINT_ALL);
13+
14+ // First measurement
1515 timer.start ();
1616 this_thread::sleep_for (chrono::milliseconds (100 ));
1717 timer.stop ();
1818 std::string output = timer.inc (1000 );
1919
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);
20+ auto actualJson = nlohmann::json::parse (output);
21+ EXPECT_EQ (actualJson[" API" ], " basic" );
22+ EXPECT_EQ (actualJson[" Tasks" ], 1000 );
23+ EXPECT_NEAR (actualJson[" RPS[k]" ].get <double >(), 10.0 , 0.1 );
24+ EXPECT_NEAR (static_cast <double >(actualJson[" busy[ms]" ].get <uint64_t >()), 100.0 , 10.0 );
25+ EXPECT_LE (actualJson[" idle[ms]" ].get <uint64_t >(), 10u );
2226
27+ // Configuration changes
2328 timer.setTimerName (" basic_set_name" );
2429 timer.setTimerVerbose (true );
2530 timer.setTimerThreshold (3000 );
2631
32+ // Second measurement
2733 timer.start ();
2834 this_thread::sleep_for (chrono::milliseconds (100 ));
2935 timer.stop ();
3036 output = timer.inc (1000 );
3137 EXPECT_EQ (output, " " );
3238
39+ // Third measurement
3340 this_thread::sleep_for (chrono::milliseconds (200 ));
34-
3541 timer.start ();
3642 this_thread::sleep_for (chrono::milliseconds (300 ));
3743 timer.stop ();
3844 output = timer.inc (2000 );
3945
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- }
46+ actualJson = nlohmann::json::parse (output);
47+ EXPECT_EQ (actualJson[" API" ], " basic_set_name" );
48+ EXPECT_EQ (actualJson[" Tasks" ], 3000 );
49+ EXPECT_NEAR (actualJson[" RPS[k]" ].get <double >(), 5.0 , 0.1 );
50+ EXPECT_NEAR (static_cast <double >(actualJson[" busy[ms]" ].get <uint64_t >()), 400.0 , 20.0 );
51+ EXPECT_NEAR (static_cast <double >(actualJson[" idle[ms]" ].get <uint64_t >()), 200.0 , 20.0 );
52+ EXPECT_EQ (actualJson[" m_incs" ].get <std::vector<uint64_t >>(), std::vector<uint64_t >({1000 , 2000 }));
53+ EXPECT_EQ (actualJson[" m_intervals" ].get <std::vector<uint64_t >>(), std::vector<uint64_t >({100 , 300 }));
54+ }
0 commit comments