Skip to content

Instantly share code, notes, and snippets.

@JamoCA
Last active October 27, 2020 17:28
Show Gist options
  • Save JamoCA/c2b90b5596d0b9c352aafc3a76ef0674 to your computer and use it in GitHub Desktop.
Save JamoCA/c2b90b5596d0b9c352aafc3a76ef0674 to your computer and use it in GitHub Desktop.
Returns a query with execution stack containing line, parent, template, function and totaltime. (ColdFusion) Requires debugging to be enabled.
<cfscript>
/* 10/26/2020 Returns a query with execution stack containing line, parent, template, function and totaltime.
Requires debugging to be enabled. If debugging is not enabled, an empty query is returned
Inspired by combination of scripts here https://www.bennadel.com/blog/116-finding-template-execution-stack-in-coldfusion.htm
If desired, replace encrypted "WEB-INF/debug/classic.cfm" template with empty script (or edit from https://github.com/joshknutson/coldfusion-debug ) */
query function getTemplatesUsed() output="false" hint="Returns query with execution stack: line, parent, template, function and totaltime." {
var temp = {r=0, re = "\|(.*?)(\(|\])" };
var objFactory = CreateObject("java", "coldfusion.server.ServiceFactory");
var st = CreateObject("java", "java.lang.String");
var objDebugging = objFactory.GetDebuggingService();
var qTemplates = "";
var qEvents = "";
try {
qEvents = objDebugging.GetDebugger().GetData();
qTemplates = queryExecute("SELECT DISTINCT line, parent, template, endtime - starttime AS totaltime FROM qEvents WHERE type = :t ORDER BY template ASC", {t='Template'}, {dbtype="query"});
queryAddColumn(qTemplates, "function", "varchar", []);
for (temp.row in qTemplates){
temp.r += 1;
temp.st = ST.init(temp.row.template);
temp.a = temp.st.split("\sfrom\s");
temp.temp2 = trim(temp.a[arraylen(temp.a)]);
temp.sRet = refind(temp.re, temp.st, 1, true);
temp.temp3 = "";
if (arraylen(temp.sRet.pos)>1){
temp.temp3 = trim(mid(temp.st, temp.sRet.pos[2], temp.sRet.len[2]));
}
temp.a = ST.init(temp.temp2).split("wwwroot\\");
temp.out = temp.a[arraylen(temp.a)];
qTemplates.template[temp.r] = temp.a[arraylen(temp.a)];
if(LEN(temp.temp3)){
qTemplates.function[temp.r] = temp.temp3;
}
}
} catch (any e) {
qTemplates = queryNew("line,parent,template,function,totaltime");
}
return qTemplates;
}
</cfscript>
<cfdump var="#getTemplatesUsed()#" label="getTemplatesUsed()">
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment