Skip to content

Add top-ranges command #127

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
82 changes: 82 additions & 0 deletions analyzer/src/BitOne/PhpMemInfo/Analyzer/TopRanges.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
<?php

namespace BitOne\PhpMemInfo\Analyzer;

use BitOne\PhpMemInfo\Loader;
use Symfony\Component\Finder\Finder;
use Symfony\Component\Finder\SplFileInfo;

class TopRanges
{
public function get(Finder $sortedDumpFiles, bool $showZeroRange = true)
{
list($min, $max) = $this->getMinMax($sortedDumpFiles);

$allTypes = [];
foreach (array_keys($min) as $type) {
$allTypes[$type] = $type;
}
foreach (array_keys($max) as $type) {
$allTypes[$type] = $type;
}

$ranges = [];
foreach ($allTypes as $type) {
$minVal = $min[$type] ?? 0;
$maxVal = $max[$type] ?? 0;

$rangeVal = $maxVal - $minVal;

if (($showZeroRange && 0 === $rangeVal) || $rangeVal > 0) {
$ranges[$type] = $rangeVal;
}
}

arsort($ranges, SORT_NUMERIC);

return $ranges;
}

private function getTypeCount(array $items)
{
$typeCount = [];

foreach ($items as $item) {
$type = ('object' === $item['type']) ? $item['class'] : $item['type'];

if (!array_key_exists($type, $typeCount)) {
$typeCount[$type] = 0;
}

++$typeCount[$type];
}

return $typeCount;
}

private function getMinMax(Finder $sortedDumpFiles)
{
$loader = new Loader();
$min = [];
$max = [];

/** @var SplFileInfo $sortedDumpFile */
foreach ($sortedDumpFiles as $sortedDumpFile) {
$items = $loader->load($sortedDumpFile->getPathname());

$typeCount = $this->getTypeCount($items);

foreach ($typeCount as $type => $count) {
if (!array_key_exists($type, $min) || $count < $min[$type]) {
$min[$type] = $count;
}

if (!array_key_exists($type, $max) || $count > $max[$type]) {
$max[$type] = $count;
}
}
}

return [$min, $max];
}
}
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 @@ -6,6 +6,7 @@
use BitOne\PhpMemInfo\Console\Command\ReferencePathCommand;
use BitOne\PhpMemInfo\Console\Command\SummaryCommand;
use BitOne\PhpMemInfo\Console\Command\TopChildrenCommand;
use BitOne\PhpMemInfo\Console\Command\TopRangesCommand;
use BitOne\PhpMemInfo\Console\Command\TopSizeCommand;
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 TopRangesCommand());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php

namespace BitOne\PhpMemInfo\Console\Command;

use BitOne\PhpMemInfo\Analyzer\TopRanges;
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\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Finder\Finder;

class TopRangesCommand extends Command
{
/**
* {@inheritedDoc}.
*/
protected function configure()
{
$this
->setName('top-ranges')
->setDescription('Given a directory of dump files sorted by name, this command displays the top ranges (difference between the largest and smallest values) of the dumped types.')
->addArgument(
'dump-dir',
InputArgument::REQUIRED,
'PHP Meminfo Dump Files directory'
)
->addOption('show-zero', null, InputOption::VALUE_NONE, 'Show zero ranges')
;
}

/**
* {@inheritdoc}
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$dumpDir = $input->getArgument('dump-dir');

if (!file_exists($dumpDir)) {
throw new \InvalidArgumentException(sprintf('Directory %s does not exist.', $dumpDir));
}

$sortedDumpFiles = Finder::create()->files()->in($dumpDir)->name('*.json')->sortByName(true);
$output->writeln(sprintf('<info>%d dump files found.</info>', $sortedDumpFiles->count()));

$topRanges = new TopRanges();
$topRangeByType = $topRanges->get($sortedDumpFiles, (bool) $input->getOption('show-zero'));

$table = new Table($output);

$table->setHeaders(['Type', 'Range']);

foreach ($topRangeByType as $type => $range) {
$table->addRow([$type, $range]);
}

$table->render();

return Command::SUCCESS;
}
}