Skip to content

Commit

Permalink
Profile 0.2
Browse files Browse the repository at this point in the history
SimpleProfiler 0.7
AdvancedProfiler 0.3
  • Loading branch information
Petr Knap committed Jan 22, 2016
2 parents cf7a40f + 4310ff7 commit 71017b7
Show file tree
Hide file tree
Showing 9 changed files with 450 additions and 146 deletions.
64 changes: 32 additions & 32 deletions composer.json
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"
}
}
}
2 changes: 1 addition & 1 deletion phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@
</testsuite>
</testsuites>
<logging>
<log type="testdox-text" target="./tests/phpunit.log" />
<log type="testdox-text" target="./tests/phpunit.log"/>
</logging>
</phpunit>
102 changes: 102 additions & 0 deletions src/Profiler/AdvancedProfiler.php
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;
}
}
71 changes: 71 additions & 0 deletions src/Profiler/Profile.php
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
]
);
}
}
57 changes: 0 additions & 57 deletions src/Profiler/ProfilerInterface.php

This file was deleted.

Loading

0 comments on commit 71017b7

Please sign in to comment.