-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.js
77 lines (60 loc) · 1.55 KB
/
client.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
var request = require('request');
var stats = require('./stats');
var Generator = require('./generator/');
var TOTAL_METHODS = 10;
var TOTAL_PUBSUBS = 10;
var TOTAL_METRICS = 8 + TOTAL_METHODS*9 + TOTAL_PUBSUBS*11;
function Client (appId, appSecret, host, endpoint) {
this.appId = appId;
this.appSecret = appSecret;
this.host = host;
this.endpoint = endpoint;
this.interval = 1000*10;
this.methods = [];
for(var i=0; i<TOTAL_METHODS; ++i) {
this.methods[i] = 'method-' + i;
}
this.pubs = [];
for(var i=0; i<TOTAL_PUBSUBS; ++i) {
this.pubs[i] = 'pub-' + i;
}
this.headers = {
'kadira-app-id': this.appId,
'kadira-app-secret': this.appSecret,
};
}
Client.prototype.start = function() {
var self = this;
var delay = Math.floor(this.interval * Math.random());
setTimeout(function() {
self.send();
setInterval(function() {
self.send();
}, self.interval);
}, delay);
};
Client.prototype.send = function() {
var time = Date.now();
var opts = {
method: 'POST',
url: this.endpoint,
headers: this.headers,
json: true,
body: {
host: this.host,
methodMetrics: Generator.methodMetrics(time, this.methods),
methodRequests: Generator.methodRequests(time, this.methods),
pubMetrics: Generator.pubMetrics(time, this.pubs),
systemMetrics: Generator.systemMetrics(time),
},
};
request(opts, function (err) {
if(err) {
console.error(err);
return
}
stats.inc(TOTAL_METRICS);
stats.dur(Date.now()-time);
});
};
module.exports = Client;