Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

namespace BitOne\PhpMemInfo\Analyzer;

class StdClassShapesCollector
{
/** @var array */
protected $items;

/**
* @param array $items
*/
public function __construct(array $items)
{
$this->items = $items;
}

/**
* Create a summary from the existing items,
* aggregated by member list and sorted by count.
*
* @return array
*/
public function collect()
{
$summary = [];

foreach ($this->items as $item) {
if ( $item['type'] !== 'object' || $item['class'] !== 'stdClass') {
continue;
}
$shape = implode(',', array_keys($item['children'] ?? []));
if (!isset($summary[$shape])) {
$summary[$shape] = ['count' => 0, 'self_size' => 0];
}
$summary[$shape]['count']++;
$summary[$shape]['self_size']+= $item['size'];
}

uasort($summary, function ($a, $b) {
if ($a === $b) {
return 0;
}
if ($a['count'] > $b['count']) {
return -1;
} else {
return 1;
}
}
);

return $summary;
}
}
2 changes: 2 additions & 0 deletions analyzer/src/BitOne/PhpMemInfo/Console/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use BitOne\PhpMemInfo\Console\Command\SummaryCommand;
use BitOne\PhpMemInfo\Console\Command\TopChildrenCommand;
use BitOne\PhpMemInfo\Console\Command\TopSizeCommand;
use BitOne\PhpMemInfo\Console\Command\StdClassShapesCommand;
use Symfony\Component\Console\Application as BaseApplication;

/**
Expand All @@ -24,5 +25,6 @@ public function __construct()
$this->add(new ReferencePathCommand());
$this->add(new SummaryCommand());
$this->add(new TopChildrenCommand());
$this->add(new StdClassShapesCommand());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php

namespace BitOne\PhpMemInfo\Console\Command;

use BitOne\PhpMemInfo\Analyzer\StdClassShapesCollector;
use BitOne\PhpMemInfo\Loader;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Helper\Table;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

class StdClassShapesCommand extends Command {
/**
* {@inheritedDoc}.
*/
protected function configure()
{
$this
->setName('stdclass-shapes')
->setDescription('Show statistics on stdClass property names')
->addArgument(
'dump-file',
InputArgument::REQUIRED,
'PHP Meminfo Dump File in JSON format'
);
}

/**
* {@inheritdoc}
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$dumpFilename = $input->getArgument('dump-file');
$loader = new Loader();
$items = $loader->load($dumpFilename);
$shapes = (new StdClassShapesCollector($items))->collect();
$table = new Table($output);
$this->formatTable($shapes, $table);
$table->render();
return 0;
}

/**
* Format data into a detailed table.
*
* @param array $shapes
* @param Table $table
*/
protected function formatTable(array $shapes, Table $table)
{
$table->setHeaders(['Members', 'Instances Count', 'Cumulated Self Size (bytes)']);

$rows = [];

foreach($shapes as $members => $stats) {
$rows[] = [$members, $stats['count'], $stats['self_size']];
}

$table->setRows($rows);
}

}