forked from zeman/perfmap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
perfmap.js
193 lines (178 loc) · 7.29 KB
/
perfmap.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
var gZeroLeft = 0;
var gZeroTop = 0;
var gWinWidth = window.innerWidth || document.documentElement.clientWidth;
function findImages() {
var aElems = document.getElementsByTagName('*');
var re = /url\((http.*)\)/ig;
for ( var i=0, len = aElems.length; i < len; i++ ) {
var elem = aElems[i];
var style = window.getComputedStyle(elem);
var url = elem.src || elem.href;
var hasImage = 0;
var body = 0;
re.lastIndex = 0; // reset state of regex so we catch repeating spritesheet elements
if (elem.tagName == 'IMG') {
hasImage = 1;
}
if (style['background-image']) {
var backgroundImage = style['background-image'];
var matches = re.exec(style['background-image']);
if (matches && matches.length > 1){
url = backgroundImage.substring(4);
url = url.substring(0, url.length - 1);
hasImage = 1;
if(elem.tagName == 'BODY'){
body = 1;
}
}
}
if(hasImage == 1){
if ( url ) {
var entry = performance.getEntriesByName(url)[0];
if ( entry ) {
var xy = getCumulativeOffset(elem);
var wh = elem.getBoundingClientRect();
var width = wh.width;
var height = wh.height;
if(width > 10){
if(height > 10){
placeMarker(xy, width, height, entry, body);
}
}
}
}
}
}
}
function placeMarker(xy, width, height, entry, body) {
var heat = entry.responseEnd / loaded;
// adjust size of fonts/padding based on width of overlay
if(width < 170){
var padding = 12;
var size = 12;
}else if(width > 400){
var padding = 13;
var size = 26;
}else{
var padding = 9;
var size = 18;
}
// adjust opacity if it's the body element and position label top right
if(body == 1){
var opacity = 0.7;
var size = 18;
var align = "right";
var paddingTop = 10;
var bodyText = "BODY ";
}else{
var opacity = 0.95;
var align = "center";
var paddingTop = (height/2)-padding;
var bodyText = "";
}
var marker = document.createElement("div");
marker.className = "perfmap";
marker.setAttribute("data-ms", parseInt(entry.responseEnd));
marker.style.cssText = "position: absolute; box-sizing: border-box; color: #fff; padding-left:10px; padding-right:10px; line-height:14px; font-size: " + size + "px; font-weight:800; font-family:\"Helvetica Neue\",sans-serif; text-align:" + align + "; opacity: " + opacity + "; " + heatmap(heat) + " top: " + xy.top + "px; left: " + xy.left + "px; width: " + width + "px; height:" + height + "px; padding-top:" + paddingTop + "px; z-index: 4000;";
if(width > 50){
if(height > 15 ){
marker.innerHTML = bodyText + parseInt(entry.responseEnd) + "ms (" + parseInt(entry.duration) + "ms)";
}
}
document.body.appendChild(marker);
if ( 0 == xy.top ) {
gZeroLeft += marker.offsetWidth + 10;
if ( gZeroLeft + 100 > gWinWidth ) {
gZeroTop += 30;
gZeroLeft = 0;
}
}
}
function prettyType(type) {
return ( "link" == type ? "stylesheet" : type );
}
function heatmap(heat) {
if ( heat < 0.16 ) {
return "background: #1a9850;"
}
else if ( heat < 0.32 ) {
return "background: #66bd63;"
}
else if ( heat < 0.48 ) {
return "background: #a6d96a;"
}
else if ( heat < 0.64 ) {
return "background: #fdae61;"
}
else if ( heat < 0.8 ) {
return "background: #f46d43;"
}else{
return "background: #d73027;"
}
}
function typeCss(type) {
if ( "img" == type ) {
return "background: #C00;"
}
else if ( "script" == type ) {
return "background: #0C0;"
}
else if ( "link" == type ) {
return "background: #00C;"
}
}
function getCumulativeOffset(obj) {
var left, top, width;
left = top = 0;
if (obj.offsetParent) {
do {
left += obj.offsetLeft;
top += obj.offsetTop;
width += obj.offsetWidth;
} while (obj = obj.offsetParent);
}
if ( 0 == top ) {
left += gZeroLeft;
top += gZeroTop;
}
return {
left: left,
top: top,
width: width,
};
}
// get full page load time to calculate heatmap max
var loaded = performance.timing.loadEventEnd - performance.timing.navigationStart;
// backend
var backend = performance.timing.responseEnd - performance.timing.navigationStart;
var backendLeft = (backend / loaded)*100;
// first paint in chrome from https://github.com/addyosmani/timing.js
var paint = window.chrome.loadTimes().firstPaintTime * 1000;
var firstPaint = paint - (window.chrome.loadTimes().startLoadTime*1000);
var firstPaintLeft = (firstPaint / loaded)*100;
// remove any exisiting "perfmap" divs on second click
var elements = document.getElementsByClassName("perfmap");
while(elements.length > 0){
elements[0].parentNode.removeChild(elements[0]);
}
// build bottom legend
var perfmap = document.createElement("div");
perfmap.id = "perfmap";
perfmap.style.cssText = "position: fixed; width:100%; bottom:0; left:0; z-index:5000; height: 30px; background-color:#000";
perfmap.innerHTML = "<div style='width:16.666666667%; height: 50px; float:left; background-color:#1a9850;'></div><div style='width:16.666666667%; height: 50px; float:left; background-color:#66bd63;'></div><div style='width:16.666666667%; height: 50px; float:left; background-color:#a6d96a;'></div><div style='width:16.666666667%; height: 50px; float:left; background-color:#fdae61;'></div><div style='width:16.666666667%; height: 50px; float:left; background-color:#f46d43;'></div><div style='width:16.666666667%; height: 50px; float:left; background-color:#d73027;'></div><div style='position:absolute; z-index:2; right:0px; padding-top:5px; color:#fff; font-size:14px; font-weight:800; padding-right:10px;height:100%;'>Fully Loaded " + parseInt(loaded) + "ms</div><div style='position:absolute; z-index:3; left:" + firstPaintLeft + "%; padding-top:5px; color:#fff; font-size:14px; font-weight:800; border-left:2px solid white;padding-left:5px;height:100%;'>First Paint " + parseInt(firstPaint) + "ms</div><div id='perfmap-timeline' style='position:absolute; z-index:4; left:-100px; padding-top:5px; color:#fff; font-size:14px; font-weight:800; border-left:2px solid white;padding-left:5px;height:100%;'></div>";
document.body.appendChild(perfmap);
// build heatmap
findImages();
// mouse events to move timeline around on hover
var elements = document.getElementsByClassName("perfmap");
var timeline = document.getElementById('perfmap-timeline');
for ( var i=0, len = elements.length; i < len; i++ ) {
elements[i].onmouseover = function(){
var timelineLeft = document.documentElement.clientWidth * (this.dataset.ms / loaded);
timeline.style.cssText = "opacity:1; transition: 0.5s ease-in-out; transform: translate("+ parseInt(timelineLeft) + "px,0); position:absolute; z-index:4; border-left:2px solid white; height:100%;";
}
elements[i].onmouseout = function(){
var timelineLeft = document.documentElement.clientWidth * (this.dataset.ms / loaded);
timeline.style.cssText = "opacity:0; transition: 0.5s ease-in-out; transform: translate("+ parseInt(timelineLeft) + "px,0); position:absolute; z-index:4; border-left:2px solid white; height:100%;";
}
}