Added MQTT benchmarking utiliy based on FuseSource MQTT client

This commit is contained in:
2012-08-25 11:40:08 +02:00
parent 61f0e261bf
commit 57137da4bc
5 changed files with 205 additions and 1 deletions
@@ -0,0 +1,24 @@
package org.dna.mqtt.bechnmark;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
/**
*
* Benchmark tool entry point.
*
* You could set the number of producers and consumers
*
* @author andrea
*/
public class Main {
private static final int PUBLISHER_POOL_SIZE = 2;
public static void main(String[] args) {
ExecutorService pool = Executors.newFixedThreadPool(PUBLISHER_POOL_SIZE);
pool.submit(new Producer("Client1"));
pool.shutdown();
}
}
@@ -0,0 +1,63 @@
package org.dna.mqtt.bechnmark;
import java.net.URISyntaxException;
import java.util.logging.Level;
import org.fusesource.mqtt.client.BlockingConnection;
import org.fusesource.mqtt.client.MQTT;
import org.fusesource.mqtt.client.QoS;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* This class only publish MQTT messages to a define topic with a certain frequency.
*
*
* @author andrea
*/
public class Producer implements Runnable {
private static final Logger LOG = LoggerFactory.getLogger(Producer.class);
private String m_clientID;
public Producer(String clientID) {
m_clientID = clientID;
}
public void run() {
MQTT mqtt = new MQTT();
try {
// mqtt.setHost("test.mosquitto.org", 1883);
mqtt.setHost("localhost", 1883);
} catch (URISyntaxException ex) {
LOG.error(null, ex);
return;
}
mqtt.setClientId(m_clientID);
BlockingConnection connection = mqtt.blockingConnection();
try {
connection.connect();
} catch (Exception ex) {
LOG.error("Cant't CONNECT to the server", ex);
return;
}
//TODO loop
try {
LOG.info("Publishing");
connection.publish("/topic", "Hello world MQTT!!".getBytes(), QoS.AT_MOST_ONCE, false);
} catch (Exception ex) {
LOG.error("Cant't PUSBLISH to the server", ex);
return;
}
try {
LOG.info("Disconneting");
connection.disconnect();
LOG.info("Disconnected");
} catch (Exception ex) {
LOG.error("Cant't DISCONNECT to the server", ex);
}
}
}