Skip to content

Commit 434f747

Browse files
committed
Ready for version 1.0
1 parent dfbe63a commit 434f747

3 files changed

Lines changed: 136 additions & 5 deletions

File tree

README.md

Lines changed: 127 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ A simple PHP library for tracking the total amount of time a
1010
function / method) takes to execute (it can also return the result of executing
1111
the callable, if desired).
1212

13+
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.
14+
1315

1416
## Installation
1517

@@ -32,7 +34,7 @@ This library also provides information associated with each execution / invocati
3234
* the absolute path to the file in which the callable was called (string)
3335
* the exact line number in the file in which the callable was called (integer)
3436

35-
## Basic usage: Executing callables
37+
## Executing callables
3638

3739
### Executing built-in php functions
3840

@@ -287,4 +289,127 @@ $callableObj2->funcWithRefArg($numRef); // Will throw a PHP Warning.
287289
// works, meaning that $num will still
288290
// have a value of -1 after the call.
289291

290-
```
292+
```
293+
294+
295+
## Retrieving execution statistics
296+
297+
There are two ways to retrieve information associated with each execution of callables performed via this library:
298+
299+
1. 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):
300+
* **function** : A string. The name (conforming to PHP's method naming convention) you labeled the callable you executed
301+
* **args** : An array. Contains the arguments you passed to the callable you executed, if any, otherwise it would be an empty array.
302+
* **start_time** : A float or an Integer. The timestamp in nanoseconds when the execution of the callable started.
303+
* **end_time** : A float or an Integer. The timestamp in nanoseconds when the execution of the callable ended.
304+
* **total_execution_time_in_seconds** : A float or an Integer. The total number of seconds it took to execute the callable.
305+
* **return_value** : The value returned from the callable that was executed, if any, else NULL.
306+
* **file_called_from** : A string. The absolute path to the file from which the callable was executed.
307+
* **line_called_from** : An Integer. The exact line number in the file from which the callable was executed.
308+
309+
Below is an example:
310+
311+
```php
312+
<?php
313+
use \FunctionExecutionTimer\CallableExecutionTimer;
314+
315+
$funcObj = new CallableExecutionTimer('strtolower', 'strtolower');
316+
317+
$funcObj->strtolower('BOO');
318+
var_export($funcObj->getLatestBenchmark());
319+
```
320+
321+
The code above will generate output like the one below:
322+
323+
```
324+
array (
325+
'function' => 'strtolower',
326+
'args' =>
327+
array (
328+
0 => 'BOO',
329+
),
330+
'start_time' => 81023870126000,
331+
'end_time' => 81023870134000,
332+
'total_execution_time_in_seconds' => 8.0E-6,
333+
'return_value' => 'boo',
334+
'file_called_from' => 'C:\\Code\\function-execution-timer\\tester.php',
335+
'line_called_from' => 105,
336+
)
337+
```
338+
339+
2. You can call **\FunctionExecutionTimer\CallableExecutionTimer::getBenchmarks()** to get information about the all callable executions performed via
340+
* all calls to **\FunctionExecutionTimer\CallableExecutionTimer::callFunc(...)**
341+
* and all callable executions via various instances of **\FunctionExecutionTimer\CallableExecutionTimer**
342+
343+
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:
344+
345+
```php
346+
<?php
347+
use \FunctionExecutionTimer\CallableExecutionTimer;
348+
349+
// First clear previous benchmark info if any
350+
CallableExecutionTimer::clearBenchmarks();
351+
352+
$funcObj = new CallableExecutionTimer('strtolowerMethod', 'strtolower');
353+
354+
$funcObj->strtolowerMethod('BOO');
355+
$funcObj->strtolowerMethod('ABA');
356+
357+
CallableExecutionTimer::callFunc(
358+
'funcInline',
359+
function($arg) { return "Hello $arg !"; },
360+
['Jane']
361+
);
362+
363+
var_export(CallableExecutionTimer::getBenchmarks());
364+
```
365+
366+
The code above will generate output like the one below:
367+
368+
```
369+
array (
370+
0 =>
371+
array (
372+
'function' => 'strtolowerMethod',
373+
'args' =>
374+
array (
375+
0 => 'BOO',
376+
),
377+
'start_time' => 87248086831300,
378+
'end_time' => 87248086840600,
379+
'total_execution_time_in_seconds' => 9.3E-6,
380+
'return_value' => 'boo',
381+
'file_called_from' => 'C:\\Code\\function-execution-timer\\tester.php',
382+
'line_called_from' => 106,
383+
),
384+
1 =>
385+
array (
386+
'function' => 'strtolowerMethod',
387+
'args' =>
388+
array (
389+
0 => 'ABA',
390+
),
391+
'start_time' => 87248086997700,
392+
'end_time' => 87248087001600,
393+
'total_execution_time_in_seconds' => 3.9E-6,
394+
'return_value' => 'aba',
395+
'file_called_from' => 'C:\\Code\\function-execution-timer\\tester.php',
396+
'line_called_from' => 108,
397+
),
398+
2 =>
399+
array (
400+
'function' => 'funcInline',
401+
'args' =>
402+
array (
403+
0 => 'Jane',
404+
),
405+
'start_time' => 87248087019400,
406+
'end_time' => 87248087024100,
407+
'total_execution_time_in_seconds' => 4.7E-6,
408+
'return_value' => 'Hello Jane !',
409+
'file_called_from' => 'C:\\Code\\function-execution-timer\\tester.php',
410+
'line_called_from' => 110,
411+
),
412+
)
413+
```
414+
415+
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.

TODO.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
# Things To Do
2-
* Write PHPUnit tests
3-
* Write documentation
4-
* Publish to packagist
2+
* ~~Write PHPUnit tests~~
3+
* ~~Write documentation~~
4+
* ~~Publish to packagist~~

src/ObjectifiedCallable.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,12 @@ public function __call(string $methodName, array $args) {
130130
*/
131131
public function __invoke(array $args) {
132132

133+
// need to always keep this method very simple
134+
// because we don't want to add any overhead
135+
// computations for CallableExecutionTimer::__invoke
136+
// which calls this method and measures time it takes
137+
// for the callable in $this->method to be executed.
138+
133139
return \call_user_func_array($this->method, $args);
134140
}
135141

0 commit comments

Comments
 (0)