-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmonitorjs.js
More file actions
171 lines (171 loc) · 4.39 KB
/
Copy pathmonitorjs.js
File metadata and controls
171 lines (171 loc) · 4.39 KB
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
var Monitor = (function(){
var Monitor = {};
var data = {};
// Start a record
Monitor.Start = function(name) {
if (typeof name === "undefined") {
try {
// if no name, try to retrieve caller
// may be impossible in strict mode
name = arguments.callee.caller.name.toString();
}
catch(e) {
throw "Monitor.Start : You must provide a referring name as first argument";
}
}
if (!data[name]) {
data[name] = {
// total time
tt: 0,
// call count
cc: 0,
// timer queue
queue: [],
// next timer id
// Note: start at 1 to avoid identifier.id == false
nextId: 1
};
}
var id = data[name].nextId++;
data[name].queue.push({
id: id,
ct: performance.now()
});
return {
name: name,
id: id
};
};
// Stop a record
Monitor.Stop = function(input) {
var stoppedAt = performance.now();
var typeInput = typeof input;
if (typeInput === "undefined") {
try {
// if no name, try to retrieve caller
// may be impossible in strict mode
input = arguments.callee.caller.name.toString();
}
catch(e) {
throw "Monitor.Stop : You must provide a referring name as first argument";
}
}
var record = null, name;
// if only name given
if (typeInput === "string") {
name = input;
if (!data[name] || data[name].queue.length === 0) {
throw "Called Monitor.Stop before Monitor.Start"
}
record = data[name].queue.shift();
}
// if record identifier given
else if (typeInput === "object") {
if (!input.name || !input.id) {
throw "Monitor.Stop: input should be a string or an object returned by Monitor.Start";
}
name = input.name;
if (!data[name] || data[name].queue.length === 0) {
throw "Called Monitor.Stop before Monitor.Start"
}
for (var i = 0; i < data[name].queue.length; i++) {
if (data[name].queue[i].id === input.id) {
// return & remove from queue
record = data[name].queue.splice(i,1)[0];
break;
}
}
if (record === null) {
throw "Monitor.Stop: Unable to find record with provided identifier";
}
}
data[name].tt += stoppedAt - record.ct;
data[name].cc++;
}
// Cancel a record
Monitor.Cancel = function(input) {
var typeInput = typeof input;
if (typeInput === "undefined") {
try {
// if no name, try to retrieve caller
// may be impossible in strict mode
input = arguments.callee.caller.name.toString();
}
catch(e) {
throw "Monitor.Cancel : You must provide a referring name as first argument";
}
}
// if only name given
if (typeInput === "string") {
if (!data[input] || data[input].queue.length === 0) {
throw "Called Monitor.Cancel before Monitor.Start"
}
data[input].queue.shift();
}
// if record identifier given
else if (typeInput === "object") {
if (!input.name || !input.id) {
throw "Monitor.Cancel: input should be a string or an object returned by Monitor.Start";
}
if (!data[input.name] || data[input].queue.length === 0) {
throw "Called Monitor.Cancel before Monitor.Start"
}
var record = null;
for (var i = 0; i < data[input.name].queue.length; i++) {
if (data[input.name].queue[i].id === input.id) {
// return & remove from queue
record = data[input.name].queue.splice(i,1)[0];
break;
}
}
if (record === null) {
throw "Monitor.Cancel: Unable to find record with provided identifier";
}
}
}
// Get a record
Monitor.Get = function(name) {
// for this function user must specify what is wants
if (typeof name === "undefined") {
throw "Monitor.Get: You must specify the record reference";
}
if (!data[name]) {
console.error('Monitor: no records for "' + name + '"');
return null;
}
return {
name: name,
total_time: data[name].tt,
call_count: data[name].cc
};
}
// Get a record
Monitor.GetAll = function() {
var results = new Array();
for (var i in data) {
results.push({
name: i,
total_time: data[i].tt,
call_count: data[i].cc
});
}
return results;
}
// Delete a record
Monitor.Clear = function(name) {
// for this function user must specify what is wants
if (typeof name === "undefined") {
throw "Monitor.Clear: You must specify the record reference";
}
if (!data[name]) {
console.error('Monitor: no records for "' + name + '"');
return null;
}
delete data[name];
}
// Clear all logged informations
Monitor.ClearAll = function() {
data = {};
}
return Monitor;
}());