Skip to content

Instantly share code, notes, and snippets.

@oberhamsi
Last active August 29, 2015 14:07
Show Gist options
  • Save oberhamsi/d23561f10970f7a9748a to your computer and use it in GitHub Desktop.
Save oberhamsi/d23561f10970f7a9748a to your computer and use it in GitHub Desktop.
// Simple eventsource benchmark server
// sends the template file html/eventsource.html every BROADCAST_TIMEOUT seconds
// to all connected clients. The clients are forcefully disconnected by the server
// after between MIN_COUNTER and MAX_COUNTER seconds (random).
var BROADCAST_TIMEOUT = 10;
var MIN_COUNTER = 5;
var MAX_COUNTER = 10;
var response = require("ringo/jsgi/response");
var arrays = require("ringo/utils/arrays");
var fs = require("fs");
var connections = [];
var message = fs.read(module.resolve("html/eventsource.html"));
exports.app = function(req) {
return response.static(module.resolve("html/eventsource.html"), "text/html");
};
function onconnect(conn) {
conn.addListener("open", function() {
conn.counter = 0;
conn.maxCounter = MIN_COUNTER + Math.floor(Math.random() * (MAX_COUNTER-MIN_COUNTER));
connections.push(conn);
console.info("Opening connection, " + connections.length + " open");
});
conn.addListener("close", function() {
arrays.remove(connections, conn);
console.info("Closing connection, " + connections.length + " remaining");
})
}
function broadcastTime() {
var now = (new Date()).toString();
var msg = now + ':' + message;
connections.forEach(function(conn) {
conn.data(msg);
conn.counter++;
if (conn.counter >= conn.maxCounter) {
conn.close();
}
});
console.info("Broadcasting", now);
setTimeout(broadcastTime, BROADCAST_TIMEOUT * 1000);
}
if (require.main == module) {
var server = require("ringo/httpserver").main(module.id);
server.getDefaultContext().addEventSource("/eventsource", onconnect);
setTimeout(broadcastTime, BROADCAST_TIMEOUT * 1000);
}
#/bin/bash
# Note that the wrk numbers are misleading: you will have a high number of read and timeout errors because this test keeps the connection alive until such a thing happens (either wrk closes the connection, or the server or TCP timeout). The most interesting number is the MBs read. We just need wrk to keep 1000 connections open and retrieve the data the server hands out and it does that.
eventSourceUrl=http://192.168.1.100:8080/eventsource
# long timeout is important
# number of threads <= number of CPU cores on wrk machine
./wrk --timeout 40s --connections 1000 --duration 120s --threads 4 -H "Accept: text/event-stream" -H "Connection: keep-alive" $eventSourceUrl
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment