-
-
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.
SimpleProfiler 0.7 AdvancedProfiler 0.3
- Loading branch information
Showing
9 changed files
with
450 additions
and
146 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,36 +1,36 @@ | ||
{ | ||
"name": "petrknap/php-profiler", | ||
"description": "PHP profiler by Petr Knap", | ||
"license": "MIT", | ||
"authors": [ | ||
{ | ||
"name": "Petr Knap", | ||
"email": "dev@petrknap.cz", | ||
"homepage": "http://petrknap.cz", | ||
"role": "Developer" | ||
} | ||
"name": "petrknap/php-profiler", | ||
"description": "PHP profiler by Petr Knap", | ||
"license": "MIT", | ||
"authors": [ | ||
{ | ||
"name": "Petr Knap", | ||
"email": "dev@petrknap.cz", | ||
"homepage": "http://petrknap.cz", | ||
"role": "Developer" | ||
} | ||
], | ||
"homepage": "https://github.com/petrknap/php-profiler", | ||
"config": { | ||
"preferred-install": "dist" | ||
}, | ||
"require": { | ||
"php": ">=5.4" | ||
}, | ||
"require-dev": { | ||
"phpunit/phpunit": "4.*" | ||
}, | ||
"scripts": { | ||
"test": [ | ||
"./vendor/bin/phpunit" | ||
], | ||
"homepage": "https://github.com/petrknap/php-profiler", | ||
"config": { | ||
"preferred-install": "dist" | ||
}, | ||
"require": { | ||
"php": ">=5.3" | ||
}, | ||
"require-dev": { | ||
"phpunit/phpunit": "4.*" | ||
}, | ||
"scripts": { | ||
"test": [ | ||
"./vendor/bin/phpunit" | ||
], | ||
"post-autoload-dump": [ | ||
"@test" | ||
] | ||
}, | ||
"autoload": { | ||
"psr-4": { | ||
"PetrKnap\\Php\\Profiler\\": "src/Profiler" | ||
} | ||
"post-autoload-dump": [ | ||
"@test" | ||
] | ||
}, | ||
"autoload": { | ||
"psr-4": { | ||
"PetrKnap\\Php\\Profiler\\": "src/Profiler" | ||
} | ||
} | ||
} |
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,102 @@ | ||
<?php | ||
|
||
namespace PetrKnap\Php\Profiler; | ||
|
||
/** | ||
* Advanced PHP class for profiling | ||
* | ||
* @author Petr Knap <dev@petrknap.cz> | ||
* @since 2015-12-19 | ||
* @category Debug | ||
* @package PetrKnap\Php\Profiler | ||
* @version 0.3 | ||
* @license https://github.com/petrknap/php-profiler/blob/master/LICENSE MIT | ||
*/ | ||
class AdvancedProfiler extends SimpleProfiler | ||
{ | ||
/** | ||
* @var callable | ||
*/ | ||
private static $postProcessor = null; | ||
|
||
/** | ||
* Set post processor | ||
* | ||
* Post processor is callable with one input argument (return from finish method) and is called at the end of finish method. | ||
* | ||
* @param callable $postProcessor | ||
*/ | ||
public static function setPostProcessor(callable $postProcessor) | ||
{ | ||
self::$postProcessor = $postProcessor; | ||
} | ||
|
||
/** | ||
* Get current "{file}#{line}" | ||
* | ||
* @return string|bool current "{file}#{line}" on success or false on failure | ||
*/ | ||
public static function getCurrentFileHashLine() | ||
{ | ||
$args = func_get_args(); | ||
|
||
$deep = &$args[0]; | ||
|
||
$backtrace = debug_backtrace(); | ||
$backtrace = &$backtrace[$deep ? $deep : 0]; | ||
|
||
if ($backtrace) { | ||
return sprintf( | ||
"%s#%s", | ||
$backtrace["file"], | ||
$backtrace["line"] | ||
); | ||
} | ||
|
||
return false; | ||
} | ||
|
||
/** | ||
* Start profiling | ||
* | ||
* @param string $label | ||
* @return bool true on success or false on failure | ||
*/ | ||
public static function start($label = null) | ||
{ | ||
if (self::$enabled) { | ||
if ($label === null) { | ||
$label = self::getCurrentFileHashLine(1); | ||
} | ||
|
||
return parent::start($label); | ||
} | ||
|
||
return false; | ||
} | ||
|
||
/** | ||
* Finish profiling and get result | ||
* | ||
* @param string $label | ||
* @return Profile|mixed|bool profile or return from post processor on success or false on failure | ||
*/ | ||
public static function finish($label = null) | ||
{ | ||
if (self::$enabled) { | ||
if ($label === null) { | ||
$label = self::getCurrentFileHashLine(1); | ||
} | ||
|
||
$profile = parent::finish($label); | ||
|
||
if (self::$postProcessor === null) { | ||
return $profile; | ||
} | ||
|
||
return call_user_func(self::$postProcessor, $profile); | ||
} | ||
|
||
return false; | ||
} | ||
} |
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,71 @@ | ||
<?php | ||
|
||
namespace PetrKnap\Php\Profiler; | ||
|
||
use JsonSerializable; | ||
|
||
/** | ||
* Profile | ||
* | ||
* @author Petr Knap <dev@petrknap.cz> | ||
* @since 2015-12-19 | ||
* @category Debug | ||
* @package PetrKnap\Php\Profiler | ||
* @version 0.2 | ||
* @license https://github.com/petrknap/php-profiler/blob/master/LICENSE MIT | ||
*/ | ||
class Profile implements JsonSerializable | ||
{ | ||
#region JSON keys | ||
const ABSOLUTE_DURATION = "absolute_duration"; | ||
const DURATION = "duration"; | ||
const ABSOLUTE_MEMORY_USAGE_CHANGE = "absolute_memory_usage_change"; | ||
const MEMORY_USAGE_CHANGE = "memory_usage_change"; | ||
#endregion | ||
|
||
/** | ||
* @var array | ||
*/ | ||
public $meta = []; | ||
|
||
/** | ||
* Absolute duration in seconds | ||
* | ||
* @var float | ||
*/ | ||
public $absoluteDuration; | ||
|
||
/** | ||
* Duration in seconds | ||
* | ||
* @var float | ||
*/ | ||
public $duration; | ||
|
||
/** | ||
* Absolute memory usage change in bytes | ||
* | ||
* @var int | ||
*/ | ||
public $absoluteMemoryUsageChange; | ||
|
||
/** | ||
* Memory usage change in bytes | ||
* | ||
* @var int | ||
*/ | ||
public $memoryUsageChange; | ||
|
||
function jsonSerialize() | ||
{ | ||
return array_merge( | ||
$this->meta, | ||
[ | ||
self::ABSOLUTE_DURATION => $this->absoluteDuration, | ||
self::DURATION => $this->duration, | ||
self::ABSOLUTE_MEMORY_USAGE_CHANGE => $this->absoluteMemoryUsageChange, | ||
self::MEMORY_USAGE_CHANGE => $this->memoryUsageChange | ||
] | ||
); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.