ros2cli icon indicating copy to clipboard operation
ros2cli copied to clipboard

Proposal for C++ based ros2 topic hz

Open sgillen opened this issue 9 months ago • 3 comments

Hello, I am considering putting together an MR to contribute a more performant C++ implementation of ros2 topic hz (and maybe ros2 topic bw while we are at it).

I think even with #1005 merged, performance of ros2 topic hz remains poor, especially for high frequency topics. I think #843 is still an issue. I did some informal experiments awhile ago to confirm this, which I can replicate and share if desired (did anyone else run experiments on this btw? would be good to compare notes)

When I switch to a C++ based implementation I see a ~8x reduction in CPU usage, and can accurately report topic frequencies up to 10Khz, whereas the existing ros2 topic hz caps out around a 2-3khz.

There are two caveats to this that I see already.

  1. Using C++ from the python topic verb will require pybind11 or ctypes, which may complicate distribution.
  2. the --filter option accepts arbitrary python code, so likely we would want to fallback to the original hz.py implementation just for --filter, which is a little ugly.

I'd like to get the maintainers thoughts on this, would this MR be something you are interested in merging? Are there other gotchas I am not considering?

sgillen avatar Jun 03 '25 23:06 sgillen

Currently the ros2cli packages are all pure Python, so there'd be some work to make the ros2topic package use CMake in order to create a cPython extension that used C++.

There's an easier thing we can try first. Would you be willing to try switching the executor used by ros2 topic hz to the events executor? Currently it uses the single threaded executor, and the events executor should have much better performance.

https://github.com/ros2/ros2cli/blob/c24da0adf9b64b2d9e24253fc4bd799778a52ba1/ros2topic/ros2topic/verb/hz.py#L342

https://discourse.ros.org/t/faster-rclpy-executor-now-in-rolling/42852

sloretz avatar Jun 12 '25 17:06 sloretz

From CWG today, would also like to see results with @nadavelkabets asyncio executor: https://github.com/ros2/rclpy/pull/1399

mjcarroll avatar Jun 13 '25 15:06 mjcarroll

Hey just want to share the work we did here was eventually released as greenwave monitor. We have some performance data that we need to put together/publish. The main value in greenwave is the UX though, rather than just the performance vs ros2 topic hz.

sgillen avatar Oct 31 '25 06:10 sgillen