PHP code example of yaknet / lighthouse

1. Go to this page and download the library: Download yaknet/lighthouse 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/ */

    

yaknet / lighthouse example snippets


use YakNet\Lighthouse\Profiler;

// Start profiling the main flow with metadata tags
Profiler::start('Main Operation', ['controller' => 'UserController']);

// Start a sub-span (automatically becomes a child of 'Main Operation')
Profiler::start('DB Query', ['sql' => 'SELECT * FROM users']);
usleep(25000); // 25ms delay
Profiler::stop('DB Query');

// Stop the parent operation
Profiler::stop('Main Operation');

$result = Profiler::measure('API Call', function () {
    return file_get_contents('https://api.example.com/data');
}, ['endpoint' => 'get_data']);

Profiler::checkpoint('Application Started');
// ... run code ...
Profiler::checkpoint('Cache Loaded');

use YakNet\Lighthouse\Renderer\CliRenderer;

$totalDuration = Profiler::getInstance()->getTotalDuration();
echo CliRenderer::render(Profiler::getRootSpans(), $totalDuration);

use YakNet\Lighthouse\Renderer\HtmlRenderer;

$htmlReport = HtmlRenderer::render(
    Profiler::getRootSpans(),
    Profiler::getCheckpoints(),
    Profiler::getInstance()->getTotalDuration(),
    Profiler::getInstance()->getPeakMemory(),
    Profiler::getInstance()->getInitMemory(),
    Profiler::getInstance()->getInitTime()
);

file_put_contents('lighthouse_report.html', $htmlReport);