@@ -49,6 +49,18 @@ explicit TimeKeeper(Reporters... reporters);
49
49
- Ends tracking the processing time of a function.
50
50
- `func_name`: Name of the function to end tracking.
51
51
52
+ ##### Note
53
+
54
+ - It's possible to start and end time measurements using `start_track` and `end_track` as shown below:
55
+
56
+ ```cpp
57
+ time_keeper.start_track("example_function");
58
+ // Your function code here
59
+ time_keeper.end_track("example_function");
60
+ ```
61
+
62
+ - For safety and to ensure proper tracking, it is recommended to use ` ScopedTimeTrack ` .
63
+
52
64
##### Example
53
65
54
66
``` cpp
@@ -62,7 +74,7 @@ explicit TimeKeeper(Reporters... reporters);
62
74
int main (int argc, char ** argv)
63
75
{
64
76
rclcpp::init(argc, argv);
65
- auto node = std::make_shared<rclcpp::Node>("time_keeper_example ");
77
+ auto node = std::make_shared< rclcpp::Node > ("scoped_time_track_example ");
66
78
67
79
auto time_keeper = std::make_shared< autoware::universe_utils::TimeKeeper > ();
68
80
@@ -78,23 +90,20 @@ int main(int argc, char ** argv)
78
90
time_keeper->add_reporter(publisher_str);
79
91
80
92
auto funcA = [ &time_keeper] ( ) {
81
- time_keeper->start_track ("funcA");
93
+ autoware::universe_utils::ScopedTimeTrack st ("funcA", * time_keeper );
82
94
std::this_thread::sleep_for(std::chrono::seconds(1));
83
- time_keeper->end_track("funcA");
84
95
};
85
96
86
97
auto funcB = [ &time_keeper, &funcA] ( ) {
87
- time_keeper->start_track ("funcB");
98
+ autoware::universe_utils::ScopedTimeTrack st ("funcB", * time_keeper );
88
99
std::this_thread::sleep_for(std::chrono::seconds(2));
89
100
funcA();
90
- time_keeper->end_track("funcB");
91
101
};
92
102
93
103
auto funcC = [ &time_keeper, &funcB] ( ) {
94
- time_keeper->start_track ("funcC");
104
+ autoware::universe_utils::ScopedTimeTrack st ("funcC", * time_keeper );
95
105
std::this_thread::sleep_for(std::chrono::seconds(3));
96
106
funcB();
97
- time_keeper->end_track("funcC");
98
107
};
99
108
100
109
funcC();
@@ -165,94 +174,3 @@ ScopedTimeTrack(const std::string & func_name, TimeKeeper & time_keeper);
165
174
```
166
175
167
176
- Destroys the ` ScopedTimeTrack ` object, ending the tracking of the function.
168
-
169
- ##### Example
170
-
171
- ``` cpp
172
- #include " autoware/universe_utils/system/time_keeper.hpp"
173
-
174
- #include < rclcpp/rclcpp.hpp>
175
-
176
- #include < iostream>
177
- #include < memory>
178
-
179
- int main (int argc, char ** argv)
180
- {
181
- rclcpp::init(argc, argv);
182
- auto node = std::make_shared< rclcpp::Node > ("scoped_time_track_example");
183
-
184
- auto time_keeper = std::make_shared< autoware::universe_utils::TimeKeeper > ();
185
-
186
- time_keeper->add_reporter(&std::cout);
187
-
188
- auto publisher =
189
- node->create_publisher< autoware::universe_utils::ProcessingTimeDetail > ("processing_time", 10);
190
-
191
- time_keeper->add_reporter(publisher);
192
-
193
- auto publisher_str = node->create_publisher<std_msgs::msg::String>("processing_time_str", 10);
194
-
195
- time_keeper->add_reporter(publisher_str);
196
-
197
- auto funcA = [ &time_keeper] ( ) {
198
- autoware::universe_utils::ScopedTimeTrack st("funcA", * time_keeper);
199
- std::this_thread::sleep_for(std::chrono::seconds(1));
200
- };
201
-
202
- auto funcB = [ &time_keeper, &funcA] ( ) {
203
- autoware::universe_utils::ScopedTimeTrack st("funcB", * time_keeper);
204
- std::this_thread::sleep_for(std::chrono::seconds(2));
205
- funcA();
206
- };
207
-
208
- auto funcC = [ &time_keeper, &funcB] ( ) {
209
- autoware::universe_utils::ScopedTimeTrack st("funcC", * time_keeper);
210
- std::this_thread::sleep_for(std::chrono::seconds(3));
211
- funcB();
212
- };
213
-
214
- funcC();
215
-
216
- rclcpp::spin(node);
217
- rclcpp::shutdown();
218
- return 0;
219
- }
220
- ```
221
-
222
- - Output (console)
223
-
224
- ```text
225
- ==========================
226
- funcC (6000.7ms)
227
- └── funcB (3000.44ms)
228
- └── funcA (1000.19ms)
229
- ```
230
-
231
- - Output (` ros2 topic echo /processing_time ` )
232
-
233
- ``` text
234
- nodes:
235
- - id: 1
236
- name: funcC
237
- processing_time: 6000.659
238
- parent_id: 0
239
- - id: 2
240
- name: funcB
241
- processing_time: 3000.415
242
- parent_id: 1
243
- - id: 3
244
- name: funcA
245
- processing_time: 1000.181
246
- parent_id: 2
247
- ---
248
- ```
249
-
250
- - Output (` ros2 topic echo /processing_time_str --field data ` )
251
-
252
- ``` text
253
- funcC (6000.67ms)
254
- └── funcB (3000.42ms)
255
- └── funcA (1000.19ms)
256
-
257
- ---
258
- ```
0 commit comments