Skip to content

Commit e0c97c0

Browse files
author
jparisu
committed
Refs #15785: Add more test cases
Signed-off-by: jparisu <[email protected]>
1 parent 8e46377 commit e0c97c0

File tree

12 files changed

+838
-23
lines changed

12 files changed

+838
-23
lines changed

examples/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414

1515
set(fastdds_statistics_backend_FOUND TRUE)
1616
add_subdirectory(cpp/HelloWorldExample)
17+
add_subdirectory(cpp/DummyJsonExample)
18+
add_subdirectory(cpp/DummyMapExample)
1719

1820
find_package(prometheus-cpp CONFIG)
1921
if (prometheus-cpp_FOUND)
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
# Copyright 2021 Proyectos y Sistemas de Mantenimiento SL (eProsima).
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
cmake_minimum_required(VERSION 2.8.12)
16+
17+
if(NOT CMAKE_VERSION VERSION_LESS 3.0)
18+
cmake_policy(SET CMP0048 NEW)
19+
endif()
20+
21+
project(DummyJson)
22+
23+
# Find requirements
24+
if(NOT fastcdr_FOUND)
25+
find_package(fastcdr REQUIRED)
26+
endif()
27+
28+
if(NOT fastrtps_FOUND)
29+
find_package(fastrtps REQUIRED)
30+
endif()
31+
32+
if(NOT fastdds_statistics_backend_FOUND)
33+
find_package(fastdds_statistics_backend REQUIRED)
34+
endif()
35+
36+
#Check C++11
37+
include(CheckCXXCompilerFlag)
38+
if(CMAKE_COMPILER_IS_GNUCXX OR CMAKE_COMPILER_IS_CLANG OR
39+
CMAKE_CXX_COMPILER_ID MATCHES "Clang")
40+
check_cxx_compiler_flag(-std=c++11 SUPPORTS_CXX11)
41+
if(NOT SUPPORTS_CXX11)
42+
message(FATAL_ERROR "Compiler doesn't support C++11")
43+
endif()
44+
endif()
45+
46+
message(STATUS "Configuring HelloWorld example...")
47+
file(GLOB HELLOWORLD_EXAMPLE_SOURCES_CXX "*.cxx")
48+
file(GLOB HELLOWORLD_EXAMPLE_SOURCES_CPP "*.cpp")
49+
50+
add_executable(DummyJson ${HELLOWORLD_EXAMPLE_SOURCES_CXX} ${HELLOWORLD_EXAMPLE_SOURCES_CPP})
51+
target_compile_definitions(DummyJson PRIVATE
52+
$<$<AND:$<NOT:$<BOOL:${WIN32}>>,$<STREQUAL:"${CMAKE_BUILD_TYPE}","Debug">>:__DEBUG>
53+
$<$<BOOL:${INTERNAL_DEBUG}>:__INTERNALDEBUG> # Internal debug activated.
54+
)
55+
target_link_libraries(DummyJson fastrtps fastcdr fastdds_statistics_backend)
56+
install(TARGETS DummyJson
57+
RUNTIME DESTINATION examples/cpp/DummyJson/${BIN_INSTALL_DIR})
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
// Copyright 2021 Proyectos y Sistemas de Mantenimiento SL (eProsima).
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
/**
16+
* @file main.cpp
17+
*
18+
*/
19+
20+
#include <iostream>
21+
#include <chrono>
22+
#include <thread>
23+
24+
#include <fastdds_statistics_backend/nlohmann-json/json.hpp>
25+
26+
void sleep_ms(unsigned int n)
27+
{
28+
std::this_thread::sleep_for(std::chrono::milliseconds(n));
29+
}
30+
31+
void sleep_s(unsigned int n)
32+
{
33+
sleep_ms(n * 1000);
34+
}
35+
36+
int main(
37+
int argc,
38+
char** argv)
39+
{
40+
unsigned int n_secs = 0;
41+
// Get N for seconds to wait in each step
42+
if(argc != 2)
43+
{
44+
return 1;
45+
}
46+
else
47+
{
48+
n_secs = std::strtol(argv[1], nullptr, 10);
49+
}
50+
51+
std::cout << "STARTING" << std::endl;
52+
53+
for (int i=0; i<3; i++)
54+
{
55+
sleep_s(n_secs);
56+
57+
// Create json object
58+
std::cout << "Create object " << i << std::endl;
59+
nlohmann::json json_object;
60+
sleep_s(n_secs);
61+
62+
// Populate json
63+
std::cout << "Populate object" << std::endl;
64+
json_object["value0"] = true;
65+
json_object["value1"] = 3;
66+
json_object["value2"] = nlohmann::json();
67+
json_object["value2"]["other value"] = "value";
68+
sleep_s(n_secs);
69+
70+
// Populate again with many values
71+
std::cout << "Dense populate object" << std::endl;
72+
for (int j=0; j<1000; j++)
73+
{
74+
json_object[std::to_string(j)] =
75+
"veeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeery long string.";
76+
}
77+
sleep_s(n_secs);
78+
79+
// Remove object
80+
// std::cout << "Remove object: " << json_object << std::endl;
81+
std::cout << "Remove object" << std::endl;
82+
}
83+
84+
sleep_s(n_secs);
85+
std::cout << "FINISHING" << std::endl;
86+
87+
return 0;
88+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
# Copyright 2021 Proyectos y Sistemas de Mantenimiento SL (eProsima).
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
cmake_minimum_required(VERSION 2.8.12)
16+
17+
if(NOT CMAKE_VERSION VERSION_LESS 3.0)
18+
cmake_policy(SET CMP0048 NEW)
19+
endif()
20+
21+
project(DummyMap)
22+
23+
# Find requirements
24+
if(NOT fastcdr_FOUND)
25+
find_package(fastcdr REQUIRED)
26+
endif()
27+
28+
if(NOT fastrtps_FOUND)
29+
find_package(fastrtps REQUIRED)
30+
endif()
31+
32+
if(NOT fastdds_statistics_backend_FOUND)
33+
find_package(fastdds_statistics_backend REQUIRED)
34+
endif()
35+
36+
#Check C++11
37+
include(CheckCXXCompilerFlag)
38+
if(CMAKE_COMPILER_IS_GNUCXX OR CMAKE_COMPILER_IS_CLANG OR
39+
CMAKE_CXX_COMPILER_ID MATCHES "Clang")
40+
check_cxx_compiler_flag(-std=c++11 SUPPORTS_CXX11)
41+
if(NOT SUPPORTS_CXX11)
42+
message(FATAL_ERROR "Compiler doesn't support C++11")
43+
endif()
44+
endif()
45+
46+
message(STATUS "Configuring HelloWorld example...")
47+
file(GLOB HELLOWORLD_EXAMPLE_SOURCES_CXX "*.cxx")
48+
file(GLOB HELLOWORLD_EXAMPLE_SOURCES_CPP "*.cpp")
49+
50+
add_executable(DummyMap ${HELLOWORLD_EXAMPLE_SOURCES_CXX} ${HELLOWORLD_EXAMPLE_SOURCES_CPP})
51+
target_compile_definitions(DummyMap PRIVATE
52+
$<$<AND:$<NOT:$<BOOL:${WIN32}>>,$<STREQUAL:"${CMAKE_BUILD_TYPE}","Debug">>:__DEBUG>
53+
$<$<BOOL:${INTERNAL_DEBUG}>:__INTERNALDEBUG> # Internal debug activated.
54+
)
55+
target_link_libraries(DummyMap fastrtps fastcdr fastdds_statistics_backend)
56+
install(TARGETS DummyMap
57+
RUNTIME DESTINATION examples/cpp/DummyMap/${BIN_INSTALL_DIR})

examples/cpp/DummyMapExample/main.cpp

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
// Copyright 2021 Proyectos y Sistemas de Mantenimiento SL (eProsima).
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
/**
16+
* @file main.cpp
17+
*
18+
*/
19+
20+
#include <iostream>
21+
#include <chrono>
22+
#include <thread>
23+
24+
#include <fastdds_statistics_backend/nlohmann-json/json.hpp>
25+
26+
void sleep_ms(unsigned int n)
27+
{
28+
std::this_thread::sleep_for(std::chrono::milliseconds(n));
29+
}
30+
31+
void sleep_s(unsigned int n)
32+
{
33+
sleep_ms(n * 1000);
34+
}
35+
36+
int main(
37+
int argc,
38+
char** argv)
39+
{
40+
unsigned int n_secs = 0;
41+
// Get N for seconds to wait in each step
42+
if(argc != 2)
43+
{
44+
return 1;
45+
}
46+
else
47+
{
48+
n_secs = std::strtol(argv[1], nullptr, 10);
49+
}
50+
51+
std::cout << "STARTING" << std::endl;
52+
53+
for (int i=0; i<3; i++)
54+
{
55+
sleep_s(n_secs);
56+
57+
// Create json object
58+
std::cout << "Create object " << i << std::endl;
59+
std::map<std::string, std::string> map_object;
60+
sleep_s(n_secs);
61+
62+
// Populate json
63+
std::cout << "Populate object" << std::endl;
64+
map_object["value0"] = "true";
65+
map_object["value1"] = "3";
66+
map_object["value2"] = "std::map<std::string, std::string>()";
67+
map_object["value2"] += "other value: value";
68+
sleep_s(n_secs);
69+
70+
// Populate again with many values
71+
std::cout << "Dense populate object" << std::endl;
72+
for (int j=0; j<(1000*i); j++)
73+
{
74+
map_object[std::to_string(j)] =
75+
"veeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeery long string.";
76+
}
77+
sleep_s(n_secs);
78+
79+
// Remove object
80+
// for (auto const& it : map_object)
81+
// {
82+
// std::cout << " Elements: " << it.first << " : " << it.second << std::endl;
83+
// }
84+
std::cout << "Remove object" << std::endl;
85+
}
86+
87+
sleep_s(n_secs);
88+
std::cout << "FINISHING" << std::endl;
89+
90+
return 0;
91+
}

examples/cpp/HelloWorldExample/Monitor.cpp

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,14 @@ void Monitor::stop()
6161
bool Monitor::init(
6262
uint32_t domain,
6363
uint32_t n_bins,
64-
uint32_t t_interval)
64+
uint32_t t_interval,
65+
bool reset /* = false */)
6566
{
6667
n_bins_ = n_bins;
6768
t_interval_ = t_interval;
6869
monitor_id_ = StatisticsBackend::init_monitor(domain);
70+
reset_ = reset;
71+
6972
if (!monitor_id_.is_valid())
7073
{
7174
std::cout << "Error creating monitor" << std::endl;
@@ -97,6 +100,12 @@ void Monitor::run()
97100
std::cout << std::endl;
98101
get_fastdds_latency_mean();
99102
get_publication_throughput_mean();
103+
104+
if (reset_)
105+
{
106+
std::cout << "Removing internal data from Statistics Backend." << std::endl;
107+
StatisticsBackend::dump_database(true);
108+
}
100109
}
101110
}
102111

examples/cpp/HelloWorldExample/Monitor.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,8 @@ class Monitor
4545
bool init(
4646
uint32_t domain,
4747
uint32_t n_bins,
48-
uint32_t t_interval);
48+
uint32_t t_interval,
49+
bool reset = false);
4950

5051
//! Run the monitor
5152
void run();
@@ -132,6 +133,9 @@ class Monitor
132133
//! Time interval of the returned measures
133134
uint32_t t_interval_;
134135

136+
//! Whether the internal data must be removed each interval
137+
bool reset_;
138+
135139
//! Member used for control flow purposes
136140
static std::atomic<bool> stop_;
137141

examples/cpp/HelloWorldExample/arg_configuration.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,8 @@ enum optionIndex
109109
INTERVAL,
110110
DOMAIN_ID,
111111
N_BINS,
112-
T_INTERVAL
112+
T_INTERVAL,
113+
RESET
113114
};
114115

115116
const option::Descriptor usage[] = {
@@ -139,7 +140,8 @@ const option::Descriptor usage[] = {
139140
" (0 => no mean calculation, return raw data)." },
140141
{ T_INTERVAL, 0, "t", "time", Arg::Numeric,
141142
" -t <num> \t--time=<num> \tDuration in seconds of each time frame (Default: 5)." },
142-
143+
{ RESET, 0, "r", "reset", Arg::None,
144+
" -r \t--reset \tIf set each time interval the Monitor will remove every internal data." },
143145

144146
{ 0, 0, 0, 0, 0, 0 }
145147
};

0 commit comments

Comments
 (0)