Skip to content

Instantly share code, notes, and snippets.

@ekashida
Last active January 4, 2016 21:19
Show Gist options
  • Select an option

  • Save ekashida/8679996 to your computer and use it in GitHub Desktop.

Select an option

Save ekashida/8679996 to your computer and use it in GitHub Desktop.
setup for perf testing yui loader
function getTime () {
return new Date().getTime();
}
function patchLoader (Y, loader) {
var YUI = Y.config.global.YUI,
seq = 0,
methods,
current,
perf,
orig,
len,
i;
perf = YUI.Env.perf = {
YUI_start: Y.config.YUI_start,
first_Get: null,
YUI_instantiation_to_first_Get: null
};
perf.instantiation = {};
// Generic way to instrument a function. Uses sequencing to support
// multiple invocations during a single `Y.use()` call.
function instrument (obj, name) {
var orig = obj[name];
obj[name] = function () {
var now = getTime(),
key = name + '__' + seq++,
contextTimers,
methodTimers,
value;
// If a `use` statement isn't in the current stack, then its timer
// obj isn't available. Use the instantiation timer obj instead.
contextTimers = current || perf.instantiation;
// Initialize the method count object
contextTimers.count = contextTimers.count || {};
// Initialize the time object for the currently instrumented method
methodTimers = contextTimers[key] = {};
methodTimers.start = now;
value = orig.apply(obj, arguments);
methodTimers.end = getTime();
methodTimers.duration = methodTimers.end - methodTimers.start;
contextTimers.count[name] = contextTimers.count[name] || 0;
contextTimers.count[name] += 1;
return value;
};
}
orig = {
YGetjs: Y.Get.js,
Y_use: Y._use
};
// Start of the `Y.use()` invocation
Y._use = function () {
var now = getTime(),
useId;
// unique id for current `use` invocation
useId = '_use__' + arguments[0].join(',') + '__' + now;
current = perf[useId] = {
start: now,
end: null,
duration: null
};
orig.Y_use.apply(Y, arguments);
};
// Effective end of the `Y.use()` invocation before things get async
Y.Get.js = function () {
var now = getTime();
current.end = now;
current.duration = current.end - current.start;
if (!perf.first_Get) {
perf.first_Get = now;
perf.YUI_instantiation_to_first_Get = perf.first_Get - perf.YUI_start;
}
return orig.YGetjs.apply(Y.Get, arguments);
};
methods = [
'_setup',
'_rollup',
'_sort',
'_explode',
'addModule',
'calculate'
];
for (i = 0, len = methods.length; i < len; i += 1) {
instrument(loader, methods[i]);
}
}
YUI_config = window.YUI_config || {};
YUI_config.YUI_start = getTime();
YUI_config.hookForLoader = patchLoader;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment