-
Notifications
You must be signed in to change notification settings - Fork 13
/
measure.js
79 lines (69 loc) · 1.99 KB
/
measure.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
'use strict';
/**
* Small helpr library for creating metrics.
*
* @param {Versions} versions Reference to versions
* @api private
*/
exports.collect = function collect(versions) {
var metrics = Object.create(null, {
/**
* Display the requests per second. The current implementation is wrong. And
* it show shows the average amount of requests for the lifetime of the
* server.
*/
'requests per second': {
get: function requests() {
var seconds = ((Date.now() - start) / 1000).toFixed(0)
, persec = (this.requests / seconds).toFixed(2);
return persec + ' requests per second';
},
enumerable: true
},
/**
* Displays the current size of our internal cache. It's bit flacky as it
* doesn't include the headers and key of the cache. But it gives a clear
* indication
*/
'cache size': {
get: function cachesize() {
var size = 0;
versions.cache.forEach(function forEach(key, value) {
if (value.buffer) size += value.buffer.length;
if (value.gzip) size += value.gzip.length;
});
return (size / 1024).toFixed(2) +'kb';
},
enumerable: true
},
/**
* Displays the memory usage of the current node process.
*
* @TODO format in to bytes
*/
'memory': {
get: function memory() {
var mem = process.memoryUsage();
return mem;
},
enumerable: true
}
});
/**
* Increase a metric. We can attach this directly to the metrics object as
* JSON.stringify does not include functions.
*
* @param {String} counter Name of the counter that we need to increase
* @param {Mixed} meta Meta data
* @returns {Metrics}
* @api public
*/
metrics.incr = function incr(counter, meta) {
if (counter in metrics) metrics[counter]++;
else metrics[counter] = 1;
versions.emit(counter, metrics[counter], meta);
return metrics;
};
var start = Date.now();
return metrics;
};