PHP code example of chus / php-execution-tracker

1. Go to this page and download the library: Download chus/php-execution-tracker library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

chus / php-execution-tracker example snippets




use ExecutionTracker/Tracker;

// Start tracking the execution and get the trace object
$trace = Tracker::track("Count the cats in the array");

$animals = ['🐵', '🐱', '🐶', '🐷', '🐱', '🐴', '🐱', '🐸', '🐰'];

$cats = 0;

foreach ($animals as $key => $animal) {

    if($animal === '🐱') {
        $cats++;
        $trace->log("Cat found! We have $cats cats so far");
    }

}

// End the trace and set the result
$trace->end("$cats cats found in the array");

// Print the result as JSON
echo $trace->result()->asJson();

// Result as an array
$array = $trace->result()->asArray();

// Result as a JSON string
$json = $trace->result()->asJson();

// Result as HTML
$html = $trace->result()->asHtml();

$array = $trace->result([
    'reduced' => true
])->asJson();

$array = $trace->result([
    'withDuration' => true
])->asJson();

$array = $trace->result([
    'withDuration' => true,
    'withHumanTimes' => true
])->asJson();

$array = $trace->result([
    'withDuration' => true,
    'withHumanTimes' => true,
    'withoutTimestamps' => true
])->asJson();

function sum($a, $b) {
    $trace = Tracker::track("Sum $a + $b");
    $result = $a + $b;
    $trace->end("Obtained $result");
    return $a + $b;
}

function multiply($a, $b) {
    $trace = Tracker::track("Multiply $a * $b");
    $result = 0;
    for ($i = 0; $i < $b; $i++) {
        $result = sum($result, $a);
    }
    $trace->end("Obtained $result");
    return $a * $b;
}

function power($a, $b) {
    $trace = Tracker::track("Power $a ^ $b");
    $result = 1;
    for ($i = 0; $i < $b; $i++) {
        $result = multiply($result, $a);
    }
    $trace->end("Obtained $result");
    return $result;
}

$trace = Tracker::track("Calculate 3 ^ 3");
$result = power(3, 3);
$trace->end("Obtained $result");

echo $trace->result([
    'reduced' => true
])->asJson();

$trace = Tracker::track("Calculate 3 ^ 3");

Tracker::disable();
$result = power(3, 3);
Tracker::enable();

$trace->end("Obtained $result");

echo $trace->result([
    'reduced' => true
])->asJson();

// Hiding a trace after ending it
$trace->end("Something done!");
$trace->hide();

// Alternatively, you can hide the trace directly
$trace->end("Something done!")->hide();

// Or you can pass a boolean to the end method to hide the trace
$trace->end("Something done!", true);

$trace = Tracker::track("Wait 1 second");
sleep(1);
$trace->end("Waited 1 second");

Tracker::clear();

$trace = Tracker::track("Wait 2 seconds");
sleep(2);
$trace->end("Waited 2 seconds");

$mainTrace = Tracker::getMainTrace();

echo $mainTrace->result(['reduced' => true, 'withHumanTimes' => true, 'withDuration' => true])->asJson();

$trace1 = Tracker::track("Wait 1 second");
sleep(1);

$trace2 = Tracker::track("Wait 1 second");
sleep(1);

$trace2->end("Waited 1 second")->hide(); // Or $trace2->end(); then $trace2->hide();

$trace1->end("Waited 1 second");

echo $trace1->result(['reduced' => true, 'withHumanTimes' => true, 'withDuration' => true])->asJson();
bash
composer 
json
{
    "name": "Wait 2 seconds",
    "result": "Waited 2 seconds",
    "startTime": "2024-04-17 21:30:56",
    "endTime": "2024-04-17 21:30:58",
    "duration": "2s"
}
bash
composer update
./vendor/bin/phpunit ./tests/test.php