Skip to content

Commit

Permalink
Merge pull request #265 from Mte90/filter-methods
Browse files Browse the repository at this point in the history
Filter by methods
  • Loading branch information
markstory authored Apr 15, 2019
2 parents ebf0119 + 2350ea1 commit 022281a
Show file tree
Hide file tree
Showing 7 changed files with 109 additions and 2 deletions.
4 changes: 4 additions & 0 deletions config/config.default.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@
'db.host' => getenv('XHGUI_MONGO_HOST') ?: 'mongodb://127.0.0.1:27017',
'db.db' => getenv('XHGUI_MONGO_DATABASE') ?: 'xhprof',
'db.options' => array(),
'run.view.filter.names' => array(
'Zend*',
'Composer*',
),

// Whether to instrument a user request.
//
Expand Down
27 changes: 26 additions & 1 deletion src/Xhgui/Controller/Run.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@

class Xhgui_Controller_Run extends Xhgui_Controller
{
/**
* HTTP GET attribute name for comma separated filters
*/
const FILTER_ARGUMENT_NAME = 'filter';

/**
* @var Xhgui_Profiles
*/
Expand Down Expand Up @@ -95,7 +100,11 @@ public function view()
}
}

$profile = $result->sort('ewt', $result->getProfile());
if (false !== $request->get(self::FILTER_ARGUMENT_NAME, false)) {
$profile = $result->sort('ewt', $result->filter($result->getProfile(), $this->getFilters()));
} else {
$profile = $result->sort('ewt', $result->getProfile());
}

$this->_template = 'runs/view.twig';
$this->set(array(
Expand All @@ -107,6 +116,22 @@ public function view()
'date_format' => $this->app->config('date.format'),
));
}

/**
* @return array
*/
protected function getFilters()
{
$request = $this->app->request();
$filterString = $request->get(self::FILTER_ARGUMENT_NAME);
if (strlen($filterString) > 1 && $filterString !== 'true') {
$filters = array_map('trim', explode(',', $filterString));
} else {
$filters = $this->app->config('run.view.filter.names');
}

return $filters;
}

public function deleteForm()
{
Expand Down
19 changes: 19 additions & 0 deletions src/Xhgui/Profile.php
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,25 @@ public function sort($dimension, $data)
uasort($data, $sorter);
return $data;
}

/**
* @param array $profileData
* @param array $filters
*
* @return array
*/
public function filter($profileData, $filters = [])
{
foreach ($filters as $key => $item) {
foreach ($profileData as $keyItem => $method) {
if (fnmatch($item, $keyItem)) {
unset($profileData[ $keyItem ]);
}
}
}

return $profileData;
}

/**
* Split a key name into the parent==>child format.
Expand Down
3 changes: 3 additions & 0 deletions src/templates/runs/paginated-list.twig
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@
<td>
<a href="{{ url('run.view', {'id': result.id|trim }) }}">
{{ result.date|date(date_format) }}
</a>
<a href="{{ url('run.view', {'id': result.id|trim, 'filter': true }) }}">
[filter]
</a>
</td>
<td class="right">{{ result.get('main()', 'wt') |as_time }}</td>
Expand Down
49 changes: 49 additions & 0 deletions tests/Controller/RunTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public function setUp()
$di['app'] = $di->share(function ($c) use ($mock) {
return $mock;
});
$this->import = $di['importController'];
$this->runs = $di['runController'];
$this->app = $di['app'];
$this->profiles = $di['profiles'];
Expand Down Expand Up @@ -220,4 +221,52 @@ public function testDeleteAllSubmit()
$result = $this->profiles->getAll();
$this->assertCount(0, $result['results']);
}

public function testFilterCustomMethods()
{
loadFixture($this->profiles, XHGUI_ROOT_DIR . '/tests/fixtures/results.json');

Environment::mock(array(
'SCRIPT_NAME' => 'index.php',
'PATH_INFO' => '/run/view',
'QUERY_STRING' => 'id=aaaaaaaaaaaaaaaaaaaaaaad&filter=main*,strpos()',
));

$this->runs->view();
$result = $this->runs->templateVars();

$this->assertCount(1, $result['profile']);
}

public function testFilterCustomMethod()
{
loadFixture($this->profiles, XHGUI_ROOT_DIR . '/tests/fixtures/results.json');

Environment::mock(array(
'SCRIPT_NAME' => 'index.php',
'PATH_INFO' => '/run/view',
'QUERY_STRING' => 'id=aaaaaaaaaaaaaaaaaaaaaaad&filter=main*',
));

$this->runs->view();
$result = $this->runs->templateVars();

$this->assertCount(2, $result['profile']);
}

public function testFilterMethods()
{
loadFixture($this->profiles, XHGUI_ROOT_DIR . '/tests/fixtures/results.json');

Environment::mock(array(
'SCRIPT_NAME' => 'index.php',
'PATH_INFO' => '/run/view',
'QUERY_STRING' => 'id=aaaaaaaaaaaaaaaaaaaaaaad&filter=true',
));

$this->runs->view();
$result = $this->runs->templateVars();

$this->assertCount(2, $result['profile']);
}
}
2 changes: 1 addition & 1 deletion tests/ProfileTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,7 @@ public function testCompareWithDifferences()
$result['diff']['strpos()']['ewt'],
'Should include exclusives'
);
$this->assertEquals(0.5, $result['diffPercent']['functionCount']);
$this->assertEquals(0.33, number_format($result['diffPercent']['functionCount'], 2));
}

public function testGetCallgraph()
Expand Down
7 changes: 7 additions & 0 deletions tests/fixtures/results.json
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,13 @@
"cpu": 12,
"mu": 3000,
"pmu": 3001
},
"Composer/Autoload": {
"ct": 1,
"wt": 10,
"cpu": 12,
"mu": 3000,
"pmu": 3001
}
}
},
Expand Down

0 comments on commit 022281a

Please sign in to comment.