PHP code example of aeatech / snapshot-profiler-xhprof

1. Go to this page and download the library: Download aeatech/snapshot-profiler-xhprof 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/ */

    

aeatech / snapshot-profiler-xhprof example snippets



declare(strict_types=1);

use AEATech\SnapshotProfiler\Profiler;
use AEATech\SnapshotProfilerXhprof\Adapter;
use AEATech\SnapshotProfilerXhprof\Saver;
use Xhgui\Profiler\Profiler as BackendProfiler;
use Xhgui\Profiler\ProfilingFlags;

.upload' => [
        'uri' => 'http://xhgui:80/run/import',
        'timeout' => 1,
    ],
];
$backendProfiler = new BackendProfiler($profilerConfig);

$saver = new Saver($backendProfiler);

$adapterIniSettings = [
    'xhprof.collect_additional_info' => '1',
];
$adapter = new Adapter($backendProfiler, $adapterIniSettings);
$profiler = new Profiler($adapter);

$profilingOptions = [];
/**
 * End initialization
 */

/**
 * Start profiling
 */

$profiler->enable();
/**
 * It's doing nothing and added for backward compatibility with newrelic
 */
$profiler->setProfilingOptions($profilingOptions);

for ($i = 0; $i < 10; $i++) {
    random_int(1, 10);
}

$data = $profiler->disable();
/**
 * End profiling
 */

/**
 * Start customization (not necessarily)
 */

/**
 * If you use fpm it will take current URL as snapshot name
 * If you use cli it will take script.php as snapshot name
 * Example of custom snapshot name
 */
$data[Adapter::KEY_META][Adapter::KEY_META_URL] = '1.0.0|serviceName|env|snapshotName';

/**
 * Set snapshot timestamps (usable for cli processes)
 */
$dateTime = new DateTime();
$timestamp = $dateTime->getTimestamp();
$data[Adapter::KEY_META][Adapter::KEY_META_REQUEST_TS_MICRO] = [
    Adapter::KEY_META_REQUEST_TS_MICRO_SEC => $timestamp,
    Adapter::KEY_META_REQUEST_TS_MICRO_USEC => $dateTime->format('u'),
];
$data[Adapter::KEY_META][Adapter::KEY_META_SERVER][Adapter::KEY_META_SERVER_REQUEST_TIME]
    = $timestamp;
$data[Adapter::KEY_META][Adapter::KEY_META_SERVER][Adapter::KEY_META_SERVER_REQUEST_TIME_FLOAT]
    = $dateTime->format('U.u');
/**
 * End customization
 */

/**
 * Save profiling result
 */
$saver->save($data);

/**
 * http://127.0.0.1:8142 - profile results here
 */