Download the PHP package rotexsoft/callable-execution-timer without Composer
On this page you can find all versions of the php package rotexsoft/callable-execution-timer. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download rotexsoft/callable-execution-timer
More information about rotexsoft/callable-execution-timer
Files in rotexsoft/callable-execution-timer
Package callable-execution-timer
Short Description A simple PHP library for tracking the total amount of time a callable (e.g. function / method) takes to execute (it can also return the result of executing the callable, if desired).
License BSD-3-Clause
Homepage https://github.com/rotexsoft/function-execution-timer
Informations about the package callable-execution-timer
Callable Execution Timer
A simple PHP library for tracking the total amount of time a callable (e.g. function / method) takes to execute (it can also return the result of executing the callable, if desired).
If you want to do some simple execution time profiling in your application, without using some full blown tool like debugbar or xdebug, then this is the package for you.
Installation
Via composer: (Requires PHP 7.4+ or PHP 8.0+).
composer require rotexsoft/callable-execution-timer
Branching
These are the branches in this repository:
- main: contains code for the latest major version of this package
- 1.x: contains code for the 1.x version of this package
Introduction
A simple PHP library for tracking the total amount of time a callable (e.g. function / method) takes to execute and return result(s) (if any).
For the rest of this documentation the term callable will mostly be referring to functions / methods
This library also provides information associated with each execution / invocation of the callable such as:
- the arguments passed to the callable (array)
- the total time it took to execute the callable in seconds (int / float)
- the value returned from calling the callable (mixed)
- the absolute path to the file in which the callable was called (string)
- the exact line number in the file in which the callable was called (integer)
Executing callables
Executing built-in php functions
Let's call php's built-in strtolower & strtoupper functions:
NOTE: The first argument passed to CallableExecutionTimer::callFunc(...) is a name (conforming to PHP's method naming convention) you want to name the callable you are about to execute. This name will be used to label the information related to the execution of the callable as will be shown later on in this documentation.
NOTE: The second argument passed to CallableExecutionTimer::callFunc(...) is the callable you are trying to execute.
NOTE: The third argument passed to CallableExecutionTimer::callFunc(...) is an array containing all the arguments to be passed to the callable you are trying to execute. You can omit this argument if the callable to be executed does not accept any arguments.
Executing user defined functions
Executing class methods
Executing parent and child class methods
Executing namespaced static class methods
Executing lambda / anonymous functions
Executing an object that is an instance of a class that has an __invoke method
You can also use instances of \FunctionExecutionTimer\CallableExecutionTimer to execute callables like below:
WARNING: Executing a callable that has one or more parameters that should be passed by reference should be done using \FunctionExecutionTimer\CallableExecutionTimer::callFunc(...) or executing the function by using the __invoke(array $args) mechanism on the instance of \FunctionExecutionTimer\CallableExecutionTimer the callable is bound to.
It won't work by trying to invoke the callable on an instance of \FunctionExecutionTimer\CallableExecutionTimer using the method call syntax that triggers __call() under the hood.
For example, you can execute the lambda function below that accepts an argument by reference via the following two ways:
Retrieving execution statistics
There are two ways to retrieve information associated with each execution of callables performed via this library:
-
You can call the getLatestBenchmark() method on an instance of \FunctionExecutionTimer\CallableExecutionTimer which you just used to execute a callable to get information about the most recent callable execution via that object. This method returns an array with the following keys (in bold, not including the colon):
- function : A string. The name (conforming to PHP's method naming convention) you labeled the callable you executed
- args : An array. Contains the arguments you passed to the callable you executed, if any, otherwise it would be an empty array.
- start_time : A float or an Integer. The timestamp in nanoseconds when the execution of the callable started.
- end_time : A float or an Integer. The timestamp in nanoseconds when the execution of the callable ended.
- total_execution_time_in_seconds : A float or an Integer. The total number of seconds it took to execute the callable.
- return_value : The value returned from the callable that was executed, if any, else NULL.
- file_called_from : A string. The absolute path to the file from which the callable was executed.
- line_called_from : An Integer. The exact line number in the file from which the callable was executed.
Below is an example:
The code above will generate output like the one below:
-
You can call \FunctionExecutionTimer\CallableExecutionTimer::getBenchmarks() to get information about the all callable executions performed via
- all calls to \FunctionExecutionTimer\CallableExecutionTimer::callFunc(...)
- and all callable executions via various instances of \FunctionExecutionTimer\CallableExecutionTimer
This method returns an array of arrays. Each sub-array has the structure of the array returned by the getLatestBenchmark() method described above. Below is some sample code:
The code above will generate output like the one below:
IT IS RECOMMENDED THAT YOU CALL \FunctionExecutionTimer\CallableExecutionTimer::clearBenchmarks() BEFORE YOU START EXECUTING THE CALLABLES THAT YOU WANT TO GET EXECUTION INFORMATION FOR. THIS WILL CLEAR ALL PREVIOUS EXECUTION INFO FROM PRIOR CALLABLE EXECUTIONS.