| 1 | <!----------------------------------------------------------------------- |
|---|
| 2 | Template : messagebox.cfc |
|---|
| 3 | Author : Luis Majano |
|---|
| 4 | Date : 3/13/2007 8:28:31 AM |
|---|
| 5 | Description : |
|---|
| 6 | This is a timer plugin |
|---|
| 7 | |
|---|
| 8 | Modification History: |
|---|
| 9 | 3/13/2007 - Created Template |
|---|
| 10 | ----------------------------------------------------------------------> |
|---|
| 11 | <cfcomponent name="timer" |
|---|
| 12 | hint="This is the timer plugin. It is used to time executions. Facade for request variable" |
|---|
| 13 | extends="coldbox.system.Plugin" |
|---|
| 14 | output="false" |
|---|
| 15 | cache="true" |
|---|
| 16 | cachetimeout="5"> |
|---|
| 17 | |
|---|
| 18 | <!------------------------------------------- CONSTRUCTOR -------------------------------------------> |
|---|
| 19 | |
|---|
| 20 | <cffunction name="init" access="public" returntype="timer" output="false" hint="Constructor"> |
|---|
| 21 | <cfargument name="controller" type="any" required="true"> |
|---|
| 22 | <cfset super.Init(arguments.controller) /> |
|---|
| 23 | <cfset setpluginName("Timer Plugin")> |
|---|
| 24 | <cfset setpluginVersion("1.0")> |
|---|
| 25 | <cfset setpluginDescription("A useful code timer plugin.")> |
|---|
| 26 | <cfreturn this> |
|---|
| 27 | </cffunction> |
|---|
| 28 | |
|---|
| 29 | <!------------------------------------------- PUBLIC -------------------------------------------> |
|---|
| 30 | |
|---|
| 31 | <cffunction name="start" access="public" returntype="void" output="false" hint="Start the timer with label."> |
|---|
| 32 | <cfargument name="Label" required="true" type="string"> |
|---|
| 33 | <!--- Create request timer ---> |
|---|
| 34 | <cfset var timerStruct = structnew()> |
|---|
| 35 | <cfset timerStruct.stime = getTickcount()> |
|---|
| 36 | <cfset timerStruct.label = arguments.label> |
|---|
| 37 | <!--- Place timer struct in request scope ---> |
|---|
| 38 | <cfset request[hash(arguments.label)] = timerStruct> |
|---|
| 39 | </cffunction> |
|---|
| 40 | |
|---|
| 41 | <cffunction name="stop" access="public" returntype="void" output="false" hint="Stop the timer with label"> |
|---|
| 42 | <cfargument name="Label" required="true" type="string"> |
|---|
| 43 | <cfset var stopTime = getTickcount()> |
|---|
| 44 | <cfset var timerStruct = ""> |
|---|
| 45 | <cfset var labelhash = hash(arguments.label)> |
|---|
| 46 | |
|---|
| 47 | <!--- Check if the label exists ---> |
|---|
| 48 | <cfif StructKeyExists(request,labelhash)> |
|---|
| 49 | <cfset timerStruct = request[labelhash]> |
|---|
| 50 | <cfset addRow(timerStruct.label,stopTime - timerStruct.stime)> |
|---|
| 51 | <cfelse> |
|---|
| 52 | <cfset addRow("#arguments.label# invalid",0)> |
|---|
| 53 | </cfif> |
|---|
| 54 | </cffunction> |
|---|
| 55 | |
|---|
| 56 | <cffunction name="logTime" access="public" returntype="void" output="false" hint="Use this method to add a new timer entry to the timers."> |
|---|
| 57 | <cfargument name="Label" required="true" type="string" hint="The lable of the timer."> |
|---|
| 58 | <cfargument name="Tickcount" required="true" type="string" hint="The tickcounts of the time."> |
|---|
| 59 | <cfset addRow(arguments.label,arguments.tickcount)> |
|---|
| 60 | </cffunction> |
|---|
| 61 | |
|---|
| 62 | <cffunction name="getTimerScope" access="public" returntype="query" output="false" hint="Returns the entire timer query from the request scope."> |
|---|
| 63 | <!---Get the timer scope if it exists, else create it ---> |
|---|
| 64 | <cfif not structKeyExists(request,"DebugTimers")> |
|---|
| 65 | <cfset request.DebugTimers = QueryNew("Id,Method,Time,Timestamp,RC")> |
|---|
| 66 | </cfif> |
|---|
| 67 | <cfreturn request.DebugTimers> |
|---|
| 68 | </cffunction> |
|---|
| 69 | |
|---|
| 70 | <!------------------------------------------- PRIVATE -------------------------------------------> |
|---|
| 71 | |
|---|
| 72 | <cffunction name="addRow" access="private" returntype="void" output="false" hint="Add a new timer row."> |
|---|
| 73 | <cfargument name="Label" required="true" type="string" hint="The lable of the timer."> |
|---|
| 74 | <cfargument name="Tickcount" required="true" type="string" hint="The tickcounts of the time."> |
|---|
| 75 | <cfscript> |
|---|
| 76 | var qTimer = getTimerScope(); |
|---|
| 77 | QueryAddRow(qTimer,1); |
|---|
| 78 | QuerySetCell(qTimer, "Id", createUUID()); |
|---|
| 79 | QuerySetCell(qTimer, "Method", arguments.Label); |
|---|
| 80 | QuerySetCell(qTimer, "Time", arguments.Tickcount); |
|---|
| 81 | QuerySetCell(qTimer, "Timestamp", now()); |
|---|
| 82 | QuerySetCell(qTimer, "RC", ''); |
|---|
| 83 | </cfscript> |
|---|
| 84 | </cffunction> |
|---|
| 85 | |
|---|
| 86 | |
|---|
| 87 | </cfcomponent> |
|---|