Subscriber for benchmarking using Paho API
This commit is contained in:
@@ -1,4 +1,3 @@
|
|||||||
|
|
||||||
import org.eclipse.paho.client.mqttv3.IMqttActionListener
|
import org.eclipse.paho.client.mqttv3.IMqttActionListener
|
||||||
import org.eclipse.paho.client.mqttv3.IMqttToken
|
import org.eclipse.paho.client.mqttv3.IMqttToken
|
||||||
import org.eclipse.paho.client.mqttv3.MqttConnectOptions
|
import org.eclipse.paho.client.mqttv3.MqttConnectOptions
|
||||||
|
|||||||
@@ -1,122 +1,110 @@
|
|||||||
import org.fusesource.mqtt.client.BlockingConnection
|
import org.eclipse.paho.client.mqttv3.IMqttActionListener
|
||||||
import org.fusesource.mqtt.client.CallbackConnection
|
import org.eclipse.paho.client.mqttv3.IMqttToken
|
||||||
import org.fusesource.mqtt.client.MQTT
|
import org.eclipse.paho.client.mqttv3.MqttConnectOptions
|
||||||
import org.fusesource.mqtt.client.Listener
|
import org.eclipse.paho.client.mqttv3.IMqttAsyncClient
|
||||||
import org.fusesource.mqtt.client.Message
|
import org.eclipse.paho.client.mqttv3.MqttMessage
|
||||||
import org.fusesource.mqtt.client.QoS
|
import org.eclipse.paho.client.mqttv3.IMqttDeliveryToken
|
||||||
import org.fusesource.mqtt.client.Topic
|
import org.eclipse.paho.client.mqttv3.MqttCallback
|
||||||
import org.fusesource.mqtt.client.Callback
|
|
||||||
import org.fusesource.hawtbuf.Buffer
|
|
||||||
import org.fusesource.hawtbuf.UTF8Buffer
|
|
||||||
import java.util.concurrent.CountDownLatch
|
import java.util.concurrent.CountDownLatch
|
||||||
import java.util.concurrent.TimeUnit
|
import java.util.concurrent.TimeUnit
|
||||||
import org.HdrHistogram.Histogram
|
import org.HdrHistogram.Histogram
|
||||||
|
|
||||||
class BenchmarkSubscriber {
|
class BenchmarkSubscriber {
|
||||||
|
class SubscriberCallback implements MqttCallback {
|
||||||
|
public void messageArrived(String topic, MqttMessage message) throws Exception {
|
||||||
|
//used to catch the first message published
|
||||||
|
if (!alreadyStarted) {
|
||||||
|
startTime = System.currentTimeMillis()
|
||||||
|
alreadyStarted = true
|
||||||
|
}
|
||||||
|
|
||||||
|
//println "Received a publish on topic ${topic}"
|
||||||
|
if (topic.toString().startsWith("/exit")) {
|
||||||
|
println "SUB: ok Exit!"
|
||||||
|
exit = true
|
||||||
|
connection.disconnect()
|
||||||
|
long stopTime = System.currentTimeMillis()
|
||||||
|
long spentTime = stopTime - startTime
|
||||||
|
println "SUB: " + topic.toString() + " received"
|
||||||
|
println "SUB: subscriber disconnected, received ${numReceived} messages in ${spentTime} ms"
|
||||||
|
double msgPerSec = (numReceived / spentTime) * 1000
|
||||||
|
println "SUB: Speed: $msgPerSec msg/sec"
|
||||||
|
println "SUB: Latency diagram"
|
||||||
|
histogram.outputPercentileDistribution(System.out, 1000.0); //nanos/1000
|
||||||
|
m_latch.countDown()
|
||||||
|
} else {
|
||||||
|
String payload = new String(message.payload)
|
||||||
|
long sentTime = payload.split('-')[1] as long
|
||||||
|
long delay = System.nanoTime() - sentTime
|
||||||
|
histogram.recordValue(delay)
|
||||||
|
print '+'
|
||||||
|
numReceived++
|
||||||
|
// if ((numReceived % 10000) == 0) {
|
||||||
|
// print '.'
|
||||||
|
// }
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public void connectionLost(Throwable cause) {
|
||||||
|
println "Client ${clientCounter} lost connection, caused by ${cause}"
|
||||||
|
client.disconnect()
|
||||||
|
println "SUB: Something failed during message reception"
|
||||||
|
th.printStackTrace()
|
||||||
|
m_latch.countDown()
|
||||||
|
}
|
||||||
|
|
||||||
|
public void deliveryComplete(IMqttDeliveryToken token) {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
private int numReceived = 0
|
private int numReceived = 0
|
||||||
private long startTime = System.currentTimeMillis()
|
private long startTime = System.currentTimeMillis()
|
||||||
private boolean exit = false
|
private boolean exit = false
|
||||||
private CountDownLatch m_latch = new CountDownLatch(1)
|
private CountDownLatch m_latch = new CountDownLatch(1)
|
||||||
private boolean alreadyStarted = false
|
private boolean alreadyStarted = false
|
||||||
private Histogram histogram = new Histogram(5)
|
private Histogram histogram = new Histogram(5)
|
||||||
private CallbackConnection connection
|
|
||||||
|
|
||||||
private MQTT mqtt
|
private IMqttAsyncClient client
|
||||||
private String dialog_id
|
private String dialog_id
|
||||||
|
|
||||||
// BenchmarkSubscriber(MQTT mqtt) {
|
|
||||||
// this.mqtt = mqtt
|
|
||||||
// }
|
|
||||||
def connect() {
|
def connect() {
|
||||||
this.connection = mqtt.callbackConnection()
|
MqttConnectOptions connectOptions = new MqttConnectOptions()
|
||||||
connection.listener(new Listener() {
|
connectOptions.cleanSession = true
|
||||||
|
this.client.callback = new SubscriberCallback()
|
||||||
|
this.client.connect(connectOptions, null, new IMqttActionListener() {
|
||||||
|
|
||||||
public void onDisconnected() {
|
public void onFailure(IMqttToken asyncActionToken, Throwable exception) {
|
||||||
println "SUB: listener received disconnection"
|
println "SUB: connect fail"
|
||||||
}
|
println "Excp: ${exception}"
|
||||||
public void onConnected() {
|
System.exit(2)
|
||||||
println "SUB: listener received a connected to ${mqtt.host}"
|
|
||||||
// startTime = System.currentTimeMillis()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void onPublish(UTF8Buffer topic, Buffer payload, Runnable ack) {
|
public void onSuccess(IMqttToken asyncActionToken) {
|
||||||
// You can now process a received message from a topic.
|
this.client.subscribe("/topic" + dialog_id, 1, null, new IMqttActionListener() {
|
||||||
// Once process execute the ack runnable.
|
public void onFailure(IMqttToken asyncActionToken2, Throwable exception) {
|
||||||
ack.run()
|
this.connection.disconnect()
|
||||||
|
|
||||||
//used to catch the first message published
|
|
||||||
if (!alreadyStarted) {
|
|
||||||
startTime = System.currentTimeMillis()
|
|
||||||
alreadyStarted = true
|
|
||||||
}
|
|
||||||
|
|
||||||
//println "Received a publish on topic ${topic}"
|
|
||||||
if (topic.toString().startsWith("/exit")) {
|
|
||||||
println "SUB: ok Exit!"
|
|
||||||
exit = true
|
|
||||||
connection.disconnect(null)
|
|
||||||
long stopTime = System.currentTimeMillis()
|
|
||||||
long spentTime = stopTime - startTime
|
|
||||||
println "SUB: " + topic.toString() + " received"
|
|
||||||
println "SUB: subscriber disconnected, received ${numReceived} messages in ${spentTime} ms"
|
|
||||||
double msgPerSec = (numReceived / spentTime) * 1000
|
|
||||||
println "SUB: Speed: $msgPerSec msg/sec"
|
|
||||||
println "SUB: Latency diagram"
|
|
||||||
histogram.outputPercentileDistribution(System.out, 1000.0); //nanos/1000
|
|
||||||
m_latch.countDown()
|
|
||||||
} else {
|
|
||||||
String message = payload as String
|
|
||||||
long sentTime = message.split('-')[1] as long
|
|
||||||
long delay = System.nanoTime() - sentTime
|
|
||||||
histogram.recordValue(delay)
|
|
||||||
print '+'
|
|
||||||
numReceived++
|
|
||||||
// if ((numReceived % 10000) == 0) {
|
|
||||||
// print '.'
|
|
||||||
// }
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
public void onFailure(Throwable th) {
|
|
||||||
connection.disconnect(null)
|
|
||||||
println "SUB: Something failed during message reception"
|
|
||||||
th.printStackTrace()
|
|
||||||
m_latch.countDown()
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
connection.connect(new Callback<Void>() {
|
|
||||||
public void onFailure(Throwable value) {
|
|
||||||
System.exit(2);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Once we connect..
|
|
||||||
public void onSuccess(Void v) {
|
|
||||||
// Subscribe to /topic
|
|
||||||
Topic[] topics = [new Topic("/topic" + dialog_id, QoS.AT_LEAST_ONCE)]
|
|
||||||
connection.subscribe(topics, new Callback<byte[]>() {
|
|
||||||
public void onSuccess(byte[] qoses) {
|
|
||||||
// The result of the subcribe request.
|
|
||||||
println "SUB: subscribed to /topic${dialog_id} ${qoses}"
|
|
||||||
}
|
}
|
||||||
public void onFailure(Throwable value) {
|
|
||||||
connection.disconnect(null); // subscribe failed.
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
|
public void onSuccess(IMqttToken asyncActionToken2) {
|
||||||
// Subscribe to /topic
|
println "SUB: subscribed to /topic${dialog_id} qos: 1"
|
||||||
Topic[] exitTopic = [new Topic("/exit" + dialog_id, QoS.AT_LEAST_ONCE)]
|
|
||||||
connection.subscribe(exitTopic, new Callback<byte[]>() {
|
|
||||||
public void onSuccess(byte[] qoses) {
|
|
||||||
// The result of the subcribe request.
|
|
||||||
println "SUB: subscribed to /exit${dialog_id} ${qoses}"
|
|
||||||
}
|
}
|
||||||
public void onFailure(Throwable value) {
|
})
|
||||||
connection.disconnect(null); // subscribe failed.
|
|
||||||
|
this.client.subscribe("/exit" + dialog_id, 1, null, new IMqttActionListener() {
|
||||||
|
public void onFailure(IMqttToken asyncActionToken2, Throwable exception) {
|
||||||
|
this.connection.disconnect()
|
||||||
m_latch.countDown()
|
m_latch.countDown()
|
||||||
}
|
}
|
||||||
});
|
|
||||||
|
public void onSuccess(IMqttToken asyncActionToken2) {
|
||||||
|
println "SUB: subscribed to /exit${dialog_id} qos: 1"
|
||||||
|
}
|
||||||
|
})
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -41,15 +41,18 @@ MqttAsyncClient pub = new MqttAsyncClient("tcp://${host}:1883", "PublisherClient
|
|||||||
//sub.setCleanSession(true)
|
//sub.setCleanSession(true)
|
||||||
//sub.setClientId("SubscriberClient${dialog_id}")
|
//sub.setClientId("SubscriberClient${dialog_id}")
|
||||||
|
|
||||||
//BenchmarkSubscriber suscriberBench = new BenchmarkSubscriber(mqtt: sub, dialog_id: dialog_id)
|
MqttDefaultFilePersistence dataStoreSub = new MqttDefaultFilePersistence(tmpDir)
|
||||||
//suscriberBench.connect()
|
MqttAsyncClient sub = new MqttAsyncClient("tcp://${host}:1883", "SubscriberClient${dialog_id}", dataStoreSub)
|
||||||
|
|
||||||
|
BenchmarkSubscriber suscriberBench = new BenchmarkSubscriber(client: sub, dialog_id: dialog_id)
|
||||||
|
suscriberBench.connect()
|
||||||
|
|
||||||
BenchmarkPublisher publisherBench = new BenchmarkPublisher(client: pub, numToSend: numToSend,
|
BenchmarkPublisher publisherBench = new BenchmarkPublisher(client: pub, numToSend: numToSend,
|
||||||
messagesPerSecond: messagesPerSecond, dialog_id: dialog_id)
|
messagesPerSecond: messagesPerSecond, dialog_id: dialog_id)
|
||||||
publisherBench.connect()
|
publisherBench.connect()
|
||||||
publisherBench.firePublishes()
|
publisherBench.firePublishes()
|
||||||
|
|
||||||
//suscriberBench.waitFinish()
|
suscriberBench.waitFinish()
|
||||||
publisherBench.waitFinish()
|
publisherBench.waitFinish()
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user