Skip to content

Add sorters for the summary command #80

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 2 commits 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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# 1.0.4 Ygramul (2018-08-13)
- [GITHUB-75](https://github.com/BitOne/php-meminfo/issues/75) Now goes through static members of classes as well the usual frame execution browsing.

# 1.0.3 Ygramul (2018-08-13)
- [GITHUB-76](https://github.com/BitOne/php-meminfo/issues/76) Fixes wrong non matching item reference from children, introduced in v1.0.2

Expand Down
32 changes: 20 additions & 12 deletions analyzer/src/BitOne/PhpMemInfo/Analyzer/SummaryCreator.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,11 @@ public function __construct(array $items)
* Create a summary from the existing items,
* aggregated by type/class and sorted by count.
*
* @param array $sorters
*
* @return array
*/
public function createSummary()
public function createSummary($sorters)
{
$summary = [];

Expand All @@ -45,19 +47,25 @@ public function createSummary()
$summary[$type]['self_size']+= $item['size'];
}

uasort($summary, function ($a, $b) {
$aCount = $a['count'];
$bCount = $b['count'];
uasort($summary, function ($a, $b) use ($sorters) {
$aCount = $a[$sorters['column']];
$bCount = $b[$sorters['column']];

if ($a === $b) {
return 0;
}

$comp = $a[$sorters['column']] > $b[$sorters['column']];
if ($sorters['order'] === 'asc') {
$comp = $a[$sorters['column']] < $b[$sorters['column']];
}

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

return $summary;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,18 @@ protected function configure()
'dump-file',
InputArgument::REQUIRED,
'PHP Meminfo Dump File in JSON format'
)
->addOption(
'column-to-sort',
null,
InputOption::VALUE_REQUIRED,
'The column to sort ("count" or "self_size")'
)
->addOption(
'order-to-sort',
null,
InputOption::VALUE_REQUIRED,
'The order to sort ("asc" or "desc")'
);
}

Expand All @@ -42,14 +54,18 @@ protected function configure()
protected function execute(InputInterface $input, OutputInterface $output)
{
$dumpFilename = $input->getArgument('dump-file');
$columnToSort = $input->getOption('column-to-sort') ? $input->getOption('column-to-sort') : 'count';
$orderToSort = $input->getOption('order-to-sort');

$loader = new Loader();

$items = $loader->load($dumpFilename);

$summaryCreator = new SummaryCreator($items);

$summary = $summaryCreator->createSummary();
$sorters = ['column' => $columnToSort, 'order' => $orderToSort];

$summary = $summaryCreator->createSummary($sorters);

$table = new Table($output);
$this->formatTable($summary, $table);
Expand Down