From 57137da4bc1e5f3da79bc05f2ec0de0f91e72a3e Mon Sep 17 00:00:00 2001 From: "selva.andre@gmail.com" Date: Sat, 25 Aug 2012 11:40:08 +0200 Subject: [PATCH] Added MQTT benchmarking utiliy based on FuseSource MQTT client --- .gitignore | 3 +- benchmarking/pom.xml | 115 ++++++++++++++++++ .../java/org/dna/mqtt/bechnmark/Main.java | 24 ++++ .../java/org/dna/mqtt/bechnmark/Producer.java | 63 ++++++++++ pom.xml | 1 + 5 files changed, 205 insertions(+), 1 deletion(-) create mode 100644 benchmarking/pom.xml create mode 100644 benchmarking/src/main/java/org/dna/mqtt/bechnmark/Main.java create mode 100644 benchmarking/src/main/java/org/dna/mqtt/bechnmark/Producer.java diff --git a/.gitignore b/.gitignore index d639c352..0b8993ab 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ /target/ -/parser/target/ \ No newline at end of file +/parser/target/ +/benchmarking/target/ \ No newline at end of file diff --git a/benchmarking/pom.xml b/benchmarking/pom.xml new file mode 100644 index 00000000..98f4655c --- /dev/null +++ b/benchmarking/pom.xml @@ -0,0 +1,115 @@ + + + 4.0.0 + + ../pom.xml + moquette-parent + org.dna.mqtt + 0.1 + + + moquette-bechmark + jar + 0.1 + Moquette - Benchmarking tool + + + UTF-8 + + + + + + + + + + org.slf4j + slf4j-log4j12 + 1.6.4 + + + + org.dna.mqtt + moquette-parser + 0.1 + + + + org.fusesource.mqtt-client + mqtt-client + 1.3 + + + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + + 1.5 + 1.5 + + + + + + + + + diff --git a/benchmarking/src/main/java/org/dna/mqtt/bechnmark/Main.java b/benchmarking/src/main/java/org/dna/mqtt/bechnmark/Main.java new file mode 100644 index 00000000..7625ae28 --- /dev/null +++ b/benchmarking/src/main/java/org/dna/mqtt/bechnmark/Main.java @@ -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(); + } +} diff --git a/benchmarking/src/main/java/org/dna/mqtt/bechnmark/Producer.java b/benchmarking/src/main/java/org/dna/mqtt/bechnmark/Producer.java new file mode 100644 index 00000000..14641e2d --- /dev/null +++ b/benchmarking/src/main/java/org/dna/mqtt/bechnmark/Producer.java @@ -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); + } + } + +} diff --git a/pom.xml b/pom.xml index 1b48caba..6f6446a4 100644 --- a/pom.xml +++ b/pom.xml @@ -13,6 +13,7 @@ parser + benchmarking