Skip to content

Commit

Permalink
WIP: Packet generator
Browse files Browse the repository at this point in the history
  • Loading branch information
Mircea Danila Dumitrescu committed May 15, 2014
1 parent f20e3fe commit bd7d324
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 65 deletions.
125 changes: 63 additions & 62 deletions bin/generator.js
Original file line number Diff line number Diff line change
@@ -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)
packetGenerator.computePacketsPerSampleInInterval();
5 changes: 2 additions & 3 deletions lib/cube/aggregator.js
Original file line number Diff line number Diff line change
@@ -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"),
Expand Down Expand Up @@ -472,6 +472,5 @@ exports.register = function (dbs, endpoints, options) {
options.aggregations.forEach(function (aggregationObject) {
validateAndRunAggregation(aggregationObject, 50);
});
}
;
};

0 comments on commit bd7d324

Please sign in to comment.