-
Notifications
You must be signed in to change notification settings - Fork 5
/
polytiming.js
205 lines (168 loc) · 5.39 KB
/
polytiming.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
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
(function() {
'use strict';
function setOfQueryParams(paramName) {
return new Set(window.location.search.slice(1).split('&')
.map(function(part) {
return part.split('=');
}).reduce(function(l, r) {
if (r[0] === paramName) {
return l.concat(decodeURIComponent(r[1]).split(','));
}
return l;
}, []));
}
const measuredElements = new Set();
const measuredMethods = new Set();
const configuredMethods = setOfQueryParams('instrumentPolymer');
const trackedElements = setOfQueryParams('trackElement');
const recordedMetrics = setOfQueryParams('recordMetric')
let shouldTrackElement = (elementName) => true;
if (trackedElements.size > 0) {
shouldTrackElement = (elementName) => {
return trackedElements.has(elementName);
}
}
let AuthenticPolymer;
let AuthenticBase;
let AuthenticTemplatizer;
Object.defineProperty(window, 'Polymer', {
get: function() {
return AuthenticPolymer;
},
set: function(Polymer) {
AuthenticPolymer = Polymer;
Object.defineProperty(Polymer, 'Base', {
get: function() {
return AuthenticBase;
},
set: function(Base) {
AuthenticBase = Base;
}
});
Object.defineProperty(Polymer, 'Templatizer', {
get: function() {
return AuthenticTemplatizer;
},
set: function(Templatizer) {
AuthenticTemplatizer = Templatizer;
instrumentLifecycle(AuthenticBase, configuredMethods);
}
});
}
});
function measuredMethod(name, work) {
measuredMethods.add(name);
return function() {
let element = this.is || this.tagName;
let elementName = `${element}-${name}`;
let startMark = `${elementName}-start`;
let endMark = `${elementName}-end`;
let result;
if (!shouldTrackElement(element)) {
return work.apply(this, arguments);
}
measuredElements.add(element);
window.performance.mark(startMark);
result = work.apply(this, arguments);
window.performance.mark(endMark);
window.performance.measure(elementName, startMark, endMark);
return result;
};
}
function instrumentLifecycle(proto, methods) {
for (const method of methods) {
proto[method] = measuredMethod(method, proto[method]);
}
}
function statsForElementMethod(element, method) {
let measures = window.performance.getEntriesByName(`${element}-${method}`);
let count = measures.length;
let sum = measures.reduce(function(sum, measure) {
return sum + measure.duration;
}, 0);
let average = sum ? sum / count : 0;
return { count, average, sum };
}
function statsForMethod(method) {
let count = 0;
let sum = 0;
let average = 0;
measuredMethods.forEach(function(_method) {
if (method === _method) {
measuredElements.forEach(function(element) {
let stats = statsForElementMethod(element, method)
sum += stats.sum;
count += stats.count;
});
};
});
average = count > 0 ? sum / count : average;
return { count, average, sum };
}
window.console.polymerTimingCsv = function() {
let header = 'element';
measuredMethods.forEach(function(method) {
header = `${header},${method} #,${method} avg. ms,${method} total`;
});
let rows = header;
measuredElements.forEach(function(element) {
let row = element;
measuredMethods.forEach(function(method) {
let stats = statsForElementMethod(element, method);
Object.keys(stats).forEach(function(stat) {
row = `${row},${stats[stat]}`;
});
});
rows = `${rows}\n${row}`;
});
console.log(rows);
};
window.console.polymerTiming = function() {
let elementData = {};
measuredElements.forEach(function(element) {
let methodData = {};
measuredMethods.forEach(function(method) {
let stats = statsForElementMethod(element, method);
methodData[`${method} #`] = stats.count;
methodData[`${method} avg. ms`] = stats.average;
methodData[`${method} total`] = stats.sum;
});
elementData[element] = methodData;
});
let totals = {};
measuredMethods.forEach(function(method) {
totals[method] = statsForMethod(method);
});
console.table(totals);
console.table(elementData);
}
if (recordedMetrics.size) {
window.addEventListener('load', function() {
window.setTimeout(function() {
recordedMetrics.forEach(measure => {
var entries = window.performance.getEntriesByName(measure);
if (!entries.length) {
console.warn(`No User Timing entries found for ${measure}!`);
return;
}
try {
var recorded = JSON.parse(localStorage.getItem(measure));
} catch (e) {}
recorded = recorded || [];
entries.forEach(entry => {
if (entry.entryType == 'mark') {
recorded.push(entry.startTime);
} else {
recorded.push(entry.duration);
}
});
console.log(`${recorded.length} records for ${measure}: ${recorded}`);
try {
localStorage.setItem(measure, JSON.stringify(recorded));
} catch (e) {}
});
console.log('Finished recording measures.');
}, 1000);
});
}
})();