From 03822dd848e8cbe8b4a6a1de4a5f6654e2fdf2fb Mon Sep 17 00:00:00 2001 From: Petr Knap Date: Sat, 19 Dec 2015 10:22:58 +0100 Subject: [PATCH 1/8] $enabled is now protected (was private) --- src/Profiler/SimpleProfiler.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Profiler/SimpleProfiler.php b/src/Profiler/SimpleProfiler.php index 7055151..470138f 100644 --- a/src/Profiler/SimpleProfiler.php +++ b/src/Profiler/SimpleProfiler.php @@ -14,7 +14,7 @@ */ class SimpleProfiler implements ProfilerInterface { - private static $enabled = false; + protected static $enabled = false; private static $stack = []; From ff330e33daf9073b41620d88458edf4278f12cfe Mon Sep 17 00:00:00 2001 From: Petr Knap Date: Sat, 19 Dec 2015 12:26:10 +0100 Subject: [PATCH 2/8] AdvancedProfiler 0.1 --- src/Profiler/AdvancedProfiler.php | 79 +++++++++++++++++++++++++ tests/Profiler/AdvancedProfilerTest.php | 52 ++++++++++++++++ 2 files changed, 131 insertions(+) create mode 100644 src/Profiler/AdvancedProfiler.php create mode 100644 tests/Profiler/AdvancedProfilerTest.php diff --git a/src/Profiler/AdvancedProfiler.php b/src/Profiler/AdvancedProfiler.php new file mode 100644 index 0000000..fd9c5bd --- /dev/null +++ b/src/Profiler/AdvancedProfiler.php @@ -0,0 +1,79 @@ + + * @since 2015-12-19 + * @category Debug + * @package PetrKnap\Php\Profiler + * @version 0.1 + * @license https://github.com/petrknap/php-profiler/blob/master/LICENSE MIT + */ +class AdvancedProfiler extends SimpleProfiler implements ProfilerInterface +{ + /** + * 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 array|bool result as array on success or false on failure + */ + public static function finish($label = null) + { + if (self::$enabled) { + if ($label === null) { + $label = self::getCurrentFileHashLine(1); + } + + return parent::finish($label); + } + + return false; + } +} diff --git a/tests/Profiler/AdvancedProfilerTest.php b/tests/Profiler/AdvancedProfilerTest.php new file mode 100644 index 0000000..b690fa4 --- /dev/null +++ b/tests/Profiler/AdvancedProfilerTest.php @@ -0,0 +1,52 @@ +assertEquals( + sprintf( + "%s#%s", + __FILE__, + 22 + ), + AdvancedProfiler::getCurrentFileHashLine() + ); + + $this->assertFalse(AdvancedProfiler::getCurrentFileHashLine(-999)); + $this->assertFalse(AdvancedProfiler::getCurrentFileHashLine(+999)); + } + + public function testAutomaticGenerationOfLabels() + { + AdvancedProfiler::start(); + $result = AdvancedProfiler::finish(); + + $this->assertEquals( + sprintf( + "%s#%s", + __FILE__, + 31 + ), + $result[AdvancedProfiler::START_LABEL] + ); + + $this->assertEquals( + sprintf( + "%s#%s", + __FILE__, + 32 + ), + $result[AdvancedProfiler::FINISH_LABEL] + ); + } +} From b0628d5b8af93468efb672b92a4678d754f424a8 Mon Sep 17 00:00:00 2001 From: Petr Knap Date: Sun, 20 Dec 2015 10:46:41 +0100 Subject: [PATCH 3/8] AdvancedProfiler 0.2 Added support for $postProcessor --- src/Profiler/AdvancedProfiler.php | 27 +++++++++++++++++++++++-- tests/Profiler/AdvancedProfilerTest.php | 20 ++++++++++++++++++ 2 files changed, 45 insertions(+), 2 deletions(-) diff --git a/src/Profiler/AdvancedProfiler.php b/src/Profiler/AdvancedProfiler.php index fd9c5bd..980211a 100644 --- a/src/Profiler/AdvancedProfiler.php +++ b/src/Profiler/AdvancedProfiler.php @@ -9,11 +9,28 @@ * @since 2015-12-19 * @category Debug * @package PetrKnap\Php\Profiler - * @version 0.1 + * @version 0.2 * @license https://github.com/petrknap/php-profiler/blob/master/LICENSE MIT */ class AdvancedProfiler extends SimpleProfiler implements ProfilerInterface { + /** + * @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}" * @@ -71,7 +88,13 @@ public static function finish($label = null) $label = self::getCurrentFileHashLine(1); } - return parent::finish($label); + $result = parent::finish($label); + + if (self::$postProcessor === null) { + return $result; + } + + return call_user_func(self::$postProcessor, $result); } return false; diff --git a/tests/Profiler/AdvancedProfilerTest.php b/tests/Profiler/AdvancedProfilerTest.php index b690fa4..d7d7382 100644 --- a/tests/Profiler/AdvancedProfilerTest.php +++ b/tests/Profiler/AdvancedProfilerTest.php @@ -49,4 +49,24 @@ public function testAutomaticGenerationOfLabels() $result[AdvancedProfiler::FINISH_LABEL] ); } + + public function testPostProcessorSupport() + { + $postProcessorCallsCounter = 0; + AdvancedProfiler::setPostProcessor( + function ($result) use (&$postProcessorCallsCounter) { + $postProcessorCallsCounter++; + $this->assertTrue(is_array($result)); + } + ); + + for ($i = 0; $i < 10; $i++) { + $this->assertEquals($i, $postProcessorCallsCounter); + + AdvancedProfiler::start(); + AdvancedProfiler::finish(); + + $this->assertEquals($i + 1, $postProcessorCallsCounter); + } + } } From b2167d87efa86ca0dd63bc5c454db0cdc11231fb Mon Sep 17 00:00:00 2001 From: Petr Knap Date: Sun, 20 Dec 2015 10:57:06 +0100 Subject: [PATCH 4/8] Improved testPostProcessorSupport --- tests/Profiler/AdvancedProfilerTest.php | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/tests/Profiler/AdvancedProfilerTest.php b/tests/Profiler/AdvancedProfilerTest.php index d7d7382..d014652 100644 --- a/tests/Profiler/AdvancedProfilerTest.php +++ b/tests/Profiler/AdvancedProfilerTest.php @@ -57,6 +57,10 @@ public function testPostProcessorSupport() function ($result) use (&$postProcessorCallsCounter) { $postProcessorCallsCounter++; $this->assertTrue(is_array($result)); + + $result["post_processors_note"] = "note"; + + return $result; } ); @@ -64,7 +68,10 @@ function ($result) use (&$postProcessorCallsCounter) { $this->assertEquals($i, $postProcessorCallsCounter); AdvancedProfiler::start(); - AdvancedProfiler::finish(); + $result = AdvancedProfiler::finish(); + + $this->assertArrayHasKey("post_processors_note", $result); + $this->assertEquals("note", $result["post_processors_note"]); $this->assertEquals($i + 1, $postProcessorCallsCounter); } From b05e58d9ff320e9e7910c68b2eef357bf9284a09 Mon Sep 17 00:00:00 2001 From: Petr Knap Date: Tue, 5 Jan 2016 13:37:05 +0100 Subject: [PATCH 5/8] Removed ProfilerInterface and added Profile --- composer.json | 2 +- src/Profiler/AdvancedProfiler.php | 12 ++-- src/Profiler/Profile.php | 71 ++++++++++++++++++++ src/Profiler/ProfilerInterface.php | 57 ---------------- src/Profiler/SimpleProfiler.php | 66 ++++++++++++------- tests/Profiler/AdvancedProfilerTest.php | 50 +++++++++++--- tests/Profiler/ProfileTest.php | 35 ++++++++++ tests/Profiler/SimpleProfilerTest.php | 86 ++++++++++++++++--------- 8 files changed, 250 insertions(+), 129 deletions(-) create mode 100644 src/Profiler/Profile.php delete mode 100644 src/Profiler/ProfilerInterface.php create mode 100644 tests/Profiler/ProfileTest.php diff --git a/composer.json b/composer.json index 1ce6417..500daf8 100644 --- a/composer.json +++ b/composer.json @@ -15,7 +15,7 @@ "preferred-install": "dist" }, "require": { - "php": ">=5.3" + "php": ">=5.4" }, "require-dev": { "phpunit/phpunit": "4.*" diff --git a/src/Profiler/AdvancedProfiler.php b/src/Profiler/AdvancedProfiler.php index 60edd67..46ba404 100644 --- a/src/Profiler/AdvancedProfiler.php +++ b/src/Profiler/AdvancedProfiler.php @@ -9,10 +9,10 @@ * @since 2015-12-19 * @category Debug * @package PetrKnap\Php\Profiler - * @version 0.2 + * @version 0.3 * @license https://github.com/petrknap/php-profiler/blob/master/LICENSE MIT */ -class AdvancedProfiler extends SimpleProfiler implements ProfilerInterface +class AdvancedProfiler extends SimpleProfiler { /** * @var callable @@ -79,7 +79,7 @@ public static function start($label = null) * Finish profiling and get result * * @param string $label - * @return array|bool result as array on success or false on failure + * @return Profile|mixed|bool profile or return from post processor on success or false on failure */ public static function finish($label = null) { @@ -88,13 +88,13 @@ public static function finish($label = null) $label = self::getCurrentFileHashLine(1); } - $result = parent::finish($label); + $profile = parent::finish($label); if (self::$postProcessor === null) { - return $result; + return $profile; } - return call_user_func(self::$postProcessor, $result); + return call_user_func(self::$postProcessor, $profile); } return false; diff --git a/src/Profiler/Profile.php b/src/Profiler/Profile.php new file mode 100644 index 0000000..83351bc --- /dev/null +++ b/src/Profiler/Profile.php @@ -0,0 +1,71 @@ + + * @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 + ] + ); + } +} diff --git a/src/Profiler/ProfilerInterface.php b/src/Profiler/ProfilerInterface.php deleted file mode 100644 index 6e947fd..0000000 --- a/src/Profiler/ProfilerInterface.php +++ /dev/null @@ -1,57 +0,0 @@ - - * @since 2015-12-19 - * @category Debug - * @package PetrKnap\Php\Profiler - * @version 0.1 - * @license https://github.com/petrknap/php-profiler/blob/master/LICENSE MIT - */ -interface ProfilerInterface -{ - #region Result keys - const START_LABEL = "start_label"; // string - const START_TIME = "start_time"; // float start time in seconds - const START_MEMORY_USAGE = "start_memory_usage"; // int amount of used memory at start in bytes - const FINISH_LABEL = "finish_label"; // string - const FINISH_TIME = "finish_time"; // float finish time in seconds - const FINISH_MEMORY_USAGE = "finish_memory_usage"; // int amount of used memory at finish in bytes - const TIME_OFFSET = "time_offset"; // float time offset in seconds - const MEMORY_USAGE_OFFSET = "memory_usage_offset"; // int amount of memory usage offset in bytes - const ABSOLUTE_DURATION = "absolute_duration"; // float absolute duration in seconds - const DURATION = "duration"; // float duration in seconds - const ABSOLUTE_MEMORY_USAGE_CHANGE = "absolute_memory_usage_change"; // int absolute memory usage change in bytes - const MEMORY_USAGE_CHANGE = "memory_usage_change"; // int memory usage change in bytes - #endregion - - /** - * Enable profiler - */ - public static function enable(); - - /** - * Disable profiler - */ - public static function disable(); - - /** - * Start profiling - * - * @param string $label - * @return bool true on success or false on failure - */ - public static function start($label = null); - - /** - * Finish profiling and get result - * - * @param string $label - * @return array|bool result as array on success or false on failure - */ - public static function finish($label = null); -} diff --git a/src/Profiler/SimpleProfiler.php b/src/Profiler/SimpleProfiler.php index 470138f..1b0cc3f 100644 --- a/src/Profiler/SimpleProfiler.php +++ b/src/Profiler/SimpleProfiler.php @@ -9,13 +9,30 @@ * @since 2015-12-13 * @category Debug * @package PetrKnap\Php\Profiler - * @version 0.6 + * @version 0.7 * @license https://github.com/petrknap/php-profiler/blob/master/LICENSE MIT */ -class SimpleProfiler implements ProfilerInterface +class SimpleProfiler { + #region Meta keys + const START_LABEL = "start_label"; // string + const START_TIME = "start_time"; // float start time in seconds + const START_MEMORY_USAGE = "start_memory_usage"; // int amount of used memory at start in bytes + const FINISH_LABEL = "finish_label"; // string + const FINISH_TIME = "finish_time"; // float finish time in seconds + const FINISH_MEMORY_USAGE = "finish_memory_usage"; // int amount of used memory at finish in bytes + const TIME_OFFSET = "time_offset"; // float time offset in seconds + const MEMORY_USAGE_OFFSET = "memory_usage_offset"; // int amount of memory usage offset in bytes + #endregion + + /** + * @var bool + */ protected static $enabled = false; + /** + * @var Profile[] + */ private static $stack = []; /** @@ -46,13 +63,14 @@ public static function start($label = null) $now = microtime(true); $memoryUsage = memory_get_usage(true); - array_push(self::$stack, [ - self::START_LABEL => $label, - self::TIME_OFFSET => 0, - self::START_TIME => $now, - self::MEMORY_USAGE_OFFSET => 0, - self::START_MEMORY_USAGE => $memoryUsage - ]); + $profile = new Profile(); + $profile->meta[self::START_LABEL] = $label; + $profile->meta[self::TIME_OFFSET] = 0; + $profile->meta[self::MEMORY_USAGE_OFFSET] = 0; + $profile->meta[self::START_TIME] = $now; + $profile->meta[self::START_MEMORY_USAGE] = $memoryUsage; + + array_push(self::$stack, $profile); return true; } @@ -64,7 +82,7 @@ public static function start($label = null) * Finish profiling and get result * * @param string $label - * @return array|bool result as array on success or false on failure + * @return Profile|bool profile on success or false on failure */ public static function finish($label = null) { @@ -76,25 +94,25 @@ public static function finish($label = null) throw new \OutOfRangeException("Call " . __CLASS__ . "::start() first."); } - $result = array_pop(self::$stack); - - $result[self::FINISH_LABEL] = $label; - $result[self::FINISH_TIME] = $now; - $result[self::FINISH_MEMORY_USAGE] = $memoryUsage; - $result[self::ABSOLUTE_DURATION] = $result[self::FINISH_TIME] - $result[self::START_TIME]; - $result[self::DURATION] = $result[self::ABSOLUTE_DURATION] - $result[self::TIME_OFFSET]; - $result[self::ABSOLUTE_MEMORY_USAGE_CHANGE] = $result[self::FINISH_MEMORY_USAGE] - $result[self::START_MEMORY_USAGE]; - $result[self::MEMORY_USAGE_CHANGE] = $result[self::ABSOLUTE_MEMORY_USAGE_CHANGE] - $result[self::MEMORY_USAGE_OFFSET]; + /** @var Profile $profile */ + $profile = array_pop(self::$stack); + $profile->meta[self::FINISH_LABEL] = $label; + $profile->meta[self::FINISH_TIME] = $now; + $profile->meta[self::FINISH_MEMORY_USAGE] = $memoryUsage; + $profile->absoluteDuration = $profile->meta[self::FINISH_TIME] - $profile->meta[self::START_TIME]; + $profile->duration = $profile->absoluteDuration - $profile->meta[self::TIME_OFFSET]; + $profile->absoluteMemoryUsageChange = $profile->meta[self::FINISH_MEMORY_USAGE] - $profile->meta[self::START_MEMORY_USAGE]; + $profile->memoryUsageChange = $profile->absoluteMemoryUsageChange - $profile->meta[self::MEMORY_USAGE_OFFSET]; if (!empty(self::$stack)) { - $timeOffset = &self::$stack[count(self::$stack) - 1][self::TIME_OFFSET]; - $timeOffset = $timeOffset + $result[self::ABSOLUTE_DURATION]; + $timeOffset = &self::$stack[count(self::$stack) - 1]->meta[self::TIME_OFFSET]; + $timeOffset = $timeOffset + $profile->absoluteDuration; - $memoryUsageOffset = &self::$stack[count(self::$stack) - 1][self::MEMORY_USAGE_OFFSET]; - $memoryUsageOffset = $memoryUsageOffset + $result[self::ABSOLUTE_MEMORY_USAGE_CHANGE]; + $memoryUsageOffset = &self::$stack[count(self::$stack) - 1]->meta[self::MEMORY_USAGE_OFFSET]; + $memoryUsageOffset = $memoryUsageOffset + $profile->absoluteMemoryUsageChange; } - return $result; + return $profile; } return false; diff --git a/tests/Profiler/AdvancedProfilerTest.php b/tests/Profiler/AdvancedProfilerTest.php index d014652..6358501 100644 --- a/tests/Profiler/AdvancedProfilerTest.php +++ b/tests/Profiler/AdvancedProfilerTest.php @@ -1,6 +1,7 @@ meta[AdvancedProfiler::START_LABEL] ); $this->assertEquals( sprintf( "%s#%s", __FILE__, - 32 + 33 ), - $result[AdvancedProfiler::FINISH_LABEL] + $result->meta[AdvancedProfiler::FINISH_LABEL] ); } @@ -56,9 +57,10 @@ public function testPostProcessorSupport() AdvancedProfiler::setPostProcessor( function ($result) use (&$postProcessorCallsCounter) { $postProcessorCallsCounter++; - $this->assertTrue(is_array($result)); - $result["post_processors_note"] = "note"; + $this->assertInstanceOf(get_class(new Profile()), $result); + + $result->meta["post_processors_note"] = "note"; return $result; } @@ -70,10 +72,40 @@ function ($result) use (&$postProcessorCallsCounter) { AdvancedProfiler::start(); $result = AdvancedProfiler::finish(); - $this->assertArrayHasKey("post_processors_note", $result); - $this->assertEquals("note", $result["post_processors_note"]); + $this->assertArrayHasKey("post_processors_note", $result->meta); + $this->assertEquals("note", $result->meta["post_processors_note"]); $this->assertEquals($i + 1, $postProcessorCallsCounter); } } + + public function testPerformanceTest() + { + $start = microtime(true); + + AdvancedProfiler::start(); + + $diff = microtime(true) - $start; + + $this->assertLessThanOrEqual(0.001, $diff); + + + $start = microtime(true); + + AdvancedProfiler::finish(); + + $diff = microtime(true) - $start; + + $this->assertLessThanOrEqual(0.001, $diff); + + + $start = microtime(true); + + AdvancedProfiler::start(); + AdvancedProfiler::finish(); + + $diff = microtime(true) - $start; + + $this->assertLessThanOrEqual(0.002, $diff); + } } diff --git a/tests/Profiler/ProfileTest.php b/tests/Profiler/ProfileTest.php new file mode 100644 index 0000000..1b53e6a --- /dev/null +++ b/tests/Profiler/ProfileTest.php @@ -0,0 +1,35 @@ +getProfileAsArray(new Profile()); + + $this->assertArrayHasKey(Profile::ABSOLUTE_DURATION, $profile); + $this->assertArrayHasKey(Profile::DURATION, $profile); + $this->assertArrayHasKey(Profile::ABSOLUTE_MEMORY_USAGE_CHANGE, $profile); + $this->assertArrayHasKey(Profile::MEMORY_USAGE_CHANGE, $profile); + } + + public function testMetaConflict() + { + $absoluteDuration = 100; + + $profile = new Profile(); + $profile->meta[Profile::ABSOLUTE_DURATION] = $absoluteDuration; + $profile->meta["meta_" . Profile::ABSOLUTE_DURATION] = $absoluteDuration; + + $profile = $this->getProfileAsArray($profile); + + $this->assertNotEquals($absoluteDuration, $profile[Profile::ABSOLUTE_DURATION]); + $this->assertEquals($absoluteDuration, $profile["meta_" . Profile::ABSOLUTE_DURATION]); + } +} diff --git a/tests/Profiler/SimpleProfilerTest.php b/tests/Profiler/SimpleProfilerTest.php index 006b6a7..13d0245 100644 --- a/tests/Profiler/SimpleProfilerTest.php +++ b/tests/Profiler/SimpleProfilerTest.php @@ -1,11 +1,10 @@ assertArrayHasKey(SimpleProfiler::START_LABEL, $result); - $this->assertArrayHasKey(SimpleProfiler::START_TIME, $result); - $this->assertArrayHasKey(SimpleProfiler::START_MEMORY_USAGE, $result); - $this->assertArrayHasKey(SimpleProfiler::FINISH_LABEL, $result); - $this->assertArrayHasKey(SimpleProfiler::FINISH_TIME, $result); - $this->assertArrayHasKey(SimpleProfiler::FINISH_MEMORY_USAGE, $result); - $this->assertArrayHasKey(SimpleProfiler::TIME_OFFSET, $result); - $this->assertArrayHasKey(SimpleProfiler::ABSOLUTE_DURATION, $result); - $this->assertArrayHasKey(SimpleProfiler::DURATION, $result); - - $this->assertLessThanOrEqual($result[SimpleProfiler::ABSOLUTE_DURATION], $result[SimpleProfiler::DURATION]); - - $this->assertEquals($startLabel, $result[SimpleProfiler::START_LABEL]); - $this->assertEquals($finishLabel, $result[SimpleProfiler::FINISH_LABEL]); + /** @var Profile $result */ + $this->assertInstanceOf(get_class(new Profile()), $result); + + $this->assertLessThanOrEqual($result->absoluteDuration, $result->duration); + + $this->assertEquals($startLabel, $result->meta[SimpleProfiler::START_LABEL]); + $this->assertEquals($finishLabel, $result->meta[SimpleProfiler::FINISH_LABEL]); } public function testEmptyStack() @@ -43,7 +35,7 @@ public function testEnable() SimpleProfiler::enable(); $this->assertTrue(SimpleProfiler::start()); - $this->assertTrue(is_array(SimpleProfiler::finish())); + $this->assertInstanceOf(get_class(new Profile()), SimpleProfiler::finish()); } public function testDisable() @@ -116,13 +108,13 @@ public function testTimeProfiling() $A = SimpleProfiler::finish(); #endregion - $this->assertEquals(6, $A[SimpleProfiler::ABSOLUTE_DURATION], "", self::ACCEPTABLE_DELAY); - $this->assertEquals(3, $B[SimpleProfiler::ABSOLUTE_DURATION], "", self::ACCEPTABLE_DELAY); - $this->assertEquals(2, $C[SimpleProfiler::ABSOLUTE_DURATION], "", self::ACCEPTABLE_DELAY); + $this->assertEquals(6, $A->absoluteDuration, "", 0.5); + $this->assertEquals(3, $B->absoluteDuration, "", 0.5); + $this->assertEquals(2, $C->absoluteDuration, "", 0.5); - $this->assertEquals(1, $A[SimpleProfiler::DURATION], "", self::ACCEPTABLE_DELAY); - $this->assertEquals(3, $B[SimpleProfiler::DURATION], "", self::ACCEPTABLE_DELAY); - $this->assertEquals(2, $C[SimpleProfiler::DURATION], "", self::ACCEPTABLE_DELAY); + $this->assertEquals(1, $A->duration, "", 0.5); + $this->assertEquals(3, $B->duration, "", 0.5); + $this->assertEquals(2, $C->duration, "", 0.5); } public function testMemoryProfiling() @@ -138,10 +130,10 @@ public function testMemoryProfiling() $result = SimpleProfiler::finish(); - $this->assertGreaterThan($result[SimpleProfiler::START_MEMORY_USAGE], $result[SimpleProfiler::FINISH_MEMORY_USAGE]); - $this->assertGreaterThan($result[SimpleProfiler::MEMORY_USAGE_CHANGE], $result[SimpleProfiler::ABSOLUTE_MEMORY_USAGE_CHANGE]); - $this->assertGreaterThan(0, $result[SimpleProfiler::ABSOLUTE_MEMORY_USAGE_CHANGE]); - $this->assertGreaterThan(0, $result[SimpleProfiler::MEMORY_USAGE_CHANGE]); + $this->assertGreaterThan($result->meta[SimpleProfiler::START_MEMORY_USAGE], $result->meta[SimpleProfiler::FINISH_MEMORY_USAGE]); + $this->assertGreaterThan($result->memoryUsageChange, $result->absoluteMemoryUsageChange); + $this->assertGreaterThan(0, $result->absoluteMemoryUsageChange); + $this->assertGreaterThan(0, $result->memoryUsageChange); SimpleProfiler::start(); @@ -149,9 +141,39 @@ public function testMemoryProfiling() $result = SimpleProfiler::finish(); - $this->assertLessThan($result[SimpleProfiler::START_MEMORY_USAGE], $result[SimpleProfiler::FINISH_MEMORY_USAGE]); - $this->assertEquals($result[SimpleProfiler::ABSOLUTE_MEMORY_USAGE_CHANGE], $result[SimpleProfiler::MEMORY_USAGE_CHANGE]); - $this->assertLessThan(0, $result[SimpleProfiler::ABSOLUTE_MEMORY_USAGE_CHANGE]); - $this->assertLessThan(0, $result[SimpleProfiler::MEMORY_USAGE_CHANGE]); + $this->assertLessThan($result->meta[SimpleProfiler::START_MEMORY_USAGE], $result->meta[SimpleProfiler::FINISH_MEMORY_USAGE]); + $this->assertEquals($result->absoluteMemoryUsageChange, $result->memoryUsageChange); + $this->assertLessThan(0, $result->absoluteMemoryUsageChange); + $this->assertLessThan(0, $result->memoryUsageChange); + } + + public function testPerformanceTest() + { + $start = microtime(true); + + SimpleProfiler::start(); + + $diff = microtime(true) - $start; + + $this->assertLessThanOrEqual(0.001, $diff); + + + $start = microtime(true); + + SimpleProfiler::finish(); + + $diff = microtime(true) - $start; + + $this->assertLessThanOrEqual(0.001, $diff); + + + $start = microtime(true); + + SimpleProfiler::start(); + SimpleProfiler::finish(); + + $diff = microtime(true) - $start; + + $this->assertLessThanOrEqual(0.002, $diff); } } From 487628d5ae8056c02fe1322434f5efb0006e1b50 Mon Sep 17 00:00:00 2001 From: Petr Knap Date: Tue, 5 Jan 2016 14:20:41 +0100 Subject: [PATCH 6/8] Improved start method --- src/Profiler/SimpleProfiler.php | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/src/Profiler/SimpleProfiler.php b/src/Profiler/SimpleProfiler.php index 1b0cc3f..c9ea4a1 100644 --- a/src/Profiler/SimpleProfiler.php +++ b/src/Profiler/SimpleProfiler.php @@ -64,11 +64,13 @@ public static function start($label = null) $memoryUsage = memory_get_usage(true); $profile = new Profile(); - $profile->meta[self::START_LABEL] = $label; - $profile->meta[self::TIME_OFFSET] = 0; - $profile->meta[self::MEMORY_USAGE_OFFSET] = 0; - $profile->meta[self::START_TIME] = $now; - $profile->meta[self::START_MEMORY_USAGE] = $memoryUsage; + $profile->meta = [ + self::START_LABEL => $label, + self::TIME_OFFSET => 0, + self::MEMORY_USAGE_OFFSET => 0, + self::START_TIME => $now, + self::START_MEMORY_USAGE => $memoryUsage + ]; array_push(self::$stack, $profile); From 8e24cd66b250b36c4277c1a11306f92cc0438298 Mon Sep 17 00:00:00 2001 From: Petr Knap Date: Tue, 5 Jan 2016 14:55:49 +0100 Subject: [PATCH 7/8] testPerformanceTest renamed to testPerformanceIsNotIntrusive --- tests/Profiler/AdvancedProfilerTest.php | 2 +- tests/Profiler/SimpleProfilerTest.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/Profiler/AdvancedProfilerTest.php b/tests/Profiler/AdvancedProfilerTest.php index 6358501..d287592 100644 --- a/tests/Profiler/AdvancedProfilerTest.php +++ b/tests/Profiler/AdvancedProfilerTest.php @@ -79,7 +79,7 @@ function ($result) use (&$postProcessorCallsCounter) { } } - public function testPerformanceTest() + public function testPerformanceIsNotIntrusive() { $start = microtime(true); diff --git a/tests/Profiler/SimpleProfilerTest.php b/tests/Profiler/SimpleProfilerTest.php index 13d0245..9ff8a67 100644 --- a/tests/Profiler/SimpleProfilerTest.php +++ b/tests/Profiler/SimpleProfilerTest.php @@ -147,7 +147,7 @@ public function testMemoryProfiling() $this->assertLessThan(0, $result->memoryUsageChange); } - public function testPerformanceTest() + public function testPerformanceIsNotIntrusive() { $start = microtime(true); From 4310ff725fca9c98b6bd8b41c167bc56a15281be Mon Sep 17 00:00:00 2001 From: Petr Knap Date: Tue, 5 Jan 2016 16:27:21 +0100 Subject: [PATCH 8/8] reformatted --- composer.json | 64 +++++++++++++-------------- phpunit.xml | 2 +- src/Profiler/SimpleProfiler.php | 4 +- tests/Profiler/SimpleProfilerTest.php | 2 +- 4 files changed, 36 insertions(+), 36 deletions(-) diff --git a/composer.json b/composer.json index 500daf8..bde0b0c 100644 --- a/composer.json +++ b/composer.json @@ -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.4" - }, - "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" } + } } diff --git a/phpunit.xml b/phpunit.xml index 5b8a19b..771ed39 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -5,6 +5,6 @@ - + diff --git a/src/Profiler/SimpleProfiler.php b/src/Profiler/SimpleProfiler.php index c9ea4a1..487a229 100644 --- a/src/Profiler/SimpleProfiler.php +++ b/src/Profiler/SimpleProfiler.php @@ -59,7 +59,7 @@ public static function disable() */ public static function start($label = null) { - if(self::$enabled) { + if (self::$enabled) { $now = microtime(true); $memoryUsage = memory_get_usage(true); @@ -88,7 +88,7 @@ public static function start($label = null) */ public static function finish($label = null) { - if(self::$enabled) { + if (self::$enabled) { $now = microtime(true); $memoryUsage = memory_get_usage(true); diff --git a/tests/Profiler/SimpleProfilerTest.php b/tests/Profiler/SimpleProfilerTest.php index 9ff8a67..ad3a38c 100644 --- a/tests/Profiler/SimpleProfilerTest.php +++ b/tests/Profiler/SimpleProfilerTest.php @@ -122,7 +122,7 @@ public function testMemoryProfiling() SimpleProfiler::start(); $largeObject = null; - for($i = 0; $i < 1000; $i++) { + for ($i = 0; $i < 1000; $i++) { SimpleProfiler::start(); $largeObject = new \Exception("Large object", 0, $largeObject); SimpleProfiler::finish();