diff --git a/tools_scripts/benchmarker.groovy b/tools_scripts/benchmarker.groovy new file mode 100644 index 00000000..fb99460a --- /dev/null +++ b/tools_scripts/benchmarker.groovy @@ -0,0 +1,112 @@ +//@GrabResolver(name='Paho', root='https://repo.eclipse.org/content/repositories/paho-snapshots/') +//@Grab(group='org.eclipse.paho', module='org.eclipse.paho.client.mqttv3', version='1.0.1-SNAPSHOT') +@GrabResolver(name='Paho', root='https://repo.eclipse.org/content/repositories/paho-releases/') +@Grab(group='org.eclipse.paho', module='mqtt-client', version='0.4.0') + +import org.eclipse.paho.client.mqttv3.MqttAsyncClient +import org.eclipse.paho.client.mqttv3.MqttMessage +import org.eclipse.paho.client.mqttv3.MqttException +import org.eclipse.paho.client.mqttv3.MqttCallback +import org.eclipse.paho.client.mqttv3.IMqttDeliveryToken +import org.eclipse.paho.client.mqttv3.persist.MqttDefaultFilePersistence +import java.util.concurrent.CountDownLatch +import static groovy.json.JsonOutput.* +import java.nio.ByteBuffer + + +class SubscriberCallback implements MqttCallback { + + int m_numReceived = 0 + long m_startTime + boolean firstMessageReceived = false + + private CountDownLatch m_latch = new CountDownLatch(1) + + //Maps delay to num of messages reached with that delay + protected Map distribution = [:] + + void waitFinish() { + m_latch.await() + } + + public void connectionLost(Throwable cause) { + m_latch.countDown() + } + + public void messageArrived(String topic, MqttMessage message) throws Exception { + //println "Received message ${new String(message.payload)} on topic [${topic}]" + if (!firstMessageReceived) { + m_startTime = System.currentTimeMillis() + firstMessageReceived = true + } + if (topic == "/exit") { + long stopTime = System.currentTimeMillis() + long spentTime = stopTime - m_startTime + println "/exit received" + println "subscriber disconnected, received ${m_numReceived} messages in ${spentTime} ms (from first received to last one on topic /exit)" + client.disconnect() + m_latch.countDown() + } else { + ByteBuffer bb = ByteBuffer.wrap(message.payload) + long sentTime = bb.getLong() + long delay = System.currentTimeMillis() - sentTime + //println "-received in ${delay}(ms) on ${topic} with QoS ${message.qos}" + //distribution[delay] = distribution[delay] != null ? distribution[delay]++ : 1 + if (distribution[delay] != null) { + distribution[delay]++ + } else { + distribution[delay] = 1 + } + m_numReceived++ + } + } + + public void deliveryComplete(IMqttDeliveryToken token) { + //To change body of implemented methods use File | Settings | File Templates. + } +} + + +def publishQos0(client, long numMessages) { + print "publishing.." + long startTime = System.currentTimeMillis() + (1..numMessages).each { + byte[] bytes = ByteBuffer.allocate(8).putLong(System.currentTimeMillis()).array(); + client.publish('topic', bytes, 0, false) + } + client.publish('/exit', 'Exit'.bytes, 0, false) + long stopTime = System.currentTimeMillis() + long spentTime = stopTime -startTime + println "published in ${spentTime} ms" +} + +if (args.size() < 2) { + println "Usage benchmarker " + return +} + +String host = args[0] +long numMessages = args[1] as long +String tmpDir = System.getProperty("java.io.tmpdir") +MqttDefaultFilePersistence dataStore = new MqttDefaultFilePersistence(tmpDir) + +int rnd = (Math.random() * 100) as int +MqttAsyncClient client = new MqttAsyncClient("tcp://${host}:1883", "SubscriberClient${rnd}", dataStore) +def callback = new SubscriberCallback() +client.callback = callback +client.connect().waitForCompletion(1000); +client.subscribe("topic", 0) +println "subscribed to topic" +client.subscribe("/exit", 0) +println "subscribed to /exit" + +publishQos0(client, numMessages) + +callback.waitFinish() +Map sorted = callback.distribution.sort( { k1, k2 -> k1 <=> k2 } as Comparator) +//println prettyPrint(toJson(sorted)) +File csv = new File("perf.csv") +sorted.each {delay, count -> csv << "${delay}, ${count}\r\n"} + + + diff --git a/tools_scripts/index.html b/tools_scripts/index.html new file mode 100644 index 00000000..ca365799 --- /dev/null +++ b/tools_scripts/index.html @@ -0,0 +1,104 @@ + + + + + Moquette performances + + + + + + + + + + + + + + +
+ + + + diff --git a/tools_scripts/launghLocalJetty.groovy b/tools_scripts/launghLocalJetty.groovy new file mode 100644 index 00000000..29a55806 --- /dev/null +++ b/tools_scripts/launghLocalJetty.groovy @@ -0,0 +1,30 @@ +@Grab(group='org.eclipse.jetty.aggregate', module='jetty-server', version='8.1.16.v20140903') +@Grab(group='javax.servlet', module='javax.servlet-api', version='3.0.1') + +import org.eclipse.jetty.server.Server +import org.eclipse.jetty.server.nio.SelectChannelConnector +import org.eclipse.jetty.server.handler.ResourceHandler +import org.eclipse.jetty.server.handler.HandlerList +import org.eclipse.jetty.server.handler.DefaultHandler +import org.eclipse.jetty.server.Handler + +Server server = new Server(); +SelectChannelConnector connector = new SelectChannelConnector(); +connector.setPort(8080); +server.addConnector(connector); + +ResourceHandler resource_handler = new ResourceHandler(); +resource_handler.setDirectoriesListed(true); +//resource_handler.setWelcomeFiles(["index.html"] as new String[0]); + +resource_handler.setResourceBase("."); + +HandlerList handlers = new HandlerList(); +handlers.addHandler(resource_handler) +handlers.addHandler(new DefaultHandler()); +server.setHandler(handlers); + +server.start(); +println "Local server started" + +server.join(); \ No newline at end of file diff --git a/tools_scripts/perf_tmp.csv b/tools_scripts/perf_tmp.csv new file mode 100644 index 00000000..c4e5f643 --- /dev/null +++ b/tools_scripts/perf_tmp.csv @@ -0,0 +1,96 @@ +ms, num +2245, 7 +2246, 14 +2247, 11 +2248, 8 +2249, 8 +2250, 7 +2251, 14 +2252, 10 +2253, 8 +2254, 9 +2255, 8 +2256, 16 +2257, 9 +2258, 8 +2259, 8 +2260, 9 +2261, 16 +2262, 8 +2263, 8 +2264, 8 +2265, 10 +2266, 21 +2267, 19 +2268, 18 +2269, 18 +2270, 20 +2271, 39 +2272, 16 +2273, 18 +2274, 17 +2275, 24 +2276, 26 +2277, 18 +2278, 25 +2279, 17 +2280, 21 +2281, 31 +2282, 10 +2283, 11 +2284, 15 +2285, 10 +2286, 9 +2287, 9 +2288, 13 +2289, 14 +2290, 10 +2291, 17 +2292, 14 +2293, 14 +2294, 16 +2295, 9 +2296, 17 +2297, 9 +2298, 17 +2299, 8 +2300, 16 +2301, 13 +2302, 16 +2303, 15 +2304, 18 +2305, 11 +2306, 16 +2307, 10 +2308, 14 +2309, 12 +2310, 14 +2311, 11 +2312, 15 +2313, 12 +2314, 14 +2315, 9 +2316, 17 +2317, 8 +2318, 15 +2319, 13 +2320, 8 +2321, 18 +2322, 9 +2323, 9 +2324, 14 +2325, 18 +2326, 18 +2327, 19 +2328, 8 +2329, 16 +2330, 11 +2331, 14 +2332, 11 +2333, 17 +2334, 8 +2335, 16 +2336, 10 +2337, 16 +2338, 11 +2339, 15