Skip to content

Commit

Permalink
Refactor methods into Result class.
Browse files Browse the repository at this point in the history
  • Loading branch information
c9s committed Nov 14, 2011
1 parent 1ed3983 commit 4577050
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 28 deletions.
1 change: 1 addition & 0 deletions README.mkd
Expand Up @@ -31,6 +31,7 @@ PHP GrindKit for reading cachegrind compatible file.
$result->headers; // header list
}


## Required PHP Extensions

* xdebug http://xdebug.org/docs/profiler
Expand Down
28 changes: 3 additions & 25 deletions lib/GrindKit/GrindParser.php
Expand Up @@ -76,28 +76,6 @@ function isMethodCall($fn)
}
}

/* Calcualte Invocation summary for functions
*
* */
function calculateInvocationSummary($result,$funcdata)
{
$funcname = $funcdata['function'];

// if the function exists
if( isset( $result->summary[ $funcname ] ) ) {
// calculate here...
$result->summary[ $funcname ]['cost'] += (int) $funcdata['self_cost'];

if( isset($funcdata['invocation_count'] ) )
$result->summary[ $funcname ]['invocation_count'] += (int) $funcdata['invocation_count'];
} else {
$result->summary[ $funcname ] = array();
$result->summary[ $funcname ]['cost'] = (int) $funcdata['self_cost'];

if( isset($funcdata['invocation_count'] ) )
$result->summary[ $funcname ]['invocation_count'] = (int) $funcdata['invocation_count'];
}
}

/* the valgrind parser implementation for version 1 spec
*
Expand Down Expand Up @@ -160,9 +138,9 @@ function parse()
$funcdata['self_cost'] = (int)$selfCost;
}

$result->functions[] = $lastFunction = $funcdata;
$result->addCall( $funcdata );
$lastFunction = $funcdata;

$this->calculateInvocationSummary( $result, $funcdata );
// $info01 = rtrim(fgets($in));
// $info02 = rtrim(fgets($in));
}
Expand Down Expand Up @@ -204,7 +182,7 @@ function parse()
list($sourceline,$inclusivecost) = $costs;
$funcdata['line'] = (int) $sourceline;
$funcdata['self_cost'] = (int) $inclusivecost;
$this->calculateInvocationSummary( $result, $funcdata );
$result->addCall( $funcdata );
$this->advanceLine();
}
$result->functions[] = $funcdata;
Expand Down
60 changes: 59 additions & 1 deletion lib/GrindKit/GrindParserResult.php
Expand Up @@ -9,18 +9,76 @@
*
*/
namespace GrindKit;
use Exception;

class GrindParserResult
{
/* function invocation data , parsed from cachegrind file. */
/* function invocation list, parsed from cachegrind file. */
public $functions = array();
public $functionMapping = array();

/* function invocation summary data, which is calculated */
public $summary = array();

/* cachegrind file headers */
public $headers = array();

function addCall( & $func )
{
$this->functions[] = $func;
$this->functionMapping[ $func['function'] ] = $func;
$this->calculateInvocationSummary( $func );
}

/* Calcualte Invocation summary for functions
* */
function calculateInvocationSummary($funcdata)
{
$funcname = $funcdata['function'];

// if the function exists
if( isset( $this->summary[ $funcname ] ) ) {
// calculate here...
$this->summary[ $funcname ]['cost'] += (int) $funcdata['self_cost'];

if( isset($funcdata['invocation_count'] ) )
$this->summary[ $funcname ]['invocation_count'] += (int) $funcdata['invocation_count'];
} else {
$this->summary[ $funcname ] = array();
$this->summary[ $funcname ]['cost'] = (int) $funcdata['self_cost'];

if( isset($funcdata['invocation_count'] ) )
$this->summary[ $funcname ]['invocation_count'] = (int) $funcdata['invocation_count'];
}
}

function getCall($name)
{
if( isset($this->functionMapping[ $name ] ) ) {
return $this->functionMapping[ $name ];
}
}

function listCalls()
{
foreach( $this->functions as $func ) {
if( isset($func['called_from'] ) )
echo "\t";
printf("%s [Line %d]: %s\n", $func['filename'], $func['line'] , $func['function'] );
}
}

/* return tree structure result */
function getTree()
{
$entrypoint = '{main}';
$main = $this->getCall($entrypoint);
if( ! $main )
throw new Exception( 'Entrypoint {{main}} not found.' );

// get entrypoint function
// if( isset( $this->functions )
}

}

2 changes: 0 additions & 2 deletions lib/GrindKit/Node.php
Expand Up @@ -14,6 +14,4 @@ class Node
{




}
7 changes: 7 additions & 0 deletions tests/GrindKit/GrindParserTest.php
Expand Up @@ -28,6 +28,13 @@ function testParser()
$this->assertNotEmpty( $result );
$this->assertNotEmpty( $result->summary );
$this->assertNotEmpty( $result->functions );

$result->listCalls();
ob_flush();

$tree = $result->getTree();
var_dump( $tree );
ob_flush();
}

}

0 comments on commit 4577050

Please sign in to comment.