Skip to content

Instantly share code, notes, and snippets.

@vigneshshanmugam
Created April 4, 2018 14:29
Show Gist options
  • Save vigneshshanmugam/1498585bd425834840aae341383a9972 to your computer and use it in GitHub Desktop.
Save vigneshshanmugam/1498585bd425834840aae341383a9972 to your computer and use it in GitHub Desktop.
Hooks
'use strict';
(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;
}
/**
* Group all the timing groups associated with all fragments
* and check if the scripts are done executing
*
* Meausre -> 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;
// early exit if the timing group is already done
if (doneGroups[groupName] === true) {
continue;
}
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) {
doneGroups[groupName] = true;
perf.measure(groupName.trim());
}
}
}
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() {
/**
* Measure when all the script tags from fragments
* are done executing on the page
*/
perf.measure('all-done');
});
})(window.TailorPipe, window.performance);
'use strict';
(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;
}
/**
* Group all the timing groups associated with all fragments
* and check if the scripts are done executing
*
* Meausre -> 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;
// early exit if the timing group is already done
if (doneGroups[groupName] === undefined) {
continue;
}
var keys = Object.keys(fragmentMap);
for (var j = 0; j < keys.length; j++) {
var fragment = fragmentMap[keys[j]];
var groups = fragment.groups;
if (groups.indexOf(groupName) >= 0 && fragment.count > 0) {
isGroupDone = false;
break;
}
}
if (isGroupDone) {
doneGroups[groupName] = true;
perf.measure(groupName.trim());
}
}
}
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() {
/**
* Measure when all the script tags from fragments
* are done executing on the page
*/
perf.measure('all-done');
});
})(window.TailorPipe, window.performance);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment