Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 package org.ros.rosjava_benchmarks;
00018
00019 import org.ros.concurrent.CancellableLoop;
00020 import org.ros.concurrent.Rate;
00021 import org.ros.concurrent.WallTimeRate;
00022 import org.ros.message.Duration;
00023 import org.ros.message.Time;
00024 import org.ros.namespace.GraphName;
00025 import org.ros.node.AbstractNodeMain;
00026 import org.ros.node.ConnectedNode;
00027 import org.ros.node.topic.Publisher;
00028
00029 import java.util.concurrent.TimeUnit;
00030 import java.util.concurrent.atomic.AtomicInteger;
00031
00035 public class MessagesBenchmark extends AbstractNodeMain {
00036
00037 private final AtomicInteger counter;
00038
00039 private Time time;
00040
00041 public MessagesBenchmark() {
00042 counter = new AtomicInteger();
00043 }
00044
00045 @Override
00046 public GraphName getDefaultNodeName() {
00047 return GraphName.of("messages_benchmark");
00048 }
00049
00050 @Override
00051 public void onStart(final ConnectedNode connectedNode) {
00052 connectedNode.executeCancellableLoop(new CancellableLoop() {
00053 @Override
00054 protected void loop() throws InterruptedException {
00055 geometry_msgs.TransformStamped message =
00056 connectedNode.getTopicMessageFactory()
00057 .newFromType(geometry_msgs.TransformStamped._TYPE);
00058 message.getHeader().setFrameId("world");
00059 message.setChildFrameId("turtle");
00060 message.getHeader().setStamp(connectedNode.getCurrentTime());
00061 message.getTransform().getRotation().setW(Math.random());
00062 message.getTransform().getRotation().setX(Math.random());
00063 message.getTransform().getRotation().setY(Math.random());
00064 message.getTransform().getRotation().setZ(Math.random());
00065 message.getTransform().getTranslation().setX(Math.random());
00066 message.getTransform().getTranslation().setY(Math.random());
00067 message.getTransform().getTranslation().setZ(Math.random());
00068 counter.incrementAndGet();
00069 }
00070 });
00071
00072 time = connectedNode.getCurrentTime();
00073 final Publisher<std_msgs.String> statusPublisher =
00074 connectedNode.newPublisher("status", std_msgs.String._TYPE);
00075 final Rate rate = new WallTimeRate(1);
00076 final std_msgs.String status = statusPublisher.newMessage();
00077 connectedNode.executeCancellableLoop(new CancellableLoop() {
00078 @Override
00079 protected void loop() throws InterruptedException {
00080 Time now = connectedNode.getCurrentTime();
00081 Duration delta = now.subtract(time);
00082 if (delta.totalNsecs() > TimeUnit.NANOSECONDS.convert(5, TimeUnit.SECONDS)) {
00083 double hz = counter.getAndSet(0) * 1e9 / delta.totalNsecs();
00084 status.setData(String.format("%.2f Hz", hz));
00085 statusPublisher.publish(status);
00086 time = now;
00087 }
00088 rate.sleep();
00089 }
00090 });
00091 }
00092 }