From bd7d32445d8c92432f3a39924c94d8f60d2d0c05 Mon Sep 17 00:00:00 2001 From: Mircea Danila Dumitrescu Date: Thu, 15 May 2014 10:19:14 +0100 Subject: [PATCH] WIP: Packet generator --- bin/generator.js | 125 +++++++++++++++++++++-------------------- lib/cube/aggregator.js | 5 +- 2 files changed, 65 insertions(+), 65 deletions(-) diff --git a/bin/generator.js b/bin/generator.js index 3a32504..dfcd635 100644 --- a/bin/generator.js +++ b/bin/generator.js @@ -1,76 +1,77 @@ "use strict"; var options = require("./generator-config.js"), samples = Math.floor(options.intervalInMS / options.sampleRateInMS), - WebSocket = require("ws"), - ws = new WebSocket('ws://www.host.com/path'); + WS = require("ws"), + ws = new WS('ws://localhost:1080/1.0/event/put'), + randomTools = { -function rnd_snd() { - return (Math.random() * 2 - 1) + (Math.random() * 2 - 1) + (Math.random() * 2 - 1); -} + basicGaussianRandom: function () { + return (Math.random() * 2 - 1) + (Math.random() * 2 - 1) + (Math.random() * 2 - 1); + }, -function rnd(min, max, mean, stdev) { - var x = Math.round(rnd_snd() * stdev + mean); - if (x < min) { - return 0; - } - if (x > max) { - return max; - } -} + gaussianRandom: function (min, max, mean, stdev) { + var x = Math.round(this.basicGaussianRandom() * stdev + mean); + if (x < min) { + return 0; + } + if (x > max) { + return max; + } + }, -function computePacketsPerSampleInInterval(packets, samples) { - var a = [], - i, - mean = options.packetsInInterval / samples, - deviation = mean, - remainingPackets; - remainingPackets = packets; - for (i = 0; i < samples; i++) { - mean = remainingPackets / (samples - i); - a[i] = rnd(mean, deviation); - remainingPackets -= a[i]; - } - return a; -} + getRandomInt: function (min, max) { + return Math.floor(Math.random() * (max - min + 1)) + min; + } + }, -function getRandomInt(min, max) { - return Math.floor(Math.random() * (max - min + 1)) + min; -} + packetGenerator = { + computePacketsPerSampleInInterval: function (packets, samples) { + var a = [], + i, + mean = options.packetsInInterval / samples, + deviation = mean, + remainingPackets; + remainingPackets = packets; + for (i = 0; i < samples; i++) { + mean = remainingPackets / (samples - i); + a[i] = randomTools.gaussianRandom(0, remainingPackets, mean, deviation); + remainingPackets -= a[i]; + } + return a; + }, + sendPacket: function () { + var country = options.data.country[randomTools.getRandomInt(0, options.data.country.length - 1)], + gender = options.data.gender[randomTools.getRandomInt(0, options.data.gender.length - 1)], + device = options.data.device[randomTools.getRandomInt(0, options.data.device.length - 1)], + type = options.data.type[randomTools.getRandomInt(0, options.data.device.type - 1)]; + ws.send( + JSON.stringify({ + type: type, + data: { + v1: gender, + v2: device, + v3: country + }}) + ); + }, -function sendPacket() { - var country = options.data.country[getRandomInt(0, options.data.country.length - 1)], - gender = options.data.gender[getRandomInt(0, options.data.gender.length - 1)], - device = options.data.device[getRandomInt(0, options.data.device.length - 1)], - type = options.data.type[getRandomInt(0, options.data.device.type - 1)], - ws = new WebSocket('ws://localhost:1080/1.0/event/put'); + sendPackets: function (n) { + var i; + for (i = 0; i < n; i++) { + this.sendPacket(); + } + }, - ws.send( - JSON.stringify({ - type: type, - data: { - v1: gender, - v2: device, - v3: country - }}) - ); -} - -function sendPackets(n) { - var i; - for (i = 0; i < n; i++) { - sendPacket(); - } -} - -function sendForIntervals(a) { - var i; - for (i = 0; i < a.length; i++) { - setTimeout(sendPackets(a[i]), i * options.sampleRateInMS); - } -} + sendForIntervals: function (a) { + var i; + for (i = 0; i < a.length; i++) { + setTimeout(this.sendPackets(a[i]), i * options.sampleRateInMS); + } + } + }; ws.on('open', function () { console.log("connected!"); }); -sendForIntervals(computePacketsPerSampleInInterval) \ No newline at end of file +packetGenerator.computePacketsPerSampleInInterval(); diff --git a/lib/cube/aggregator.js b/lib/cube/aggregator.js index 15c684e..a941f37 100644 --- a/lib/cube/aggregator.js +++ b/lib/cube/aggregator.js @@ -1,6 +1,6 @@ "use strict"; /* - * Aggregation have fixed times of execution: 1m, 5m and 1h + * Aggregations have fixed times of execution: 1m, 5m and 1h */ var util = require('util'), myutils = require("./myutils.js"), @@ -472,6 +472,5 @@ exports.register = function (dbs, endpoints, options) { options.aggregations.forEach(function (aggregationObject) { validateAndRunAggregation(aggregationObject, 50); }); -} -; +};