Skip to content

Commit

Permalink
Improves information about preloading
Browse files Browse the repository at this point in the history
  • Loading branch information
natanfelles committed Dec 8, 2024
1 parent 7165f53 commit 22c8114
Showing 1 changed file with 56 additions and 3 deletions.
59 changes: 56 additions & 3 deletions src/Debug/AutoloadCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Framework\Autoload\Autoloader;
use Framework\Autoload\Preloader;
use Framework\Debug\Collector;
use Framework\Debug\Debugger;
use UnitEnum;

/**
Expand Down Expand Up @@ -81,8 +82,14 @@ public function getContents() : string
<?= $this->renderPreload() ?>
<h1>Declarations</h1>
<?php
$declarations = Preloader::getAllDeclarations(); ?>
<p>Total of <?= \count($declarations) ?> declarations.</p>
$declarations = Preloader::getAllDeclarations();
$classes = $this->getPreloadStatistics()['classes'] ?? [];
?>
<p>Total of <?= \count($declarations) ?> declarations.
<?php if ($classes): ?>
<span style="color: green"><?= \count($classes) ?> are preloaded.</span>
<?php endif ?>
</p>
<table>
<thead>
<tr>
Expand All @@ -94,14 +101,24 @@ public function getContents() : string
</tr>
</thead>
<tbody>
<?php
$count = 0;
?>
<?php foreach ($declarations as $index => $declaration): ?>
<?php
$data = $this->getDataByDeclaration($declaration); ?>
<tr<?= $data ? ' class="active" title="Searched with the current Autoloader"'
: '' ?>>
<td><?= $index + 1 ?></td>
<td><?= $this->getDeclarationType($declaration) ?></td>
<td><?= $declaration ?></td>
<td<?= \in_array($declaration, $classes)
? ' style="color: green" title="' . (\MessageFormatter::formatMessage(
'en',
'{number, ordinal}',
['number' => ++$count]
)) . ' preloaded"' : '' ?>>
<?= $declaration ?>
</td>
<td><?php
if ($data && isset($data['loaded'])) {
echo $data['loaded'] ? 'Yes' : 'No';
Expand Down Expand Up @@ -234,6 +251,7 @@ protected function renderPreload() : string
$result .= '<p><strong>User:</strong> '
. \htmlentities($conf['directives']['opcache.preload_user']) . '</p>';
}
$result .= $this->renderPreloadStatistics();
return $result;
}
return '<p>Preload file has not been set.</p>';
Expand All @@ -249,6 +267,41 @@ protected function getOpcacheConfiguration() : ?array
: null;
}

protected function renderPreloadStatistics() : string
{
$result = '';
$statistics = $this->getPreloadStatistics();
if ($statistics) {
$memory = Debugger::convertSize($statistics['memory_consumption']);
$classes = $statistics['classes'];
$result .= '<p><strong>Memory:</strong> ' . $memory . '</p>';
$result .= '<p><strong>Classes:</strong> ' . \count($classes) . '</p>';
}
return $result;
}

/**
* @return array<string,mixed>
*/
protected function getPreloadStatistics() : array
{
$statistics = [];
if (\function_exists('opcache_get_status')) {
$status = (array) \opcache_get_status();
$statistics = $status['preload_statistics'] ?? [];
}
if (empty($statistics) && \defined('IS_TESTING') && IS_TESTING) {
$statistics = [
'memory_consumption' => 500000,
'classes' => [
'Foo',
'Bar',
],
];
}
return $statistics;
}

protected function getDeclarationType(string $declaration) : string
{
if (\in_array($declaration, \get_declared_classes(), true)) {
Expand Down

0 comments on commit 22c8114

Please sign in to comment.