-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
140 lines (106 loc) · 2.69 KB
/
index.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
"use strict";
var Timer = require("timer-machine-node");
var slugify = require("slugify");
// SERVER - TIMING
class ServerTiming {
// CONSTRUCTOR
constructor() {
this.metrics = {};
this.times = {};
}
// START TIMER
startTimer(name) {
// slugify name
var slug = slugify(name).toLowerCase();
if (!slug || slug.length === 0) return false;
this.metrics[slug] = name;
// start timer
var timer = Timer.get(slug);
timer.start();
var self = this;
return {
stop() {
// check if timer even exists
if (self.metrics && !(slug in self.metrics)) return false;
// already stopped and recorded
if (slug in self.times) return self.times[slug];
// stop timer
Timer.get(slug).stop();
// get time
self.times[slug] = Timer.get(slug).time();
Timer.destroy(slug);
return self.times[slug];
},
clear() {
// check if timer even exists
if (self.metrics && !(slug in self.metrics)) return false;
// stop and destory timer
Timer.get(slug).stop();
Timer.destroy(slug);
// delete references to self slug
delete self.metrics[slug];
delete self.times[slug];
return true;
}
};
}
// STOP TIMER
stopTimer(name) {
// slugify name
var slug = slugify(name).toLowerCase();
if (!slug || slug.length === 0) return false;
// check if timer even exists
if (this.metrics && !(slug in this.metrics)) return false;
// stop timer
Timer.get(slug).stop();
// get time
this.times[slug] = Timer.get(slug).time();
Timer.destroy(slug);
return this.times[slug];
}
// CLEAR TIMER
clearTimer(name) {
// slugify name
var slug = slugify(name).toLowerCase();
if (!slug || slug.length === 0) return false;
// check if timer even exists
if (this.metrics && !(slug in this.metrics)) return false;
// stop and destory timer
Timer.get(slug).stop();
Timer.destroy(slug);
// delete references to this slug
delete this.metrics[slug];
delete this.times[slug];
return true;
}
// ADD METRIC
addMetric(name, value) {
// slugify name
var slug = slugify(name).toLowerCase();
if (!slug || slug.length === 0) return false;
var parsedValue = parseFloat(value);
// check if value is not a number
if (isNaN(parsedValue)) return false;
// add metric manually
this.metrics[slug] = name;
this.times[slug] = parsedValue;
return true;
}
// GENERATE HEADER
generateHeader() {
var header = "";
// loop the metrics
Object.keys(this.metrics).forEach(slug => {
header +=
slug +
"; dur=" +
this.times[slug] +
'; desc="' +
this.metrics[slug] +
'",';
});
// remove trailing comma and return header string
return header.replace(/,\s*$/, "");
}
}
module.exports = ServerTiming;