Permalink
Browse files

Rewrite the random emitter as a random walk.

  • Loading branch information...
1 parent 683bb5b commit 6dfc0dd1a6dcba1d7e6308da1204961310bbbdfd @mbostock mbostock committed Apr 20, 2012
Showing with 37 additions and 40 deletions.
  1. +13 −2 examples/emitter/random/random-config.js
  2. +24 −38 examples/emitter/random/random.js
@@ -1,5 +1,16 @@
-// Default configuration for development.
module.exports = {
+
+ // The collector to send events to.
"http-host": "127.0.0.1",
- "http-port": 1080
+ "http-port": 1080,
+
+ // The offset and duration to backfill, in milliseconds.
+ // For example, if the offset is minus four hours, then the first event that
+ // the random emitter sends will be four hours old. It will then generate more
+ // recent events based on the step interval, all the way up to the duration.
+ "offset": -4 * 60 * 60 * 1000,
+ "duration": 8 * 60 * 60 * 1000,
+
+ // The time between random events.
+ "step": 1000 * 10
};
@@ -1,44 +1,30 @@
+process.env.TZ = 'UTC';
+
var util = require("util"),
- cube = require("../../../"),
- options = require("./random-config"),
- count = 0,
- batch = 10,
- hour = 60 * 60 * 1000,
- start = Date.now(),
- offset = -Math.abs(random()) * 24 * hour;
+ cube = require("../../../"), // replace with require("cube")
+ options = require("./random-config");
-// Connect to websocket.
util.log("starting websocket client");
-var client = cube.emitter().open(options["http-host"], options["http-port"]);
-
-// Emit random values.
-var interval = setInterval(function() {
- for (var i = -1; ++i < batch;) {
- client.send({
- type: "random",
- time: new Date(Date.now() + random() * 2 * hour + offset),
- data: {random: (offset & 1 ? 1 : -1) * Math.random()}
- });
- count++;
- }
- var duration = Date.now() - start;
- console.log(count + " events in " + duration + " ms: " + Math.round(1000 * count / duration) + " sps");
-}, 10);
+var emitter = cube.emitter().open(options["http-host"], options["http-port"]);
-// Display stats on shutdown.
-process.on("SIGINT", function() {
- console.log("stopping websocket client");
- client.close();
- clearInterval(interval);
-});
+var start = Date.now() + options["offset"],
+ stop = start + options["duration"],
+ step = options["step"],
+ value = 0,
+ count = 0;
-// Sample from a normal distribution with mean 0, stddev 1.
-function random() {
- var x = 0, y = 0, r;
- do {
- x = Math.random() * 2 - 1;
- y = Math.random() * 2 - 1;
- r = x * x + y * y;
- } while (!r || r > 1);
- return x * Math.sqrt(-2 * Math.log(r) / r);
+while (start < stop) {
+ emitter.send({
+ type: "random",
+ time: new Date(start),
+ data: {
+ value: value += Math.random() - .5
+ }
+ });
+ start += step;
+ ++count;
}
+
+util.log("sent " + count + " events");
+util.log("stopping websocket client");
+emitter.close();

0 comments on commit 6dfc0dd

Please sign in to comment.