-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add CallbackContainerBuilder, LoggableContainerBuilder
- Loading branch information
Showing
23 changed files
with
1,088 additions
and
132 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
An example output produced by the `LoggableContainerBuilder`. | ||
|
||
``` | ||
"NamespaceExaminer": { | ||
"prototype": 3, | ||
"prototype-backtrace": [ | ||
[ | ||
"require", | ||
"require_once", | ||
"call_user_func", | ||
"{closure}", | ||
"SemanticCite::onExtensionFunction", | ||
"SCI\\HookRegistry::__construct", | ||
"SCI\\HookRegistry::addCallbackHandlers", | ||
"SMW\\ApplicationFactory::getNamespaceExaminer", | ||
"Onoi\\CallbackContainer\\LoggableContainerBuilder::create" | ||
], | ||
[ | ||
"OutputPage::addParserOutput", | ||
"OutputPage::addParserOutputMetadata", | ||
"Hooks::run", | ||
"call_user_func_array", | ||
"SMW\\MediaWiki\\Hooks\\HookRegistry::SMW\\MediaWiki\\Hooks\\{closure}", | ||
"SMW\\MediaWiki\\Hooks\\OutputPageParserOutput::process", | ||
"SMW\\MediaWiki\\Hooks\\OutputPageParserOutput::canPerformUpdate", | ||
"SMW\\MediaWiki\\Hooks\\OutputPageParserOutput::isSemanticEnabledNamespace", | ||
"SMW\\ApplicationFactory::getNamespaceExaminer", | ||
"Onoi\\CallbackContainer\\LoggableContainerBuilder::create" | ||
], | ||
[ | ||
"SkinTemplate::outputPage", | ||
"SkinTemplate::prepareQuickTemplate", | ||
"Hooks::run", | ||
"call_user_func_array", | ||
"SBL\\HookRegistry::SBL\\{closure}", | ||
"SBL\\SkinTemplateOutputModifier::modifyOutput", | ||
"SBL\\SkinTemplateOutputModifier::canModifyOutput", | ||
"SBL\\SkinTemplateOutputModifier::isEnabled", | ||
"SMW\\ApplicationFactory::getNamespaceExaminer", | ||
"Onoi\\CallbackContainer\\LoggableContainerBuilder::create" | ||
] | ||
], | ||
"singleton": 0, | ||
"singleton-memory": [], | ||
"singleton-time": [], | ||
"prototype-memory-median": 1432, | ||
"prototype-time-median": 0.00018199284871419 | ||
}, | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
<?php | ||
|
||
namespace Onoi\CallbackContainer; | ||
|
||
/** | ||
* @license GNU GPL v2+ | ||
* @since 2.0 | ||
*/ | ||
class BacktraceSniffer { | ||
|
||
/** | ||
* @var integer | ||
*/ | ||
private $depth = 1; | ||
|
||
/** | ||
* @since 2.0 | ||
* | ||
* @param integer $depth | ||
*/ | ||
public function __construct( $depth = 1 ) { | ||
$this->depth = $depth; | ||
} | ||
|
||
/** | ||
* @see MediaWiki::wfGetCaller | ||
* @since 2.0 | ||
* | ||
* @return $string | ||
*/ | ||
public function getCaller( $depth = null ) { | ||
|
||
$depth = $depth === null ? $this->depth : $depth; | ||
$backtrace = $this->getBackTrace( $depth + 1 ); | ||
|
||
if ( isset( $backtrace[$depth] ) ) { | ||
return $this->doFormatStackFrame( $backtrace[$depth] ); | ||
} | ||
|
||
return 'unknown'; | ||
} | ||
|
||
/** | ||
* @see MediaWiki::wfGetCallers | ||
* @since 2.0 | ||
* | ||
* @return array | ||
*/ | ||
public function getCallers( $depth = null ) { | ||
|
||
$depth = $depth === null ? $this->depth : $depth; | ||
$backtrace = array_reverse( $this->getBackTrace() ); | ||
|
||
|
||
if ( !$depth || $depth > count( $backtrace ) - 1 ) { | ||
$depth = count( $backtrace ) - 1; | ||
} | ||
|
||
$backtrace = array_slice( $backtrace, -$depth - 1, $depth ); | ||
|
||
return array_map( array( $this, 'doFormatStackFrame' ), $backtrace ); | ||
} | ||
|
||
private function doFormatStackFrame( $frame ) { | ||
return isset( $frame['class'] ) ? $frame['class'] . '::' . $frame['function'] : $frame['function']; | ||
} | ||
|
||
private function getBackTrace( $limit = 0 ) { | ||
static $disabled = null; | ||
|
||
if ( $disabled || ( $disabled = !function_exists( 'debug_backtrace' ) ) === true ) { | ||
return array(); | ||
} | ||
|
||
if ( $limit && version_compare( PHP_VERSION, '5.4.0', '>=' ) ) { | ||
return array_slice( debug_backtrace( DEBUG_BACKTRACE_PROVIDE_OBJECT, $limit + 1 ), 1 ); | ||
} else { | ||
return array_slice( debug_backtrace(), 1 ); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
<?php | ||
|
||
namespace Onoi\CallbackContainer; | ||
|
||
use RuntimeException; | ||
|
||
/** | ||
* @see http://stackoverflow.com/questions/19973037/benchmark-memory-usage-in-php | ||
* | ||
* @license GNU GPL v2+ | ||
* @since 2.0 | ||
*/ | ||
class CallFuncMemorySniffer { | ||
|
||
/** | ||
* @var integer | ||
*/ | ||
private static $max; | ||
|
||
/** | ||
* @var integer | ||
*/ | ||
private static $memory; | ||
|
||
/** | ||
* @var integer | ||
*/ | ||
private $time; | ||
|
||
/** | ||
* @since 2.0 | ||
*/ | ||
public static function memoryTick() { | ||
self::$memory = memory_get_usage() - self::$memory; | ||
self::$max = self::$memory > self::$max ? self::$memory : self::$max; | ||
self::$memory = memory_get_usage(); | ||
} | ||
|
||
/** | ||
* @since 2.0 | ||
* | ||
* @param Closure|callable $func | ||
* @param array|null $args | ||
* | ||
* @return mixed | ||
* @throws RuntimeException | ||
*/ | ||
public function call( $func, $args = null ) { | ||
|
||
if ( !is_callable( $func ) ) { | ||
throw new RuntimeException( "Function is not callable" ); | ||
} | ||
|
||
// HHVM ... Fatal error: Call to undefined function | ||
if ( !function_exists( 'register_tick_function' ) ) { | ||
return is_array( $args ) ? call_user_func_array( $func, $args ): call_user_func( $func ); | ||
} | ||
|
||
declare( ticks=1 ); | ||
|
||
self::$memory = memory_get_usage(); | ||
self::$max = 0; | ||
|
||
register_tick_function( | ||
'call_user_func_array', | ||
array( '\Onoi\CallbackContainer\CallFuncMemorySniffer', 'memoryTick' ), | ||
array() | ||
); | ||
|
||
$this->time = microtime( true ); | ||
$result = is_array( $args ) ? call_user_func_array( $func, $args ): call_user_func( $func ); | ||
$this->time = microtime( true ) - $this->time; | ||
|
||
unregister_tick_function( 'call_user_func_array' ); | ||
|
||
return $result; | ||
} | ||
|
||
/** | ||
* @since 2.0 | ||
* | ||
* @return integer | ||
*/ | ||
public function getMemoryUsed() { | ||
return self::$max; | ||
} | ||
|
||
/** | ||
* @since 2.0 | ||
* | ||
* @return float | ||
*/ | ||
public function getTimeUsed() { | ||
return $this->time; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.