-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Mircea Danila Dumitrescu
committed
May 15, 2014
1 parent
f20e3fe
commit bd7d324
Showing
2 changed files
with
65 additions
and
65 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters