Subscriber for benchmarking using Paho API

This commit is contained in:
andsel
2016-12-21 23:17:22 +01:00
parent 631049877e
commit 68a4093e1a
3 changed files with 90 additions and 100 deletions
@@ -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,49 +1,19 @@
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 {
private int numReceived = 0 class SubscriberCallback implements MqttCallback {
private long startTime = System.currentTimeMillis() public void messageArrived(String topic, MqttMessage message) throws Exception {
private boolean exit = false
private CountDownLatch m_latch = new CountDownLatch(1)
private boolean alreadyStarted = false
private Histogram histogram = new Histogram(5)
private CallbackConnection connection
private MQTT mqtt
private String dialog_id
// BenchmarkSubscriber(MQTT mqtt) {
// this.mqtt = mqtt
// }
def connect() {
this.connection = mqtt.callbackConnection()
connection.listener(new Listener() {
public void onDisconnected() {
println "SUB: listener received disconnection"
}
public void onConnected() {
println "SUB: listener received a connected to ${mqtt.host}"
// startTime = System.currentTimeMillis()
}
public void onPublish(UTF8Buffer topic, Buffer payload, Runnable ack) {
// You can now process a received message from a topic.
// Once process execute the ack runnable.
ack.run()
//used to catch the first message published //used to catch the first message published
if (!alreadyStarted) { if (!alreadyStarted) {
startTime = System.currentTimeMillis() startTime = System.currentTimeMillis()
@@ -54,7 +24,7 @@ class BenchmarkSubscriber {
if (topic.toString().startsWith("/exit")) { if (topic.toString().startsWith("/exit")) {
println "SUB: ok Exit!" println "SUB: ok Exit!"
exit = true exit = true
connection.disconnect(null) connection.disconnect()
long stopTime = System.currentTimeMillis() long stopTime = System.currentTimeMillis()
long spentTime = stopTime - startTime long spentTime = stopTime - startTime
println "SUB: " + topic.toString() + " received" println "SUB: " + topic.toString() + " received"
@@ -65,8 +35,8 @@ class BenchmarkSubscriber {
histogram.outputPercentileDistribution(System.out, 1000.0); //nanos/1000 histogram.outputPercentileDistribution(System.out, 1000.0); //nanos/1000
m_latch.countDown() m_latch.countDown()
} else { } else {
String message = payload as String String payload = new String(message.payload)
long sentTime = message.split('-')[1] as long long sentTime = payload.split('-')[1] as long
long delay = System.nanoTime() - sentTime long delay = System.nanoTime() - sentTime
histogram.recordValue(delay) histogram.recordValue(delay)
print '+' print '+'
@@ -77,46 +47,64 @@ class BenchmarkSubscriber {
} }
} }
public void onFailure(Throwable th) {
connection.disconnect(null) public void connectionLost(Throwable cause) {
println "Client ${clientCounter} lost connection, caused by ${cause}"
client.disconnect()
println "SUB: Something failed during message reception" println "SUB: Something failed during message reception"
th.printStackTrace() th.printStackTrace()
m_latch.countDown() m_latch.countDown()
} }
public void deliveryComplete(IMqttDeliveryToken token) {
}
}
private int numReceived = 0
private long startTime = System.currentTimeMillis()
private boolean exit = false
private CountDownLatch m_latch = new CountDownLatch(1)
private boolean alreadyStarted = false
private Histogram histogram = new Histogram(5)
private IMqttAsyncClient client
private String dialog_id
def connect() {
MqttConnectOptions connectOptions = new MqttConnectOptions()
connectOptions.cleanSession = true
this.client.callback = new SubscriberCallback()
this.client.connect(connectOptions, null, new IMqttActionListener() {
public void onFailure(IMqttToken asyncActionToken, Throwable exception) {
println "SUB: connect fail"
println "Excp: ${exception}"
System.exit(2)
}
public void onSuccess(IMqttToken asyncActionToken) {
this.client.subscribe("/topic" + dialog_id, 1, null, new IMqttActionListener() {
public void onFailure(IMqttToken asyncActionToken2, Throwable exception) {
this.connection.disconnect()
}
public void onSuccess(IMqttToken asyncActionToken2) {
println "SUB: subscribed to /topic${dialog_id} qos: 1"
}
}) })
connection.connect(new Callback<Void>() { this.client.subscribe("/exit" + dialog_id, 1, null, new IMqttActionListener() {
public void onFailure(Throwable value) { public void onFailure(IMqttToken asyncActionToken2, Throwable exception) {
System.exit(2); this.connection.disconnect()
}
// 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.
}
});
// Subscribe to /topic
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.
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()