forked from pubnub/pubnub-rickshaw-memory
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
95 lines (72 loc) · 1.84 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
var pubnub = require('pubnub');
var express = require('express');
var uuid = require('node-uuid');
var colors = require('colors');
var mem = false;
// set defaults
var publish_key = "demo";
var channel = 'pnrickmem-' + uuid.v4();
var interval_timeout = 1000;
var dev_mode = false;
var port = 3345;
// init pubnub
var pubnub = require("pubnub")({
publish_key: publish_key
});
var megabyte = 1024 * 1024;
var interval = false;
var publish_mem = function() {
mem = process.memoryUsage();
// publish to pubnub
pubnub.publish({
channel: channel,
message: {
y: [
Math.ceil(mem.rss / megabyte * 100) / 100,
Math.ceil(mem.heapTotal / megabyte * 100) / 100,
Math.ceil(mem.heapUsed / megabyte * 100) / 100
],
x: new Date().getTime() / 1000
}
});
};
var start = function() {
interval = setInterval(publish_mem, interval_timeout);
};
var stop = function() {
clearInterval(interval);
};
var server = function() {
var app = express();
app.use(express.static(__dirname + '/test'));
app.listen(port);
console.log('');
console.log('----------------------')
console.log('pubnub-rickshaw-memory'.red);
console.log('----------------------')
console.log('');
console.log('Monitor this instance:');
console.log('');
console.log('http://localhost:' + String(port) + '?' + channel.red);
console.log('');
console.log('----------------------')
console.log('');
}
var init = function(options) {
if(typeof options !== "undefined") {
publish_key = options.publish_key || publish_key;
channel = options.channel || channel;
interval_timeout = options.timeout || interval_timeout;
dev_mode = options.dev || dev_mode;
port = options.port || port;
if(dev_mode) {
server();
}
}
start();
};
module.exports = {
start: start,
stop: stop,
init: init
};