Added some benchmariking utils: benchmarker produces tiing csv and an html using Highcharts JS lib

This commit is contained in:
selva.andre
2014-09-19 21:51:24 +02:00
parent 1a66bc4645
commit cc45b87ebe
4 changed files with 342 additions and 0 deletions
+112
View File
@@ -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<Long, Long> 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 <host> <numMessages>"
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"}
+104
View File
@@ -0,0 +1,104 @@
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Moquette performances</title>
<!-- 1. Add these JavaScript inclusions in the head of your page -->
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script type="text/javascript" src="http://code.highcharts.com/highcharts.js"></script>
<!-- 2. Add the JavaScript to initialize the chart on document ready -->
<script type="text/javascript">
$(document).ready(function() {
var options = {
chart: {
renderTo: 'container',
type: 'line'
},
title: {
text: 'Moquette ping-pong Qos0'
},
xAxis: {
categories: [],
tickInterval: 100
},
yAxis: {
title: {
text: 'num msgs'
}
},
series: []
};
/*
Load the data from the CSV file. This is the contents of the file:
Apples,Pears,Oranges,Bananas,Plums
John,8,4,6,5
Jane,3,4,2,3
Joe,86,76,79,77
Janet,3,16,13,15
*/
//$.get('file:///home/andrea/workspace/moquette/tools_scripts/perf_1000.csv', function(data) {
$.get('perf_tmp.csv', function(data) {
// Split the lines
var lines = data.split('\n');
var series = {
name: 'Bira',
//data: [1, 2, 4, 5, 6]
data: [/*[100,1], [200, 2], [300, 4], [400, 5], [500, 6]*/]
};
$.each(lines, function(lineNo, line) {
var items = line.split(',');
if (lineNo > 0) {
var tick = parseFloat(items[0]);
var value = parseFloat(items[1]);
if (!isNaN(tick)) {
series.data.push([tick, value]);
}
}
// header line containes categories
/*if (lineNo == 0) {
$.each(items, function(itemNo, item) {
if (itemNo > 0) options.xAxis.categories.push(item);
});
}*/
// the rest of the lines contain data with their name in the first position
/*else {
$.each(items, function(itemNo, item) {
if (itemNo == 0) {
series.name = item;
} else {
series.data.push(parseFloat(item));
});
//options.series.push(series);
}*/
});
options.series.push(series);
var chart = new Highcharts.Chart(options);
});
});
</script>
</head>
<body>
<!-- 3. Add the container -->
<div id="container" style="width: 800px; height: 400px; margin: 0 auto"></div>
</body>
</html>
+30
View File
@@ -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();
+96
View File
@@ -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
1 ms num
2 2245 7
3 2246 14
4 2247 11
5 2248 8
6 2249 8
7 2250 7
8 2251 14
9 2252 10
10 2253 8
11 2254 9
12 2255 8
13 2256 16
14 2257 9
15 2258 8
16 2259 8
17 2260 9
18 2261 16
19 2262 8
20 2263 8
21 2264 8
22 2265 10
23 2266 21
24 2267 19
25 2268 18
26 2269 18
27 2270 20
28 2271 39
29 2272 16
30 2273 18
31 2274 17
32 2275 24
33 2276 26
34 2277 18
35 2278 25
36 2279 17
37 2280 21
38 2281 31
39 2282 10
40 2283 11
41 2284 15
42 2285 10
43 2286 9
44 2287 9
45 2288 13
46 2289 14
47 2290 10
48 2291 17
49 2292 14
50 2293 14
51 2294 16
52 2295 9
53 2296 17
54 2297 9
55 2298 17
56 2299 8
57 2300 16
58 2301 13
59 2302 16
60 2303 15
61 2304 18
62 2305 11
63 2306 16
64 2307 10
65 2308 14
66 2309 12
67 2310 14
68 2311 11
69 2312 15
70 2313 12
71 2314 14
72 2315 9
73 2316 17
74 2317 8
75 2318 15
76 2319 13
77 2320 8
78 2321 18
79 2322 9
80 2323 9
81 2324 14
82 2325 18
83 2326 18
84 2327 19
85 2328 8
86 2329 16
87 2330 11
88 2331 14
89 2332 11
90 2333 17
91 2334 8
92 2335 16
93 2336 10
94 2337 16
95 2338 11
96 2339 15