This repository has been archived by the owner on Dec 5, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 141
/
Copy pathindex.html
199 lines (189 loc) · 7.54 KB
/
index.html
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
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<link href="data:image/x-icon;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQEAYAAABPYyMiAAAABmJLR0T///////8JWPfcAAAACXBIWXMAAABIAAAASABGyWs+AAAAF0lEQVRIx2NgGAWjYBSMglEwCkbBSAcACBAAAeaR9cIAAAAASUVORK5CYII="
rel="icon" type="image/x-icon" />
<script>
(function (Pipe, perf) {
if (Pipe === undefined) {
return;
}
if (!('mark' in perf && 'measure' in perf)) {
return;
}
var fragmentMap = Object.create(null);
var doneGroups = Object.create(null);
function getCount(range) {
return range[1] - range[0] + 1;
}
/**
* Elapsed time from browsers navigation start
*/
function elapsedTime() {
return perf.now() || Date.now() - perf.timing.navigationStart;
}
function addGroupEntries() {
for (var name in doneGroups) {
var duration = doneGroups[name];
if (duration !== undefined) {
Pipe.addPerfEntry(name, duration);
}
}
}
/**
* Group all the timing groups associated with all fragments
* and check if the scripts are done executing
*
* If there are any fragment that gets lazily rendered then we need to
* account for the scripts from that fragment as well
*
* Measure -> time from navigation start - all scripts from
* fragments associated with given timing groups are done executing
*/
function measureGroups(groups) {
if (groups.length < 1) {
return;
}
for (var i = 0; i < groups.length; i++) {
var groupName = groups[i];
var isGroupDone = true;
var keys = Object.keys(fragmentMap);
for (var j = 0; j < keys.length; j++) {
var fragment = fragmentMap[keys[j]];
if (
fragment.groups.indexOf(groupName) >= 0 &&
fragment.count > 0
) {
isGroupDone = false;
break;
}
}
if (isGroupDone) {
// Keep updating the timings for the group to account for
// fragments that are streamed later
doneGroups[groupName.trim()] = elapsedTime();
}
}
}
Pipe.onStart(function (attributes) {
var id = attributes.id;
if (fragmentMap[id] === undefined) {
var count = getCount(attributes.range);
/*
* count - denotes the number of script tags
* marked - flag to check if fragment started executing
* measure - flag to measure sripts and fragments differently
* groups - timing groups associated with a fragment
*/
fragmentMap[id] = {
count: count,
marked: false,
measure: count !== 1,
groups: attributes.timingGroups
};
}
});
Pipe.onBeforeInit(function (attributes, index) {
var id = attributes.id;
var fragment = fragmentMap[id];
/**
* Mark only once even if fragments send multiple scripts
* Helps to capture the overall time from start till all the
* scripts are executed from fragment
*
* Includes network time of other scripts as well.
*/
if (!fragment.marked) {
fragment.marked = true;
perf.mark(id);
}
/**
* Mark for each script tag from the fragments
* Helps us to track how much time is spent executing
* each script on the fragments
*
* Includes only the execution time of the exported function
* in the script
*/
if (fragment.measure) {
perf.mark(id + index);
}
});
Pipe.onAfterInit(function (attributes, index) {
var id = attributes.id;
var timingGroups = attributes.timingGroups;
var fragment = fragmentMap[id];
// Measure for each script per fragment only if
// the script count is more than 1 per fragment
var markStart = '',
markEnd = '',
measureName = '';
if (fragment.measure) {
markStart = id + index;
markEnd = markStart + 'end';
measureName = 'fragment-' + id + fragment.count;
perf.mark(markEnd);
perf.measure(measureName, markStart, markEnd);
}
fragment.count -= 1;
/**
* Measures from the first script tag execution to the last one
* for a single fragment
*
* fragment-{name} -> start to end
*/
if (fragment.count === 0) {
markStart = id;
markEnd = markStart + 'end';
measureName = 'fragment-' + id;
perf.mark(markEnd);
perf.measure(measureName, markStart, markEnd);
// Measure when primary fragment is done
if (attributes.primary) {
perf.measure('primary-done');
}
// Measure if fragments assosiated with given timing groups are done
measureGroups(timingGroups);
}
});
Pipe.onDone(function () {
/**
* Create performance entries for all the collected metrics from
* timing groups
*/
addGroupEntries();
/**
* Measure when all the script tags from fragments
* are done executing on the page
*/
perf.measure('all-done');
});
Pipe.onDone(() => {
perf.measure('all-done-2');
});
})(window.TailorPipe, window.performance);
</script>
<script>
define('js1', function () {
return 'js1';
});
define('js2', function () {
return 'js2';
});
define('js3', function () {
return 'js3';
});
</script>
</head>
<body>
<div>
<h2>Header:</h2>
<fragment timing-group="abovethefold,interactive" id="header" src="http://localhost:8081"></fragment>
<h2>Product/Primary:</h2>
<fragment timing-group="interactive" id="product" primary src="http://localhost:8082"></fragment>
<h2>Async Footer:</h2>
<fragment id="footer" timing-group="belowthefold" async src="http://localhost:8083"></fragment>
</div>
</body>
</html>