I'm working HiveMQ and I have two clients one each each thread and I have a main thread that starts these two threads. I've been using System.nanoTime()
to time both the sending (for one thread) and the receiving (for the other thread) so I can add up the values to get the total time to send and receive a given number of message. I used synchronized block with wait()
and notify()
in Java so that the timers start at near the same time as one thread will have to wake up the other. My final time to send and receive seems to vary be about 0-350 milliseconds with 15 messages being sent and received naturally when I run my program. For context the server and the client are running on the same machine using localhost as the server address. Is there anyway I could get more precise time (less varying) across my threads? I want to get as much precision as possible.
Code for Subscriber (sending client):
scheduler.waitToReceive(); // makes the SubThread wait until the PubThread is ready to send a message
System.out.println("Timer started");
startTime = System.nanoTime();
for (int i = 1; i <= 15; i++) {
Mqtt5Publish receivedMessage = receivingClient1.receive(MESSAGEWAITTIME,TimeUnit.SECONDS).get(); // receives the message using the "publishes" instance // .get() returns the object if available or throws a NoSuchElementException
PubSubUtility.convertMessage(receivedMessage); // Converts a Mqtt5Publish instance to string and prints
}
endTime = System.nanoTime();
Code for Publisher (publishing client):
readyToSend = true;
scheduler.notifyStartReceive(); // notifies SubThread it can starts receiving messages
startTime = System.nanoTime();
for (int i = 1; i <= 15; i++) {
publisher.publishWith()
.topic(publisherTopic) // publishes to the specified topic
.qos(MqttQos.EXACTLY_ONCE) // sets the quality of service to 2
.payload(convertedMessage) // the contents of the message
.send();
}
endTime = System.nanoTime();