1. Go to this page and download the library: Download bakame/aide-profiler 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/ */
bakame / aide-profiler example snippets
$start = microtime(true);
$service->calculateHeavyStuff(new DateTimeImmutable('2024-12-24'));
echo microtime(true) - $start; // the execution time of your code
use Bakame\Aide\Profiler\Profiler;
$duration = Profiler::executionTime(
$service->calculateHeavyStuff(new DateTimeImmutable('2024-12-24'))
);
// $duration is the execution time in nanosecond using hrtime instead of microtime
use Bakame\Aide\Profiler\Profiler;
// you create a new Profiler by passing the callback you want to profile
$metrics = Profiler::metrics(
$service->calculateHeavyStuff(new DateTimeImmutable('2024-12-24'))
);
$metrics->executionTime;
$metrics->cpuTime;
$metrics->memoryUsage;
$metrics->peakMemoryUsage;
$metrics->realMemoryUsage;
$metrics->realPeakMemoryUsage;
use Bakame\Aide\Profiler\Profiler;
// you create a new Profiler by passing the callback you want to profile
$metrics = Profiler::metrics(
$service->calculateHeavyStuff(new DateTimeImmutable('2024-12-24'))
);
$metrics->forHuman();
// returns
// [
// "cpu_time" => "30.000 µs"
// "execution_time" => "1.271 ms"
// "memory_usage" => "2.5 KB"
// "real_memory_usage" => "0.0 B"
// "peak_memory_usage" => "0.0 B"
// "real_peak_memory_usage" => "0.0 B"
// ]
$metrics->forHuman('memory_usage'); //returns "2.5 KB"
use Bakame\Aide\Profiler\Profiler;
$cpuTime = Profiler::cpuTime(
$service->calculateHeavyStuff(new DateTimeImmutable('2024-12-24')),
5
);
// the average CPU Time used when executing 5 times the code.
use Bakame\Aide\Profiler\Profiler;
$profiled = Profiler::execute($service->calculateHeavyStuff(new DateTimeImmutable('2024-12-24')));
$profiled->returnValue; // the result of executing the `calculateHeavyStuff` method
$profiled->summary; // the profiling data associated with the call.
$profiled->summary->metrics; // returns a Metrics instance
$profiled->summary->start; // returns a Snapshot instance
$profiled->summary->end; // returns a Snapshot instance
$profiled->summary->label; // returns an identifier as a string
use Bakame\Aide\Profiler\Profiler;
// you create a new Profiler by passing the callback you want to profile
$profiler = new Profiler($service->calculateHeavyStuff(...));
//we invoke the `run` method of the Profiler which will execute the callback
//$result is the result of executing the calculateHeavyStuff method
$result = $profiler->run(new DateTimeImmutable('2024-12-24'));
// you can use `__invoke` as a syntactic sugar method.
$summary = $profiler->latest(); // returns the Summary from the last call
// the $summary->metrics property returns a Metrics instance
$metrics = $summary->metrics;
$metrics->executionTime;
$metrics->cpuTime;
$metrics->memoryUsage;
$metrics->peakMemoryUsage;
$metrics->realMemoryUsage;
$metrics->realPeakMemoryUsage;
$result1 = $profiler(new DateTimeImmutable('2024-12-24'));
$result2 = $profiler(new DateTimeImmutable('2025-03-02'));
$result3 = $profiler(new DateTimeImmutable('2024-05-11'));
count($profiler); // the number of summaries already recorded
$profiler->latest(); // returns the Summary from the last call
$profiler->nth(-1); // returns the same Summary as Profile::last
$profiler->first(); // returns the first Summary ever generated
$profiler->isEmpty(); // returns true when the profiler contains no summary
$profiler->hasSummaries(); // returns true when at least on Summary is present
$profiler->average(); // returns the average Metrics of all the calls
use Bakame\Aide\Profiler\Profiler;
$callback = function (int ...$args): int|float => {
usleep(100)
return array_sum($args);
};
$profiler = new Profiler($callback);
$profiler(1, 2, 3); // returns 6
$summary = $profiler->latest(); // returns the last Summary object from the last call
$profiler->profile('my_test', 7, 8, 9); // returns 24
$namedSummary = $profiler->get('my_test'); // returns the associated Summary
$profiler->get('foobar'); // returns null because the `foobar` label does not exist
$profiler->has('foobar'); // returns false because the label does not exist
$profiler->labels(); // returns all the labels attached to the Profiler
$profiler->average('my_test'); // returns the Metrics average for all the calls whose label is `my_test`
use App\Profiler\Marker;
$marker = Marker::start('boot');
$marker->mark('init');
// some code
$marker->mark('load');
// some code
$marker->mark('render');
$summary = $marker->summary(); // Returns a Summary instance
echo $summary->metrics->executionTime; // Access execution time, CPU time, memory, etc.
$summary = $marker->summary('full_request'); // Returns a Summary instance
$delta = $marker->delta('init', 'render'); // Returns Summary
$cpuTime = $marker->delta('init', 'render', 'cpu_time'); // Returns the CPU Time as a float (nanoseconds)
$marker->labels(); // returns all the snapshot labels (in order)
$marker->has($label); // tells whether the label is used
$marker->first(); // returns the first snapshot taken
$marker->latest(); // returns the most recent snapshot
$marker->isEmpty(); // return true when no snapshot has been taken
$marker->hasSnapshots(); // return true when snapshots are available
$marker->canSummarize(); // returns true if the marker can safely generate a report/summary
$marker->toArray(); // returns all snapshots as structured arrays
$marker->isComplete(); // tells whether the marker is complete
$marker->reset(); // Reset the marker to its initial state open and with no snapshot
use Bakame\Aide\Profiler\Marker;
use Bakame\Aide\Profiler\Profiler;
$marker = Marker::start(label: 'start', identifier: 'user_import');
// or
$marker = new Marker(identifier: 'user_import');
$marker->mark(label: 'start');
echo $marker->identifier(); // 'user_import'
$profiler = new Profiler(function (): string {
usleep(1_000);
return 'done';
}, 'user_export');
echo $profiler->identifier(); // 'user_export
use Bakame\Aide\Profiler\Marker;
use Bakame\Aide\Profiler\Profiler;
use Monolog\Level;
use Monolog\Logger;
use Monolog\Handler\StreamHandler;
$logger = new Logger('profiler');
$logger->pushHandler(new StreamHandler(STDOUT, Level::Debug));
//logging with the Profiler instance
$profiler = new Profiler(function () {
usleep(1_000);
return 'end';
}, logger: $logger);
$profiler->profile('toto');
$profiler->profile('tata');
//logging the marker process
$marker = Marker::start('init', logger: $logger);
usleep(1_000);;
$marker->finish('render', 'server_cycle');
echo json_encode($profiler), PHP_EOL;
echo json_encode($marker), PHP_EOL;
use Bakame\Aide\Profiler\Profiler;
use Bakame\Aide\Profiler\ConsoleTableExporter;
$callback = function (int ...$args): int|float => {
usleep(100)
return array_sum($args);
};
$profiler = new Profiler($callback);
$profiler->profile('first_run', 1, 2);
$profiler->profile('last_run', 1, 2);
$profiler(1, 2);
$renderer = new ConsoleTableExporter();
$renderer->exportProfiler($profiler);
use Bakame\Aide\Profiler\OpenTelemetryExporter;
use Bakame\Aide\Profiler\Profiler;
use Monolog\Level;
use Monolog\Logger;
use Monolog\Handler\StreamHandler;
use OpenTelemetry\SDK\Trace\SpanExporter\InMemoryExporter;
use OpenTelemetry\SDK\Trace\SpanProcessor\SimpleSpanProcessor;
use OpenTelemetry\SDK\Trace\TracerProvider;
// adding a logger is optional.
$logger = new Logger('profiler');
$logger->pushHandler(new StreamHandler('php://stdout', Level::Debug));
$tracerProvider = new TracerProvider(new SimpleSpanProcessor(new InMemoryExporter()));
$exporter = new OpenTelemetryExporter($tracerProvider, $logger);
$callback = function (int ...$args): int|float => {
usleep(100)
return array_sum($args);
};
$profiler = new Profiler($callback);
$profiler->profile('first_run', 1, 2);
$profiler->profile('last_run', 1, 2);
$profiler(1, 2);
$exporter->exportProfilter($profiler);
// the Profiler content is exported to the Open Telemetry Server.
use Bakame\Aide\Profiler\Environment;;
$system = Environment::current();
$system->os; // the Operating System
$system->osFamily; // OS Family
$system->hostname; // the hostname
$system->machine; // the Architecture
$system->phpIntSize; // PHP Integer Size
$system->phpArchitecture; //returns 64-bits
$system->sapi; // SAPI
$system->memoryLimit; // Memory Limit
$system->cpuCores; // CPU Cores
$system->totalDisk; // the total available disk space in bytes
$system->freeDisk; // the remaining free disk space in bytes
use Bakame\Aide\Profiler\Environment;
$system = Environment::current();
$system->is32Bit(); // returns true on a 32-bit architecture
$system->is64Bit(); // returns true on a 64-bit architecture
$system->unlimitedMemory(); // returns true if there is no memory limit
$system->isWindows(); // returns true if the OS is a Windows
$system->isMac(); // returns true if the OS is a Mac
$system->isUnixLike(); // returns true if the OS is a Unix like
use Bakame\Aide\Profiler\ConsoleTableExporter;
use Bakame\Aide\Profiler\Environment;;
(new ConsoleTableExporter())->exportEnvironment($system);