Last active
August 3, 2022 01:54
-
-
Save JamoCA/905c21d434cbef10b03cf799f9b93f16 to your computer and use it in GitHub Desktop.
CF_Timer... like Adobe's built-in CFTimer, but without requiring admin IP rule and also including nanoTime
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<cfsilent><!--- | |
CF_Timer: Like Adobe's built-in CFTimer, but without requiring admin IP rule and also including nanoTime. | |
Author: James Moberg | |
Date: 9/20/2013 | |
Shared: 7/3/2022 | |
Notes: Called like cftimer but without enabling debugging. (NOTE: "debug" mode not supported.) | |
Types: outline (default) | |
Credit: Initial "myTimer" CFTag by Chris Phillips ( http://www.cfchris.com/cfchris/index.cfm/2007/4/5/My-CFTIMER-Custom-Tag ) | |
Adobe CFTimer Docs: https://helpx.adobe.com/coldfusion/cfml-reference/coldfusion-tags/tags-t/cftimer.html | |
Blog Entry: https://dev.to/gamesover/cftimer-no-debugging-ip-address-required-introducing-nanotime-1k8h | |
Tweet: https://twitter.com/gamesover/status/1543638190761816065 | |
EXAMPLE USAGE | |
<cf_timer label="Sleep Test" type="outline"> | |
<!--- Do some stuff ---> | |
<cfset sleep(50)> | |
</cf_timer> | |
STORE EXECUTION TIME (MILLISECONDS) AS VARIABLE "variables.TimeLapsed" | |
<cf_timer variable="TimeLapsed"> | |
<!--- Do some stuff ---> | |
<cfset sleep(50)> | |
</cf_timer> | |
<cfoutput><p>Time Lapsed = #TimeLapsed# ms</p></cfoutput> | |
STORE EXECUTION TIME (MILLISECONDS) AS VARIABLE "variables.TimeLapsed" | |
<cf_timer> | |
<!--- Do some stuff ---> | |
<cfset sleep(50)> | |
</cf_timer> | |
<cfoutput> | |
---> | |
<cfparam name="attributes.label" default="cftimer"> | |
<cfparam name="attributes.type" default="outline"><!--- comment, inline, outline ("debug" not supported) ---> | |
<cfparam name="attributes.variable" default=""> | |
<cfparam name="attributes.allowedIPs" default=""> | |
<cfparam name="ATTRIBUTES.nano" type="boolean" default="false"> | |
<cfparam name="ATTRIBUTES.returnType" type="string" default=""> <!--- "ns", "μs", "ms", "s" ---> | |
<cfscript> | |
numeric function getNano() hint="returns nano time (more accurate)" { | |
return createObject("java", "java.lang.System").nanoTime(); | |
} | |
numeric function getTick() hint="Railo or OpenBD may not return ms since EPOC" { | |
var tickCount = GetTickCount(); | |
if (not find("ColdFusion", server.coldfusion.productname)){ | |
try { | |
tickCount = CreateObject("java", "java.lang.System").currentTimeMillis(); | |
} catch(any e){}; | |
} | |
return tickCount; | |
} | |
string function prettyDecimal(numeric inputString=0){ | |
var response = trim(REReplace(numberformat(val(arguments.inputString), '9,999.999999999999'), "0+$", "")); | |
return response.replaceAll("\.$", ""); | |
} | |
</cfscript> | |
</cfsilent> | |
<cfif thistag.executionmode is "Start"> | |
<cfif ATTRIBUTES.nano> | |
<cfset variables.startTimer = getNano()> | |
<cfelse> | |
<cfset variables.startTimer = getTick()> | |
</cfif> | |
<cfelseif thistag.executionmode is "End"> | |
<cfif ATTRIBUTES.nano> | |
<cfset variables.endTimer = getNano()> | |
<cfelse> | |
<cfset variables.endTimer = getTick()> | |
</cfif> | |
<cfset variables.final = (variables.endTimer - variables.startTimer)> | |
<cfif len(ATTRIBUTES.returnType)> | |
<cfif ATTRIBUTES.nano> | |
<cfif variables.final gt 10000000000> | |
<cfset ATTRIBUTES.returnType = "s"> | |
<cfelse> | |
<cfset ATTRIBUTES.returnType = "ms"> | |
</cfif> | |
<cfelse> | |
<cfif variables.final gt 10000> | |
<cfset ATTRIBUTES.returnType = "s"> | |
<cfelse> | |
<cfset ATTRIBUTES.returnType = "ms"> | |
</cfif> | |
</cfif> | |
</cfif> | |
<cfswitch expression="#ATTRIBUTES.returnType#"> | |
<cfcase value="ns nanoseconds" delimiters=" "> | |
<cfif ATTRIBUTES.nano> | |
<cfset variables.PerfTime = (prettyDecimal(variables.final) & " ns")> | |
<cfelse> | |
<cfset variables.PerfTime = (prettyDecimal(variables.final * 1000000) & " ns")> | |
</cfif> | |
</cfcase> | |
<cfcase value="μs microseconds" delimiters=" "> | |
<cfif ATTRIBUTES.nano> | |
<cfset variables.PerfTime = (prettyDecimal(variables.final / 1000) & " μs")> | |
<cfelse> | |
<cfset variables.PerfTime = (prettyDecimal(variables.final * 1000) & " μs")> | |
</cfif> | |
</cfcase> | |
<cfcase value="ms milliseconds" delimiters=" "> | |
<cfif ATTRIBUTES.nano> | |
<cfset variables.PerfTime = (prettyDecimal(variables.final / 1000000) & " ms")> | |
<cfelse> | |
<cfset variables.PerfTime = (prettyDecimal(variables.final) & " ms")> | |
</cfif> | |
</cfcase> | |
<cfcase value="s seconds" delimiters=" "> | |
<cfif ATTRIBUTES.nano> | |
<cfset variables.PerfTime = (prettyDecimal(variables.final / 1000000000) & " s")> | |
<cfelse> | |
<cfset variables.PerfTime = (prettyDecimal(variables.final / 1000) & " s")> | |
</cfif> | |
</cfcase> | |
<cfdefaultcase> | |
<cfset variables.PerfTime = variables.final> | |
</cfdefaultcase> | |
</cfswitch> | |
<cfif len(trim(attributes.variable)) AND isvalid("variableName", attributes.variable)> | |
<cfset caller[attributes.variable] = variables.PerfTime> | |
<cfelseif attributes.type IS "outline"> | |
<cfoutput><fieldset style="word-break:break-word;"><legend><CFIF LEN(Attributes.Label)>#attributes.label#: </CFIF>#variables.PerfTime#</legend>#THISTAG.GeneratedContent#</fieldset></cfoutput> | |
<CFSET THISTAG.GeneratedContent = ""> | |
<cfelseif attributes.type IS "inline"> | |
<cfoutput><CFIF LEN(Attributes.Label)>#attributes.label#: </CFIF>#variables.PerfTime# ms</cfoutput> | |
<cfelseif attributes.type IS "comment"> | |
<cfoutput><!-- <CFIF LEN(Attributes.Label)>#attributes.label#: </CFIF>#variables.PerfTime# ms--></cfoutput> | |
<cfelse> | |
<!--- unknown, just output the content ---> | |
</cfif> | |
</cfif> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment