LICENSE 0000666 00000002072 13436755073 0005573 0 ustar 00 MIT License Copyright (c) 2016 netpromotion group s.r.o. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. README.md 0000666 00000006770 13436755073 0006056 0 ustar 00 # [Profiler] with adapter for [Tracy] This repository contains lightweight, very quick and easy to use [Profiler] with adapter for [Tracy]. ## Usage If you wish to profile a block of code, simply encapsulate it between `Profiler::start` and `Profiler::finish` calls. ```php "effective", // or "absolute" "show" => [ "memoryUsageChart" => true, // or false "shortProfiles" => true, // or false "timeLines" => true // or false ] ])]); ``` There is a live demo available - run `make demo` and [click here](http://127.0.0.1:8080/lumen/). [Profiler]:https://packagist.org/packages/petrknap/php-profiler [Tracy]:https://tracy.nette.org/ [Profiler's README.md]:https://github.com/petrknap/php-profiler/blob/master/README.md [Nette]:https://nette.org/ [Lumen]:https://lumen.laravel.com/ [netpromotion/tracy-wrapper]:https://github.com/netpromotion/tracy-wrapper src/Profiler/Service/ProfilerService.php 0000666 00000012377 13436755073 0014364 0 ustar 00 metaData = []; $this->profiles[] = $profile; return $profile; } /** * @return Profile[] */ public function getProfiles() { return $this->profiles; } private function getMetaData() { if (empty($this->metaData)) { $this->metaData[self::META_TIME_LINE] = []; $this->metaData[self::META_MEMORY_PEAK] = 0; if (count($this->profiles) == 0) { $this->metaData[self::META_TIME_ZERO] = 0; $timeEnd = 0; } else { $this->metaData[self::META_TIME_ZERO] = $this->profiles[0]->meta[Profiler::START_TIME]; $timeEnd = $this->profiles[0]->meta[Profiler::FINISH_TIME]; foreach ($this->profiles as $profile) { $this->metaData[self::META_TIME_ZERO] = min( $this->metaData[self::META_TIME_ZERO], $profile->meta[Profiler::START_TIME] ); $timeEnd = max( $timeEnd, $profile->meta[Profiler::FINISH_TIME] ); $this->metaData[self::META_MEMORY_PEAK] = max( $this->metaData[self::META_MEMORY_PEAK], $profile->meta[Profiler::START_MEMORY_USAGE], $profile->meta[Profiler::FINISH_MEMORY_USAGE] ); $this->metaData[self::META_TIME_LINE][$profile->meta[Profiler::START_TIME] * 1000] = [ self::META_TIME_LINE__MEMORY_USAGE => $profile->meta[Profiler::START_MEMORY_USAGE] ]; $this->metaData[self::META_TIME_LINE][$profile->meta[Profiler::FINISH_TIME] * 1000] = [ self::META_TIME_LINE__MEMORY_USAGE => $profile->meta[Profiler::FINISH_MEMORY_USAGE] ]; } } $this->metaData[self::META_TIME_TOTAL] = max($timeEnd - $this->metaData[self::META_TIME_ZERO], 0.001); ksort($this->metaData[self::META_TIME_LINE]); } return $this->metaData; } public function iterateProfiles(callable $callback) { $metaData = $this->getMetaData(); foreach ($this->profiles as $profile) { $profile->meta[static::TIME_LINE_BEFORE] = floor( ($profile->meta[Profiler::START_TIME] - $metaData[self::META_TIME_ZERO]) / $metaData[self::META_TIME_TOTAL] * 100 ); $profile->meta[static::TIME_LINE_ACTIVE] = floor( $profile->duration / $metaData[self::META_TIME_TOTAL] * 100 ); $profile->meta[static::TIME_LINE_INACTIVE] = floor( ($profile->absoluteDuration - $profile->duration) / $metaData[self::META_TIME_TOTAL] * 100 ); $profile->meta[static::TIME_LINE_AFTER] = 100 - $profile->meta[static::TIME_LINE_BEFORE] - $profile->meta[static::TIME_LINE_ACTIVE] - $profile->meta[static::TIME_LINE_INACTIVE]; call_user_func($callback, $profile, $metaData); } } public function iterateMemoryTimeLine(callable $callback) { $metaData = $this->getMetaData(); $totalTime = 0; $height = 0; foreach ($metaData[self::META_TIME_LINE] as $time => $values) { $time = $time / 1000 - $metaData[self::META_TIME_ZERO]; $height = floor( $values[self::META_TIME_LINE__MEMORY_USAGE] / $metaData[self::META_MEMORY_PEAK] * 100 ); $totalTime += $time; call_user_func($callback, $time, $height, $metaData); } if ($totalTime < $metaData[self::META_TIME_TOTAL]) { call_user_func($callback, $metaData[self::META_TIME_TOTAL] - $totalTime, $height, $metaData); } } } src/Profiler/Profiler.php 0000666 00000001564 13436755073 0011437 0 ustar 00 service = ProfilerService::getInstance(); $this->logger = $logger; } /** * Logs known profiles * * @return void */ public function log() { if (Profiler::isEnabled()) { $this->service->iterateProfiles(function (Profile $profile) { $this->logger->debug(sprintf( "%s -> %s: %d ms, %d kB", $profile->meta[Profiler::START_LABEL], $profile->meta[Profiler::FINISH_LABEL], $profile->duration * 1000, $profile->memoryUsageChange / 1024 ), $profile->jsonSerialize()); }); } } } src/Profiler/Adapter/TracyBarAdapter.php 0000666 00000023714 13436755073 0014246 0 ustar 00 config = array_replace_recursive(TracyBarAdapter::getDefaultConfig(), $config); /** @noinspection PhpInternalEntityUsedInspection */ $this->profilerService = ProfilerService::getInstance(); } /** * @internal * @return array */ public static function getDefaultConfig() { return [ self::CONFIG_PRIMARY_VALUE => self::CONFIG_PRIMARY_VALUE_EFFECTIVE, self::CONFIG_SHOW => [ self::CONFIG_SHOW_MEMORY_USAGE_CHART => true, self::CONFIG_SHOW_SHORT_PROFILES => true, self::CONFIG_SHOW_TIME_LINES => true ] ]; } /** * @inheritdoc */ public function getTab() { $countOfProfiles = count($this->profilerService->getProfiles()); return sprintf( "⏱ %s", "Profiler info", Profiler::isEnabled() ? sprintf( $countOfProfiles == 1 ? "%d profile" : "%d profiles", $countOfProfiles ) : "disabled" ); } /** * @inheritdoc */ public function getPanel() { $table = ""; $table .= "
" . $this->getMemoryChart() . " | |||
Start | Finish | Time (absolute) | Memory change (absolute) |
---|---|---|---|
Start | Finish | Time (effective) | Memory change (effective) | %s | ", $profile->meta[Profiler::START_LABEL], $profile->meta[Profiler::FINISH_LABEL] ); } else { $labels = sprintf( "%s | %s | ", $profile->meta[Profiler::START_LABEL], $profile->meta[Profiler::FINISH_LABEL] ); } if ($this->config[self::CONFIG_PRIMARY_VALUE] == self::CONFIG_PRIMARY_VALUE_EFFECTIVE) { $table .= sprintf( "
%d ms (%d ms) | %d kB (%d kB) | ||
%d ms (%d ms) | %d kB (%d kB) | ||
" . "" . "" . "" . "" . " |