Skip to content
veloper edited this page Oct 28, 2010 · 17 revisions

Marks

Marks are simply points in code that one would like tracked by Bench. They are easy to use, quick to implement, and very useful when tracking multiple parts of a script/request.

Basic Examples

Using Marks Bench::start(); // [Application Bootstrap] Bench::mark('bootstrap'); // [Database Connection Opened] Bench::mark('database'); // [Data Processing + Manipulation] Bench::mark('processing'); // [HTML Creation] Bench::mark('html');

Retrieving Mark Data Using getMarkById() (Continued from above example.) print_r(Bench::getMarkById('database')); // -> Array ( // The mark id. [id] => database // The microtime(true) of when this mark occurred. [microtime] => 1287969552.88 // The time (in seconds) since start() was called. [since_start] => 1.10582304001 // The time since the last mark [or] since start(), if first call to mark(). [since_last_mark] => 0.171210050583 )

There are a few other methods and techniques available for retrieving mark data ...

  • mark(...) - Returns the [since_last_mark] time in seconds.
  • getMarks() - Returns an [array] of [mark data array]s in the order they occurred.
  • getLastMark() - Returns the [mark data array] of the last mark() call.
  • getLongestMark() - Returns the [mark data array] of the longest mark() call based on [since_last_mark].
  • getShortestMark() - Returns the [mark data array] of the shortest mark() call based on [since_last_mark].

Advanced Examples

Averaging Mark Call Times

In this example we time how long it takes to connection to a particular mail server 30 times using Zend Framework's Zend_Mail_Storage_Pop3 Class. Then we get the average time in seconds between all mark() calls.

Bench::start();
for($i++;$i<30;$i++) {
    $inbox = Zend_Mail_Storage_Pop3(array(...));
    $inbox->connect();
    Bench::mark('Connection Test '. $i);
}
echo 'Average: ' . Bench::getMarkAverage() . ' Seconds';

Getting Elapsed Time Between Marks Using getElapsed()

Adapting the example from above we pass two mark-ids to Bench::getElapsed() and get the time in seconds between them.

Bench::start();
// [Application Bootstrap]
Bench::mark('bootstrap');
// [Database Connection Opened]
Bench::mark('database');
// [Data Processing + Manipulation]
Bench::mark('processing');
// [HTML Creation]
Bench::mark('html');

echo Bench::getElapsed('bootstrap','processing'); -> 0.7827241420746

Note: The order of the passed mark-ids does not have to be chronological.

Clone this wiki locally